]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
kernel 5.4 unexported set_memory_x symbol because
[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 bfb1c6095c7a..bddba5bb307e 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -261,6 +261,7 @@ source "fs/romfs/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 d60089fd689b..924c16a40b00 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -132,3 +132,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 a50e97a63bc8..bd7c76319a85 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2822,6 +2822,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 ab7ca5989097..80d06084b043 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -738,6 +738,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 e88cf0554e65..7ce4ccf5a51c 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1264,7 +1264,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 3d40771e8e7c..d62303c5472a 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 0f1e3b563c47..43df8452f97d 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1670,7 +1670,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 d28d30b13043..34c8093ddb1d 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..3265bb84f152 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);
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);
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 98412721f056..75b489fcb66f 100644
181 --- a/fs/splice.c
182 +++ b/fs/splice.c
183 @@ -834,8 +834,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 @@ -851,9 +851,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 997a530ff4e9..3dbec51c2037 100644
222 --- a/include/linux/fs.h
223 +++ b/include/linux/fs.h
224 @@ -1331,6 +1331,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 @@ -1810,6 +1811,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 @@ -1880,6 +1882,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 @@ -2298,6 +2306,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 @@ -2586,6 +2595,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 0b0d7259276d..12d18e180348 100644
271 --- a/include/linux/lockdep.h
272 +++ b/include/linux/lockdep.h
273 @@ -334,6 +334,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 @@ -476,6 +478,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 4861cf8e274b..ec87ccf82025 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 731642e0f5a0..e8d6259de65b 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 @@ -1807,7 +1810,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 mm_walk walk = {
401                 .hugetlb_entry = gather_hugetlb_stats,
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 0334ca97c584..3bdae823daee 100644
420 --- a/include/linux/mm.h
421 +++ b/include/linux/mm.h
422 @@ -1538,6 +1538,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 6a7a1083b6fb..461db8843e32 100644
453 --- a/include/linux/mm_types.h
454 +++ b/include/linux/mm_types.h
455 @@ -262,6 +262,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 @@ -336,6 +337,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 541fd805fb88..3208fcf3b3c9 100644
473 --- a/kernel/fork.c
474 +++ b/kernel/fork.c
475 @@ -553,7 +553,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 d0b295c3b764..6156cd3d1ab0 100644
486 --- a/mm/Makefile
487 +++ b/mm/Makefile
488 @@ -39,7 +39,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 d0cf700bf201..5cac93e7bf67 100644
499 --- a/mm/filemap.c
500 +++ b/mm/filemap.c
501 @@ -2721,7 +2721,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 7e8c3e8ae75f..d8a903f61262 100644
512 --- a/mm/mmap.c
513 +++ b/mm/mmap.c
514 @@ -182,7 +182,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 @@ -931,7 +931,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 @@ -1847,8 +1847,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 @@ -2680,7 +2680,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 @@ -2699,7 +2699,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 @@ -2890,7 +2890,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 @@ -2965,10 +2965,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 @@ -3258,7 +3275,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 fed1b6e9c89b..0d43b248ed54 100644
609 --- a/mm/nommu.c
610 +++ b/mm/nommu.c
611 @@ -552,7 +552,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 @@ -690,7 +690,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 @@ -1213,7 +1213,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 @@ -1290,10 +1290,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..024cdcfae1b1
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-2019 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 7ce4ccf5a51c..00d7e6a08026 100644
748 --- a/fs/dcache.c
749 +++ b/fs/dcache.c
750 @@ -1369,6 +1369,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 @@ -2914,6 +2915,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 f7f6a140856a..1a740f3c42ba 100644
768 --- a/fs/exec.c
769 +++ b/fs/exec.c
770 @@ -110,6 +110,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 d62303c5472a..d4ad34463848 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 b07b53f24ff5..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 @@ -374,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 43df8452f97d..8a5c14630ff0 100644
812 --- a/fs/inode.c
813 +++ b/fs/inode.c
814 @@ -1679,6 +1679,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 34c8093ddb1d..2b64af58b643 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 @@ -1900,6 +1902,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 --- linux-5.4/fs/notify/group.c~        2019-11-25 01:32:01.000000000 +0100
851 +++ linux-5.4/fs/notify/group.c 2019-11-26 20:14:34.917150671 +0100
852 @@ -99,6 +99,7 @@ void fsnotify_get_group(struct fsnotify_
853  {
854         refcount_inc(&group->refcnt);
855  }
856 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
857  
858  /*
859   * Drop a reference to a group.  Free it if it's through.
860 diff --git a/fs/open.c b/fs/open.c
861 index a59abe3c669a..78809163ba03 100644
862 --- a/fs/open.c
863 +++ b/fs/open.c
864 @@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
865         inode_unlock(dentry->d_inode);
866         return ret;
867  }
868 +EXPORT_SYMBOL_GPL(do_truncate);
869  
870  long vfs_truncate(const struct path *path, loff_t length)
871  {
872 diff --git a/fs/read_write.c b/fs/read_write.c
873 index 3265bb84f152..5b2dbddb0efe 100644
874 --- a/fs/read_write.c
875 +++ b/fs/read_write.c
876 @@ -468,6 +468,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
877  
878         return ret;
879  }
880 +EXPORT_SYMBOL_GPL(vfs_read);
881  
882  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
883  {
884 @@ -508,6 +509,7 @@ vfs_readf_t vfs_readf(struct file *file)
885                 return new_sync_read;
886         return ERR_PTR(-ENOSYS);
887  }
888 +EXPORT_SYMBOL_GPL(vfs_readf);
889  
890  vfs_writef_t vfs_writef(struct file *file)
891  {
892 @@ -519,6 +521,7 @@ vfs_writef_t vfs_writef(struct file *file)
893                 return new_sync_write;
894         return ERR_PTR(-ENOSYS);
895  }
896 +EXPORT_SYMBOL_GPL(vfs_writef);
897  
898  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
899  {
900 @@ -588,6 +591,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
901  
902         return ret;
903  }
904 +EXPORT_SYMBOL_GPL(vfs_write);
905  
906  /* file_ppos returns &file->f_pos or NULL if file is stream */
907  static inline loff_t *file_ppos(struct file *file)
908 diff --git a/fs/splice.c b/fs/splice.c
909 index 75b489fcb66f..0a1f7498c22b 100644
910 --- a/fs/splice.c
911 +++ b/fs/splice.c
912 @@ -847,6 +847,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
913  
914         return splice_write(pipe, out, ppos, len, flags);
915  }
916 +EXPORT_SYMBOL_GPL(do_splice_from);
917  
918  /*
919   * Attempt to initiate a splice from a file to a pipe.
920 @@ -876,6 +877,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
921  
922         return splice_read(in, ppos, pipe, len, flags);
923  }
924 +EXPORT_SYMBOL_GPL(do_splice_to);
925  
926  /**
927   * splice_direct_to_actor - splices data directly between two non-pipes
928 diff --git a/fs/sync.c b/fs/sync.c
929 index 457f4e4a5cc1..67c66358f3fe 100644
930 --- a/fs/sync.c
931 +++ b/fs/sync.c
932 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
933                 sb->s_op->sync_fs(sb, wait);
934         return __sync_blockdev(sb->s_bdev, wait);
935  }
936 +EXPORT_SYMBOL_GPL(__sync_filesystem);
937  
938  /*
939   * Write out and wait upon all dirty data associated with this
940 diff --git a/fs/xattr.c b/fs/xattr.c
941 index 90dd78f0eb27..40b01dd1b14a 100644
942 --- a/fs/xattr.c
943 +++ b/fs/xattr.c
944 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
945         *xattr_value = value;
946         return error;
947  }
948 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
949  
950  ssize_t
951  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
952 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
953 index ec87ccf82025..739d1c8a81f5 100644
954 --- a/kernel/locking/lockdep.c
955 +++ b/kernel/locking/lockdep.c
956 @@ -174,6 +174,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
957          */
958         return lock_classes + class_idx;
959  }
960 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
961  #define hlock_class(hlock) lockdep_hlock_class(hlock)
962  
963  #ifdef CONFIG_LOCK_STAT
964 diff --git a/kernel/task_work.c b/kernel/task_work.c
965 index 0fef395662a6..83fb1ecfc33d 100644
966 --- a/kernel/task_work.c
967 +++ b/kernel/task_work.c
968 @@ -116,3 +116,4 @@ void task_work_run(void)
969                 } while (work);
970         }
971  }
972 +EXPORT_SYMBOL_GPL(task_work_run);
973 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
974 index 725674f3276d..83f6494c52a2 100644
975 --- a/security/device_cgroup.c
976 +++ b/security/device_cgroup.c
977 @@ -824,3 +824,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
978  
979         return 0;
980  }
981 +EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
982 diff --git a/security/security.c b/security/security.c
983 index 250ee2d76406..2765042d4846 100644
984 --- a/security/security.c
985 +++ b/security/security.c
986 @@ -996,6 +996,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
987                 return 0;
988         return call_int_hook(path_rmdir, 0, dir, dentry);
989  }
990 +EXPORT_SYMBOL_GPL(security_path_rmdir);
991  
992  int security_path_unlink(const struct path *dir, struct dentry *dentry)
993  {
994 @@ -1012,6 +1013,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
995                 return 0;
996         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
997  }
998 +EXPORT_SYMBOL_GPL(security_path_symlink);
999  
1000  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1001                        struct dentry *new_dentry)
1002 @@ -1020,6 +1022,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1003                 return 0;
1004         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1005  }
1006 +EXPORT_SYMBOL_GPL(security_path_link);
1007  
1008  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1009                          const struct path *new_dir, struct dentry *new_dentry,
1010 @@ -1047,6 +1050,7 @@ int security_path_truncate(const struct path *path)
1011                 return 0;
1012         return call_int_hook(path_truncate, 0, path);
1013  }
1014 +EXPORT_SYMBOL_GPL(security_path_truncate);
1015  
1016  int security_path_chmod(const struct path *path, umode_t mode)
1017  {
1018 @@ -1054,6 +1058,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1019                 return 0;
1020         return call_int_hook(path_chmod, 0, path, mode);
1021  }
1022 +EXPORT_SYMBOL_GPL(security_path_chmod);
1023  
1024  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1025  {
1026 @@ -1061,6 +1066,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1027                 return 0;
1028         return call_int_hook(path_chown, 0, path, uid, gid);
1029  }
1030 +EXPORT_SYMBOL_GPL(security_path_chown);
1031  
1032  int security_path_chroot(const struct path *path)
1033  {
1034 @@ -1161,6 +1167,7 @@ int security_inode_permission(struct inode *inode, int mask)
1035                 return 0;
1036         return call_int_hook(inode_permission, 0, inode, mask);
1037  }
1038 +EXPORT_SYMBOL_GPL(security_inode_permission);
1039  
1040  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1041  {
1042 @@ -1338,6 +1345,7 @@ int security_file_permission(struct file *file, int mask)
1043  
1044         return fsnotify_perm(file, mask);
1045  }
1046 +EXPORT_SYMBOL_GPL(security_file_permission);
1047  
1048  int security_file_alloc(struct file *file)
1049  {
1050 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1051 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1052 +++ linux/Documentation/ABI/testing/debugfs-aufs        2019-07-11 15:42:14.455570938 +0200
1053 @@ -0,0 +1,55 @@
1054 +What:          /debug/aufs/si_<id>/
1055 +Date:          March 2009
1056 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1057 +Description:
1058 +               Under /debug/aufs, a directory named si_<id> is created
1059 +               per aufs mount, where <id> is a unique id generated
1060 +               internally.
1061 +
1062 +What:          /debug/aufs/si_<id>/plink
1063 +Date:          Apr 2013
1064 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1065 +Description:
1066 +               It has three lines and shows the information about the
1067 +               pseudo-link. The first line is a single number
1068 +               representing a number of buckets. The second line is a
1069 +               number of pseudo-links per buckets (separated by a
1070 +               blank). The last line is a single number representing a
1071 +               total number of psedo-links.
1072 +               When the aufs mount option 'noplink' is specified, it
1073 +               will show "1\n0\n0\n".
1074 +
1075 +What:          /debug/aufs/si_<id>/xib
1076 +Date:          March 2009
1077 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1078 +Description:
1079 +               It shows the consumed blocks by xib (External Inode Number
1080 +               Bitmap), its block size and file size.
1081 +               When the aufs mount option 'noxino' is specified, it
1082 +               will be empty. About XINO files, see the aufs manual.
1083 +
1084 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1085 +Date:          March 2009
1086 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1087 +Description:
1088 +               It shows the consumed blocks by xino (External Inode Number
1089 +               Translation Table), its link count, block size and file
1090 +               size.
1091 +               Due to the file size limit, there may exist multiple
1092 +               xino files per branch.  In this case, "-N" is added to
1093 +               the filename and it corresponds to the index of the
1094 +               internal xino array.  "-0" is omitted.
1095 +               When the aufs mount option 'noxino' is specified, Those
1096 +               entries won't exist.  About XINO files, see the aufs
1097 +               manual.
1098 +
1099 +What:          /debug/aufs/si_<id>/xigen
1100 +Date:          March 2009
1101 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1102 +Description:
1103 +               It shows the consumed blocks by xigen (External Inode
1104 +               Generation Table), its block size and file size.
1105 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1106 +               be created.
1107 +               When the aufs mount option 'noxino' is specified, it
1108 +               will be empty. About XINO files, see the aufs manual.
1109 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1110 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1111 +++ linux/Documentation/ABI/testing/sysfs-aufs  2019-07-11 15:42:14.455570938 +0200
1112 @@ -0,0 +1,31 @@
1113 +What:          /sys/fs/aufs/si_<id>/
1114 +Date:          March 2009
1115 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1116 +Description:
1117 +               Under /sys/fs/aufs, a directory named si_<id> is created
1118 +               per aufs mount, where <id> is a unique id generated
1119 +               internally.
1120 +
1121 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1122 +Date:          March 2009
1123 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1124 +Description:
1125 +               It shows the abolute path of a member directory (which
1126 +               is called branch) in aufs, and its permission.
1127 +
1128 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1129 +Date:          July 2013
1130 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1131 +Description:
1132 +               It shows the id of a member directory (which is called
1133 +               branch) in aufs.
1134 +
1135 +What:          /sys/fs/aufs/si_<id>/xi_path
1136 +Date:          March 2009
1137 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1138 +Description:
1139 +               It shows the abolute path of XINO (External Inode Number
1140 +               Bitmap, Translation Table and Generation Table) file
1141 +               even if it is the default path.
1142 +               When the aufs mount option 'noxino' is specified, it
1143 +               will be empty. About XINO files, see the aufs manual.
1144 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1145 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1146 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2019-07-11 15:42:14.455570938 +0200
1147 @@ -0,0 +1,171 @@
1148 +
1149 +# Copyright (C) 2005-2019 Junjiro R. Okajima
1150 +# 
1151 +# This program is free software; you can redistribute it and/or modify
1152 +# it under the terms of the GNU General Public License as published by
1153 +# the Free Software Foundation; either version 2 of the License, or
1154 +# (at your option) any later version.
1155 +# 
1156 +# This program is distributed in the hope that it will be useful,
1157 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1158 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1159 +# GNU General Public License for more details.
1160 +# 
1161 +# You should have received a copy of the GNU General Public License
1162 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1163 +
1164 +Introduction
1165 +----------------------------------------
1166 +
1167 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1168 +1. abbrev. for "advanced multi-layered unification filesystem".
1169 +2. abbrev. for "another unionfs".
1170 +3. abbrev. for "auf das" in German which means "on the" in English.
1171 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1172 +   But "Filesystem aufs Filesystem" is hard to understand.
1173 +4. abbrev. for "African Urban Fashion Show".
1174 +
1175 +AUFS is a filesystem with features:
1176 +- multi layered stackable unification filesystem, the member directory
1177 +  is called as a branch.
1178 +- branch permission and attribute, 'readonly', 'real-readonly',
1179 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1180 +  combination.
1181 +- internal "file copy-on-write".
1182 +- logical deletion, whiteout.
1183 +- dynamic branch manipulation, adding, deleting and changing permission.
1184 +- allow bypassing aufs, user's direct branch access.
1185 +- external inode number translation table and bitmap which maintains the
1186 +  persistent aufs inode number.
1187 +- seekable directory, including NFS readdir.
1188 +- file mapping, mmap and sharing pages.
1189 +- pseudo-link, hardlink over branches.
1190 +- loopback mounted filesystem as a branch.
1191 +- several policies to select one among multiple writable branches.
1192 +- revert a single systemcall when an error occurs in aufs.
1193 +- and more...
1194 +
1195 +
1196 +Multi Layered Stackable Unification Filesystem
1197 +----------------------------------------------------------------------
1198 +Most people already knows what it is.
1199 +It is a filesystem which unifies several directories and provides a
1200 +merged single directory. When users access a file, the access will be
1201 +passed/re-directed/converted (sorry, I am not sure which English word is
1202 +correct) to the real file on the member filesystem. The member
1203 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1204 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1205 +readonly branch is handled by creating 'whiteout' on the upper writable
1206 +branch.
1207 +
1208 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1209 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1210 +different approaches to implement the merged-view.
1211 +The former tries putting it into VFS, and the latter implements as a
1212 +separate filesystem.
1213 +(If I misunderstand about these implementations, please let me know and
1214 +I shall correct it. Because it is a long time ago when I read their
1215 +source files last time).
1216 +
1217 +UnionMount's approach will be able to small, but may be hard to share
1218 +branches between several UnionMount since the whiteout in it is
1219 +implemented in the inode on branch filesystem and always
1220 +shared. According to Bharata's post, readdir does not seems to be
1221 +finished yet.
1222 +There are several missing features known in this implementations such as
1223 +- for users, the inode number may change silently. eg. copy-up.
1224 +- link(2) may break by copy-up.
1225 +- read(2) may get an obsoleted filedata (fstat(2) too).
1226 +- fcntl(F_SETLK) may be broken by copy-up.
1227 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1228 +  open(O_RDWR).
1229 +
1230 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1231 +merged into mainline. This is another implementation of UnionMount as a
1232 +separated filesystem. All the limitations and known problems which
1233 +UnionMount are equally inherited to "overlay" filesystem.
1234 +
1235 +Unionfs has a longer history. When I started implementing a stackable
1236 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1237 +inode, dentry and file objects and they have an array pointing lower
1238 +same kind objects. After contributing many patches for Unionfs, I
1239 +re-started my project AUFS (Jun 2006).
1240 +
1241 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1242 +implemented my own ideas, approaches and enhancements and it became
1243 +totally different one.
1244 +
1245 +Comparing DM snapshot and fs based implementation
1246 +- the number of bytes to be copied between devices is much smaller.
1247 +- the type of filesystem must be one and only.
1248 +- the fs must be writable, no readonly fs, even for the lower original
1249 +  device. so the compression fs will not be usable. but if we use
1250 +  loopback mount, we may address this issue.
1251 +  for instance,
1252 +       mount /cdrom/squashfs.img /sq
1253 +       losetup /sq/ext2.img
1254 +       losetup /somewhere/cow
1255 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1256 +- it will be difficult (or needs more operations) to extract the
1257 +  difference between the original device and COW.
1258 +- DM snapshot-merge may help a lot when users try merging. in the
1259 +  fs-layer union, users will use rsync(1).
1260 +
1261 +You may want to read my old paper "Filesystems in LiveCD"
1262 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1263 +
1264 +
1265 +Several characters/aspects/persona of aufs
1266 +----------------------------------------------------------------------
1267 +
1268 +Aufs has several characters, aspects or persona.
1269 +1. a filesystem, callee of VFS helper
1270 +2. sub-VFS, caller of VFS helper for branches
1271 +3. a virtual filesystem which maintains persistent inode number
1272 +4. reader/writer of files on branches such like an application
1273 +
1274 +1. Callee of VFS Helper
1275 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1276 +unlink(2) from an application reaches sys_unlink() kernel function and
1277 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1278 +calls filesystem specific unlink operation. Actually aufs implements the
1279 +unlink operation but it behaves like a redirector.
1280 +
1281 +2. Caller of VFS Helper for Branches
1282 +aufs_unlink() passes the unlink request to the branch filesystem as if
1283 +it were called from VFS. So the called unlink operation of the branch
1284 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1285 +every necessary pre/post operation for the branch filesystem.
1286 +- acquire the lock for the parent dir on a branch
1287 +- lookup in a branch
1288 +- revalidate dentry on a branch
1289 +- mnt_want_write() for a branch
1290 +- vfs_unlink() for a branch
1291 +- mnt_drop_write() for a branch
1292 +- release the lock on a branch
1293 +
1294 +3. Persistent Inode Number
1295 +One of the most important issue for a filesystem is to maintain inode
1296 +numbers. This is particularly important to support exporting a
1297 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1298 +backend block device for its own. But some storage is necessary to
1299 +keep and maintain the inode numbers. It may be a large space and may not
1300 +suit to keep in memory. Aufs rents some space from its first writable
1301 +branch filesystem (by default) and creates file(s) on it. These files
1302 +are created by aufs internally and removed soon (currently) keeping
1303 +opened.
1304 +Note: Because these files are removed, they are totally gone after
1305 +      unmounting aufs. It means the inode numbers are not persistent
1306 +      across unmount or reboot. I have a plan to make them really
1307 +      persistent which will be important for aufs on NFS server.
1308 +
1309 +4. Read/Write Files Internally (copy-on-write)
1310 +Because a branch can be readonly, when you write a file on it, aufs will
1311 +"copy-up" it to the upper writable branch internally. And then write the
1312 +originally requested thing to the file. Generally kernel doesn't
1313 +open/read/write file actively. In aufs, even a single write may cause a
1314 +internal "file copy". This behaviour is very similar to cp(1) command.
1315 +
1316 +Some people may think it is better to pass such work to user space
1317 +helper, instead of doing in kernel space. Actually I am still thinking
1318 +about it. But currently I have implemented it in kernel space.
1319 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1320 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1321 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2019-07-11 15:42:14.455570938 +0200
1322 @@ -0,0 +1,258 @@
1323 +
1324 +# Copyright (C) 2005-2019 Junjiro R. Okajima
1325 +# 
1326 +# This program is free software; you can redistribute it and/or modify
1327 +# it under the terms of the GNU General Public License as published by
1328 +# the Free Software Foundation; either version 2 of the License, or
1329 +# (at your option) any later version.
1330 +# 
1331 +# This program is distributed in the hope that it will be useful,
1332 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1333 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1334 +# GNU General Public License for more details.
1335 +# 
1336 +# You should have received a copy of the GNU General Public License
1337 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1338 +
1339 +Basic Aufs Internal Structure
1340 +
1341 +Superblock/Inode/Dentry/File Objects
1342 +----------------------------------------------------------------------
1343 +As like an ordinary filesystem, aufs has its own
1344 +superblock/inode/dentry/file objects. All these objects have a
1345 +dynamically allocated array and store the same kind of pointers to the
1346 +lower filesystem, branch.
1347 +For example, when you build a union with one readwrite branch and one
1348 +readonly, mounted /au, /rw and /ro respectively.
1349 +- /au = /rw + /ro
1350 +- /ro/fileA exists but /rw/fileA
1351 +
1352 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1353 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1354 +- [0] = NULL (because /rw/fileA doesn't exist)
1355 +- [1] = /ro/fileA
1356 +
1357 +This style of an array is essentially same to the aufs
1358 +superblock/inode/dentry/file objects.
1359 +
1360 +Because aufs supports manipulating branches, ie. add/delete/change
1361 +branches dynamically, these objects has its own generation. When
1362 +branches are changed, the generation in aufs superblock is
1363 +incremented. And a generation in other object are compared when it is
1364 +accessed. When a generation in other objects are obsoleted, aufs
1365 +refreshes the internal array.
1366 +
1367 +
1368 +Superblock
1369 +----------------------------------------------------------------------
1370 +Additionally aufs superblock has some data for policies to select one
1371 +among multiple writable branches, XIB files, pseudo-links and kobject.
1372 +See below in detail.
1373 +About the policies which supports copy-down a directory, see
1374 +wbr_policy.txt too.
1375 +
1376 +
1377 +Branch and XINO(External Inode Number Translation Table)
1378 +----------------------------------------------------------------------
1379 +Every branch has its own xino (external inode number translation table)
1380 +file. The xino file is created and unlinked by aufs internally. When two
1381 +members of a union exist on the same filesystem, they share the single
1382 +xino file.
1383 +The struct of a xino file is simple, just a sequence of aufs inode
1384 +numbers which is indexed by the lower inode number.
1385 +In the above sample, assume the inode number of /ro/fileA is i111 and
1386 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1387 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1388 +
1389 +When the inode numbers are not contiguous, the xino file will be sparse
1390 +which has a hole in it and doesn't consume as much disk space as it
1391 +might appear. If your branch filesystem consumes disk space for such
1392 +holes, then you should specify 'xino=' option at mounting aufs.
1393 +
1394 +Aufs has a mount option to free the disk blocks for such holes in XINO
1395 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1396 +meet a problem of disk shortage due to XINO files, then you should try
1397 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1398 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1399 +the holes in XINO files.
1400 +
1401 +Also a writable branch has three kinds of "whiteout bases". All these
1402 +are existed when the branch is joined to aufs, and their names are
1403 +whiteout-ed doubly, so that users will never see their names in aufs
1404 +hierarchy.
1405 +1. a regular file which will be hardlinked to all whiteouts.
1406 +2. a directory to store a pseudo-link.
1407 +3. a directory to store an "orphan"-ed file temporary.
1408 +
1409 +1. Whiteout Base
1410 +   When you remove a file on a readonly branch, aufs handles it as a
1411 +   logical deletion and creates a whiteout on the upper writable branch
1412 +   as a hardlink of this file in order not to consume inode on the
1413 +   writable branch.
1414 +2. Pseudo-link Dir
1415 +   See below, Pseudo-link.
1416 +3. Step-Parent Dir
1417 +   When "fileC" exists on the lower readonly branch only and it is
1418 +   opened and removed with its parent dir, and then user writes
1419 +   something into it, then aufs copies-up fileC to this
1420 +   directory. Because there is no other dir to store fileC. After
1421 +   creating a file under this dir, the file is unlinked.
1422 +
1423 +Because aufs supports manipulating branches, ie. add/delete/change
1424 +dynamically, a branch has its own id. When the branch order changes,
1425 +aufs finds the new index by searching the branch id.
1426 +
1427 +
1428 +Pseudo-link
1429 +----------------------------------------------------------------------
1430 +Assume "fileA" exists on the lower readonly branch only and it is
1431 +hardlinked to "fileB" on the branch. When you write something to fileA,
1432 +aufs copies-up it to the upper writable branch. Additionally aufs
1433 +creates a hardlink under the Pseudo-link Directory of the writable
1434 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1435 +simple list. If fileB is read after unlinking fileA, aufs returns
1436 +filedata from the pseudo-link instead of the lower readonly
1437 +branch. Because the pseudo-link is based upon the inode, to keep the
1438 +inode number by xino (see above) is essentially necessary.
1439 +
1440 +All the hardlinks under the Pseudo-link Directory of the writable branch
1441 +should be restored in a proper location later. Aufs provides a utility
1442 +to do this. The userspace helpers executed at remounting and unmounting
1443 +aufs by default.
1444 +During this utility is running, it puts aufs into the pseudo-link
1445 +maintenance mode. In this mode, only the process which began the
1446 +maintenance mode (and its child processes) is allowed to operate in
1447 +aufs. Some other processes which are not related to the pseudo-link will
1448 +be allowed to run too, but the rest have to return an error or wait
1449 +until the maintenance mode ends. If a process already acquires an inode
1450 +mutex (in VFS), it has to return an error.
1451 +
1452 +
1453 +XIB(external inode number bitmap)
1454 +----------------------------------------------------------------------
1455 +Addition to the xino file per a branch, aufs has an external inode number
1456 +bitmap in a superblock object. It is also an internal file such like a
1457 +xino file.
1458 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1459 +not.
1460 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1461 +
1462 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1463 +reduce the number of consumed disk blocks for these files.
1464 +
1465 +
1466 +Virtual or Vertical Dir, and Readdir in Userspace
1467 +----------------------------------------------------------------------
1468 +In order to support multiple layers (branches), aufs readdir operation
1469 +constructs a virtual dir block on memory. For readdir, aufs calls
1470 +vfs_readdir() internally for each dir on branches, merges their entries
1471 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1472 +object. So the file object has its entry list until it is closed. The
1473 +entry list will be updated when the file position is zero and becomes
1474 +obsoleted. This decision is made in aufs automatically.
1475 +
1476 +The dynamically allocated memory block for the name of entries has a
1477 +unit of 512 bytes (by default) and stores the names contiguously (no
1478 +padding). Another block for each entry is handled by kmem_cache too.
1479 +During building dir blocks, aufs creates hash list and judging whether
1480 +the entry is whiteouted by its upper branch or already listed.
1481 +The merged result is cached in the corresponding inode object and
1482 +maintained by a customizable life-time option.
1483 +
1484 +Some people may call it can be a security hole or invite DoS attack
1485 +since the opened and once readdir-ed dir (file object) holds its entry
1486 +list and becomes a pressure for system memory. But I'd say it is similar
1487 +to files under /proc or /sys. The virtual files in them also holds a
1488 +memory page (generally) while they are opened. When an idea to reduce
1489 +memory for them is introduced, it will be applied to aufs too.
1490 +For those who really hate this situation, I've developed readdir(3)
1491 +library which operates this merging in userspace. You just need to set
1492 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1493 +kernel space for readdir(3).
1494 +
1495 +
1496 +Workqueue
1497 +----------------------------------------------------------------------
1498 +Aufs sometimes requires privilege access to a branch. For instance,
1499 +in copy-up/down operation. When a user process is going to make changes
1500 +to a file which exists in the lower readonly branch only, and the mode
1501 +of one of ancestor directories may not be writable by a user
1502 +process. Here aufs copy-up the file with its ancestors and they may
1503 +require privilege to set its owner/group/mode/etc.
1504 +This is a typical case of a application character of aufs (see
1505 +Introduction).
1506 +
1507 +Aufs uses workqueue synchronously for this case. It creates its own
1508 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1509 +passes the request to call mkdir or write (for example), and wait for
1510 +its completion. This approach solves a problem of a signal handler
1511 +simply.
1512 +If aufs didn't adopt the workqueue and changed the privilege of the
1513 +process, then the process may receive the unexpected SIGXFSZ or other
1514 +signals.
1515 +
1516 +Also aufs uses the system global workqueue ("events" kernel thread) too
1517 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1518 +whiteout base and etc. This is unrelated to a privilege.
1519 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1520 +superblock at the beginning, at the same time waits for the completion
1521 +of all queued asynchronous tasks.
1522 +
1523 +
1524 +Whiteout
1525 +----------------------------------------------------------------------
1526 +The whiteout in aufs is very similar to Unionfs's. That is represented
1527 +by its filename. UnionMount takes an approach of a file mode, but I am
1528 +afraid several utilities (find(1) or something) will have to support it.
1529 +
1530 +Basically the whiteout represents "logical deletion" which stops aufs to
1531 +lookup further, but also it represents "dir is opaque" which also stop
1532 +further lookup.
1533 +
1534 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1535 +In order to make several functions in a single systemcall to be
1536 +revertible, aufs adopts an approach to rename a directory to a temporary
1537 +unique whiteouted name.
1538 +For example, in rename(2) dir where the target dir already existed, aufs
1539 +renames the target dir to a temporary unique whiteouted name before the
1540 +actual rename on a branch, and then handles other actions (make it opaque,
1541 +update the attributes, etc). If an error happens in these actions, aufs
1542 +simply renames the whiteouted name back and returns an error. If all are
1543 +succeeded, aufs registers a function to remove the whiteouted unique
1544 +temporary name completely and asynchronously to the system global
1545 +workqueue.
1546 +
1547 +
1548 +Copy-up
1549 +----------------------------------------------------------------------
1550 +It is a well-known feature or concept.
1551 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1552 +internally and makes change to the new file on the upper writable branch.
1553 +When the trigger systemcall does not update the timestamps of the parent
1554 +dir, aufs reverts it after copy-up.
1555 +
1556 +
1557 +Move-down (aufs3.9 and later)
1558 +----------------------------------------------------------------------
1559 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1560 +the lower readonly branch to the upper writable branch when a user
1561 +changes something about the file.
1562 +"Move-down" is an opposite action of copy-up. Basically this action is
1563 +ran manually instead of automatically and internally.
1564 +For desgin and implementation, aufs has to consider these issues.
1565 +- whiteout for the file may exist on the lower branch.
1566 +- ancestor directories may not exist on the lower branch.
1567 +- diropq for the ancestor directories may exist on the upper branch.
1568 +- free space on the lower branch will reduce.
1569 +- another access to the file may happen during moving-down, including
1570 +  UDBA (see "Revalidate Dentry and UDBA").
1571 +- the file should not be hard-linked nor pseudo-linked. they should be
1572 +  handled by auplink utility later.
1573 +
1574 +Sometimes users want to move-down a file from the upper writable branch
1575 +to the lower readonly or writable branch. For instance,
1576 +- the free space of the upper writable branch is going to run out.
1577 +- create a new intermediate branch between the upper and lower branch.
1578 +- etc.
1579 +
1580 +For this purpose, use "aumvdown" command in aufs-util.git.
1581 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1582 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1583 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2019-07-11 15:42:14.458904362 +0200
1584 @@ -0,0 +1,85 @@
1585 +
1586 +# Copyright (C) 2015-2019 Junjiro R. Okajima
1587 +# 
1588 +# This program is free software; you can redistribute it and/or modify
1589 +# it under the terms of the GNU General Public License as published by
1590 +# the Free Software Foundation; either version 2 of the License, or
1591 +# (at your option) any later version.
1592 +# 
1593 +# This program is distributed in the hope that it will be useful,
1594 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1595 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1596 +# GNU General Public License for more details.
1597 +# 
1598 +# You should have received a copy of the GNU General Public License
1599 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1600 +
1601 +Support for a branch who has its ->atomic_open()
1602 +----------------------------------------------------------------------
1603 +The filesystems who implement its ->atomic_open() are not majority. For
1604 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1605 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1606 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1607 +sure whether all filesystems who have ->atomic_open() behave like this,
1608 +but NFSv4 surely returns the error.
1609 +
1610 +In order to support ->atomic_open() for aufs, there are a few
1611 +approaches.
1612 +
1613 +A. Introduce aufs_atomic_open()
1614 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1615 +     branch fs.
1616 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1617 +   an aufs user Pip Cet's approach
1618 +   - calls aufs_create(), VFS finish_open() and notify_change().
1619 +   - pass fake-mode to finish_open(), and then correct the mode by
1620 +     notify_change().
1621 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1622 +   - no aufs_atomic_open().
1623 +   - aufs_lookup() registers the TID to an aufs internal object.
1624 +   - aufs_create() does nothing when the matching TID is registered, but
1625 +     registers the mode.
1626 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1627 +     TID is registered.
1628 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1629 +   credential
1630 +   - no aufs_atomic_open().
1631 +   - aufs_create() registers the TID to an internal object. this info
1632 +     represents "this process created this file just now."
1633 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1634 +     registered TID and re-try open() with superuser's credential.
1635 +
1636 +Pros and cons for each approach.
1637 +
1638 +A.
1639 +   - straightforward but highly depends upon VFS internal.
1640 +   - the atomic behavaiour is kept.
1641 +   - some of parameters such as nameidata are hard to reproduce for
1642 +     branch fs.
1643 +   - large overhead.
1644 +B.
1645 +   - easy to implement.
1646 +   - the atomic behavaiour is lost.
1647 +C.
1648 +   - the atomic behavaiour is kept.
1649 +   - dirty and tricky.
1650 +   - VFS checks whether the file is created correctly after calling
1651 +     ->create(), which means this approach doesn't work.
1652 +D.
1653 +   - easy to implement.
1654 +   - the atomic behavaiour is lost.
1655 +   - to open a file with superuser's credential and give it to a user
1656 +     process is a bad idea, since the file object keeps the credential
1657 +     in it. It may affect LSM or something. This approach doesn't work
1658 +     either.
1659 +
1660 +The approach A is ideal, but it hard to implement. So here is a
1661 +variation of A, which is to be implemented.
1662 +
1663 +A-1. Introduce aufs_atomic_open()
1664 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1665 +       vfs_create() and finish_open().
1666 +     - the demerit is that the several checks after branch fs
1667 +       ->atomic_open() are lost. in the ordinary case, the checks are
1668 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1669 +       be implemented in aufs, but not all I am afraid.
1670 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1671 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1672 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2019-07-11 15:42:14.458904362 +0200
1673 @@ -0,0 +1,113 @@
1674 +
1675 +# Copyright (C) 2005-2019 Junjiro R. Okajima
1676 +# 
1677 +# This program is free software; you can redistribute it and/or modify
1678 +# it under the terms of the GNU General Public License as published by
1679 +# the Free Software Foundation; either version 2 of the License, or
1680 +# (at your option) any later version.
1681 +# 
1682 +# This program is distributed in the hope that it will be useful,
1683 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1684 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1685 +# GNU General Public License for more details.
1686 +# 
1687 +# You should have received a copy of the GNU General Public License
1688 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1689 +
1690 +Lookup in a Branch
1691 +----------------------------------------------------------------------
1692 +Since aufs has a character of sub-VFS (see Introduction), it operates
1693 +lookup for branches as VFS does. It may be a heavy work. But almost all
1694 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1695 +directly connected to its parent. Digging down the directory hierarchy
1696 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1697 +aufs calls it.
1698 +
1699 +When a branch is a remote filesystem, aufs basically relies upon its
1700 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1701 +them.
1702 +For d_revalidate, aufs implements three levels of revalidate tests. See
1703 +"Revalidate Dentry and UDBA" in detail.
1704 +
1705 +
1706 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1707 +----------------------------------------------------------------------
1708 +Let's try case study.
1709 +- aufs has two branches, upper readwrite and lower readonly.
1710 +  /au = /rw + /ro
1711 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1712 +- user invoked "chmod a+rx /au/dirA"
1713 +- the internal copy-up is activated and "/rw/dirA" is created and its
1714 +  permission bits are set to world readable.
1715 +- then "/au/dirA" becomes world readable?
1716 +
1717 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1718 +or it may be a natively readonly filesystem. If aufs respects the lower
1719 +branch, it should not respond readdir request from other users. But user
1720 +allowed it by chmod. Should really aufs rejects showing the entries
1721 +under /ro/dirA?
1722 +
1723 +To be honest, I don't have a good solution for this case. So aufs
1724 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1725 +users.
1726 +When dirperm1 is specified, aufs checks only the highest one for the
1727 +directory permission, and shows the entries. Otherwise, as usual, checks
1728 +every dir existing on all branches and rejects the request.
1729 +
1730 +As a side effect, dirperm1 option improves the performance of aufs
1731 +because the number of permission check is reduced when the number of
1732 +branch is many.
1733 +
1734 +
1735 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1736 +----------------------------------------------------------------------
1737 +Generally VFS helpers re-validate a dentry as a part of lookup.
1738 +0. digging down the directory hierarchy.
1739 +1. lock the parent dir by its i_mutex.
1740 +2. lookup the final (child) entry.
1741 +3. revalidate it.
1742 +4. call the actual operation (create, unlink, etc.)
1743 +5. unlock the parent dir
1744 +
1745 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1746 +called. Actually aufs implements it and checks the dentry on a branch is
1747 +still valid.
1748 +But it is not enough. Because aufs has to release the lock for the
1749 +parent dir on a branch at the end of ->lookup() (step 2) and
1750 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1751 +held by VFS.
1752 +If the file on a branch is changed directly, eg. bypassing aufs, after
1753 +aufs released the lock, then the subsequent operation may cause
1754 +something unpleasant result.
1755 +
1756 +This situation is a result of VFS architecture, ->lookup() and
1757 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1758 +design from VFS's point of view. It is just not suitable for sub-VFS
1759 +character in aufs.
1760 +
1761 +Aufs supports such case by three level of revalidation which is
1762 +selectable by user.
1763 +1. Simple Revalidate
1764 +   Addition to the native flow in VFS's, confirm the child-parent
1765 +   relationship on the branch just after locking the parent dir on the
1766 +   branch in the "actual operation" (step 4). When this validation
1767 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1768 +   checks the validation of the dentry on branches.
1769 +2. Monitor Changes Internally by Inotify/Fsnotify
1770 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1771 +   the dentry on the branch, and returns EBUSY if it finds different
1772 +   dentry.
1773 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1774 +   during it is in cache. When the event is notified, aufs registers a
1775 +   function to kernel 'events' thread by schedule_work(). And the
1776 +   function sets some special status to the cached aufs dentry and inode
1777 +   private data. If they are not cached, then aufs has nothing to
1778 +   do. When the same file is accessed through aufs (step 0-3) later,
1779 +   aufs will detect the status and refresh all necessary data.
1780 +   In this mode, aufs has to ignore the event which is fired by aufs
1781 +   itself.
1782 +3. No Extra Validation
1783 +   This is the simplest test and doesn't add any additional revalidation
1784 +   test, and skip the revalidation in step 4. It is useful and improves
1785 +   aufs performance when system surely hide the aufs branches from user,
1786 +   by over-mounting something (or another method).
1787 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1788 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1789 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2019-07-11 15:42:14.458904362 +0200
1790 @@ -0,0 +1,74 @@
1791 +
1792 +# Copyright (C) 2005-2019 Junjiro R. Okajima
1793 +# 
1794 +# This program is free software; you can redistribute it and/or modify
1795 +# it under the terms of the GNU General Public License as published by
1796 +# the Free Software Foundation; either version 2 of the License, or
1797 +# (at your option) any later version.
1798 +# 
1799 +# This program is distributed in the hope that it will be useful,
1800 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1801 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1802 +# GNU General Public License for more details.
1803 +# 
1804 +# You should have received a copy of the GNU General Public License
1805 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1806 +
1807 +Branch Manipulation
1808 +
1809 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1810 +and changing its permission/attribute, there are a lot of works to do.
1811 +
1812 +
1813 +Add a Branch
1814 +----------------------------------------------------------------------
1815 +o Confirm the adding dir exists outside of aufs, including loopback
1816 +  mount, and its various attributes.
1817 +o Initialize the xino file and whiteout bases if necessary.
1818 +  See struct.txt.
1819 +
1820 +o Check the owner/group/mode of the directory
1821 +  When the owner/group/mode of the adding directory differs from the
1822 +  existing branch, aufs issues a warning because it may impose a
1823 +  security risk.
1824 +  For example, when a upper writable branch has a world writable empty
1825 +  top directory, a malicious user can create any files on the writable
1826 +  branch directly, like copy-up and modify manually. If something like
1827 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1828 +  writable branch, and the writable branch is world-writable, then a
1829 +  malicious guy may create /etc/passwd on the writable branch directly
1830 +  and the infected file will be valid in aufs.
1831 +  I am afraid it can be a security issue, but aufs can do nothing except
1832 +  producing a warning.
1833 +
1834 +
1835 +Delete a Branch
1836 +----------------------------------------------------------------------
1837 +o Confirm the deleting branch is not busy
1838 +  To be general, there is one merit to adopt "remount" interface to
1839 +  manipulate branches. It is to discard caches. At deleting a branch,
1840 +  aufs checks the still cached (and connected) dentries and inodes. If
1841 +  there are any, then they are all in-use. An inode without its
1842 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1843 +
1844 +  For the cached one, aufs checks whether the same named entry exists on
1845 +  other branches.
1846 +  If the cached one is a directory, because aufs provides a merged view
1847 +  to users, as long as one dir is left on any branch aufs can show the
1848 +  dir to users. In this case, the branch can be removed from aufs.
1849 +  Otherwise aufs rejects deleting the branch.
1850 +
1851 +  If any file on the deleting branch is opened by aufs, then aufs
1852 +  rejects deleting.
1853 +
1854 +
1855 +Modify the Permission of a Branch
1856 +----------------------------------------------------------------------
1857 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1858 +  See struct.txt.
1859 +
1860 +o rw --> ro: Confirm the modifying branch is not busy
1861 +  Aufs rejects the request if any of these conditions are true.
1862 +  - a file on the branch is mmap-ed.
1863 +  - a regular file on the branch is opened for write and there is no
1864 +    same named entry on the upper branch.
1865 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1866 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1867 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2019-07-11 15:42:14.458904362 +0200
1868 @@ -0,0 +1,64 @@
1869 +
1870 +# Copyright (C) 2005-2019 Junjiro R. Okajima
1871 +# 
1872 +# This program is free software; you can redistribute it and/or modify
1873 +# it under the terms of the GNU General Public License as published by
1874 +# the Free Software Foundation; either version 2 of the License, or
1875 +# (at your option) any later version.
1876 +# 
1877 +# This program is distributed in the hope that it will be useful,
1878 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1879 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1880 +# GNU General Public License for more details.
1881 +# 
1882 +# You should have received a copy of the GNU General Public License
1883 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1884 +
1885 +Policies to Select One among Multiple Writable Branches
1886 +----------------------------------------------------------------------
1887 +When the number of writable branch is more than one, aufs has to decide
1888 +the target branch for file creation or copy-up. By default, the highest
1889 +writable branch which has the parent (or ancestor) dir of the target
1890 +file is chosen (top-down-parent policy).
1891 +By user's request, aufs implements some other policies to select the
1892 +writable branch, for file creation several policies, round-robin,
1893 +most-free-space, and other policies. For copy-up, top-down-parent,
1894 +bottom-up-parent, bottom-up and others.
1895 +
1896 +As expected, the round-robin policy selects the branch in circular. When
1897 +you have two writable branches and creates 10 new files, 5 files will be
1898 +created for each branch. mkdir(2) systemcall is an exception. When you
1899 +create 10 new directories, all will be created on the same branch.
1900 +And the most-free-space policy selects the one which has most free
1901 +space among the writable branches. The amount of free space will be
1902 +checked by aufs internally, and users can specify its time interval.
1903 +
1904 +The policies for copy-up is more simple,
1905 +top-down-parent is equivalent to the same named on in create policy,
1906 +bottom-up-parent selects the writable branch where the parent dir
1907 +exists and the nearest upper one from the copyup-source,
1908 +bottom-up selects the nearest upper writable branch from the
1909 +copyup-source, regardless the existence of the parent dir.
1910 +
1911 +There are some rules or exceptions to apply these policies.
1912 +- If there is a readonly branch above the policy-selected branch and
1913 +  the parent dir is marked as opaque (a variation of whiteout), or the
1914 +  target (creating) file is whiteout-ed on the upper readonly branch,
1915 +  then the result of the policy is ignored and the target file will be
1916 +  created on the nearest upper writable branch than the readonly branch.
1917 +- If there is a writable branch above the policy-selected branch and
1918 +  the parent dir is marked as opaque or the target file is whiteouted
1919 +  on the branch, then the result of the policy is ignored and the target
1920 +  file will be created on the highest one among the upper writable
1921 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1922 +  it as usual.
1923 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1924 +  They try selecting the branch where the source exists as possible
1925 +  since copyup a large file will take long time. If it can't be,
1926 +  ie. the branch where the source exists is readonly, then they will
1927 +  follow the copyup policy.
1928 +- There is an exception for rename(2) when the target exists.
1929 +  If the rename target exists, aufs compares the index of the branches
1930 +  where the source and the target exists and selects the higher
1931 +  one. If the selected branch is readonly, then aufs follows the
1932 +  copyup policy.
1933 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1934 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1935 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2019-07-11 15:42:14.458904362 +0200
1936 @@ -0,0 +1,31 @@
1937 +
1938 +// to view this graph, run dot(1) command in GRAPHVIZ.
1939 +
1940 +digraph G {
1941 +node [shape=box];
1942 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1943 +
1944 +node [shape=oval];
1945 +
1946 +aufs_rename -> whinfo [label="store/remove"];
1947 +
1948 +node [shape=oval];
1949 +inode_list [label="h_inum list in branch\ncache"];
1950 +
1951 +node [shape=box];
1952 +whinode [label="h_inum list file"];
1953 +
1954 +node [shape=oval];
1955 +brmgmt [label="br_add/del/mod/umount"];
1956 +
1957 +brmgmt -> inode_list [label="create/remove"];
1958 +brmgmt -> whinode [label="load/store"];
1959 +
1960 +inode_list -> whinode [style=dashed,dir=both];
1961 +
1962 +aufs_rename -> inode_list [label="add/del"];
1963 +
1964 +aufs_lookup -> inode_list [label="search"];
1965 +
1966 +aufs_lookup -> whinfo [label="load/remove"];
1967 +}
1968 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1969 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1970 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2019-07-11 15:42:14.458904362 +0200
1971 @@ -0,0 +1,102 @@
1972 +
1973 +# Copyright (C) 2017-2019 Junjiro R. Okajima
1974 +#
1975 +# This program is free software; you can redistribute it and/or modify
1976 +# it under the terms of the GNU General Public License as published by
1977 +# the Free Software Foundation; either version 2 of the License, or
1978 +# (at your option) any later version.
1979 +#
1980 +# This program is distributed in the hope that it will be useful,
1981 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1982 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1983 +# GNU General Public License for more details.
1984 +#
1985 +# You should have received a copy of the GNU General Public License
1986 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1987 +
1988 +Special handling for renaming a directory (DIRREN)
1989 +----------------------------------------------------------------------
1990 +First, let's assume we have a simple usecase.
1991 +
1992 +- /u = /rw + /ro
1993 +- /rw/dirA exists
1994 +- /ro/dirA and /ro/dirA/file exist too
1995 +- there is no dirB on both branches
1996 +- a user issues rename("dirA", "dirB")
1997 +
1998 +Now, what should aufs behave against this rename(2)?
1999 +There are a few possible cases.
2000 +
2001 +A. returns EROFS.
2002 +   since dirA exists on a readonly branch which cannot be renamed.
2003 +B. returns EXDEV.
2004 +   it is possible to copy-up dirA (only the dir itself), but the child
2005 +   entries ("file" in this case) should not be. it must be a bad
2006 +   approach to copy-up recursively.
2007 +C. returns a success.
2008 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
2009 +   is a violation of aufs' policy.
2010 +D. construct an extra information which indicates that /ro/dirA should
2011 +   be handled as the name of dirB.
2012 +   overlayfs has a similar feature called REDIRECT.
2013 +
2014 +Until now, aufs implements the case B only which returns EXDEV, and
2015 +expects the userspace application behaves like mv(1) which tries
2016 +issueing rename(2) recursively.
2017 +
2018 +A new aufs feature called DIRREN is introduced which implements the case
2019 +D. There are several "extra information" added.
2020 +
2021 +1. detailed info per renamed directory
2022 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2023 +2. the inode-number list of directories on a branch
2024 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
2025 +
2026 +The filename of "detailed info per directory" represents the lower
2027 +branch, and its format is
2028 +- a type of the branch id
2029 +  one of these.
2030 +  + uuid (not implemented yet)
2031 +  + fsid
2032 +  + dev
2033 +- the inode-number of the branch root dir
2034 +
2035 +And it contains these info in a single regular file.
2036 +- magic number
2037 +- branch's inode-number of the logically renamed dir
2038 +- the name of the before-renamed dir
2039 +
2040 +The "detailed info per directory" file is created in aufs rename(2), and
2041 +loaded in any lookup.
2042 +The info is considered in lookup for the matching case only. Here
2043 +"matching" means that the root of branch (in the info filename) is same
2044 +to the current looking-up branch. After looking-up the before-renamed
2045 +name, the inode-number is compared. And the matched dentry is used.
2046 +
2047 +The "inode-number list of directories" is a regular file which contains
2048 +simply the inode-numbers on the branch. The file is created or updated
2049 +in removing the branch, and loaded in adding the branch. Its lifetime is
2050 +equal to the branch.
2051 +The list is refered in lookup, and when the current target inode is
2052 +found in the list, the aufs tries loading the "detailed info per
2053 +directory" and get the changed and valid name of the dir.
2054 +
2055 +Theoretically these "extra informaiton" may be able to be put into XATTR
2056 +in the dir inode. But aufs doesn't choose this way because
2057 +1. XATTR may not be supported by the branch (or its configuration)
2058 +2. XATTR may have its size limit.
2059 +3. XATTR may be less easy to convert than a regular file, when the
2060 +   format of the info is changed in the future.
2061 +At the same time, I agree that the regular file approach is much slower
2062 +than XATTR approach. So, in the future, aufs may take the XATTR or other
2063 +better approach.
2064 +
2065 +This DIRREN feature is enabled by aufs configuration, and is activated
2066 +by a new mount option.
2067 +
2068 +For the more complicated case, there is a work with UDBA option, which
2069 +is to dected the direct access to the branches (by-passing aufs) and to
2070 +maintain the cashes in aufs. Since a single cached aufs dentry may
2071 +contains two names, before- and after-rename, the name comparision in
2072 +UDBA handler may not work correctly. In this case, the behaviour will be
2073 +equivalen to udba=reval case.
2074 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2075 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2076 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2019-07-11 15:42:14.458904362 +0200
2077 @@ -0,0 +1,120 @@
2078 +
2079 +# Copyright (C) 2011-2019 Junjiro R. Okajima
2080 +# 
2081 +# This program is free software; you can redistribute it and/or modify
2082 +# it under the terms of the GNU General Public License as published by
2083 +# the Free Software Foundation; either version 2 of the License, or
2084 +# (at your option) any later version.
2085 +# 
2086 +# This program is distributed in the hope that it will be useful,
2087 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2088 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2089 +# GNU General Public License for more details.
2090 +# 
2091 +# You should have received a copy of the GNU General Public License
2092 +# along with this program; if not, write to the Free Software
2093 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2094 +
2095 +
2096 +File-based Hierarchical Storage Management (FHSM)
2097 +----------------------------------------------------------------------
2098 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2099 +storage world. Aufs provides this feature as file-based with multiple
2100 +writable branches, based upon the principle of "Colder, the Lower".
2101 +Here the word "colder" means that the less used files, and "lower" means
2102 +that the position in the order of the stacked branches vertically.
2103 +These multiple writable branches are prioritized, ie. the topmost one
2104 +should be the fastest drive and be used heavily.
2105 +
2106 +o Characters in aufs FHSM story
2107 +- aufs itself and a new branch attribute.
2108 +- a new ioctl interface to move-down and to establish a connection with
2109 +  the daemon ("move-down" is a converse of "copy-up").
2110 +- userspace tool and daemon.
2111 +
2112 +The userspace daemon establishes a connection with aufs and waits for
2113 +the notification. The notified information is very similar to struct
2114 +statfs containing the number of consumed blocks and inodes.
2115 +When the consumed blocks/inodes of a branch exceeds the user-specified
2116 +upper watermark, the daemon activates its move-down process until the
2117 +consumed blocks/inodes reaches the user-specified lower watermark.
2118 +
2119 +The actual move-down is done by aufs based upon the request from
2120 +user-space since we need to maintain the inode number and the internal
2121 +pointer arrays in aufs.
2122 +
2123 +Currently aufs FHSM handles the regular files only. Additionally they
2124 +must not be hard-linked nor pseudo-linked.
2125 +
2126 +
2127 +o Cowork of aufs and the user-space daemon
2128 +  During the userspace daemon established the connection, aufs sends a
2129 +  small notification to it whenever aufs writes something into the
2130 +  writable branch. But it may cost high since aufs issues statfs(2)
2131 +  internally. So user can specify a new option to cache the
2132 +  info. Actually the notification is controlled by these factors.
2133 +  + the specified cache time.
2134 +  + classified as "force" by aufs internally.
2135 +  Until the specified time expires, aufs doesn't send the info
2136 +  except the forced cases. When aufs decide forcing, the info is always
2137 +  notified to userspace.
2138 +  For example, the number of free inodes is generally large enough and
2139 +  the shortage of it happens rarely. So aufs doesn't force the
2140 +  notification when creating a new file, directory and others. This is
2141 +  the typical case which aufs doesn't force.
2142 +  When aufs writes the actual filedata and the files consumes any of new
2143 +  blocks, the aufs forces notifying.
2144 +
2145 +
2146 +o Interfaces in aufs
2147 +- New branch attribute.
2148 +  + fhsm
2149 +    Specifies that the branch is managed by FHSM feature. In other word,
2150 +    participant in the FHSM.
2151 +    When nofhsm is set to the branch, it will not be the source/target
2152 +    branch of the move-down operation. This attribute is set
2153 +    independently from coo and moo attributes, and if you want full
2154 +    FHSM, you should specify them as well.
2155 +- New mount option.
2156 +  + fhsm_sec
2157 +    Specifies a second to suppress many less important info to be
2158 +    notified.
2159 +- New ioctl.
2160 +  + AUFS_CTL_FHSM_FD
2161 +    create a new file descriptor which userspace can read the notification
2162 +    (a subset of struct statfs) from aufs.
2163 +- Module parameter 'brs'
2164 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2165 +  be set.
2166 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2167 +  When there are two or more branches with fhsm attributes,
2168 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2169 +  terminates it. As a result of remounting and branch-manipulation, the
2170 +  number of branches with fhsm attribute can be one. In this case,
2171 +  /sbin/mount.aufs will terminate the user-space daemon.
2172 +
2173 +
2174 +Finally the operation is done as these steps in kernel-space.
2175 +- make sure that,
2176 +  + no one else is using the file.
2177 +  + the file is not hard-linked.
2178 +  + the file is not pseudo-linked.
2179 +  + the file is a regular file.
2180 +  + the parent dir is not opaqued.
2181 +- find the target writable branch.
2182 +- make sure the file is not whiteout-ed by the upper (than the target)
2183 +  branch.
2184 +- make the parent dir on the target branch.
2185 +- mutex lock the inode on the branch.
2186 +- unlink the whiteout on the target branch (if exists).
2187 +- lookup and create the whiteout-ed temporary name on the target branch.
2188 +- copy the file as the whiteout-ed temporary name on the target branch.
2189 +- rename the whiteout-ed temporary name to the original name.
2190 +- unlink the file on the source branch.
2191 +- maintain the internal pointer array and the external inode number
2192 +  table (XINO).
2193 +- maintain the timestamps and other attributes of the parent dir and the
2194 +  file.
2195 +
2196 +And of course, in every step, an error may happen. So the operation
2197 +should restore the original file state after an error happens.
2198 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2199 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2200 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2019-07-11 15:42:14.458904362 +0200
2201 @@ -0,0 +1,72 @@
2202 +
2203 +# Copyright (C) 2005-2019 Junjiro R. Okajima
2204 +# 
2205 +# This program is free software; you can redistribute it and/or modify
2206 +# it under the terms of the GNU General Public License as published by
2207 +# the Free Software Foundation; either version 2 of the License, or
2208 +# (at your option) any later version.
2209 +# 
2210 +# This program is distributed in the hope that it will be useful,
2211 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2212 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2213 +# GNU General Public License for more details.
2214 +# 
2215 +# You should have received a copy of the GNU General Public License
2216 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2217 +
2218 +mmap(2) -- File Memory Mapping
2219 +----------------------------------------------------------------------
2220 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2221 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2222 +->mmap().
2223 +This approach is simple and good, but there is one problem.
2224 +Under /proc, several entries show the mmapped files by its path (with
2225 +device and inode number), and the printed path will be the path on the
2226 +branch fs's instead of virtual aufs's.
2227 +This is not a problem in most cases, but some utilities lsof(1) (and its
2228 +user) may expect the path on aufs.
2229 +
2230 +To address this issue, aufs adds a new member called vm_prfile in struct
2231 +vm_area_struct (and struct vm_region). The original vm_file points to
2232 +the file on the branch fs in order to handle everything correctly as
2233 +usual. The new vm_prfile points to a virtual file in aufs, and the
2234 +show-functions in procfs refers to vm_prfile if it is set.
2235 +Also we need to maintain several other places where touching vm_file
2236 +such like
2237 +- fork()/clone() copies vma and the reference count of vm_file is
2238 +  incremented.
2239 +- merging vma maintains the ref count too.
2240 +
2241 +This is not a good approach. It just fakes the printed path. But it
2242 +leaves all behaviour around f_mapping unchanged. This is surely an
2243 +advantage.
2244 +Actually aufs had adopted another complicated approach which calls
2245 +generic_file_mmap() and handles struct vm_operations_struct. In this
2246 +approach, aufs met a hard problem and I could not solve it without
2247 +switching the approach.
2248 +
2249 +There may be one more another approach which is
2250 +- bind-mount the branch-root onto the aufs-root internally
2251 +- grab the new vfsmount (ie. struct mount)
2252 +- lazy-umount the branch-root internally
2253 +- in open(2) the aufs-file, open the branch-file with the hidden
2254 +  vfsmount (instead of the original branch's vfsmount)
2255 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2256 +  but it may be possible from userspace by the mount helper.
2257 +
2258 +Adding the internal hidden vfsmount and using it in opening a file, the
2259 +file path under /proc will be printed correctly. This approach looks
2260 +smarter, but is not possible I am afraid.
2261 +- aufs-root may be bind-mount later. when it happens, another hidden
2262 +  vfsmount will be required.
2263 +- it is hard to get the chance to bind-mount and lazy-umount
2264 +  + in kernel-space, FS can have vfsmount in open(2) via
2265 +    file->f_path, and aufs can know its vfsmount. But several locks are
2266 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2267 +    here, then it may cause a deadlock.
2268 +  + in user-space, bind-mount doesn't invoke the mount helper.
2269 +- since /proc shows dev and ino, aufs has to give vma these info. it
2270 +  means a new member vm_prinode will be necessary. this is essentially
2271 +  equivalent to vm_prfile described above.
2272 +
2273 +I have to give up this "looks-smater" approach.
2274 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2275 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2276 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2019-07-11 15:42:14.458904362 +0200
2277 @@ -0,0 +1,96 @@
2278 +
2279 +# Copyright (C) 2014-2019 Junjiro R. Okajima
2280 +#
2281 +# This program is free software; you can redistribute it and/or modify
2282 +# it under the terms of the GNU General Public License as published by
2283 +# the Free Software Foundation; either version 2 of the License, or
2284 +# (at your option) any later version.
2285 +#
2286 +# This program is distributed in the hope that it will be useful,
2287 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2288 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2289 +# GNU General Public License for more details.
2290 +#
2291 +# You should have received a copy of the GNU General Public License
2292 +# along with this program; if not, write to the Free Software
2293 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2294 +
2295 +
2296 +Listing XATTR/EA and getting the value
2297 +----------------------------------------------------------------------
2298 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2299 +shows the values from the topmost existing file. This behaviour is good
2300 +for the non-dir entries since the bahaviour exactly matches the shown
2301 +information. But for the directories, aufs considers all the same named
2302 +entries on the lower branches. Which means, if one of the lower entry
2303 +rejects readdir call, then aufs returns an error even if the topmost
2304 +entry allows it. This behaviour is necessary to respect the branch fs's
2305 +security, but can make users confused since the user-visible standard
2306 +attributes don't match the behaviour.
2307 +To address this issue, aufs has a mount option called dirperm1 which
2308 +checks the permission for the topmost entry only, and ignores the lower
2309 +entry's permission.
2310 +
2311 +A similar issue can happen around XATTR.
2312 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2313 +always set. Otherwise these very unpleasant situation would happen.
2314 +- listxattr(2) may return the duplicated entries.
2315 +- users may not be able to remove or reset the XATTR forever,
2316 +
2317 +
2318 +XATTR/EA support in the internal (copy,move)-(up,down)
2319 +----------------------------------------------------------------------
2320 +Generally the extended attributes of inode are categorized as these.
2321 +- "security" for LSM and capability.
2322 +- "system" for posix ACL, 'acl' mount option is required for the branch
2323 +  fs generally.
2324 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2325 +- "user" for userspace, 'user_xattr' mount option is required for the
2326 +  branch fs generally.
2327 +
2328 +Moreover there are some other categories. Aufs handles these rather
2329 +unpopular categories as the ordinary ones, ie. there is no special
2330 +condition nor exception.
2331 +
2332 +In copy-up, the support for XATTR on the dst branch may differ from the
2333 +src branch. In this case, the copy-up operation will get an error and
2334 +the original user operation which triggered the copy-up will fail. It
2335 +can happen that even all copy-up will fail.
2336 +When both of src and dst branches support XATTR and if an error occurs
2337 +during copying XATTR, then the copy-up should fail obviously. That is a
2338 +good reason and aufs should return an error to userspace. But when only
2339 +the src branch support that XATTR, aufs should not return an error.
2340 +For example, the src branch supports ACL but the dst branch doesn't
2341 +because the dst branch may natively un-support it or temporary
2342 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2343 +may NOT return an error even if the XATTR is not supported. It is
2344 +totally up to the branch fs.
2345 +
2346 +Anyway when the aufs internal copy-up gets an error from the dst branch
2347 +fs, then aufs tries removing the just copied entry and returns the error
2348 +to the userspace. The worst case of this situation will be all copy-up
2349 +will fail.
2350 +
2351 +For the copy-up operation, there two basic approaches.
2352 +- copy the specified XATTR only (by category above), and return the
2353 +  error unconditionally if it happens.
2354 +- copy all XATTR, and ignore the error on the specified category only.
2355 +
2356 +In order to support XATTR and to implement the correct behaviour, aufs
2357 +chooses the latter approach and introduces some new branch attributes,
2358 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2359 +They correspond to the XATTR namespaces (see above). Additionally, to be
2360 +convenient, "icex" is also provided which means all "icex*" attributes
2361 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2362 +
2363 +The meaning of these attributes is to ignore the error from setting
2364 +XATTR on that branch.
2365 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2366 +error from the dst branch according to the specified attributes.
2367 +
2368 +Some XATTR may have its default value. The default value may come from
2369 +the parent dir or the environment. If the default value is set at the
2370 +file creating-time, it will be overwritten by copy-up.
2371 +Some contradiction may happen I am afraid.
2372 +Do we need another attribute to stop copying XATTR? I am unsure. For
2373 +now, aufs implements the branch attributes to ignore the error.
2374 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2375 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2376 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2019-07-11 15:42:14.458904362 +0200
2377 @@ -0,0 +1,58 @@
2378 +
2379 +# Copyright (C) 2005-2019 Junjiro R. Okajima
2380 +# 
2381 +# This program is free software; you can redistribute it and/or modify
2382 +# it under the terms of the GNU General Public License as published by
2383 +# the Free Software Foundation; either version 2 of the License, or
2384 +# (at your option) any later version.
2385 +# 
2386 +# This program is distributed in the hope that it will be useful,
2387 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2388 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2389 +# GNU General Public License for more details.
2390 +# 
2391 +# You should have received a copy of the GNU General Public License
2392 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2393 +
2394 +Export Aufs via NFS
2395 +----------------------------------------------------------------------
2396 +Here is an approach.
2397 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2398 +  generation.
2399 +- iget_locked(): initialize aufs inode generation for a new inode, and
2400 +  store it in xigen file.
2401 +- destroy_inode(): increment aufs inode generation and store it in xigen
2402 +  file. it is necessary even if it is not unlinked, because any data of
2403 +  inode may be changed by UDBA.
2404 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2405 +  build file handle by
2406 +  + branch id (4 bytes)
2407 +  + superblock generation (4 bytes)
2408 +  + inode number (4 or 8 bytes)
2409 +  + parent dir inode number (4 or 8 bytes)
2410 +  + inode generation (4 bytes))
2411 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2412 +    bytes)
2413 +  + file handle for a branch (by exportfs_encode_fh())
2414 +- fh_to_dentry():
2415 +  + find the index of a branch from its id in handle, and check it is
2416 +    still exist in aufs.
2417 +  + 1st level: get the inode number from handle and search it in cache.
2418 +  + 2nd level: if not found in cache, get the parent inode number from
2419 +    the handle and search it in cache. and then open the found parent
2420 +    dir, find the matching inode number by vfs_readdir() and get its
2421 +    name, and call lookup_one_len() for the target dentry.
2422 +  + 3rd level: if the parent dir is not cached, call
2423 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2424 +    build a pathname of it, convert it a pathname in aufs, call
2425 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2426 +    the 2nd level.
2427 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2428 +    for every branch, but not itself. to get this, (currently) aufs
2429 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2430 +    idea, but I didn't get other approach.
2431 +  + test the generation of the gotten inode.
2432 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2433 +  convert it into ESTALE for NFSD.
2434 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2435 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2436 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2437 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2438 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2019-07-11 15:42:14.458904362 +0200
2439 @@ -0,0 +1,52 @@
2440 +
2441 +# Copyright (C) 2005-2019 Junjiro R. Okajima
2442 +# 
2443 +# This program is free software; you can redistribute it and/or modify
2444 +# it under the terms of the GNU General Public License as published by
2445 +# the Free Software Foundation; either version 2 of the License, or
2446 +# (at your option) any later version.
2447 +# 
2448 +# This program is distributed in the hope that it will be useful,
2449 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2450 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2451 +# GNU General Public License for more details.
2452 +# 
2453 +# You should have received a copy of the GNU General Public License
2454 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2455 +
2456 +Show Whiteout Mode (shwh)
2457 +----------------------------------------------------------------------
2458 +Generally aufs hides the name of whiteouts. But in some cases, to show
2459 +them is very useful for users. For instance, creating a new middle layer
2460 +(branch) by merging existing layers.
2461 +
2462 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2463 +When you have three branches,
2464 +- Bottom: 'system', squashfs (underlying base system), read-only
2465 +- Middle: 'mods', squashfs, read-only
2466 +- Top: 'overlay', ram (tmpfs), read-write
2467 +
2468 +The top layer is loaded at boot time and saved at shutdown, to preserve
2469 +the changes made to the system during the session.
2470 +When larger changes have been made, or smaller changes have accumulated,
2471 +the size of the saved top layer data grows. At this point, it would be
2472 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2473 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2474 +restoring save and load speed.
2475 +
2476 +This merging is simplified by the use of another aufs mount, of just the
2477 +two overlay branches using the 'shwh' option.
2478 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2479 +       aufs /livesys/merge_union
2480 +
2481 +A merged view of these two branches is then available at
2482 +/livesys/merge_union, and the new feature is that the whiteouts are
2483 +visible!
2484 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2485 +writing to all branches. Also the default mode for all branches is 'ro'.
2486 +It is now possible to save the combined contents of the two overlay
2487 +branches to a new squashfs, e.g.:
2488 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2489 +
2490 +This new squashfs archive can be stored on the boot device and the
2491 +initramfs will use it to replace the old one at the next boot.
2492 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2493 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2494 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2019-07-11 15:42:14.458904362 +0200
2495 @@ -0,0 +1,47 @@
2496 +
2497 +# Copyright (C) 2010-2019 Junjiro R. Okajima
2498 +#
2499 +# This program is free software; you can redistribute it and/or modify
2500 +# it under the terms of the GNU General Public License as published by
2501 +# the Free Software Foundation; either version 2 of the License, or
2502 +# (at your option) any later version.
2503 +#
2504 +# This program is distributed in the hope that it will be useful,
2505 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2506 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2507 +# GNU General Public License for more details.
2508 +#
2509 +# You should have received a copy of the GNU General Public License
2510 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2511 +
2512 +Dynamically customizable FS operations
2513 +----------------------------------------------------------------------
2514 +Generally FS operations (struct inode_operations, struct
2515 +address_space_operations, struct file_operations, etc.) are defined as
2516 +"static const", but it never means that FS have only one set of
2517 +operation. Some FS have multiple sets of them. For instance, ext2 has
2518 +three sets, one for XIP, for NOBH, and for normal.
2519 +Since aufs overrides and redirects these operations, sometimes aufs has
2520 +to change its behaviour according to the branch FS type. More importantly
2521 +VFS acts differently if a function (member in the struct) is set or
2522 +not. It means aufs should have several sets of operations and select one
2523 +among them according to the branch FS definition.
2524 +
2525 +In order to solve this problem and not to affect the behaviour of VFS,
2526 +aufs defines these operations dynamically. For instance, aufs defines
2527 +dummy direct_IO function for struct address_space_operations, but it may
2528 +not be set to the address_space_operations actually. When the branch FS
2529 +doesn't have it, aufs doesn't set it to its address_space_operations
2530 +while the function definition itself is still alive. So the behaviour
2531 +itself will not change, and it will return an error when direct_IO is
2532 +not set.
2533 +
2534 +The lifetime of these dynamically generated operation object is
2535 +maintained by aufs branch object. When the branch is removed from aufs,
2536 +the reference counter of the object is decremented. When it reaches
2537 +zero, the dynamically generated operation object will be freed.
2538 +
2539 +This approach is designed to support AIO (io_submit), Direct I/O and
2540 +XIP (DAX) mainly.
2541 +Currently this approach is applied to address_space_operations for
2542 +regular files only.
2543 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2544 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2545 +++ linux/Documentation/filesystems/aufs/README 2019-07-11 15:42:14.455570938 +0200
2546 @@ -0,0 +1,399 @@
2547 +
2548 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2549 +http://aufs.sf.net
2550 +Junjiro R. Okajima
2551 +
2552 +
2553 +0. Introduction
2554 +----------------------------------------
2555 +In the early days, aufs was entirely re-designed and re-implemented
2556 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2557 +improvements and implementations, it became totally different from
2558 +Unionfs while keeping the basic features.
2559 +Later, Unionfs Version 2.x series began taking some of the same
2560 +approaches to aufs1's.
2561 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2562 +University and his team.
2563 +
2564 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2565 +support,
2566 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2567 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2568 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2569 +  or aufs1 from CVS on SourceForge.
2570 +
2571 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2572 +      According to Christoph Hellwig, linux rejects all union-type
2573 +      filesystems but UnionMount.
2574 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2575 +
2576 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2577 +    UnionMount, and he pointed out an issue around a directory mutex
2578 +    lock and aufs addressed it. But it is still unsure whether aufs will
2579 +    be merged (or any other union solution).
2580 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2581 +
2582 +
2583 +1. Features
2584 +----------------------------------------
2585 +- unite several directories into a single virtual filesystem. The member
2586 +  directory is called as a branch.
2587 +- you can specify the permission flags to the branch, which are 'readonly',
2588 +  'readwrite' and 'whiteout-able.'
2589 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2590 +  readonly branch are modifiable logically.
2591 +- dynamic branch manipulation, add, del.
2592 +- etc...
2593 +
2594 +Also there are many enhancements in aufs, such as:
2595 +- test only the highest one for the directory permission (dirperm1)
2596 +- copyup on open (coo=)
2597 +- 'move' policy for copy-up between two writable branches, after
2598 +  checking free space.
2599 +- xattr, acl
2600 +- readdir(3) in userspace.
2601 +- keep inode number by external inode number table
2602 +- keep the timestamps of file/dir in internal copyup operation
2603 +- seekable directory, supporting NFS readdir.
2604 +- whiteout is hardlinked in order to reduce the consumption of inodes
2605 +  on branch
2606 +- do not copyup, nor create a whiteout when it is unnecessary
2607 +- revert a single systemcall when an error occurs in aufs
2608 +- remount interface instead of ioctl
2609 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2610 +- loopback mounted filesystem as a branch
2611 +- kernel thread for removing the dir who has a plenty of whiteouts
2612 +- support copyup sparse file (a file which has a 'hole' in it)
2613 +- default permission flags for branches
2614 +- selectable permission flags for ro branch, whether whiteout can
2615 +  exist or not
2616 +- export via NFS.
2617 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2618 +- support multiple writable branches, some policies to select one
2619 +  among multiple writable branches.
2620 +- a new semantics for link(2) and rename(2) to support multiple
2621 +  writable branches.
2622 +- no glibc changes are required.
2623 +- pseudo hardlink (hardlink over branches)
2624 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2625 +  including NFS or remote filesystem branch.
2626 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2627 +- and more...
2628 +
2629 +Currently these features are dropped temporary from aufs5.
2630 +See design/08plan.txt in detail.
2631 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2632 +  (robr)
2633 +- statistics of aufs thread (/sys/fs/aufs/stat)
2634 +
2635 +Features or just an idea in the future (see also design/*.txt),
2636 +- reorder the branch index without del/re-add.
2637 +- permanent xino files for NFSD
2638 +- an option for refreshing the opened files after add/del branches
2639 +- light version, without branch manipulation. (unnecessary?)
2640 +- copyup in userspace
2641 +- inotify in userspace
2642 +- readv/writev
2643 +
2644 +
2645 +2. Download
2646 +----------------------------------------
2647 +There are three GIT trees for aufs5, aufs5-linux.git,
2648 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2649 +"aufs-util.git."
2650 +While the aufs-util is always necessary, you need either of aufs5-linux
2651 +or aufs5-standalone.
2652 +
2653 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2654 +git://git.kernel.org/.../torvalds/linux.git.
2655 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2656 +build aufs5 as an external kernel module.
2657 +Several extra patches are not included in this tree. Only
2658 +aufs5-standalone tree contains them. They are described in the later
2659 +section "Configuration and Compilation."
2660 +
2661 +On the other hand, the aufs5-standalone tree has only aufs source files
2662 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2663 +But you need to apply all aufs patches manually.
2664 +
2665 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2666 +represents the linux kernel version, "linux-5.x". For instance,
2667 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2668 +"aufs5.x-rcN" branch.
2669 +
2670 +o aufs5-linux tree
2671 +$ git clone --reference /your/linux/git/tree \
2672 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2673 +- if you don't have linux GIT tree, then remove "--reference ..."
2674 +$ cd aufs5-linux.git
2675 +$ git checkout origin/aufs5.0
2676 +
2677 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2678 +leave the patch-work to GIT.
2679 +$ cd /your/linux/git/tree
2680 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2681 +$ git fetch aufs5
2682 +$ git checkout -b my5.0 v5.0
2683 +$ (add your local change...)
2684 +$ git pull aufs5 aufs5.0
2685 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2686 +- you may need to solve some conflicts between your_changes and
2687 +  aufs5.0. in this case, git-rerere is recommended so that you can
2688 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2689 +  later in the future.
2690 +
2691 +o aufs5-standalone tree
2692 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2693 +$ cd aufs5-standalone.git
2694 +$ git checkout origin/aufs5.0
2695 +
2696 +o aufs-util tree
2697 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2698 +- note that the public aufs-util.git is on SourceForge instead of
2699 +  GitHUB.
2700 +$ cd aufs-util.git
2701 +$ git checkout origin/aufs5.0
2702 +
2703 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2704 +The minor version number, 'x' in '5.x', of aufs may not always
2705 +follow the minor version number of the kernel.
2706 +Because changes in the kernel that cause the use of a new
2707 +minor version number do not always require changes to aufs-util.
2708 +
2709 +Since aufs-util has its own minor version number, you may not be
2710 +able to find a GIT branch in aufs-util for your kernel's
2711 +exact minor version number.
2712 +In this case, you should git-checkout the branch for the
2713 +nearest lower number.
2714 +
2715 +For (an unreleased) example:
2716 +If you are using "linux-5.10" and the "aufs5.10" branch
2717 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2718 +or something numerically smaller is the branch for your kernel.
2719 +
2720 +Also you can view all branches by
2721 +       $ git branch -a
2722 +
2723 +
2724 +3. Configuration and Compilation
2725 +----------------------------------------
2726 +Make sure you have git-checkout'ed the correct branch.
2727 +
2728 +For aufs5-linux tree,
2729 +- enable CONFIG_AUFS_FS.
2730 +- set other aufs configurations if necessary.
2731 +
2732 +For aufs5-standalone tree,
2733 +There are several ways to build.
2734 +
2735 +1.
2736 +- apply ./aufs5-kbuild.patch to your kernel source files.
2737 +- apply ./aufs5-base.patch too.
2738 +- apply ./aufs5-mmap.patch too.
2739 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2740 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2741 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2742 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2743 +- enable CONFIG_AUFS_FS, you can select either
2744 +  =m or =y.
2745 +- and build your kernel as usual.
2746 +- install the built kernel.
2747 +- install the header files too by "make headers_install" to the
2748 +  directory where you specify. By default, it is $PWD/usr.
2749 +  "make help" shows a brief note for headers_install.
2750 +- and reboot your system.
2751 +
2752 +2.
2753 +- module only (CONFIG_AUFS_FS=m).
2754 +- apply ./aufs5-base.patch to your kernel source files.
2755 +- apply ./aufs5-mmap.patch too.
2756 +- apply ./aufs5-standalone.patch too.
2757 +- build your kernel, don't forget "make headers_install", and reboot.
2758 +- edit ./config.mk and set other aufs configurations if necessary.
2759 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2760 +  every aufs configurations.
2761 +- build the module by simple "make".
2762 +- you can specify ${KDIR} make variable which points to your kernel
2763 +  source tree.
2764 +- install the files
2765 +  + run "make install" to install the aufs module, or copy the built
2766 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2767 +  + run "make install_headers" (instead of headers_install) to install
2768 +    the modified aufs header file (you can specify DESTDIR which is
2769 +    available in aufs standalone version's Makefile only), or copy
2770 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2771 +    you like manually. By default, the target directory is $PWD/usr.
2772 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2773 +  kernel source tree.
2774 +
2775 +Note: The header file aufs_type.h is necessary to build aufs-util
2776 +      as well as "make headers_install" in the kernel source tree.
2777 +      headers_install is subject to be forgotten, but it is essentially
2778 +      necessary, not only for building aufs-util.
2779 +      You may not meet problems without headers_install in some older
2780 +      version though.
2781 +
2782 +And then,
2783 +- read README in aufs-util, build and install it
2784 +- note that your distribution may contain an obsoleted version of
2785 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2786 +  utilities, make sure that your compiler refers the correct aufs header
2787 +  file which is built by "make headers_install."
2788 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2789 +  then run "make install_ulib" too. And refer to the aufs manual in
2790 +  detail.
2791 +
2792 +There several other patches in aufs5-standalone.git. They are all
2793 +optional. When you meet some problems, they will help you.
2794 +- aufs5-loopback.patch
2795 +  Supports a nested loopback mount in a branch-fs. This patch is
2796 +  unnecessary until aufs produces a message like "you may want to try
2797 +  another patch for loopback file".
2798 +- proc_mounts.patch
2799 +  When there are many mountpoints and many mount(2)/umount(2) are
2800 +  running, then /proc/mounts may not show the all mountpoints.  This
2801 +  patch makes /proc/mounts always show the full mountpoints list.
2802 +  If you don't want to apply this patch and meet such problem, then you
2803 +  need to increase the value of 'ProcMounts_Times' make-variable in
2804 +  aufs-util.git as a second best solution.
2805 +- vfs-ino.patch
2806 +  Modifies a system global kernel internal function get_next_ino() in
2807 +  order to stop assigning 0 for an inode-number. Not directly related to
2808 +  aufs, but recommended generally.
2809 +- tmpfs-idr.patch
2810 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2811 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2812 +  duplication of inode number, which is important for backup tools and
2813 +  other utilities. When you find aufs XINO files for tmpfs branch
2814 +  growing too much, try this patch.
2815 +- lockdep-debug.patch
2816 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2817 +  also a caller of VFS functions for branch filesystems, subclassing of
2818 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2819 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2820 +  need to apply this debug patch to expand several constant values.
2821 +  If don't know what LOCKDEP is, then you don't have apply this patch.
2822 +
2823 +
2824 +4. Usage
2825 +----------------------------------------
2826 +At first, make sure aufs-util are installed, and please read the aufs
2827 +manual, aufs.5 in aufs-util.git tree.
2828 +$ man -l aufs.5
2829 +
2830 +And then,
2831 +$ mkdir /tmp/rw /tmp/aufs
2832 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2833 +
2834 +Here is another example. The result is equivalent.
2835 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2836 +  Or
2837 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2838 +# mount -o remount,append:${HOME} /tmp/aufs
2839 +
2840 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2841 +you modify a file under /tmp/aufs, the one on your home directory is
2842 +not affected, instead the same named file will be newly created under
2843 +/tmp/rw. And all of your modification to a file will be applied to
2844 +the one under /tmp/rw. This is called the file based Copy on Write
2845 +(COW) method.
2846 +Aufs mount options are described in aufs.5.
2847 +If you run chroot or something and make your aufs as a root directory,
2848 +then you need to customize the shutdown script. See the aufs manual in
2849 +detail.
2850 +
2851 +Additionally, there are some sample usages of aufs which are a
2852 +diskless system with network booting, and LiveCD over NFS.
2853 +See sample dir in CVS tree on SourceForge.
2854 +
2855 +
2856 +5. Contact
2857 +----------------------------------------
2858 +When you have any problems or strange behaviour in aufs, please let me
2859 +know with:
2860 +- /proc/mounts (instead of the output of mount(8))
2861 +- /sys/module/aufs/*
2862 +- /sys/fs/aufs/* (if you have them)
2863 +- /debug/aufs/* (if you have them)
2864 +- linux kernel version
2865 +  if your kernel is not plain, for example modified by distributor,
2866 +  the url where i can download its source is necessary too.
2867 +- aufs version which was printed at loading the module or booting the
2868 +  system, instead of the date you downloaded.
2869 +- configuration (define/undefine CONFIG_AUFS_xxx)
2870 +- kernel configuration or /proc/config.gz (if you have it)
2871 +- behaviour which you think to be incorrect
2872 +- actual operation, reproducible one is better
2873 +- mailto: aufs-users at lists.sourceforge.net
2874 +
2875 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2876 +and Feature Requests) on SourceForge. Please join and write to
2877 +aufs-users ML.
2878 +
2879 +
2880 +6. Acknowledgements
2881 +----------------------------------------
2882 +Thanks to everyone who have tried and are using aufs, whoever
2883 +have reported a bug or any feedback.
2884 +
2885 +Especially donators:
2886 +Tomas Matejicek(slax.org) made a donation (much more than once).
2887 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2888 +       scripts) is making "doubling" donations.
2889 +       Unfortunately I cannot list all of the donators, but I really
2890 +       appreciate.
2891 +       It ends Aug 2010, but the ordinary donation URL is still available.
2892 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2893 +Dai Itasaka made a donation (2007/8).
2894 +Chuck Smith made a donation (2008/4, 10 and 12).
2895 +Henk Schoneveld made a donation (2008/9).
2896 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2897 +Francois Dupoux made a donation (2008/11).
2898 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2899 +       aufs2 GIT tree (2009/2).
2900 +William Grant made a donation (2009/3).
2901 +Patrick Lane made a donation (2009/4).
2902 +The Mail Archive (mail-archive.com) made donations (2009/5).
2903 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2904 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2905 +Pavel Pronskiy made a donation (2011/2).
2906 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2907 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2908 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2909 +11).
2910 +Sam Liddicott made a donation (2011/9).
2911 +Era Scarecrow made a donation (2013/4).
2912 +Bor Ratajc made a donation (2013/4).
2913 +Alessandro Gorreta made a donation (2013/4).
2914 +POIRETTE Marc made a donation (2013/4).
2915 +Alessandro Gorreta made a donation (2013/4).
2916 +lauri kasvandik made a donation (2013/5).
2917 +"pemasu from Finland" made a donation (2013/7).
2918 +The Parted Magic Project made a donation (2013/9 and 11).
2919 +Pavel Barta made a donation (2013/10).
2920 +Nikolay Pertsev made a donation (2014/5).
2921 +James B made a donation (2014/7 and 2015/7).
2922 +Stefano Di Biase made a donation (2014/8).
2923 +Daniel Epellei made a donation (2015/1).
2924 +OmegaPhil made a donation (2016/1, 2018/4).
2925 +Tomasz Szewczyk made a donation (2016/4).
2926 +James Burry made a donation (2016/12).
2927 +Carsten Rose made a donation (2018/9).
2928 +Porteus Kiosk made a donation (2018/10).
2929 +
2930 +Thank you very much.
2931 +Donations are always, including future donations, very important and
2932 +helpful for me to keep on developing aufs.
2933 +
2934 +
2935 +7.
2936 +----------------------------------------
2937 +If you are an experienced user, no explanation is needed. Aufs is
2938 +just a linux filesystem.
2939 +
2940 +
2941 +Enjoy!
2942 +
2943 +# Local variables: ;
2944 +# mode: text;
2945 +# End: ;
2946 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2947 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2948 +++ linux/fs/aufs/aufs.h        2019-07-11 15:42:14.462237786 +0200
2949 @@ -0,0 +1,62 @@
2950 +/* SPDX-License-Identifier: GPL-2.0 */
2951 +/*
2952 + * Copyright (C) 2005-2019 Junjiro R. Okajima
2953 + *
2954 + * This program, aufs is free software; you can redistribute it and/or modify
2955 + * it under the terms of the GNU General Public License as published by
2956 + * the Free Software Foundation; either version 2 of the License, or
2957 + * (at your option) any later version.
2958 + *
2959 + * This program is distributed in the hope that it will be useful,
2960 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2961 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2962 + * GNU General Public License for more details.
2963 + *
2964 + * You should have received a copy of the GNU General Public License
2965 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2966 + */
2967 +
2968 +/*
2969 + * all header files
2970 + */
2971 +
2972 +#ifndef __AUFS_H__
2973 +#define __AUFS_H__
2974 +
2975 +#ifdef __KERNEL__
2976 +
2977 +#define AuStub(type, name, body, ...) \
2978 +       static inline type name(__VA_ARGS__) { body; }
2979 +
2980 +#define AuStubVoid(name, ...) \
2981 +       AuStub(void, name, , __VA_ARGS__)
2982 +#define AuStubInt0(name, ...) \
2983 +       AuStub(int, name, return 0, __VA_ARGS__)
2984 +
2985 +#include "debug.h"
2986 +
2987 +#include "branch.h"
2988 +#include "cpup.h"
2989 +#include "dcsub.h"
2990 +#include "dbgaufs.h"
2991 +#include "dentry.h"
2992 +#include "dir.h"
2993 +#include "dirren.h"
2994 +#include "dynop.h"
2995 +#include "file.h"
2996 +#include "fstype.h"
2997 +#include "hbl.h"
2998 +#include "inode.h"
2999 +#include "lcnt.h"
3000 +#include "loop.h"
3001 +#include "module.h"
3002 +#include "opts.h"
3003 +#include "rwsem.h"
3004 +#include "super.h"
3005 +#include "sysaufs.h"
3006 +#include "vfsub.h"
3007 +#include "whout.h"
3008 +#include "wkq.h"
3009 +
3010 +#endif /* __KERNEL__ */
3011 +#endif /* __AUFS_H__ */
3012 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3013 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
3014 +++ linux/fs/aufs/branch.c      2019-09-16 09:38:43.216175640 +0200
3015 @@ -0,0 +1,1428 @@
3016 +// SPDX-License-Identifier: GPL-2.0
3017 +/*
3018 + * Copyright (C) 2005-2019 Junjiro R. Okajima
3019 + *
3020 + * This program, aufs is free software; you can redistribute it and/or modify
3021 + * it under the terms of the GNU General Public License as published by
3022 + * the Free Software Foundation; either version 2 of the License, or
3023 + * (at your option) any later version.
3024 + *
3025 + * This program is distributed in the hope that it will be useful,
3026 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3027 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3028 + * GNU General Public License for more details.
3029 + *
3030 + * You should have received a copy of the GNU General Public License
3031 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3032 + */
3033 +
3034 +/*
3035 + * branch management
3036 + */
3037 +
3038 +#include <linux/compat.h>
3039 +#include <linux/statfs.h>
3040 +#include "aufs.h"
3041 +
3042 +/*
3043 + * free a single branch
3044 + */
3045 +static void au_br_do_free(struct au_branch *br)
3046 +{
3047 +       int i;
3048 +       struct au_wbr *wbr;
3049 +       struct au_dykey **key;
3050 +
3051 +       au_hnotify_fin_br(br);
3052 +       /* always, regardless the mount option */
3053 +       au_dr_hino_free(&br->br_dirren);
3054 +       au_xino_put(br);
3055 +
3056 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3057 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3058 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3059 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
3060 +
3061 +       wbr = br->br_wbr;
3062 +       if (wbr) {
3063 +               for (i = 0; i < AuBrWh_Last; i++)
3064 +                       dput(wbr->wbr_wh[i]);
3065 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3066 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3067 +       }
3068 +
3069 +       if (br->br_fhsm) {
3070 +               au_br_fhsm_fin(br->br_fhsm);
3071 +               au_kfree_try_rcu(br->br_fhsm);
3072 +       }
3073 +
3074 +       key = br->br_dykey;
3075 +       for (i = 0; i < AuBrDynOp; i++, key++)
3076 +               if (*key)
3077 +                       au_dy_put(*key);
3078 +               else
3079 +                       break;
3080 +
3081 +       /* recursive lock, s_umount of branch's */
3082 +       /* synchronize_rcu(); */ /* why? */
3083 +       lockdep_off();
3084 +       path_put(&br->br_path);
3085 +       lockdep_on();
3086 +       au_kfree_rcu(wbr);
3087 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3088 +       au_lcnt_wait_for_fin(&br->br_count);
3089 +       /* I don't know why, but percpu_refcount requires this */
3090 +       /* synchronize_rcu(); */
3091 +       au_kfree_rcu(br);
3092 +}
3093 +
3094 +/*
3095 + * frees all branches
3096 + */
3097 +void au_br_free(struct au_sbinfo *sbinfo)
3098 +{
3099 +       aufs_bindex_t bmax;
3100 +       struct au_branch **br;
3101 +
3102 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3103 +
3104 +       bmax = sbinfo->si_bbot + 1;
3105 +       br = sbinfo->si_branch;
3106 +       while (bmax--)
3107 +               au_br_do_free(*br++);
3108 +}
3109 +
3110 +/*
3111 + * find the index of a branch which is specified by @br_id.
3112 + */
3113 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3114 +{
3115 +       aufs_bindex_t bindex, bbot;
3116 +
3117 +       bbot = au_sbbot(sb);
3118 +       for (bindex = 0; bindex <= bbot; bindex++)
3119 +               if (au_sbr_id(sb, bindex) == br_id)
3120 +                       return bindex;
3121 +       return -1;
3122 +}
3123 +
3124 +/* ---------------------------------------------------------------------- */
3125 +
3126 +/*
3127 + * add a branch
3128 + */
3129 +
3130 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3131 +                       struct dentry *h_root)
3132 +{
3133 +       if (unlikely(h_adding == h_root
3134 +                    || au_test_loopback_overlap(sb, h_adding)))
3135 +               return 1;
3136 +       if (h_adding->d_sb != h_root->d_sb)
3137 +               return 0;
3138 +       return au_test_subdir(h_adding, h_root)
3139 +               || au_test_subdir(h_root, h_adding);
3140 +}
3141 +
3142 +/*
3143 + * returns a newly allocated branch. @new_nbranch is a number of branches
3144 + * after adding a branch.
3145 + */
3146 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3147 +                                    int perm)
3148 +{
3149 +       struct au_branch *add_branch;
3150 +       struct dentry *root;
3151 +       struct inode *inode;
3152 +       int err;
3153 +
3154 +       err = -ENOMEM;
3155 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3156 +       if (unlikely(!add_branch))
3157 +               goto out;
3158 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3159 +       if (unlikely(!add_branch->br_xino))
3160 +               goto out_br;
3161 +       err = au_hnotify_init_br(add_branch, perm);
3162 +       if (unlikely(err))
3163 +               goto out_xino;
3164 +
3165 +       if (au_br_writable(perm)) {
3166 +               /* may be freed separately at changing the branch permission */
3167 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3168 +                                            GFP_NOFS);
3169 +               if (unlikely(!add_branch->br_wbr))
3170 +                       goto out_hnotify;
3171 +       }
3172 +
3173 +       if (au_br_fhsm(perm)) {
3174 +               err = au_fhsm_br_alloc(add_branch);
3175 +               if (unlikely(err))
3176 +                       goto out_wbr;
3177 +       }
3178 +
3179 +       root = sb->s_root;
3180 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3181 +       if (!err)
3182 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3183 +       if (!err) {
3184 +               inode = d_inode(root);
3185 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3186 +                                       /*may_shrink*/0);
3187 +       }
3188 +       if (!err)
3189 +               return add_branch; /* success */
3190 +
3191 +out_wbr:
3192 +       au_kfree_rcu(add_branch->br_wbr);
3193 +out_hnotify:
3194 +       au_hnotify_fin_br(add_branch);
3195 +out_xino:
3196 +       au_xino_put(add_branch);
3197 +out_br:
3198 +       au_kfree_rcu(add_branch);
3199 +out:
3200 +       return ERR_PTR(err);
3201 +}
3202 +
3203 +/*
3204 + * test if the branch permission is legal or not.
3205 + */
3206 +static int test_br(struct inode *inode, int brperm, char *path)
3207 +{
3208 +       int err;
3209 +
3210 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3211 +       if (!err)
3212 +               goto out;
3213 +
3214 +       err = -EINVAL;
3215 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3216 +
3217 +out:
3218 +       return err;
3219 +}
3220 +
3221 +/*
3222 + * returns:
3223 + * 0: success, the caller will add it
3224 + * plus: success, it is already unified, the caller should ignore it
3225 + * minus: error
3226 + */
3227 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3228 +{
3229 +       int err;
3230 +       aufs_bindex_t bbot, bindex;
3231 +       struct dentry *root, *h_dentry;
3232 +       struct inode *inode, *h_inode;
3233 +
3234 +       root = sb->s_root;
3235 +       bbot = au_sbbot(sb);
3236 +       if (unlikely(bbot >= 0
3237 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3238 +               err = 1;
3239 +               if (!remount) {
3240 +                       err = -EINVAL;
3241 +                       pr_err("%s duplicated\n", add->pathname);
3242 +               }
3243 +               goto out;
3244 +       }
3245 +
3246 +       err = -ENOSPC; /* -E2BIG; */
3247 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3248 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3249 +               pr_err("number of branches exceeded %s\n", add->pathname);
3250 +               goto out;
3251 +       }
3252 +
3253 +       err = -EDOM;
3254 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3255 +               pr_err("bad index %d\n", add->bindex);
3256 +               goto out;
3257 +       }
3258 +
3259 +       inode = d_inode(add->path.dentry);
3260 +       err = -ENOENT;
3261 +       if (unlikely(!inode->i_nlink)) {
3262 +               pr_err("no existence %s\n", add->pathname);
3263 +               goto out;
3264 +       }
3265 +
3266 +       err = -EINVAL;
3267 +       if (unlikely(inode->i_sb == sb)) {
3268 +               pr_err("%s must be outside\n", add->pathname);
3269 +               goto out;
3270 +       }
3271 +
3272 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3273 +               pr_err("unsupported filesystem, %s (%s)\n",
3274 +                      add->pathname, au_sbtype(inode->i_sb));
3275 +               goto out;
3276 +       }
3277 +
3278 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3279 +               pr_err("already stacked, %s (%s)\n",
3280 +                      add->pathname, au_sbtype(inode->i_sb));
3281 +               goto out;
3282 +       }
3283 +
3284 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3285 +       if (unlikely(err))
3286 +               goto out;
3287 +
3288 +       if (bbot < 0)
3289 +               return 0; /* success */
3290 +
3291 +       err = -EINVAL;
3292 +       for (bindex = 0; bindex <= bbot; bindex++)
3293 +               if (unlikely(test_overlap(sb, add->path.dentry,
3294 +                                         au_h_dptr(root, bindex)))) {
3295 +                       pr_err("%s is overlapped\n", add->pathname);
3296 +                       goto out;
3297 +               }
3298 +
3299 +       err = 0;
3300 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3301 +               h_dentry = au_h_dptr(root, 0);
3302 +               h_inode = d_inode(h_dentry);
3303 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3304 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3305 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3306 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3307 +                               add->pathname,
3308 +                               i_uid_read(inode), i_gid_read(inode),
3309 +                               (inode->i_mode & S_IALLUGO),
3310 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3311 +                               (h_inode->i_mode & S_IALLUGO));
3312 +       }
3313 +
3314 +out:
3315 +       return err;
3316 +}
3317 +
3318 +/*
3319 + * initialize or clean the whiteouts for an adding branch
3320 + */
3321 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3322 +                        int new_perm)
3323 +{
3324 +       int err, old_perm;
3325 +       aufs_bindex_t bindex;
3326 +       struct inode *h_inode;
3327 +       struct au_wbr *wbr;
3328 +       struct au_hinode *hdir;
3329 +       struct dentry *h_dentry;
3330 +
3331 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3332 +       if (unlikely(err))
3333 +               goto out;
3334 +
3335 +       wbr = br->br_wbr;
3336 +       old_perm = br->br_perm;
3337 +       br->br_perm = new_perm;
3338 +       hdir = NULL;
3339 +       h_inode = NULL;
3340 +       bindex = au_br_index(sb, br->br_id);
3341 +       if (0 <= bindex) {
3342 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3343 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3344 +       } else {
3345 +               h_dentry = au_br_dentry(br);
3346 +               h_inode = d_inode(h_dentry);
3347 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3348 +       }
3349 +       if (!wbr)
3350 +               err = au_wh_init(br, sb);
3351 +       else {
3352 +               wbr_wh_write_lock(wbr);
3353 +               err = au_wh_init(br, sb);
3354 +               wbr_wh_write_unlock(wbr);
3355 +       }
3356 +       if (hdir)
3357 +               au_hn_inode_unlock(hdir);
3358 +       else
3359 +               inode_unlock(h_inode);
3360 +       vfsub_mnt_drop_write(au_br_mnt(br));
3361 +       br->br_perm = old_perm;
3362 +
3363 +       if (!err && wbr && !au_br_writable(new_perm)) {
3364 +               au_kfree_rcu(wbr);
3365 +               br->br_wbr = NULL;
3366 +       }
3367 +
3368 +out:
3369 +       return err;
3370 +}
3371 +
3372 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3373 +                      int perm)
3374 +{
3375 +       int err;
3376 +       struct kstatfs kst;
3377 +       struct au_wbr *wbr;
3378 +
3379 +       wbr = br->br_wbr;
3380 +       au_rw_init(&wbr->wbr_wh_rwsem);
3381 +       atomic_set(&wbr->wbr_wh_running, 0);
3382 +
3383 +       /*
3384 +        * a limit for rmdir/rename a dir
3385 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3386 +        */
3387 +       err = vfs_statfs(&br->br_path, &kst);
3388 +       if (unlikely(err))
3389 +               goto out;
3390 +       err = -EINVAL;
3391 +       if (kst.f_namelen >= NAME_MAX)
3392 +               err = au_br_init_wh(sb, br, perm);
3393 +       else
3394 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3395 +                      au_br_dentry(br),
3396 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3397 +
3398 +out:
3399 +       return err;
3400 +}
3401 +
3402 +/* initialize a new branch */
3403 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3404 +                     struct au_opt_add *add)
3405 +{
3406 +       int err;
3407 +       struct au_branch *brbase;
3408 +       struct file *xf;
3409 +       struct inode *h_inode;
3410 +
3411 +       err = 0;
3412 +       br->br_perm = add->perm;
3413 +       br->br_path = add->path; /* set first, path_get() later */
3414 +       spin_lock_init(&br->br_dykey_lock);
3415 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3416 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3417 +       br->br_id = au_new_br_id(sb);
3418 +       AuDebugOn(br->br_id < 0);
3419 +
3420 +       /* always, regardless the given option */
3421 +       err = au_dr_br_init(sb, br, &add->path);
3422 +       if (unlikely(err))
3423 +               goto out_err;
3424 +
3425 +       if (au_br_writable(add->perm)) {
3426 +               err = au_wbr_init(br, sb, add->perm);
3427 +               if (unlikely(err))
3428 +                       goto out_err;
3429 +       }
3430 +
3431 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3432 +               brbase = au_sbr(sb, 0);
3433 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3434 +               AuDebugOn(!xf);
3435 +               h_inode = d_inode(add->path.dentry);
3436 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3437 +               if (unlikely(err)) {
3438 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3439 +                       goto out_err;
3440 +               }
3441 +       }
3442 +
3443 +       sysaufs_br_init(br);
3444 +       path_get(&br->br_path);
3445 +       goto out; /* success */
3446 +
3447 +out_err:
3448 +       memset(&br->br_path, 0, sizeof(br->br_path));
3449 +out:
3450 +       return err;
3451 +}
3452 +
3453 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3454 +                            struct au_branch *br, aufs_bindex_t bbot,
3455 +                            aufs_bindex_t amount)
3456 +{
3457 +       struct au_branch **brp;
3458 +
3459 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3460 +
3461 +       brp = sbinfo->si_branch + bindex;
3462 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3463 +       *brp = br;
3464 +       sbinfo->si_bbot++;
3465 +       if (unlikely(bbot < 0))
3466 +               sbinfo->si_bbot = 0;
3467 +}
3468 +
3469 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3470 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3471 +{
3472 +       struct au_hdentry *hdp;
3473 +
3474 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3475 +
3476 +       hdp = au_hdentry(dinfo, bindex);
3477 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3478 +       au_h_dentry_init(hdp);
3479 +       dinfo->di_bbot++;
3480 +       if (unlikely(bbot < 0))
3481 +               dinfo->di_btop = 0;
3482 +}
3483 +
3484 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3485 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3486 +{
3487 +       struct au_hinode *hip;
3488 +
3489 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3490 +
3491 +       hip = au_hinode(iinfo, bindex);
3492 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3493 +       au_hinode_init(hip);
3494 +       iinfo->ii_bbot++;
3495 +       if (unlikely(bbot < 0))
3496 +               iinfo->ii_btop = 0;
3497 +}
3498 +
3499 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3500 +                        aufs_bindex_t bindex)
3501 +{
3502 +       struct dentry *root, *h_dentry;
3503 +       struct inode *root_inode, *h_inode;
3504 +       aufs_bindex_t bbot, amount;
3505 +
3506 +       root = sb->s_root;
3507 +       root_inode = d_inode(root);
3508 +       bbot = au_sbbot(sb);
3509 +       amount = bbot + 1 - bindex;
3510 +       h_dentry = au_br_dentry(br);
3511 +       au_sbilist_lock();
3512 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3513 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3514 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3515 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3516 +       h_inode = d_inode(h_dentry);
3517 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3518 +       au_sbilist_unlock();
3519 +}
3520 +
3521 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3522 +{
3523 +       int err;
3524 +       aufs_bindex_t bbot, add_bindex;
3525 +       struct dentry *root, *h_dentry;
3526 +       struct inode *root_inode;
3527 +       struct au_branch *add_branch;
3528 +
3529 +       root = sb->s_root;
3530 +       root_inode = d_inode(root);
3531 +       IMustLock(root_inode);
3532 +       IiMustWriteLock(root_inode);
3533 +       err = test_add(sb, add, remount);
3534 +       if (unlikely(err < 0))
3535 +               goto out;
3536 +       if (err) {
3537 +               err = 0;
3538 +               goto out; /* success */
3539 +       }
3540 +
3541 +       bbot = au_sbbot(sb);
3542 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3543 +       err = PTR_ERR(add_branch);
3544 +       if (IS_ERR(add_branch))
3545 +               goto out;
3546 +
3547 +       err = au_br_init(add_branch, sb, add);
3548 +       if (unlikely(err)) {
3549 +               au_br_do_free(add_branch);
3550 +               goto out;
3551 +       }
3552 +
3553 +       add_bindex = add->bindex;
3554 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3555 +       au_br_do_add(sb, add_branch, add_bindex);
3556 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3557 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3558 +
3559 +       h_dentry = add->path.dentry;
3560 +       if (!add_bindex) {
3561 +               au_cpup_attr_all(root_inode, /*force*/1);
3562 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3563 +       } else
3564 +               au_add_nlink(root_inode, d_inode(h_dentry));
3565 +
3566 +out:
3567 +       return err;
3568 +}
3569 +
3570 +/* ---------------------------------------------------------------------- */
3571 +
3572 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3573 +                                      unsigned long long max __maybe_unused,
3574 +                                      void *arg)
3575 +{
3576 +       unsigned long long n;
3577 +       struct file **p, *f;
3578 +       struct hlist_bl_head *files;
3579 +       struct hlist_bl_node *pos;
3580 +       struct au_finfo *finfo;
3581 +
3582 +       n = 0;
3583 +       p = a;
3584 +       files = &au_sbi(sb)->si_files;
3585 +       hlist_bl_lock(files);
3586 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3587 +               f = finfo->fi_file;
3588 +               if (file_count(f)
3589 +                   && !special_file(file_inode(f)->i_mode)) {
3590 +                       get_file(f);
3591 +                       *p++ = f;
3592 +                       n++;
3593 +                       AuDebugOn(n > max);
3594 +               }
3595 +       }
3596 +       hlist_bl_unlock(files);
3597 +
3598 +       return n;
3599 +}
3600 +
3601 +static struct file **au_farray_alloc(struct super_block *sb,
3602 +                                    unsigned long long *max)
3603 +{
3604 +       struct au_sbinfo *sbi;
3605 +
3606 +       sbi = au_sbi(sb);
3607 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3608 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3609 +}
3610 +
3611 +static void au_farray_free(struct file **a, unsigned long long max)
3612 +{
3613 +       unsigned long long ull;
3614 +
3615 +       for (ull = 0; ull < max; ull++)
3616 +               if (a[ull])
3617 +                       fput(a[ull]);
3618 +       kvfree(a);
3619 +}
3620 +
3621 +/* ---------------------------------------------------------------------- */
3622 +
3623 +/*
3624 + * delete a branch
3625 + */
3626 +
3627 +/* to show the line number, do not make it inlined function */
3628 +#define AuVerbose(do_info, fmt, ...) do { \
3629 +       if (do_info) \
3630 +               pr_info(fmt, ##__VA_ARGS__); \
3631 +} while (0)
3632 +
3633 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3634 +                        aufs_bindex_t bbot)
3635 +{
3636 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3637 +}
3638 +
3639 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3640 +                        aufs_bindex_t bbot)
3641 +{
3642 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3643 +}
3644 +
3645 +/*
3646 + * test if the branch is deletable or not.
3647 + */
3648 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3649 +                           unsigned int sigen, const unsigned int verbose)
3650 +{
3651 +       int err, i, j, ndentry;
3652 +       aufs_bindex_t btop, bbot;
3653 +       struct au_dcsub_pages dpages;
3654 +       struct au_dpage *dpage;
3655 +       struct dentry *d;
3656 +
3657 +       err = au_dpages_init(&dpages, GFP_NOFS);
3658 +       if (unlikely(err))
3659 +               goto out;
3660 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3661 +       if (unlikely(err))
3662 +               goto out_dpages;
3663 +
3664 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3665 +               dpage = dpages.dpages + i;
3666 +               ndentry = dpage->ndentry;
3667 +               for (j = 0; !err && j < ndentry; j++) {
3668 +                       d = dpage->dentries[j];
3669 +                       AuDebugOn(au_dcount(d) <= 0);
3670 +                       if (!au_digen_test(d, sigen)) {
3671 +                               di_read_lock_child(d, AuLock_IR);
3672 +                               if (unlikely(au_dbrange_test(d))) {
3673 +                                       di_read_unlock(d, AuLock_IR);
3674 +                                       continue;
3675 +                               }
3676 +                       } else {
3677 +                               di_write_lock_child(d);
3678 +                               if (unlikely(au_dbrange_test(d))) {
3679 +                                       di_write_unlock(d);
3680 +                                       continue;
3681 +                               }
3682 +                               err = au_reval_dpath(d, sigen);
3683 +                               if (!err)
3684 +                                       di_downgrade_lock(d, AuLock_IR);
3685 +                               else {
3686 +                                       di_write_unlock(d);
3687 +                                       break;
3688 +                               }
3689 +                       }
3690 +
3691 +                       /* AuDbgDentry(d); */
3692 +                       btop = au_dbtop(d);
3693 +                       bbot = au_dbbot(d);
3694 +                       if (btop <= bindex
3695 +                           && bindex <= bbot
3696 +                           && au_h_dptr(d, bindex)
3697 +                           && au_test_dbusy(d, btop, bbot)) {
3698 +                               err = -EBUSY;
3699 +                               AuVerbose(verbose, "busy %pd\n", d);
3700 +                               AuDbgDentry(d);
3701 +                       }
3702 +                       di_read_unlock(d, AuLock_IR);
3703 +               }
3704 +       }
3705 +
3706 +out_dpages:
3707 +       au_dpages_free(&dpages);
3708 +out:
3709 +       return err;
3710 +}
3711 +
3712 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3713 +                          unsigned int sigen, const unsigned int verbose)
3714 +{
3715 +       int err;
3716 +       unsigned long long max, ull;
3717 +       struct inode *i, **array;
3718 +       aufs_bindex_t btop, bbot;
3719 +
3720 +       array = au_iarray_alloc(sb, &max);
3721 +       err = PTR_ERR(array);
3722 +       if (IS_ERR(array))
3723 +               goto out;
3724 +
3725 +       err = 0;
3726 +       AuDbg("b%d\n", bindex);
3727 +       for (ull = 0; !err && ull < max; ull++) {
3728 +               i = array[ull];
3729 +               if (unlikely(!i))
3730 +                       break;
3731 +               if (i->i_ino == AUFS_ROOT_INO)
3732 +                       continue;
3733 +
3734 +               /* AuDbgInode(i); */
3735 +               if (au_iigen(i, NULL) == sigen)
3736 +                       ii_read_lock_child(i);
3737 +               else {
3738 +                       ii_write_lock_child(i);
3739 +                       err = au_refresh_hinode_self(i);
3740 +                       au_iigen_dec(i);
3741 +                       if (!err)
3742 +                               ii_downgrade_lock(i);
3743 +                       else {
3744 +                               ii_write_unlock(i);
3745 +                               break;
3746 +                       }
3747 +               }
3748 +
3749 +               btop = au_ibtop(i);
3750 +               bbot = au_ibbot(i);
3751 +               if (btop <= bindex
3752 +                   && bindex <= bbot
3753 +                   && au_h_iptr(i, bindex)
3754 +                   && au_test_ibusy(i, btop, bbot)) {
3755 +                       err = -EBUSY;
3756 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3757 +                       AuDbgInode(i);
3758 +               }
3759 +               ii_read_unlock(i);
3760 +       }
3761 +       au_iarray_free(array, max);
3762 +
3763 +out:
3764 +       return err;
3765 +}
3766 +
3767 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3768 +                             const unsigned int verbose)
3769 +{
3770 +       int err;
3771 +       unsigned int sigen;
3772 +
3773 +       sigen = au_sigen(root->d_sb);
3774 +       DiMustNoWaiters(root);
3775 +       IiMustNoWaiters(d_inode(root));
3776 +       di_write_unlock(root);
3777 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3778 +       if (!err)
3779 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3780 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3781 +
3782 +       return err;
3783 +}
3784 +
3785 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3786 +                        struct file **to_free, int *idx)
3787 +{
3788 +       int err;
3789 +       unsigned char matched, root;
3790 +       aufs_bindex_t bindex, bbot;
3791 +       struct au_fidir *fidir;
3792 +       struct au_hfile *hfile;
3793 +
3794 +       err = 0;
3795 +       root = IS_ROOT(file->f_path.dentry);
3796 +       if (root) {
3797 +               get_file(file);
3798 +               to_free[*idx] = file;
3799 +               (*idx)++;
3800 +               goto out;
3801 +       }
3802 +
3803 +       matched = 0;
3804 +       fidir = au_fi(file)->fi_hdir;
3805 +       AuDebugOn(!fidir);
3806 +       bbot = au_fbbot_dir(file);
3807 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3808 +               hfile = fidir->fd_hfile + bindex;
3809 +               if (!hfile->hf_file)
3810 +                       continue;
3811 +
3812 +               if (hfile->hf_br->br_id == br_id) {
3813 +                       matched = 1;
3814 +                       break;
3815 +               }
3816 +       }
3817 +       if (matched)
3818 +               err = -EBUSY;
3819 +
3820 +out:
3821 +       return err;
3822 +}
3823 +
3824 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3825 +                         struct file **to_free, int opened)
3826 +{
3827 +       int err, idx;
3828 +       unsigned long long ull, max;
3829 +       aufs_bindex_t btop;
3830 +       struct file *file, **array;
3831 +       struct dentry *root;
3832 +       struct au_hfile *hfile;
3833 +
3834 +       array = au_farray_alloc(sb, &max);
3835 +       err = PTR_ERR(array);
3836 +       if (IS_ERR(array))
3837 +               goto out;
3838 +
3839 +       err = 0;
3840 +       idx = 0;
3841 +       root = sb->s_root;
3842 +       di_write_unlock(root);
3843 +       for (ull = 0; ull < max; ull++) {
3844 +               file = array[ull];
3845 +               if (unlikely(!file))
3846 +                       break;
3847 +
3848 +               /* AuDbg("%pD\n", file); */
3849 +               fi_read_lock(file);
3850 +               btop = au_fbtop(file);
3851 +               if (!d_is_dir(file->f_path.dentry)) {
3852 +                       hfile = &au_fi(file)->fi_htop;
3853 +                       if (hfile->hf_br->br_id == br_id)
3854 +                               err = -EBUSY;
3855 +               } else
3856 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3857 +               fi_read_unlock(file);
3858 +               if (unlikely(err))
3859 +                       break;
3860 +       }
3861 +       di_write_lock_child(root);
3862 +       au_farray_free(array, max);
3863 +       AuDebugOn(idx > opened);
3864 +
3865 +out:
3866 +       return err;
3867 +}
3868 +
3869 +static void br_del_file(struct file **to_free, unsigned long long opened,
3870 +                       aufs_bindex_t br_id)
3871 +{
3872 +       unsigned long long ull;
3873 +       aufs_bindex_t bindex, btop, bbot, bfound;
3874 +       struct file *file;
3875 +       struct au_fidir *fidir;
3876 +       struct au_hfile *hfile;
3877 +
3878 +       for (ull = 0; ull < opened; ull++) {
3879 +               file = to_free[ull];
3880 +               if (unlikely(!file))
3881 +                       break;
3882 +
3883 +               /* AuDbg("%pD\n", file); */
3884 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3885 +               bfound = -1;
3886 +               fidir = au_fi(file)->fi_hdir;
3887 +               AuDebugOn(!fidir);
3888 +               fi_write_lock(file);
3889 +               btop = au_fbtop(file);
3890 +               bbot = au_fbbot_dir(file);
3891 +               for (bindex = btop; bindex <= bbot; bindex++) {
3892 +                       hfile = fidir->fd_hfile + bindex;
3893 +                       if (!hfile->hf_file)
3894 +                               continue;
3895 +
3896 +                       if (hfile->hf_br->br_id == br_id) {
3897 +                               bfound = bindex;
3898 +                               break;
3899 +                       }
3900 +               }
3901 +               AuDebugOn(bfound < 0);
3902 +               au_set_h_fptr(file, bfound, NULL);
3903 +               if (bfound == btop) {
3904 +                       for (btop++; btop <= bbot; btop++)
3905 +                               if (au_hf_dir(file, btop)) {
3906 +                                       au_set_fbtop(file, btop);
3907 +                                       break;
3908 +                               }
3909 +               }
3910 +               fi_write_unlock(file);
3911 +       }
3912 +}
3913 +
3914 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3915 +                            const aufs_bindex_t bindex,
3916 +                            const aufs_bindex_t bbot)
3917 +{
3918 +       struct au_branch **brp, **p;
3919 +
3920 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3921 +
3922 +       brp = sbinfo->si_branch + bindex;
3923 +       if (bindex < bbot)
3924 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3925 +       sbinfo->si_branch[0 + bbot] = NULL;
3926 +       sbinfo->si_bbot--;
3927 +
3928 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3929 +                       /*may_shrink*/1);
3930 +       if (p)
3931 +               sbinfo->si_branch = p;
3932 +       /* harmless error */
3933 +}
3934 +
3935 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3936 +                            const aufs_bindex_t bbot)
3937 +{
3938 +       struct au_hdentry *hdp, *p;
3939 +
3940 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3941 +
3942 +       hdp = au_hdentry(dinfo, bindex);
3943 +       if (bindex < bbot)
3944 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3945 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3946 +       dinfo->di_bbot--;
3947 +
3948 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3949 +                       /*may_shrink*/1);
3950 +       if (p)
3951 +               dinfo->di_hdentry = p;
3952 +       /* harmless error */
3953 +}
3954 +
3955 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3956 +                            const aufs_bindex_t bbot)
3957 +{
3958 +       struct au_hinode *hip, *p;
3959 +
3960 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3961 +
3962 +       hip = au_hinode(iinfo, bindex);
3963 +       if (bindex < bbot)
3964 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3965 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3966 +       iinfo->ii_bbot--;
3967 +
3968 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3969 +                       /*may_shrink*/1);
3970 +       if (p)
3971 +               iinfo->ii_hinode = p;
3972 +       /* harmless error */
3973 +}
3974 +
3975 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3976 +                        struct au_branch *br)
3977 +{
3978 +       aufs_bindex_t bbot;
3979 +       struct au_sbinfo *sbinfo;
3980 +       struct dentry *root, *h_root;
3981 +       struct inode *inode, *h_inode;
3982 +       struct au_hinode *hinode;
3983 +
3984 +       SiMustWriteLock(sb);
3985 +
3986 +       root = sb->s_root;
3987 +       inode = d_inode(root);
3988 +       sbinfo = au_sbi(sb);
3989 +       bbot = sbinfo->si_bbot;
3990 +
3991 +       h_root = au_h_dptr(root, bindex);
3992 +       hinode = au_hi(inode, bindex);
3993 +       h_inode = au_igrab(hinode->hi_inode);
3994 +       au_hiput(hinode);
3995 +
3996 +       au_sbilist_lock();
3997 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3998 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3999 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
4000 +       au_sbilist_unlock();
4001 +
4002 +       /* ignore an error */
4003 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
4004 +
4005 +       dput(h_root);
4006 +       iput(h_inode);
4007 +       au_br_do_free(br);
4008 +}
4009 +
4010 +static unsigned long long empty_cb(struct super_block *sb, void *array,
4011 +                                  unsigned long long max, void *arg)
4012 +{
4013 +       return max;
4014 +}
4015 +
4016 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4017 +{
4018 +       int err, rerr, i;
4019 +       unsigned long long opened;
4020 +       unsigned int mnt_flags;
4021 +       aufs_bindex_t bindex, bbot, br_id;
4022 +       unsigned char do_wh, verbose;
4023 +       struct au_branch *br;
4024 +       struct au_wbr *wbr;
4025 +       struct dentry *root;
4026 +       struct file **to_free;
4027 +
4028 +       err = 0;
4029 +       opened = 0;
4030 +       to_free = NULL;
4031 +       root = sb->s_root;
4032 +       bindex = au_find_dbindex(root, del->h_path.dentry);
4033 +       if (bindex < 0) {
4034 +               if (remount)
4035 +                       goto out; /* success */
4036 +               err = -ENOENT;
4037 +               pr_err("%s no such branch\n", del->pathname);
4038 +               goto out;
4039 +       }
4040 +       AuDbg("bindex b%d\n", bindex);
4041 +
4042 +       err = -EBUSY;
4043 +       mnt_flags = au_mntflags(sb);
4044 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4045 +       bbot = au_sbbot(sb);
4046 +       if (unlikely(!bbot)) {
4047 +               AuVerbose(verbose, "no more branches left\n");
4048 +               goto out;
4049 +       }
4050 +
4051 +       br = au_sbr(sb, bindex);
4052 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
4053 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4054 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4055 +               goto out;
4056 +       }
4057 +
4058 +       br_id = br->br_id;
4059 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
4060 +       if (unlikely(opened)) {
4061 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
4062 +               err = PTR_ERR(to_free);
4063 +               if (IS_ERR(to_free))
4064 +                       goto out;
4065 +
4066 +               err = test_file_busy(sb, br_id, to_free, opened);
4067 +               if (unlikely(err)) {
4068 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4069 +                       goto out;
4070 +               }
4071 +       }
4072 +
4073 +       wbr = br->br_wbr;
4074 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4075 +       if (do_wh) {
4076 +               /* instead of WbrWhMustWriteLock(wbr) */
4077 +               SiMustWriteLock(sb);
4078 +               for (i = 0; i < AuBrWh_Last; i++) {
4079 +                       dput(wbr->wbr_wh[i]);
4080 +                       wbr->wbr_wh[i] = NULL;
4081 +               }
4082 +       }
4083 +
4084 +       err = test_children_busy(root, bindex, verbose);
4085 +       if (unlikely(err)) {
4086 +               if (do_wh)
4087 +                       goto out_wh;
4088 +               goto out;
4089 +       }
4090 +
4091 +       err = 0;
4092 +       if (to_free) {
4093 +               /*
4094 +                * now we confirmed the branch is deletable.
4095 +                * let's free the remaining opened dirs on the branch.
4096 +                */
4097 +               di_write_unlock(root);
4098 +               br_del_file(to_free, opened, br_id);
4099 +               di_write_lock_child(root);
4100 +       }
4101 +
4102 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4103 +       dbgaufs_xino_del(br);           /* remove one */
4104 +       au_br_do_del(sb, bindex, br);
4105 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4106 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4107 +
4108 +       if (!bindex) {
4109 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4110 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4111 +       } else
4112 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4113 +       if (au_opt_test(mnt_flags, PLINK))
4114 +               au_plink_half_refresh(sb, br_id);
4115 +
4116 +       goto out; /* success */
4117 +
4118 +out_wh:
4119 +       /* revert */
4120 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4121 +       if (rerr)
4122 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4123 +                       del->pathname, rerr);
4124 +out:
4125 +       if (to_free)
4126 +               au_farray_free(to_free, opened);
4127 +       return err;
4128 +}
4129 +
4130 +/* ---------------------------------------------------------------------- */
4131 +
4132 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4133 +{
4134 +       int err;
4135 +       aufs_bindex_t btop, bbot;
4136 +       struct aufs_ibusy ibusy;
4137 +       struct inode *inode, *h_inode;
4138 +
4139 +       err = -EPERM;
4140 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4141 +               goto out;
4142 +
4143 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4144 +       if (!err)
4145 +               /* VERIFY_WRITE */
4146 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4147 +       if (unlikely(err)) {
4148 +               err = -EFAULT;
4149 +               AuTraceErr(err);
4150 +               goto out;
4151 +       }
4152 +
4153 +       err = -EINVAL;
4154 +       si_read_lock(sb, AuLock_FLUSH);
4155 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4156 +               goto out_unlock;
4157 +
4158 +       err = 0;
4159 +       ibusy.h_ino = 0; /* invalid */
4160 +       inode = ilookup(sb, ibusy.ino);
4161 +       if (!inode
4162 +           || inode->i_ino == AUFS_ROOT_INO
4163 +           || au_is_bad_inode(inode))
4164 +               goto out_unlock;
4165 +
4166 +       ii_read_lock_child(inode);
4167 +       btop = au_ibtop(inode);
4168 +       bbot = au_ibbot(inode);
4169 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4170 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4171 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4172 +                       ibusy.h_ino = h_inode->i_ino;
4173 +       }
4174 +       ii_read_unlock(inode);
4175 +       iput(inode);
4176 +
4177 +out_unlock:
4178 +       si_read_unlock(sb);
4179 +       if (!err) {
4180 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4181 +               if (unlikely(err)) {
4182 +                       err = -EFAULT;
4183 +                       AuTraceErr(err);
4184 +               }
4185 +       }
4186 +out:
4187 +       return err;
4188 +}
4189 +
4190 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4191 +{
4192 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4193 +}
4194 +
4195 +#ifdef CONFIG_COMPAT
4196 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4197 +{
4198 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4199 +}
4200 +#endif
4201 +
4202 +/* ---------------------------------------------------------------------- */
4203 +
4204 +/*
4205 + * change a branch permission
4206 + */
4207 +
4208 +static void au_warn_ima(void)
4209 +{
4210 +#ifdef CONFIG_IMA
4211 +       /* since it doesn't support mark_files_ro() */
4212 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4213 +#endif
4214 +}
4215 +
4216 +static int do_need_sigen_inc(int a, int b)
4217 +{
4218 +       return au_br_whable(a) && !au_br_whable(b);
4219 +}
4220 +
4221 +static int need_sigen_inc(int old, int new)
4222 +{
4223 +       return do_need_sigen_inc(old, new)
4224 +               || do_need_sigen_inc(new, old);
4225 +}
4226 +
4227 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4228 +{
4229 +       int err, do_warn;
4230 +       unsigned int mnt_flags;
4231 +       unsigned long long ull, max;
4232 +       aufs_bindex_t br_id;
4233 +       unsigned char verbose, writer;
4234 +       struct file *file, *hf, **array;
4235 +       struct au_hfile *hfile;
4236 +       struct inode *h_inode;
4237 +
4238 +       mnt_flags = au_mntflags(sb);
4239 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4240 +
4241 +       array = au_farray_alloc(sb, &max);
4242 +       err = PTR_ERR(array);
4243 +       if (IS_ERR(array))
4244 +               goto out;
4245 +
4246 +       do_warn = 0;
4247 +       br_id = au_sbr_id(sb, bindex);
4248 +       for (ull = 0; ull < max; ull++) {
4249 +               file = array[ull];
4250 +               if (unlikely(!file))
4251 +                       break;
4252 +
4253 +               /* AuDbg("%pD\n", file); */
4254 +               fi_read_lock(file);
4255 +               if (unlikely(au_test_mmapped(file))) {
4256 +                       err = -EBUSY;
4257 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4258 +                       AuDbgFile(file);
4259 +                       FiMustNoWaiters(file);
4260 +                       fi_read_unlock(file);
4261 +                       goto out_array;
4262 +               }
4263 +
4264 +               hfile = &au_fi(file)->fi_htop;
4265 +               hf = hfile->hf_file;
4266 +               if (!d_is_reg(file->f_path.dentry)
4267 +                   || !(file->f_mode & FMODE_WRITE)
4268 +                   || hfile->hf_br->br_id != br_id
4269 +                   || !(hf->f_mode & FMODE_WRITE))
4270 +                       array[ull] = NULL;
4271 +               else {
4272 +                       do_warn = 1;
4273 +                       get_file(file);
4274 +               }
4275 +
4276 +               FiMustNoWaiters(file);
4277 +               fi_read_unlock(file);
4278 +               fput(file);
4279 +       }
4280 +
4281 +       err = 0;
4282 +       if (do_warn)
4283 +               au_warn_ima();
4284 +
4285 +       for (ull = 0; ull < max; ull++) {
4286 +               file = array[ull];
4287 +               if (!file)
4288 +                       continue;
4289 +
4290 +               /* todo: already flushed? */
4291 +               /*
4292 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4293 +                * approach which resets f_mode and calls mnt_drop_write() and
4294 +                * file_release_write() for each file, because the branch
4295 +                * attribute in aufs world is totally different from the native
4296 +                * fs rw/ro mode.
4297 +               */
4298 +               /* fi_read_lock(file); */
4299 +               hfile = &au_fi(file)->fi_htop;
4300 +               hf = hfile->hf_file;
4301 +               /* fi_read_unlock(file); */
4302 +               spin_lock(&hf->f_lock);
4303 +               writer = !!(hf->f_mode & FMODE_WRITER);
4304 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4305 +               spin_unlock(&hf->f_lock);
4306 +               if (writer) {
4307 +                       h_inode = file_inode(hf);
4308 +                       put_write_access(h_inode);
4309 +                       __mnt_drop_write(hf->f_path.mnt);
4310 +                       if ((hf->f_mode & (FMODE_READ | FMODE_WRITE))
4311 +                           == FMODE_READ)
4312 +                               i_readcount_inc(h_inode);
4313 +               }
4314 +       }
4315 +
4316 +out_array:
4317 +       au_farray_free(array, max);
4318 +out:
4319 +       AuTraceErr(err);
4320 +       return err;
4321 +}
4322 +
4323 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4324 +             int *do_refresh)
4325 +{
4326 +       int err, rerr;
4327 +       aufs_bindex_t bindex;
4328 +       struct dentry *root;
4329 +       struct au_branch *br;
4330 +       struct au_br_fhsm *bf;
4331 +
4332 +       root = sb->s_root;
4333 +       bindex = au_find_dbindex(root, mod->h_root);
4334 +       if (bindex < 0) {
4335 +               if (remount)
4336 +                       return 0; /* success */
4337 +               err = -ENOENT;
4338 +               pr_err("%s no such branch\n", mod->path);
4339 +               goto out;
4340 +       }
4341 +       AuDbg("bindex b%d\n", bindex);
4342 +
4343 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4344 +       if (unlikely(err))
4345 +               goto out;
4346 +
4347 +       br = au_sbr(sb, bindex);
4348 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4349 +       if (br->br_perm == mod->perm)
4350 +               return 0; /* success */
4351 +
4352 +       /* pre-allocate for non-fhsm --> fhsm */
4353 +       bf = NULL;
4354 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4355 +               err = au_fhsm_br_alloc(br);
4356 +               if (unlikely(err))
4357 +                       goto out;
4358 +               bf = br->br_fhsm;
4359 +               br->br_fhsm = NULL;
4360 +       }
4361 +
4362 +       if (au_br_writable(br->br_perm)) {
4363 +               /* remove whiteout base */
4364 +               err = au_br_init_wh(sb, br, mod->perm);
4365 +               if (unlikely(err))
4366 +                       goto out_bf;
4367 +
4368 +               if (!au_br_writable(mod->perm)) {
4369 +                       /* rw --> ro, file might be mmapped */
4370 +                       DiMustNoWaiters(root);
4371 +                       IiMustNoWaiters(d_inode(root));
4372 +                       di_write_unlock(root);
4373 +                       err = au_br_mod_files_ro(sb, bindex);
4374 +                       /* aufs_write_lock() calls ..._child() */
4375 +                       di_write_lock_child(root);
4376 +
4377 +                       if (unlikely(err)) {
4378 +                               rerr = -ENOMEM;
4379 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4380 +                                                    GFP_NOFS);
4381 +                               if (br->br_wbr)
4382 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4383 +                               if (unlikely(rerr)) {
4384 +                                       AuIOErr("nested error %d (%d)\n",
4385 +                                               rerr, err);
4386 +                                       br->br_perm = mod->perm;
4387 +                               }
4388 +                       }
4389 +               }
4390 +       } else if (au_br_writable(mod->perm)) {
4391 +               /* ro --> rw */
4392 +               err = -ENOMEM;
4393 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4394 +               if (br->br_wbr) {
4395 +                       err = au_wbr_init(br, sb, mod->perm);
4396 +                       if (unlikely(err)) {
4397 +                               au_kfree_rcu(br->br_wbr);
4398 +                               br->br_wbr = NULL;
4399 +                       }
4400 +               }
4401 +       }
4402 +       if (unlikely(err))
4403 +               goto out_bf;
4404 +
4405 +       if (au_br_fhsm(br->br_perm)) {
4406 +               if (!au_br_fhsm(mod->perm)) {
4407 +                       /* fhsm --> non-fhsm */
4408 +                       au_br_fhsm_fin(br->br_fhsm);
4409 +                       au_kfree_rcu(br->br_fhsm);
4410 +                       br->br_fhsm = NULL;
4411 +               }
4412 +       } else if (au_br_fhsm(mod->perm))
4413 +               /* non-fhsm --> fhsm */
4414 +               br->br_fhsm = bf;
4415 +
4416 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4417 +       br->br_perm = mod->perm;
4418 +       goto out; /* success */
4419 +
4420 +out_bf:
4421 +       au_kfree_try_rcu(bf);
4422 +out:
4423 +       AuTraceErr(err);
4424 +       return err;
4425 +}
4426 +
4427 +/* ---------------------------------------------------------------------- */
4428 +
4429 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4430 +{
4431 +       int err;
4432 +       struct kstatfs kstfs;
4433 +
4434 +       err = vfs_statfs(&br->br_path, &kstfs);
4435 +       if (!err) {
4436 +               stfs->f_blocks = kstfs.f_blocks;
4437 +               stfs->f_bavail = kstfs.f_bavail;
4438 +               stfs->f_files = kstfs.f_files;
4439 +               stfs->f_ffree = kstfs.f_ffree;
4440 +       }
4441 +
4442 +       return err;
4443 +}
4444 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4445 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4446 +++ linux/fs/aufs/branch.h      2019-07-11 15:42:14.462237786 +0200
4447 @@ -0,0 +1,366 @@
4448 +/* SPDX-License-Identifier: GPL-2.0 */
4449 +/*
4450 + * Copyright (C) 2005-2019 Junjiro R. Okajima
4451 + *
4452 + * This program, aufs is free software; you can redistribute it and/or modify
4453 + * it under the terms of the GNU General Public License as published by
4454 + * the Free Software Foundation; either version 2 of the License, or
4455 + * (at your option) any later version.
4456 + *
4457 + * This program is distributed in the hope that it will be useful,
4458 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4459 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4460 + * GNU General Public License for more details.
4461 + *
4462 + * You should have received a copy of the GNU General Public License
4463 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4464 + */
4465 +
4466 +/*
4467 + * branch filesystems and xino for them
4468 + */
4469 +
4470 +#ifndef __AUFS_BRANCH_H__
4471 +#define __AUFS_BRANCH_H__
4472 +
4473 +#ifdef __KERNEL__
4474 +
4475 +#include <linux/mount.h>
4476 +#include "dirren.h"
4477 +#include "dynop.h"
4478 +#include "lcnt.h"
4479 +#include "rwsem.h"
4480 +#include "super.h"
4481 +
4482 +/* ---------------------------------------------------------------------- */
4483 +
4484 +/* a xino file */
4485 +struct au_xino {
4486 +       struct file             **xi_file;
4487 +       unsigned int            xi_nfile;
4488 +
4489 +       struct {
4490 +               spinlock_t              spin;
4491 +               ino_t                   *array;
4492 +               int                     total;
4493 +               /* reserved for future use */
4494 +               /* unsigned long        *bitmap; */
4495 +               wait_queue_head_t       wqh;
4496 +       } xi_nondir;
4497 +
4498 +       struct mutex            xi_mtx; /* protects xi_file array */
4499 +       struct hlist_bl_head    xi_writing;
4500 +
4501 +       atomic_t                xi_truncating;
4502 +
4503 +       struct kref             xi_kref;
4504 +};
4505 +
4506 +/* File-based Hierarchical Storage Management */
4507 +struct au_br_fhsm {
4508 +#ifdef CONFIG_AUFS_FHSM
4509 +       struct mutex            bf_lock;
4510 +       unsigned long           bf_jiffy;
4511 +       struct aufs_stfs        bf_stfs;
4512 +       int                     bf_readable;
4513 +#endif
4514 +};
4515 +
4516 +/* members for writable branch only */
4517 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4518 +struct au_wbr {
4519 +       struct au_rwsem         wbr_wh_rwsem;
4520 +       struct dentry           *wbr_wh[AuBrWh_Last];
4521 +       atomic_t                wbr_wh_running;
4522 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4523 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4524 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4525 +
4526 +       /* mfs mode */
4527 +       unsigned long long      wbr_bytes;
4528 +};
4529 +
4530 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4531 +#define AuBrDynOp (AuDyLast * 4)
4532 +
4533 +#ifdef CONFIG_AUFS_HFSNOTIFY
4534 +/* support for asynchronous destruction */
4535 +struct au_br_hfsnotify {
4536 +       struct fsnotify_group   *hfsn_group;
4537 +};
4538 +#endif
4539 +
4540 +/* sysfs entries */
4541 +struct au_brsysfs {
4542 +       char                    name[16];
4543 +       struct attribute        attr;
4544 +};
4545 +
4546 +enum {
4547 +       AuBrSysfs_BR,
4548 +       AuBrSysfs_BRID,
4549 +       AuBrSysfs_Last
4550 +};
4551 +
4552 +/* protected by superblock rwsem */
4553 +struct au_branch {
4554 +       struct au_xino          *br_xino;
4555 +
4556 +       aufs_bindex_t           br_id;
4557 +
4558 +       int                     br_perm;
4559 +       struct path             br_path;
4560 +       spinlock_t              br_dykey_lock;
4561 +       struct au_dykey         *br_dykey[AuBrDynOp];
4562 +       au_lcnt_t               br_nfiles;      /* opened files */
4563 +       au_lcnt_t               br_count;       /* in-use for other */
4564 +
4565 +       struct au_wbr           *br_wbr;
4566 +       struct au_br_fhsm       *br_fhsm;
4567 +
4568 +#ifdef CONFIG_AUFS_HFSNOTIFY
4569 +       struct au_br_hfsnotify  *br_hfsn;
4570 +#endif
4571 +
4572 +#ifdef CONFIG_SYSFS
4573 +       /* entries under sysfs per mount-point */
4574 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4575 +#endif
4576 +
4577 +#ifdef CONFIG_DEBUG_FS
4578 +       struct dentry            *br_dbgaufs; /* xino */
4579 +#endif
4580 +
4581 +       struct au_dr_br         br_dirren;
4582 +};
4583 +
4584 +/* ---------------------------------------------------------------------- */
4585 +
4586 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4587 +{
4588 +       return br->br_path.mnt;
4589 +}
4590 +
4591 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4592 +{
4593 +       return br->br_path.dentry;
4594 +}
4595 +
4596 +static inline struct super_block *au_br_sb(struct au_branch *br)
4597 +{
4598 +       return au_br_mnt(br)->mnt_sb;
4599 +}
4600 +
4601 +static inline int au_br_rdonly(struct au_branch *br)
4602 +{
4603 +       return (sb_rdonly(au_br_sb(br))
4604 +               || !au_br_writable(br->br_perm))
4605 +               ? -EROFS : 0;
4606 +}
4607 +
4608 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4609 +{
4610 +#ifdef CONFIG_AUFS_HNOTIFY
4611 +       return !(brperm & AuBrPerm_RR);
4612 +#else
4613 +       return 0;
4614 +#endif
4615 +}
4616 +
4617 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4618 +{
4619 +       int err, exec_flag;
4620 +
4621 +       err = 0;
4622 +       exec_flag = oflag & __FMODE_EXEC;
4623 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4624 +               err = -EACCES;
4625 +
4626 +       return err;
4627 +}
4628 +
4629 +static inline void au_xino_get(struct au_branch *br)
4630 +{
4631 +       struct au_xino *xi;
4632 +
4633 +       xi = br->br_xino;
4634 +       if (xi)
4635 +               kref_get(&xi->xi_kref);
4636 +}
4637 +
4638 +static inline int au_xino_count(struct au_branch *br)
4639 +{
4640 +       int v;
4641 +       struct au_xino *xi;
4642 +
4643 +       v = 0;
4644 +       xi = br->br_xino;
4645 +       if (xi)
4646 +               v = kref_read(&xi->xi_kref);
4647 +
4648 +       return v;
4649 +}
4650 +
4651 +/* ---------------------------------------------------------------------- */
4652 +
4653 +/* branch.c */
4654 +struct au_sbinfo;
4655 +void au_br_free(struct au_sbinfo *sinfo);
4656 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4657 +struct au_opt_add;
4658 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4659 +struct au_opt_del;
4660 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4661 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4662 +#ifdef CONFIG_COMPAT
4663 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4664 +#endif
4665 +struct au_opt_mod;
4666 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4667 +             int *do_refresh);
4668 +struct aufs_stfs;
4669 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4670 +
4671 +/* xino.c */
4672 +static const loff_t au_loff_max = LLONG_MAX;
4673 +
4674 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4675 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4676 +                           int wbrtop);
4677 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4678 +                            struct file *copy_src);
4679 +struct au_xi_new {
4680 +       struct au_xino *xi;     /* switch between xino and xigen */
4681 +       int idx;
4682 +       struct path *base;
4683 +       struct file *copy_src;
4684 +};
4685 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4686 +
4687 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4688 +                ino_t *ino);
4689 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4690 +                 ino_t ino);
4691 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4692 +                  loff_t *pos);
4693 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4694 +                   size_t size, loff_t *pos);
4695 +
4696 +int au_xib_trunc(struct super_block *sb);
4697 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4698 +
4699 +struct au_xino *au_xino_alloc(unsigned int nfile);
4700 +int au_xino_put(struct au_branch *br);
4701 +struct file *au_xino_file1(struct au_xino *xi);
4702 +
4703 +struct au_opt_xino;
4704 +void au_xino_clr(struct super_block *sb);
4705 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4706 +struct file *au_xino_def(struct super_block *sb);
4707 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4708 +                   struct path *base);
4709 +
4710 +ino_t au_xino_new_ino(struct super_block *sb);
4711 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4712 +
4713 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4714 +                      ino_t h_ino, int idx);
4715 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4716 +                     int *idx);
4717 +
4718 +int au_xino_path(struct seq_file *seq, struct file *file);
4719 +
4720 +/* ---------------------------------------------------------------------- */
4721 +
4722 +/* @idx is signed to accept -1 meaning the first file */
4723 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4724 +{
4725 +       struct file *file;
4726 +
4727 +       file = NULL;
4728 +       if (!xi)
4729 +               goto out;
4730 +
4731 +       if (idx >= 0) {
4732 +               if (idx < xi->xi_nfile)
4733 +                       file = xi->xi_file[idx];
4734 +       } else
4735 +               file = au_xino_file1(xi);
4736 +
4737 +out:
4738 +       return file;
4739 +}
4740 +
4741 +/* ---------------------------------------------------------------------- */
4742 +
4743 +/* Superblock to branch */
4744 +static inline
4745 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4746 +{
4747 +       return au_sbr(sb, bindex)->br_id;
4748 +}
4749 +
4750 +static inline
4751 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4752 +{
4753 +       return au_br_mnt(au_sbr(sb, bindex));
4754 +}
4755 +
4756 +static inline
4757 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4758 +{
4759 +       return au_br_sb(au_sbr(sb, bindex));
4760 +}
4761 +
4762 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4763 +{
4764 +       return au_sbr(sb, bindex)->br_perm;
4765 +}
4766 +
4767 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4768 +{
4769 +       return au_br_whable(au_sbr_perm(sb, bindex));
4770 +}
4771 +
4772 +/* ---------------------------------------------------------------------- */
4773 +
4774 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4775 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4776 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4777 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4778 +/*
4779 +#define wbr_wh_read_trylock_nested(wbr) \
4780 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4781 +#define wbr_wh_write_trylock_nested(wbr) \
4782 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4783 +*/
4784 +
4785 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4786 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4787 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4788 +
4789 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4790 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4791 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4792 +
4793 +/* ---------------------------------------------------------------------- */
4794 +
4795 +#ifdef CONFIG_AUFS_FHSM
4796 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4797 +{
4798 +       mutex_init(&brfhsm->bf_lock);
4799 +       brfhsm->bf_jiffy = 0;
4800 +       brfhsm->bf_readable = 0;
4801 +}
4802 +
4803 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4804 +{
4805 +       mutex_destroy(&brfhsm->bf_lock);
4806 +}
4807 +#else
4808 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4809 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4810 +#endif
4811 +
4812 +#endif /* __KERNEL__ */
4813 +#endif /* __AUFS_BRANCH_H__ */
4814 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4815 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4816 +++ linux/fs/aufs/conf.mk       2019-07-11 15:42:14.462237786 +0200
4817 @@ -0,0 +1,40 @@
4818 +# SPDX-License-Identifier: GPL-2.0
4819 +
4820 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4821 +
4822 +define AuConf
4823 +ifdef ${1}
4824 +AuConfStr += ${1}=${${1}}
4825 +endif
4826 +endef
4827 +
4828 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4829 +       SBILIST \
4830 +       HNOTIFY HFSNOTIFY \
4831 +       EXPORT INO_T_64 \
4832 +       XATTR \
4833 +       FHSM \
4834 +       RDU \
4835 +       DIRREN \
4836 +       SHWH \
4837 +       BR_RAMFS \
4838 +       BR_FUSE POLL \
4839 +       BR_HFSPLUS \
4840 +       BDEV_LOOP \
4841 +       DEBUG MAGIC_SYSRQ
4842 +$(foreach i, ${AuConfAll}, \
4843 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4844 +
4845 +AuConfName = ${obj}/conf.str
4846 +${AuConfName}.tmp: FORCE
4847 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4848 +${AuConfName}: ${AuConfName}.tmp
4849 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4850 +       echo '  GEN    ' $@; \
4851 +       cp -p $< $@; \
4852 +       }
4853 +FORCE:
4854 +clean-files += ${AuConfName} ${AuConfName}.tmp
4855 +${obj}/sysfs.o: ${AuConfName}
4856 +
4857 +-include ${srctree}/${src}/conf_priv.mk
4858 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4859 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4860 +++ linux/fs/aufs/cpup.c        2019-07-11 15:42:14.462237786 +0200
4861 @@ -0,0 +1,1458 @@
4862 +// SPDX-License-Identifier: GPL-2.0
4863 +/*
4864 + * Copyright (C) 2005-2019 Junjiro R. Okajima
4865 + *
4866 + * This program, aufs is free software; you can redistribute it and/or modify
4867 + * it under the terms of the GNU General Public License as published by
4868 + * the Free Software Foundation; either version 2 of the License, or
4869 + * (at your option) any later version.
4870 + *
4871 + * This program is distributed in the hope that it will be useful,
4872 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4873 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4874 + * GNU General Public License for more details.
4875 + *
4876 + * You should have received a copy of the GNU General Public License
4877 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4878 + */
4879 +
4880 +/*
4881 + * copy-up functions, see wbr_policy.c for copy-down
4882 + */
4883 +
4884 +#include <linux/fs_stack.h>
4885 +#include <linux/mm.h>
4886 +#include <linux/task_work.h>
4887 +#include "aufs.h"
4888 +
4889 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4890 +{
4891 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4892 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4893 +
4894 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4895 +
4896 +       dst->i_flags |= iflags & ~mask;
4897 +       if (au_test_fs_notime(dst->i_sb))
4898 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4899 +}
4900 +
4901 +void au_cpup_attr_timesizes(struct inode *inode)
4902 +{
4903 +       struct inode *h_inode;
4904 +
4905 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4906 +       fsstack_copy_attr_times(inode, h_inode);
4907 +       fsstack_copy_inode_size(inode, h_inode);
4908 +}
4909 +
4910 +void au_cpup_attr_nlink(struct inode *inode, int force)
4911 +{
4912 +       struct inode *h_inode;
4913 +       struct super_block *sb;
4914 +       aufs_bindex_t bindex, bbot;
4915 +
4916 +       sb = inode->i_sb;
4917 +       bindex = au_ibtop(inode);
4918 +       h_inode = au_h_iptr(inode, bindex);
4919 +       if (!force
4920 +           && !S_ISDIR(h_inode->i_mode)
4921 +           && au_opt_test(au_mntflags(sb), PLINK)
4922 +           && au_plink_test(inode))
4923 +               return;
4924 +
4925 +       /*
4926 +        * 0 can happen in revalidating.
4927 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4928 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4929 +        * case.
4930 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4931 +        *       the incorrect link count.
4932 +        */
4933 +       set_nlink(inode, h_inode->i_nlink);
4934 +
4935 +       /*
4936 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4937 +        * it may includes whplink directory.
4938 +        */
4939 +       if (S_ISDIR(h_inode->i_mode)) {
4940 +               bbot = au_ibbot(inode);
4941 +               for (bindex++; bindex <= bbot; bindex++) {
4942 +                       h_inode = au_h_iptr(inode, bindex);
4943 +                       if (h_inode)
4944 +                               au_add_nlink(inode, h_inode);
4945 +               }
4946 +       }
4947 +}
4948 +
4949 +void au_cpup_attr_changeable(struct inode *inode)
4950 +{
4951 +       struct inode *h_inode;
4952 +
4953 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4954 +       inode->i_mode = h_inode->i_mode;
4955 +       inode->i_uid = h_inode->i_uid;
4956 +       inode->i_gid = h_inode->i_gid;
4957 +       au_cpup_attr_timesizes(inode);
4958 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4959 +}
4960 +
4961 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4962 +{
4963 +       struct au_iinfo *iinfo = au_ii(inode);
4964 +
4965 +       IiMustWriteLock(inode);
4966 +
4967 +       iinfo->ii_higen = h_inode->i_generation;
4968 +       iinfo->ii_hsb1 = h_inode->i_sb;
4969 +}
4970 +
4971 +void au_cpup_attr_all(struct inode *inode, int force)
4972 +{
4973 +       struct inode *h_inode;
4974 +
4975 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4976 +       au_cpup_attr_changeable(inode);
4977 +       if (inode->i_nlink > 0)
4978 +               au_cpup_attr_nlink(inode, force);
4979 +       inode->i_rdev = h_inode->i_rdev;
4980 +       inode->i_blkbits = h_inode->i_blkbits;
4981 +       au_cpup_igen(inode, h_inode);
4982 +}
4983 +
4984 +/* ---------------------------------------------------------------------- */
4985 +
4986 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4987 +
4988 +/* keep the timestamps of the parent dir when cpup */
4989 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4990 +                   struct path *h_path)
4991 +{
4992 +       struct inode *h_inode;
4993 +
4994 +       dt->dt_dentry = dentry;
4995 +       dt->dt_h_path = *h_path;
4996 +       h_inode = d_inode(h_path->dentry);
4997 +       dt->dt_atime = h_inode->i_atime;
4998 +       dt->dt_mtime = h_inode->i_mtime;
4999 +       /* smp_mb(); */
5000 +}
5001 +
5002 +void au_dtime_revert(struct au_dtime *dt)
5003 +{
5004 +       struct iattr attr;
5005 +       int err;
5006 +
5007 +       attr.ia_atime = dt->dt_atime;
5008 +       attr.ia_mtime = dt->dt_mtime;
5009 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5010 +               | ATTR_ATIME | ATTR_ATIME_SET;
5011 +
5012 +       /* no delegation since this is a directory */
5013 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
5014 +       if (unlikely(err))
5015 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
5016 +}
5017 +
5018 +/* ---------------------------------------------------------------------- */
5019 +
5020 +/* internal use only */
5021 +struct au_cpup_reg_attr {
5022 +       int             valid;
5023 +       struct kstat    st;
5024 +       unsigned int    iflags; /* inode->i_flags */
5025 +};
5026 +
5027 +static noinline_for_stack
5028 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5029 +              struct au_cpup_reg_attr *h_src_attr)
5030 +{
5031 +       int err, sbits, icex;
5032 +       unsigned int mnt_flags;
5033 +       unsigned char verbose;
5034 +       struct iattr ia;
5035 +       struct path h_path;
5036 +       struct inode *h_isrc, *h_idst;
5037 +       struct kstat *h_st;
5038 +       struct au_branch *br;
5039 +
5040 +       h_path.dentry = au_h_dptr(dst, bindex);
5041 +       h_idst = d_inode(h_path.dentry);
5042 +       br = au_sbr(dst->d_sb, bindex);
5043 +       h_path.mnt = au_br_mnt(br);
5044 +       h_isrc = d_inode(h_src);
5045 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
5046 +               | ATTR_ATIME | ATTR_MTIME
5047 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
5048 +       if (h_src_attr && h_src_attr->valid) {
5049 +               h_st = &h_src_attr->st;
5050 +               ia.ia_uid = h_st->uid;
5051 +               ia.ia_gid = h_st->gid;
5052 +               ia.ia_atime = h_st->atime;
5053 +               ia.ia_mtime = h_st->mtime;
5054 +               if (h_idst->i_mode != h_st->mode
5055 +                   && !S_ISLNK(h_idst->i_mode)) {
5056 +                       ia.ia_valid |= ATTR_MODE;
5057 +                       ia.ia_mode = h_st->mode;
5058 +               }
5059 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5060 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5061 +       } else {
5062 +               ia.ia_uid = h_isrc->i_uid;
5063 +               ia.ia_gid = h_isrc->i_gid;
5064 +               ia.ia_atime = h_isrc->i_atime;
5065 +               ia.ia_mtime = h_isrc->i_mtime;
5066 +               if (h_idst->i_mode != h_isrc->i_mode
5067 +                   && !S_ISLNK(h_idst->i_mode)) {
5068 +                       ia.ia_valid |= ATTR_MODE;
5069 +                       ia.ia_mode = h_isrc->i_mode;
5070 +               }
5071 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5072 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5073 +       }
5074 +       /* no delegation since it is just created */
5075 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5076 +
5077 +       /* is this nfs only? */
5078 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5079 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5080 +               ia.ia_mode = h_isrc->i_mode;
5081 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5082 +       }
5083 +
5084 +       icex = br->br_perm & AuBrAttr_ICEX;
5085 +       if (!err) {
5086 +               mnt_flags = au_mntflags(dst->d_sb);
5087 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5088 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5089 +       }
5090 +
5091 +       return err;
5092 +}
5093 +
5094 +/* ---------------------------------------------------------------------- */
5095 +
5096 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5097 +                          char *buf, unsigned long blksize)
5098 +{
5099 +       int err;
5100 +       size_t sz, rbytes, wbytes;
5101 +       unsigned char all_zero;
5102 +       char *p, *zp;
5103 +       struct inode *h_inode;
5104 +       /* reduce stack usage */
5105 +       struct iattr *ia;
5106 +
5107 +       zp = page_address(ZERO_PAGE(0));
5108 +       if (unlikely(!zp))
5109 +               return -ENOMEM; /* possible? */
5110 +
5111 +       err = 0;
5112 +       all_zero = 0;
5113 +       while (len) {
5114 +               AuDbg("len %lld\n", len);
5115 +               sz = blksize;
5116 +               if (len < blksize)
5117 +                       sz = len;
5118 +
5119 +               rbytes = 0;
5120 +               /* todo: signal_pending? */
5121 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5122 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5123 +                       err = rbytes;
5124 +               }
5125 +               if (unlikely(err < 0))
5126 +                       break;
5127 +
5128 +               all_zero = 0;
5129 +               if (len >= rbytes && rbytes == blksize)
5130 +                       all_zero = !memcmp(buf, zp, rbytes);
5131 +               if (!all_zero) {
5132 +                       wbytes = rbytes;
5133 +                       p = buf;
5134 +                       while (wbytes) {
5135 +                               size_t b;
5136 +
5137 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5138 +                               err = b;
5139 +                               /* todo: signal_pending? */
5140 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5141 +                                       continue;
5142 +                               if (unlikely(err < 0))
5143 +                                       break;
5144 +                               wbytes -= b;
5145 +                               p += b;
5146 +                       }
5147 +                       if (unlikely(err < 0))
5148 +                               break;
5149 +               } else {
5150 +                       loff_t res;
5151 +
5152 +                       AuLabel(hole);
5153 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5154 +                       err = res;
5155 +                       if (unlikely(res < 0))
5156 +                               break;
5157 +               }
5158 +               len -= rbytes;
5159 +               err = 0;
5160 +       }
5161 +
5162 +       /* the last block may be a hole */
5163 +       if (!err && all_zero) {
5164 +               AuLabel(last hole);
5165 +
5166 +               err = 1;
5167 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5168 +                       /* nfs requires this step to make last hole */
5169 +                       /* is this only nfs? */
5170 +                       do {
5171 +                               /* todo: signal_pending? */
5172 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5173 +                       } while (err == -EAGAIN || err == -EINTR);
5174 +                       if (err == 1)
5175 +                               dst->f_pos--;
5176 +               }
5177 +
5178 +               if (err == 1) {
5179 +                       ia = (void *)buf;
5180 +                       ia->ia_size = dst->f_pos;
5181 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5182 +                       ia->ia_file = dst;
5183 +                       h_inode = file_inode(dst);
5184 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5185 +                       /* no delegation since it is just created */
5186 +                       err = vfsub_notify_change(&dst->f_path, ia,
5187 +                                                 /*delegated*/NULL);
5188 +                       inode_unlock(h_inode);
5189 +               }
5190 +       }
5191 +
5192 +       return err;
5193 +}
5194 +
5195 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5196 +{
5197 +       int err;
5198 +       unsigned long blksize;
5199 +       unsigned char do_kfree;
5200 +       char *buf;
5201 +       struct super_block *h_sb;
5202 +
5203 +       err = -ENOMEM;
5204 +       h_sb = file_inode(dst)->i_sb;
5205 +       blksize = h_sb->s_blocksize;
5206 +       if (!blksize || PAGE_SIZE < blksize)
5207 +               blksize = PAGE_SIZE;
5208 +       AuDbg("blksize %lu\n", blksize);
5209 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5210 +       if (do_kfree)
5211 +               buf = kmalloc(blksize, GFP_NOFS);
5212 +       else
5213 +               buf = (void *)__get_free_page(GFP_NOFS);
5214 +       if (unlikely(!buf))
5215 +               goto out;
5216 +
5217 +       if (len > (1 << 22))
5218 +               AuDbg("copying a large file %lld\n", (long long)len);
5219 +
5220 +       src->f_pos = 0;
5221 +       dst->f_pos = 0;
5222 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5223 +       if (do_kfree) {
5224 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5225 +               au_kfree_do_rcu(buf);
5226 +       } else
5227 +               free_page((unsigned long)buf);
5228 +
5229 +out:
5230 +       return err;
5231 +}
5232 +
5233 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5234 +{
5235 +       int err;
5236 +       struct super_block *h_src_sb;
5237 +       struct inode *h_src_inode;
5238 +
5239 +       h_src_inode = file_inode(src);
5240 +       h_src_sb = h_src_inode->i_sb;
5241 +
5242 +       /* XFS acquires inode_lock */
5243 +       if (!au_test_xfs(h_src_sb))
5244 +               err = au_copy_file(dst, src, len);
5245 +       else {
5246 +               inode_unlock_shared(h_src_inode);
5247 +               err = au_copy_file(dst, src, len);
5248 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5249 +       }
5250 +
5251 +       return err;
5252 +}
5253 +
5254 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5255 +{
5256 +       int err;
5257 +       loff_t lo;
5258 +       struct super_block *h_src_sb;
5259 +       struct inode *h_src_inode;
5260 +
5261 +       h_src_inode = file_inode(src);
5262 +       h_src_sb = h_src_inode->i_sb;
5263 +       if (h_src_sb != file_inode(dst)->i_sb
5264 +           || !dst->f_op->remap_file_range) {
5265 +               err = au_do_copy(dst, src, len);
5266 +               goto out;
5267 +       }
5268 +
5269 +       if (!au_test_nfs(h_src_sb)) {
5270 +               inode_unlock_shared(h_src_inode);
5271 +               lo = vfsub_clone_file_range(src, dst, len);
5272 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5273 +       } else
5274 +               lo = vfsub_clone_file_range(src, dst, len);
5275 +       if (lo == len) {
5276 +               err = 0;
5277 +               goto out; /* success */
5278 +       } else if (lo >= 0)
5279 +               /* todo: possible? */
5280 +               /* paritially succeeded */
5281 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5282 +       else if (lo != -EOPNOTSUPP) {
5283 +               /* older XFS has a condition in cloning */
5284 +               err = lo;
5285 +               goto out;
5286 +       }
5287 +
5288 +       /* the backend fs on NFS may not support cloning */
5289 +       err = au_do_copy(dst, src, len);
5290 +
5291 +out:
5292 +       AuTraceErr(err);
5293 +       return err;
5294 +}
5295 +
5296 +/*
5297 + * to support a sparse file which is opened with O_APPEND,
5298 + * we need to close the file.
5299 + */
5300 +static int au_cp_regular(struct au_cp_generic *cpg)
5301 +{
5302 +       int err, i;
5303 +       enum { SRC, DST };
5304 +       struct {
5305 +               aufs_bindex_t bindex;
5306 +               unsigned int flags;
5307 +               struct dentry *dentry;
5308 +               int force_wr;
5309 +               struct file *file;
5310 +       } *f, file[] = {
5311 +               {
5312 +                       .bindex = cpg->bsrc,
5313 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5314 +               },
5315 +               {
5316 +                       .bindex = cpg->bdst,
5317 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5318 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5319 +               }
5320 +       };
5321 +       struct au_branch *br;
5322 +       struct super_block *sb, *h_src_sb;
5323 +       struct inode *h_src_inode;
5324 +       struct task_struct *tsk = current;
5325 +
5326 +       /* bsrc branch can be ro/rw. */
5327 +       sb = cpg->dentry->d_sb;
5328 +       f = file;
5329 +       for (i = 0; i < 2; i++, f++) {
5330 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5331 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5332 +                                   /*file*/NULL, f->force_wr);
5333 +               if (IS_ERR(f->file)) {
5334 +                       err = PTR_ERR(f->file);
5335 +                       if (i == SRC)
5336 +                               goto out;
5337 +                       else
5338 +                               goto out_src;
5339 +               }
5340 +       }
5341 +
5342 +       /* try stopping to update while we copyup */
5343 +       h_src_inode = d_inode(file[SRC].dentry);
5344 +       h_src_sb = h_src_inode->i_sb;
5345 +       if (!au_test_nfs(h_src_sb))
5346 +               IMustLock(h_src_inode);
5347 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5348 +
5349 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5350 +       if (tsk->flags & PF_KTHREAD)
5351 +               __fput_sync(file[DST].file);
5352 +       else {
5353 +               /* it happened actually */
5354 +               fput(file[DST].file);
5355 +               /*
5356 +                * too bad.
5357 +                * we have to call both since we don't know which place the file
5358 +                * was added to.
5359 +                */
5360 +               task_work_run();
5361 +               flush_delayed_fput();
5362 +       }
5363 +       br = au_sbr(sb, file[DST].bindex);
5364 +       au_lcnt_dec(&br->br_nfiles);
5365 +
5366 +out_src:
5367 +       fput(file[SRC].file);
5368 +       br = au_sbr(sb, file[SRC].bindex);
5369 +       au_lcnt_dec(&br->br_nfiles);
5370 +out:
5371 +       return err;
5372 +}
5373 +
5374 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5375 +                             struct au_cpup_reg_attr *h_src_attr)
5376 +{
5377 +       int err, rerr;
5378 +       loff_t l;
5379 +       struct path h_path;
5380 +       struct inode *h_src_inode, *h_dst_inode;
5381 +
5382 +       err = 0;
5383 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5384 +       l = i_size_read(h_src_inode);
5385 +       if (cpg->len == -1 || l < cpg->len)
5386 +               cpg->len = l;
5387 +       if (cpg->len) {
5388 +               /* try stopping to update while we are referencing */
5389 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5390 +               au_pin_hdir_unlock(cpg->pin);
5391 +
5392 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5393 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5394 +               h_src_attr->iflags = h_src_inode->i_flags;
5395 +               if (!au_test_nfs(h_src_inode->i_sb))
5396 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5397 +               else {
5398 +                       inode_unlock_shared(h_src_inode);
5399 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5400 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5401 +               }
5402 +               if (unlikely(err)) {
5403 +                       inode_unlock_shared(h_src_inode);
5404 +                       goto out;
5405 +               }
5406 +               h_src_attr->valid = 1;
5407 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5408 +                       err = au_cp_regular(cpg);
5409 +                       inode_unlock_shared(h_src_inode);
5410 +               } else {
5411 +                       inode_unlock_shared(h_src_inode);
5412 +                       err = au_cp_regular(cpg);
5413 +               }
5414 +               rerr = au_pin_hdir_relock(cpg->pin);
5415 +               if (!err && rerr)
5416 +                       err = rerr;
5417 +       }
5418 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5419 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5420 +               h_dst_inode = d_inode(h_path.dentry);
5421 +               spin_lock(&h_dst_inode->i_lock);
5422 +               h_dst_inode->i_state |= I_LINKABLE;
5423 +               spin_unlock(&h_dst_inode->i_lock);
5424 +       }
5425 +
5426 +out:
5427 +       return err;
5428 +}
5429 +
5430 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5431 +                             struct inode *h_dir)
5432 +{
5433 +       int err, symlen;
5434 +       mm_segment_t old_fs;
5435 +       union {
5436 +               char *k;
5437 +               char __user *u;
5438 +       } sym;
5439 +
5440 +       err = -ENOMEM;
5441 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5442 +       if (unlikely(!sym.k))
5443 +               goto out;
5444 +
5445 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5446 +       old_fs = get_fs();
5447 +       set_fs(KERNEL_DS);
5448 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5449 +       err = symlen;
5450 +       set_fs(old_fs);
5451 +
5452 +       if (symlen > 0) {
5453 +               sym.k[symlen] = 0;
5454 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5455 +       }
5456 +       free_page((unsigned long)sym.k);
5457 +
5458 +out:
5459 +       return err;
5460 +}
5461 +
5462 +/*
5463 + * regardless 'acl' option, reset all ACL.
5464 + * All ACL will be copied up later from the original entry on the lower branch.
5465 + */
5466 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5467 +{
5468 +       int err;
5469 +       struct dentry *h_dentry;
5470 +       struct inode *h_inode;
5471 +
5472 +       h_dentry = h_path->dentry;
5473 +       h_inode = d_inode(h_dentry);
5474 +       /* forget_all_cached_acls(h_inode)); */
5475 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5476 +       AuTraceErr(err);
5477 +       if (err == -EOPNOTSUPP)
5478 +               err = 0;
5479 +       if (!err)
5480 +               err = vfsub_acl_chmod(h_inode, mode);
5481 +
5482 +       AuTraceErr(err);
5483 +       return err;
5484 +}
5485 +
5486 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5487 +                         struct inode *h_dir, struct path *h_path)
5488 +{
5489 +       int err;
5490 +       struct inode *dir, *inode;
5491 +
5492 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5493 +       AuTraceErr(err);
5494 +       if (err == -EOPNOTSUPP)
5495 +               err = 0;
5496 +       if (unlikely(err))
5497 +               goto out;
5498 +
5499 +       /*
5500 +        * strange behaviour from the users view,
5501 +        * particularly setattr case
5502 +        */
5503 +       dir = d_inode(dst_parent);
5504 +       if (au_ibtop(dir) == cpg->bdst)
5505 +               au_cpup_attr_nlink(dir, /*force*/1);
5506 +       inode = d_inode(cpg->dentry);
5507 +       au_cpup_attr_nlink(inode, /*force*/1);
5508 +
5509 +out:
5510 +       return err;
5511 +}
5512 +
5513 +static noinline_for_stack
5514 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5515 +              struct au_cpup_reg_attr *h_src_attr)
5516 +{
5517 +       int err;
5518 +       umode_t mode;
5519 +       unsigned int mnt_flags;
5520 +       unsigned char isdir, isreg, force;
5521 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5522 +       struct au_dtime dt;
5523 +       struct path h_path;
5524 +       struct dentry *h_src, *h_dst, *h_parent;
5525 +       struct inode *h_inode, *h_dir;
5526 +       struct super_block *sb;
5527 +
5528 +       /* bsrc branch can be ro/rw. */
5529 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5530 +       h_inode = d_inode(h_src);
5531 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5532 +
5533 +       /* try stopping to be referenced while we are creating */
5534 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5535 +       if (au_ftest_cpup(cpg->flags, RENAME))
5536 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5537 +                                 AUFS_WH_PFX_LEN));
5538 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5539 +       h_dir = d_inode(h_parent);
5540 +       IMustLock(h_dir);
5541 +       AuDebugOn(h_parent != h_dst->d_parent);
5542 +
5543 +       sb = cpg->dentry->d_sb;
5544 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5545 +       if (do_dt) {
5546 +               h_path.dentry = h_parent;
5547 +               au_dtime_store(&dt, dst_parent, &h_path);
5548 +       }
5549 +       h_path.dentry = h_dst;
5550 +
5551 +       isreg = 0;
5552 +       isdir = 0;
5553 +       mode = h_inode->i_mode;
5554 +       switch (mode & S_IFMT) {
5555 +       case S_IFREG:
5556 +               isreg = 1;
5557 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5558 +               if (!err)
5559 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5560 +               break;
5561 +       case S_IFDIR:
5562 +               isdir = 1;
5563 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5564 +               if (!err)
5565 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5566 +               break;
5567 +       case S_IFLNK:
5568 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5569 +               break;
5570 +       case S_IFCHR:
5571 +       case S_IFBLK:
5572 +               AuDebugOn(!capable(CAP_MKNOD));
5573 +               /*FALLTHROUGH*/
5574 +       case S_IFIFO:
5575 +       case S_IFSOCK:
5576 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5577 +               break;
5578 +       default:
5579 +               AuIOErr("Unknown inode type 0%o\n", mode);
5580 +               err = -EIO;
5581 +       }
5582 +       if (!err)
5583 +               err = au_reset_acl(h_dir, &h_path, mode);
5584 +
5585 +       mnt_flags = au_mntflags(sb);
5586 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5587 +           && !isdir
5588 +           && au_opt_test(mnt_flags, XINO)
5589 +           && (h_inode->i_nlink == 1
5590 +               || (h_inode->i_state & I_LINKABLE))
5591 +           /* todo: unnecessary? */
5592 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5593 +           && cpg->bdst < cpg->bsrc
5594 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5595 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5596 +               /* ignore this error */
5597 +
5598 +       if (!err) {
5599 +               force = 0;
5600 +               if (isreg) {
5601 +                       force = !!cpg->len;
5602 +                       if (cpg->len == -1)
5603 +                               force = !!i_size_read(h_inode);
5604 +               }
5605 +               au_fhsm_wrote(sb, cpg->bdst, force);
5606 +       }
5607 +
5608 +       if (do_dt)
5609 +               au_dtime_revert(&dt);
5610 +       return err;
5611 +}
5612 +
5613 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5614 +{
5615 +       int err;
5616 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5617 +       struct inode *h_dir;
5618 +       aufs_bindex_t bdst;
5619 +
5620 +       dentry = cpg->dentry;
5621 +       bdst = cpg->bdst;
5622 +       h_dentry = au_h_dptr(dentry, bdst);
5623 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5624 +               dget(h_dentry);
5625 +               au_set_h_dptr(dentry, bdst, NULL);
5626 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5627 +               if (!err)
5628 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5629 +               au_set_h_dptr(dentry, bdst, h_dentry);
5630 +       } else {
5631 +               err = 0;
5632 +               parent = dget_parent(dentry);
5633 +               h_parent = au_h_dptr(parent, bdst);
5634 +               dput(parent);
5635 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5636 +               if (IS_ERR(h_path->dentry))
5637 +                       err = PTR_ERR(h_path->dentry);
5638 +       }
5639 +       if (unlikely(err))
5640 +               goto out;
5641 +
5642 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5643 +       h_dir = d_inode(h_parent);
5644 +       IMustLock(h_dir);
5645 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5646 +       /* no delegation since it is just created */
5647 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5648 +                          /*flags*/0);
5649 +       dput(h_path->dentry);
5650 +
5651 +out:
5652 +       return err;
5653 +}
5654 +
5655 +/*
5656 + * copyup the @dentry from @bsrc to @bdst.
5657 + * the caller must set the both of lower dentries.
5658 + * @len is for truncating when it is -1 copyup the entire file.
5659 + * in link/rename cases, @dst_parent may be different from the real one.
5660 + * basic->bsrc can be larger than basic->bdst.
5661 + * aufs doesn't touch the credential so
5662 + * security_inode_copy_up{,_xattr}() are unnecessary.
5663 + */
5664 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5665 +{
5666 +       int err, rerr;
5667 +       aufs_bindex_t old_ibtop;
5668 +       unsigned char isdir, plink;
5669 +       struct dentry *h_src, *h_dst, *h_parent;
5670 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5671 +       struct super_block *sb;
5672 +       struct au_branch *br;
5673 +       /* to reduce stack size */
5674 +       struct {
5675 +               struct au_dtime dt;
5676 +               struct path h_path;
5677 +               struct au_cpup_reg_attr h_src_attr;
5678 +       } *a;
5679 +
5680 +       err = -ENOMEM;
5681 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5682 +       if (unlikely(!a))
5683 +               goto out;
5684 +       a->h_src_attr.valid = 0;
5685 +
5686 +       sb = cpg->dentry->d_sb;
5687 +       br = au_sbr(sb, cpg->bdst);
5688 +       a->h_path.mnt = au_br_mnt(br);
5689 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5690 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5691 +       h_dir = d_inode(h_parent);
5692 +       IMustLock(h_dir);
5693 +
5694 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5695 +       inode = d_inode(cpg->dentry);
5696 +
5697 +       if (!dst_parent)
5698 +               dst_parent = dget_parent(cpg->dentry);
5699 +       else
5700 +               dget(dst_parent);
5701 +
5702 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5703 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5704 +       if (dst_inode) {
5705 +               if (unlikely(!plink)) {
5706 +                       err = -EIO;
5707 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5708 +                               "but plink is disabled\n",
5709 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5710 +                       goto out_parent;
5711 +               }
5712 +
5713 +               if (dst_inode->i_nlink) {
5714 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5715 +
5716 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5717 +                       err = PTR_ERR(h_src);
5718 +                       if (IS_ERR(h_src))
5719 +                               goto out_parent;
5720 +                       if (unlikely(d_is_negative(h_src))) {
5721 +                               err = -EIO;
5722 +                               AuIOErr("i%lu exists on b%d "
5723 +                                       "but not pseudo-linked\n",
5724 +                                       inode->i_ino, cpg->bdst);
5725 +                               dput(h_src);
5726 +                               goto out_parent;
5727 +                       }
5728 +
5729 +                       if (do_dt) {
5730 +                               a->h_path.dentry = h_parent;
5731 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5732 +                       }
5733 +
5734 +                       a->h_path.dentry = h_dst;
5735 +                       delegated = NULL;
5736 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5737 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5738 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5739 +                       if (do_dt)
5740 +                               au_dtime_revert(&a->dt);
5741 +                       if (unlikely(err == -EWOULDBLOCK)) {
5742 +                               pr_warn("cannot retry for NFSv4 delegation"
5743 +                                       " for an internal link\n");
5744 +                               iput(delegated);
5745 +                       }
5746 +                       dput(h_src);
5747 +                       goto out_parent;
5748 +               } else
5749 +                       /* todo: cpup_wh_file? */
5750 +                       /* udba work */
5751 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5752 +       }
5753 +
5754 +       isdir = S_ISDIR(inode->i_mode);
5755 +       old_ibtop = au_ibtop(inode);
5756 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5757 +       if (unlikely(err))
5758 +               goto out_rev;
5759 +       dst_inode = d_inode(h_dst);
5760 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5761 +       /* todo: necessary? */
5762 +       /* au_pin_hdir_unlock(cpg->pin); */
5763 +
5764 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5765 +       if (unlikely(err)) {
5766 +               /* todo: necessary? */
5767 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5768 +               inode_unlock(dst_inode);
5769 +               goto out_rev;
5770 +       }
5771 +
5772 +       if (cpg->bdst < old_ibtop) {
5773 +               if (S_ISREG(inode->i_mode)) {
5774 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5775 +                       if (unlikely(err)) {
5776 +                               /* ignore an error */
5777 +                               /* au_pin_hdir_relock(cpg->pin); */
5778 +                               inode_unlock(dst_inode);
5779 +                               goto out_rev;
5780 +                       }
5781 +               }
5782 +               au_set_ibtop(inode, cpg->bdst);
5783 +       } else
5784 +               au_set_ibbot(inode, cpg->bdst);
5785 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5786 +                     au_hi_flags(inode, isdir));
5787 +
5788 +       /* todo: necessary? */
5789 +       /* err = au_pin_hdir_relock(cpg->pin); */
5790 +       inode_unlock(dst_inode);
5791 +       if (unlikely(err))
5792 +               goto out_rev;
5793 +
5794 +       src_inode = d_inode(h_src);
5795 +       if (!isdir
5796 +           && (src_inode->i_nlink > 1
5797 +               || src_inode->i_state & I_LINKABLE)
5798 +           && plink)
5799 +               au_plink_append(inode, cpg->bdst, h_dst);
5800 +
5801 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5802 +               a->h_path.dentry = h_dst;
5803 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5804 +       }
5805 +       if (!err)
5806 +               goto out_parent; /* success */
5807 +
5808 +       /* revert */
5809 +out_rev:
5810 +       a->h_path.dentry = h_parent;
5811 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5812 +       a->h_path.dentry = h_dst;
5813 +       rerr = 0;
5814 +       if (d_is_positive(h_dst)) {
5815 +               if (!isdir) {
5816 +                       /* no delegation since it is just created */
5817 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5818 +                                           /*delegated*/NULL, /*force*/0);
5819 +               } else
5820 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5821 +       }
5822 +       au_dtime_revert(&a->dt);
5823 +       if (rerr) {
5824 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5825 +               err = -EIO;
5826 +       }
5827 +out_parent:
5828 +       dput(dst_parent);
5829 +       au_kfree_rcu(a);
5830 +out:
5831 +       return err;
5832 +}
5833 +
5834 +#if 0 /* reserved */
5835 +struct au_cpup_single_args {
5836 +       int *errp;
5837 +       struct au_cp_generic *cpg;
5838 +       struct dentry *dst_parent;
5839 +};
5840 +
5841 +static void au_call_cpup_single(void *args)
5842 +{
5843 +       struct au_cpup_single_args *a = args;
5844 +
5845 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5846 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5847 +       au_pin_hdir_release(a->cpg->pin);
5848 +}
5849 +#endif
5850 +
5851 +/*
5852 + * prevent SIGXFSZ in copy-up.
5853 + * testing CAP_MKNOD is for generic fs,
5854 + * but CAP_FSETID is for xfs only, currently.
5855 + */
5856 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5857 +{
5858 +       int do_sio;
5859 +       struct super_block *sb;
5860 +       struct inode *h_dir;
5861 +
5862 +       do_sio = 0;
5863 +       sb = au_pinned_parent(pin)->d_sb;
5864 +       if (!au_wkq_test()
5865 +           && (!au_sbi(sb)->si_plink_maint_pid
5866 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5867 +               switch (mode & S_IFMT) {
5868 +               case S_IFREG:
5869 +                       /* no condition about RLIMIT_FSIZE and the file size */
5870 +                       do_sio = 1;
5871 +                       break;
5872 +               case S_IFCHR:
5873 +               case S_IFBLK:
5874 +                       do_sio = !capable(CAP_MKNOD);
5875 +                       break;
5876 +               }
5877 +               if (!do_sio)
5878 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5879 +                                 && !capable(CAP_FSETID));
5880 +               /* this workaround may be removed in the future */
5881 +               if (!do_sio) {
5882 +                       h_dir = au_pinned_h_dir(pin);
5883 +                       do_sio = h_dir->i_mode & S_ISVTX;
5884 +               }
5885 +       }
5886 +
5887 +       return do_sio;
5888 +}
5889 +
5890 +#if 0 /* reserved */
5891 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5892 +{
5893 +       int err, wkq_err;
5894 +       struct dentry *h_dentry;
5895 +
5896 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5897 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5898 +               err = au_cpup_single(cpg, dst_parent);
5899 +       else {
5900 +               struct au_cpup_single_args args = {
5901 +                       .errp           = &err,
5902 +                       .cpg            = cpg,
5903 +                       .dst_parent     = dst_parent
5904 +               };
5905 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5906 +               if (unlikely(wkq_err))
5907 +                       err = wkq_err;
5908 +       }
5909 +
5910 +       return err;
5911 +}
5912 +#endif
5913 +
5914 +/*
5915 + * copyup the @dentry from the first active lower branch to @bdst,
5916 + * using au_cpup_single().
5917 + */
5918 +static int au_cpup_simple(struct au_cp_generic *cpg)
5919 +{
5920 +       int err;
5921 +       unsigned int flags_orig;
5922 +       struct dentry *dentry;
5923 +
5924 +       AuDebugOn(cpg->bsrc < 0);
5925 +
5926 +       dentry = cpg->dentry;
5927 +       DiMustWriteLock(dentry);
5928 +
5929 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5930 +       if (!err) {
5931 +               flags_orig = cpg->flags;
5932 +               au_fset_cpup(cpg->flags, RENAME);
5933 +               err = au_cpup_single(cpg, NULL);
5934 +               cpg->flags = flags_orig;
5935 +               if (!err)
5936 +                       return 0; /* success */
5937 +
5938 +               /* revert */
5939 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5940 +               au_set_dbtop(dentry, cpg->bsrc);
5941 +       }
5942 +
5943 +       return err;
5944 +}
5945 +
5946 +struct au_cpup_simple_args {
5947 +       int *errp;
5948 +       struct au_cp_generic *cpg;
5949 +};
5950 +
5951 +static void au_call_cpup_simple(void *args)
5952 +{
5953 +       struct au_cpup_simple_args *a = args;
5954 +
5955 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5956 +       *a->errp = au_cpup_simple(a->cpg);
5957 +       au_pin_hdir_release(a->cpg->pin);
5958 +}
5959 +
5960 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5961 +{
5962 +       int err, wkq_err;
5963 +       struct dentry *dentry, *parent;
5964 +       struct file *h_file;
5965 +       struct inode *h_dir;
5966 +
5967 +       dentry = cpg->dentry;
5968 +       h_file = NULL;
5969 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5970 +               AuDebugOn(cpg->bsrc < 0);
5971 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5972 +               err = PTR_ERR(h_file);
5973 +               if (IS_ERR(h_file))
5974 +                       goto out;
5975 +       }
5976 +
5977 +       parent = dget_parent(dentry);
5978 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5979 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5980 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5981 +               err = au_cpup_simple(cpg);
5982 +       else {
5983 +               struct au_cpup_simple_args args = {
5984 +                       .errp           = &err,
5985 +                       .cpg            = cpg
5986 +               };
5987 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5988 +               if (unlikely(wkq_err))
5989 +                       err = wkq_err;
5990 +       }
5991 +
5992 +       dput(parent);
5993 +       if (h_file)
5994 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5995 +
5996 +out:
5997 +       return err;
5998 +}
5999 +
6000 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
6001 +{
6002 +       aufs_bindex_t bsrc, bbot;
6003 +       struct dentry *dentry, *h_dentry;
6004 +
6005 +       if (cpg->bsrc < 0) {
6006 +               dentry = cpg->dentry;
6007 +               bbot = au_dbbot(dentry);
6008 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
6009 +                       h_dentry = au_h_dptr(dentry, bsrc);
6010 +                       if (h_dentry) {
6011 +                               AuDebugOn(d_is_negative(h_dentry));
6012 +                               break;
6013 +                       }
6014 +               }
6015 +               AuDebugOn(bsrc > bbot);
6016 +               cpg->bsrc = bsrc;
6017 +       }
6018 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
6019 +       return au_do_sio_cpup_simple(cpg);
6020 +}
6021 +
6022 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6023 +{
6024 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
6025 +       return au_do_sio_cpup_simple(cpg);
6026 +}
6027 +
6028 +/* ---------------------------------------------------------------------- */
6029 +
6030 +/*
6031 + * copyup the deleted file for writing.
6032 + */
6033 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6034 +                        struct file *file)
6035 +{
6036 +       int err;
6037 +       unsigned int flags_orig;
6038 +       aufs_bindex_t bsrc_orig;
6039 +       struct au_dinfo *dinfo;
6040 +       struct {
6041 +               struct au_hdentry *hd;
6042 +               struct dentry *h_dentry;
6043 +       } hdst, hsrc;
6044 +
6045 +       dinfo = au_di(cpg->dentry);
6046 +       AuRwMustWriteLock(&dinfo->di_rwsem);
6047 +
6048 +       bsrc_orig = cpg->bsrc;
6049 +       cpg->bsrc = dinfo->di_btop;
6050 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
6051 +       hdst.h_dentry = hdst.hd->hd_dentry;
6052 +       hdst.hd->hd_dentry = wh_dentry;
6053 +       dinfo->di_btop = cpg->bdst;
6054 +
6055 +       hsrc.h_dentry = NULL;
6056 +       if (file) {
6057 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6058 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
6059 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
6060 +       }
6061 +       flags_orig = cpg->flags;
6062 +       cpg->flags = !AuCpup_DTIME;
6063 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
6064 +       cpg->flags = flags_orig;
6065 +       if (file) {
6066 +               if (!err)
6067 +                       err = au_reopen_nondir(file);
6068 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
6069 +       }
6070 +       hdst.hd->hd_dentry = hdst.h_dentry;
6071 +       dinfo->di_btop = cpg->bsrc;
6072 +       cpg->bsrc = bsrc_orig;
6073 +
6074 +       return err;
6075 +}
6076 +
6077 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6078 +{
6079 +       int err;
6080 +       aufs_bindex_t bdst;
6081 +       struct au_dtime dt;
6082 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6083 +       struct au_branch *br;
6084 +       struct path h_path;
6085 +
6086 +       dentry = cpg->dentry;
6087 +       bdst = cpg->bdst;
6088 +       br = au_sbr(dentry->d_sb, bdst);
6089 +       parent = dget_parent(dentry);
6090 +       h_parent = au_h_dptr(parent, bdst);
6091 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6092 +       err = PTR_ERR(wh_dentry);
6093 +       if (IS_ERR(wh_dentry))
6094 +               goto out;
6095 +
6096 +       h_path.dentry = h_parent;
6097 +       h_path.mnt = au_br_mnt(br);
6098 +       au_dtime_store(&dt, parent, &h_path);
6099 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6100 +       if (unlikely(err))
6101 +               goto out_wh;
6102 +
6103 +       dget(wh_dentry);
6104 +       h_path.dentry = wh_dentry;
6105 +       if (!d_is_dir(wh_dentry)) {
6106 +               /* no delegation since it is just created */
6107 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6108 +                                  /*delegated*/NULL, /*force*/0);
6109 +       } else
6110 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6111 +       if (unlikely(err)) {
6112 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6113 +                       wh_dentry, err);
6114 +               err = -EIO;
6115 +       }
6116 +       au_dtime_revert(&dt);
6117 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6118 +
6119 +out_wh:
6120 +       dput(wh_dentry);
6121 +out:
6122 +       dput(parent);
6123 +       return err;
6124 +}
6125 +
6126 +struct au_cpup_wh_args {
6127 +       int *errp;
6128 +       struct au_cp_generic *cpg;
6129 +       struct file *file;
6130 +};
6131 +
6132 +static void au_call_cpup_wh(void *args)
6133 +{
6134 +       struct au_cpup_wh_args *a = args;
6135 +
6136 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6137 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6138 +       au_pin_hdir_release(a->cpg->pin);
6139 +}
6140 +
6141 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6142 +{
6143 +       int err, wkq_err;
6144 +       aufs_bindex_t bdst;
6145 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6146 +       struct inode *dir, *h_dir, *h_tmpdir;
6147 +       struct au_wbr *wbr;
6148 +       struct au_pin wh_pin, *pin_orig;
6149 +
6150 +       dentry = cpg->dentry;
6151 +       bdst = cpg->bdst;
6152 +       parent = dget_parent(dentry);
6153 +       dir = d_inode(parent);
6154 +       h_orph = NULL;
6155 +       h_parent = NULL;
6156 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6157 +       h_tmpdir = h_dir;
6158 +       pin_orig = NULL;
6159 +       if (!h_dir->i_nlink) {
6160 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6161 +               h_orph = wbr->wbr_orph;
6162 +
6163 +               h_parent = dget(au_h_dptr(parent, bdst));
6164 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6165 +               h_tmpdir = d_inode(h_orph);
6166 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6167 +
6168 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6169 +               /* todo: au_h_open_pre()? */
6170 +
6171 +               pin_orig = cpg->pin;
6172 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6173 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6174 +               cpg->pin = &wh_pin;
6175 +       }
6176 +
6177 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6178 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6179 +               err = au_cpup_wh(cpg, file);
6180 +       else {
6181 +               struct au_cpup_wh_args args = {
6182 +                       .errp   = &err,
6183 +                       .cpg    = cpg,
6184 +                       .file   = file
6185 +               };
6186 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6187 +               if (unlikely(wkq_err))
6188 +                       err = wkq_err;
6189 +       }
6190 +
6191 +       if (h_orph) {
6192 +               inode_unlock(h_tmpdir);
6193 +               /* todo: au_h_open_post()? */
6194 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6195 +               au_set_h_dptr(parent, bdst, h_parent);
6196 +               AuDebugOn(!pin_orig);
6197 +               cpg->pin = pin_orig;
6198 +       }
6199 +       iput(h_dir);
6200 +       dput(parent);
6201 +
6202 +       return err;
6203 +}
6204 +
6205 +/* ---------------------------------------------------------------------- */
6206 +
6207 +/*
6208 + * generic routine for both of copy-up and copy-down.
6209 + */
6210 +/* cf. revalidate function in file.c */
6211 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6212 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6213 +                        struct au_pin *pin,
6214 +                        struct dentry *h_parent, void *arg),
6215 +              void *arg)
6216 +{
6217 +       int err;
6218 +       struct au_pin pin;
6219 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6220 +
6221 +       err = 0;
6222 +       parent = dget_parent(dentry);
6223 +       if (IS_ROOT(parent))
6224 +               goto out;
6225 +
6226 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6227 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6228 +
6229 +       /* do not use au_dpage */
6230 +       real_parent = parent;
6231 +       while (1) {
6232 +               dput(parent);
6233 +               parent = dget_parent(dentry);
6234 +               h_parent = au_h_dptr(parent, bdst);
6235 +               if (h_parent)
6236 +                       goto out; /* success */
6237 +
6238 +               /* find top dir which is necessary to cpup */
6239 +               do {
6240 +                       d = parent;
6241 +                       dput(parent);
6242 +                       parent = dget_parent(d);
6243 +                       di_read_lock_parent3(parent, !AuLock_IR);
6244 +                       h_parent = au_h_dptr(parent, bdst);
6245 +                       di_read_unlock(parent, !AuLock_IR);
6246 +               } while (!h_parent);
6247 +
6248 +               if (d != real_parent)
6249 +                       di_write_lock_child3(d);
6250 +
6251 +               /* somebody else might create while we were sleeping */
6252 +               h_dentry = au_h_dptr(d, bdst);
6253 +               if (!h_dentry || d_is_negative(h_dentry)) {
6254 +                       if (h_dentry)
6255 +                               au_update_dbtop(d);
6256 +
6257 +                       au_pin_set_dentry(&pin, d);
6258 +                       err = au_do_pin(&pin);
6259 +                       if (!err) {
6260 +                               err = cp(d, bdst, &pin, h_parent, arg);
6261 +                               au_unpin(&pin);
6262 +                       }
6263 +               }
6264 +
6265 +               if (d != real_parent)
6266 +                       di_write_unlock(d);
6267 +               if (unlikely(err))
6268 +                       break;
6269 +       }
6270 +
6271 +out:
6272 +       dput(parent);
6273 +       return err;
6274 +}
6275 +
6276 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6277 +                      struct au_pin *pin,
6278 +                      struct dentry *h_parent __maybe_unused,
6279 +                      void *arg __maybe_unused)
6280 +{
6281 +       struct au_cp_generic cpg = {
6282 +               .dentry = dentry,
6283 +               .bdst   = bdst,
6284 +               .bsrc   = -1,
6285 +               .len    = 0,
6286 +               .pin    = pin,
6287 +               .flags  = AuCpup_DTIME
6288 +       };
6289 +       return au_sio_cpup_simple(&cpg);
6290 +}
6291 +
6292 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6293 +{
6294 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6295 +}
6296 +
6297 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6298 +{
6299 +       int err;
6300 +       struct dentry *parent;
6301 +       struct inode *dir;
6302 +
6303 +       parent = dget_parent(dentry);
6304 +       dir = d_inode(parent);
6305 +       err = 0;
6306 +       if (au_h_iptr(dir, bdst))
6307 +               goto out;
6308 +
6309 +       di_read_unlock(parent, AuLock_IR);
6310 +       di_write_lock_parent(parent);
6311 +       /* someone else might change our inode while we were sleeping */
6312 +       if (!au_h_iptr(dir, bdst))
6313 +               err = au_cpup_dirs(dentry, bdst);
6314 +       di_downgrade_lock(parent, AuLock_IR);
6315 +
6316 +out:
6317 +       dput(parent);
6318 +       return err;
6319 +}
6320 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6321 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6322 +++ linux/fs/aufs/cpup.h        2019-07-11 15:42:14.462237786 +0200
6323 @@ -0,0 +1,100 @@
6324 +/* SPDX-License-Identifier: GPL-2.0 */
6325 +/*
6326 + * Copyright (C) 2005-2019 Junjiro R. Okajima
6327 + *
6328 + * This program, aufs is free software; you can redistribute it and/or modify
6329 + * it under the terms of the GNU General Public License as published by
6330 + * the Free Software Foundation; either version 2 of the License, or
6331 + * (at your option) any later version.
6332 + *
6333 + * This program is distributed in the hope that it will be useful,
6334 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6335 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6336 + * GNU General Public License for more details.
6337 + *
6338 + * You should have received a copy of the GNU General Public License
6339 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6340 + */
6341 +
6342 +/*
6343 + * copy-up/down functions
6344 + */
6345 +
6346 +#ifndef __AUFS_CPUP_H__
6347 +#define __AUFS_CPUP_H__
6348 +
6349 +#ifdef __KERNEL__
6350 +
6351 +#include <linux/path.h>
6352 +
6353 +struct inode;
6354 +struct file;
6355 +struct au_pin;
6356 +
6357 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6358 +void au_cpup_attr_timesizes(struct inode *inode);
6359 +void au_cpup_attr_nlink(struct inode *inode, int force);
6360 +void au_cpup_attr_changeable(struct inode *inode);
6361 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6362 +void au_cpup_attr_all(struct inode *inode, int force);
6363 +
6364 +/* ---------------------------------------------------------------------- */
6365 +
6366 +struct au_cp_generic {
6367 +       struct dentry   *dentry;
6368 +       aufs_bindex_t   bdst, bsrc;
6369 +       loff_t          len;
6370 +       struct au_pin   *pin;
6371 +       unsigned int    flags;
6372 +};
6373 +
6374 +/* cpup flags */
6375 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6376 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6377 +                                                  for link(2) */
6378 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6379 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6380 +                                                  cpup */
6381 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6382 +                                                  existing entry */
6383 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6384 +                                                  the branch is marked as RO */
6385 +
6386 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6387 +#undef AuCpup_HOPEN
6388 +#define AuCpup_HOPEN           0
6389 +#endif
6390 +
6391 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6392 +#define au_fset_cpup(flags, name) \
6393 +       do { (flags) |= AuCpup_##name; } while (0)
6394 +#define au_fclr_cpup(flags, name) \
6395 +       do { (flags) &= ~AuCpup_##name; } while (0)
6396 +
6397 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6398 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6399 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6400 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6401 +
6402 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6403 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6404 +                        struct au_pin *pin,
6405 +                        struct dentry *h_parent, void *arg),
6406 +              void *arg);
6407 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6408 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6409 +
6410 +/* ---------------------------------------------------------------------- */
6411 +
6412 +/* keep timestamps when copyup */
6413 +struct au_dtime {
6414 +       struct dentry *dt_dentry;
6415 +       struct path dt_h_path;
6416 +       struct timespec64 dt_atime, dt_mtime;
6417 +};
6418 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6419 +                   struct path *h_path);
6420 +void au_dtime_revert(struct au_dtime *dt);
6421 +
6422 +#endif /* __KERNEL__ */
6423 +#endif /* __AUFS_CPUP_H__ */
6424 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6425 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6426 +++ linux/fs/aufs/dbgaufs.c     2019-07-11 15:42:14.462237786 +0200
6427 @@ -0,0 +1,526 @@
6428 +// SPDX-License-Identifier: GPL-2.0
6429 +/*
6430 + * Copyright (C) 2005-2019 Junjiro R. Okajima
6431 + *
6432 + * This program, aufs is free software; you can redistribute it and/or modify
6433 + * it under the terms of the GNU General Public License as published by
6434 + * the Free Software Foundation; either version 2 of the License, or
6435 + * (at your option) any later version.
6436 + *
6437 + * This program is distributed in the hope that it will be useful,
6438 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6439 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6440 + * GNU General Public License for more details.
6441 + *
6442 + * You should have received a copy of the GNU General Public License
6443 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6444 + */
6445 +
6446 +/*
6447 + * debugfs interface
6448 + */
6449 +
6450 +#include <linux/debugfs.h>
6451 +#include "aufs.h"
6452 +
6453 +#ifndef CONFIG_SYSFS
6454 +#error DEBUG_FS depends upon SYSFS
6455 +#endif
6456 +
6457 +static struct dentry *dbgaufs;
6458 +static const mode_t dbgaufs_mode = 0444;
6459 +
6460 +/* 20 is max digits length of ulong 64 */
6461 +struct dbgaufs_arg {
6462 +       int n;
6463 +       char a[20 * 4];
6464 +};
6465 +
6466 +/*
6467 + * common function for all XINO files
6468 + */
6469 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6470 +                             struct file *file)
6471 +{
6472 +       void *p;
6473 +
6474 +       p = file->private_data;
6475 +       if (p) {
6476 +               /* this is struct dbgaufs_arg */
6477 +               AuDebugOn(!au_kfree_sz_test(p));
6478 +               au_kfree_do_rcu(p);
6479 +       }
6480 +       return 0;
6481 +}
6482 +
6483 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6484 +                          int cnt)
6485 +{
6486 +       int err;
6487 +       struct kstat st;
6488 +       struct dbgaufs_arg *p;
6489 +
6490 +       err = -ENOMEM;
6491 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6492 +       if (unlikely(!p))
6493 +               goto out;
6494 +
6495 +       err = 0;
6496 +       p->n = 0;
6497 +       file->private_data = p;
6498 +       if (!xf)
6499 +               goto out;
6500 +
6501 +       err = vfsub_getattr(&xf->f_path, &st);
6502 +       if (!err) {
6503 +               if (do_fcnt)
6504 +                       p->n = snprintf
6505 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6506 +                                cnt, st.blocks, st.blksize,
6507 +                                (long long)st.size);
6508 +               else
6509 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6510 +                                       st.blocks, st.blksize,
6511 +                                       (long long)st.size);
6512 +               AuDebugOn(p->n >= sizeof(p->a));
6513 +       } else {
6514 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6515 +               err = 0;
6516 +       }
6517 +
6518 +out:
6519 +       return err;
6520 +}
6521 +
6522 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6523 +                              size_t count, loff_t *ppos)
6524 +{
6525 +       struct dbgaufs_arg *p;
6526 +
6527 +       p = file->private_data;
6528 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6529 +}
6530 +
6531 +/* ---------------------------------------------------------------------- */
6532 +
6533 +struct dbgaufs_plink_arg {
6534 +       int n;
6535 +       char a[];
6536 +};
6537 +
6538 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6539 +                                struct file *file)
6540 +{
6541 +       free_page((unsigned long)file->private_data);
6542 +       return 0;
6543 +}
6544 +
6545 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6546 +{
6547 +       int err, i, limit;
6548 +       unsigned long n, sum;
6549 +       struct dbgaufs_plink_arg *p;
6550 +       struct au_sbinfo *sbinfo;
6551 +       struct super_block *sb;
6552 +       struct hlist_bl_head *hbl;
6553 +
6554 +       err = -ENOMEM;
6555 +       p = (void *)get_zeroed_page(GFP_NOFS);
6556 +       if (unlikely(!p))
6557 +               goto out;
6558 +
6559 +       err = -EFBIG;
6560 +       sbinfo = inode->i_private;
6561 +       sb = sbinfo->si_sb;
6562 +       si_noflush_read_lock(sb);
6563 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6564 +               limit = PAGE_SIZE - sizeof(p->n);
6565 +
6566 +               /* the number of buckets */
6567 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6568 +               p->n += n;
6569 +               limit -= n;
6570 +
6571 +               sum = 0;
6572 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6573 +                    i++, hbl++) {
6574 +                       n = au_hbl_count(hbl);
6575 +                       sum += n;
6576 +
6577 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6578 +                       p->n += n;
6579 +                       limit -= n;
6580 +                       if (unlikely(limit <= 0))
6581 +                               goto out_free;
6582 +               }
6583 +               p->a[p->n - 1] = '\n';
6584 +
6585 +               /* the sum of plinks */
6586 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6587 +               p->n += n;
6588 +               limit -= n;
6589 +               if (unlikely(limit <= 0))
6590 +                       goto out_free;
6591 +       } else {
6592 +#define str "1\n0\n0\n"
6593 +               p->n = sizeof(str) - 1;
6594 +               strcpy(p->a, str);
6595 +#undef str
6596 +       }
6597 +       si_read_unlock(sb);
6598 +
6599 +       err = 0;
6600 +       file->private_data = p;
6601 +       goto out; /* success */
6602 +
6603 +out_free:
6604 +       free_page((unsigned long)p);
6605 +out:
6606 +       return err;
6607 +}
6608 +
6609 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6610 +                                 size_t count, loff_t *ppos)
6611 +{
6612 +       struct dbgaufs_plink_arg *p;
6613 +
6614 +       p = file->private_data;
6615 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6616 +}
6617 +
6618 +static const struct file_operations dbgaufs_plink_fop = {
6619 +       .owner          = THIS_MODULE,
6620 +       .open           = dbgaufs_plink_open,
6621 +       .release        = dbgaufs_plink_release,
6622 +       .read           = dbgaufs_plink_read
6623 +};
6624 +
6625 +/* ---------------------------------------------------------------------- */
6626 +
6627 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6628 +{
6629 +       int err;
6630 +       struct au_sbinfo *sbinfo;
6631 +       struct super_block *sb;
6632 +
6633 +       sbinfo = inode->i_private;
6634 +       sb = sbinfo->si_sb;
6635 +       si_noflush_read_lock(sb);
6636 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6637 +       si_read_unlock(sb);
6638 +       return err;
6639 +}
6640 +
6641 +static const struct file_operations dbgaufs_xib_fop = {
6642 +       .owner          = THIS_MODULE,
6643 +       .open           = dbgaufs_xib_open,
6644 +       .release        = dbgaufs_xi_release,
6645 +       .read           = dbgaufs_xi_read
6646 +};
6647 +
6648 +/* ---------------------------------------------------------------------- */
6649 +
6650 +#define DbgaufsXi_PREFIX "xi"
6651 +
6652 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6653 +{
6654 +       int err, idx;
6655 +       long l;
6656 +       aufs_bindex_t bindex;
6657 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6658 +       struct au_sbinfo *sbinfo;
6659 +       struct super_block *sb;
6660 +       struct au_xino *xi;
6661 +       struct file *xf;
6662 +       struct qstr *name;
6663 +       struct au_branch *br;
6664 +
6665 +       err = -ENOENT;
6666 +       name = &file->f_path.dentry->d_name;
6667 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6668 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6669 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6670 +               goto out;
6671 +
6672 +       AuDebugOn(name->len >= sizeof(a));
6673 +       memcpy(a, name->name, name->len);
6674 +       a[name->len] = '\0';
6675 +       p = strchr(a, '-');
6676 +       if (p)
6677 +               *p = '\0';
6678 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6679 +       if (unlikely(err))
6680 +               goto out;
6681 +       bindex = l;
6682 +       idx = 0;
6683 +       if (p) {
6684 +               err = kstrtol(p + 1, 10, &l);
6685 +               if (unlikely(err))
6686 +                       goto out;
6687 +               idx = l;
6688 +       }
6689 +
6690 +       err = -ENOENT;
6691 +       sbinfo = inode->i_private;
6692 +       sb = sbinfo->si_sb;
6693 +       si_noflush_read_lock(sb);
6694 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6695 +               goto out_si;
6696 +       br = au_sbr(sb, bindex);
6697 +       xi = br->br_xino;
6698 +       if (unlikely(idx >= xi->xi_nfile))
6699 +               goto out_si;
6700 +       xf = au_xino_file(xi, idx);
6701 +       if (xf)
6702 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6703 +                                     au_xino_count(br));
6704 +
6705 +out_si:
6706 +       si_read_unlock(sb);
6707 +out:
6708 +       AuTraceErr(err);
6709 +       return err;
6710 +}
6711 +
6712 +static const struct file_operations dbgaufs_xino_fop = {
6713 +       .owner          = THIS_MODULE,
6714 +       .open           = dbgaufs_xino_open,
6715 +       .release        = dbgaufs_xi_release,
6716 +       .read           = dbgaufs_xi_read
6717 +};
6718 +
6719 +void dbgaufs_xino_del(struct au_branch *br)
6720 +{
6721 +       struct dentry *dbgaufs;
6722 +
6723 +       dbgaufs = br->br_dbgaufs;
6724 +       if (!dbgaufs)
6725 +               return;
6726 +
6727 +       br->br_dbgaufs = NULL;
6728 +       /* debugfs acquires the parent i_mutex */
6729 +       lockdep_off();
6730 +       debugfs_remove(dbgaufs);
6731 +       lockdep_on();
6732 +}
6733 +
6734 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6735 +{
6736 +       aufs_bindex_t bbot;
6737 +       struct au_branch *br;
6738 +
6739 +       if (!au_sbi(sb)->si_dbgaufs)
6740 +               return;
6741 +
6742 +       bbot = au_sbbot(sb);
6743 +       for (; bindex <= bbot; bindex++) {
6744 +               br = au_sbr(sb, bindex);
6745 +               dbgaufs_xino_del(br);
6746 +       }
6747 +}
6748 +
6749 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6750 +                             unsigned int idx, struct dentry *parent,
6751 +                             struct au_sbinfo *sbinfo)
6752 +{
6753 +       struct au_branch *br;
6754 +       struct dentry *d;
6755 +       /* "xi" bindex(5) "-" idx(2) NULL */
6756 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6757 +
6758 +       if (!idx)
6759 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6760 +       else
6761 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6762 +                        bindex, idx);
6763 +       br = au_sbr(sb, bindex);
6764 +       if (br->br_dbgaufs) {
6765 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6766 +
6767 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6768 +                       /* debugfs acquires the parent i_mutex */
6769 +                       lockdep_off();
6770 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6771 +                                          name);
6772 +                       lockdep_on();
6773 +                       if (unlikely(!d))
6774 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6775 +                                       parent, name);
6776 +               }
6777 +       } else {
6778 +               lockdep_off();
6779 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6780 +                                                    sbinfo, &dbgaufs_xino_fop);
6781 +               lockdep_on();
6782 +               if (unlikely(!br->br_dbgaufs))
6783 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6784 +                               parent, name);
6785 +       }
6786 +}
6787 +
6788 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6789 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6790 +{
6791 +       struct au_branch *br;
6792 +       struct au_xino *xi;
6793 +       unsigned int u;
6794 +
6795 +       br = au_sbr(sb, bindex);
6796 +       xi = br->br_xino;
6797 +       for (u = 0; u < xi->xi_nfile; u++)
6798 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6799 +}
6800 +
6801 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6802 +{
6803 +       struct au_sbinfo *sbinfo;
6804 +       struct dentry *parent;
6805 +       aufs_bindex_t bbot;
6806 +
6807 +       if (!au_opt_test(au_mntflags(sb), XINO))
6808 +               return;
6809 +
6810 +       sbinfo = au_sbi(sb);
6811 +       parent = sbinfo->si_dbgaufs;
6812 +       if (!parent)
6813 +               return;
6814 +
6815 +       bbot = au_sbbot(sb);
6816 +       if (topdown)
6817 +               for (; bindex <= bbot; bindex++)
6818 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6819 +       else
6820 +               for (; bbot >= bindex; bbot--)
6821 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6822 +}
6823 +
6824 +/* ---------------------------------------------------------------------- */
6825 +
6826 +#ifdef CONFIG_AUFS_EXPORT
6827 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6828 +{
6829 +       int err;
6830 +       struct au_sbinfo *sbinfo;
6831 +       struct super_block *sb;
6832 +
6833 +       sbinfo = inode->i_private;
6834 +       sb = sbinfo->si_sb;
6835 +       si_noflush_read_lock(sb);
6836 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6837 +       si_read_unlock(sb);
6838 +       return err;
6839 +}
6840 +
6841 +static const struct file_operations dbgaufs_xigen_fop = {
6842 +       .owner          = THIS_MODULE,
6843 +       .open           = dbgaufs_xigen_open,
6844 +       .release        = dbgaufs_xi_release,
6845 +       .read           = dbgaufs_xi_read
6846 +};
6847 +
6848 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6849 +{
6850 +       int err;
6851 +
6852 +       /*
6853 +        * This function is a dynamic '__init' function actually,
6854 +        * so the tiny check for si_rwsem is unnecessary.
6855 +        */
6856 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6857 +
6858 +       err = -EIO;
6859 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6860 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6861 +                &dbgaufs_xigen_fop);
6862 +       if (sbinfo->si_dbgaufs_xigen)
6863 +               err = 0;
6864 +
6865 +       return err;
6866 +}
6867 +#else
6868 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6869 +{
6870 +       return 0;
6871 +}
6872 +#endif /* CONFIG_AUFS_EXPORT */
6873 +
6874 +/* ---------------------------------------------------------------------- */
6875 +
6876 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6877 +{
6878 +       /*
6879 +        * This function is a dynamic '__fin' function actually,
6880 +        * so the tiny check for si_rwsem is unnecessary.
6881 +        */
6882 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6883 +
6884 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6885 +       sbinfo->si_dbgaufs = NULL;
6886 +}
6887 +
6888 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6889 +{
6890 +       int err;
6891 +       char name[SysaufsSiNameLen];
6892 +
6893 +       /*
6894 +        * This function is a dynamic '__init' function actually,
6895 +        * so the tiny check for si_rwsem is unnecessary.
6896 +        */
6897 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6898 +
6899 +       err = -ENOENT;
6900 +       if (!dbgaufs) {
6901 +               AuErr1("/debug/aufs is uninitialized\n");
6902 +               goto out;
6903 +       }
6904 +
6905 +       err = -EIO;
6906 +       sysaufs_name(sbinfo, name);
6907 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6908 +       if (unlikely(!sbinfo->si_dbgaufs))
6909 +               goto out;
6910 +
6911 +       /* regardless plink/noplink option */
6912 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6913 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6914 +                &dbgaufs_plink_fop);
6915 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6916 +               goto out_dir;
6917 +
6918 +       /* regardless xino/noxino option */
6919 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6920 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6921 +                &dbgaufs_xib_fop);
6922 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6923 +               goto out_dir;
6924 +
6925 +       err = dbgaufs_xigen_init(sbinfo);
6926 +       if (!err)
6927 +               goto out; /* success */
6928 +
6929 +out_dir:
6930 +       dbgaufs_si_fin(sbinfo);
6931 +out:
6932 +       if (unlikely(err))
6933 +               pr_err("debugfs/aufs failed\n");
6934 +       return err;
6935 +}
6936 +
6937 +/* ---------------------------------------------------------------------- */
6938 +
6939 +void dbgaufs_fin(void)
6940 +{
6941 +       debugfs_remove(dbgaufs);
6942 +}
6943 +
6944 +int __init dbgaufs_init(void)
6945 +{
6946 +       int err;
6947 +
6948 +       err = -EIO;
6949 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6950 +       if (dbgaufs)
6951 +               err = 0;
6952 +       return err;
6953 +}
6954 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6955 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6956 +++ linux/fs/aufs/dbgaufs.h     2019-07-11 15:42:14.462237786 +0200
6957 @@ -0,0 +1,53 @@
6958 +/* SPDX-License-Identifier: GPL-2.0 */
6959 +/*
6960 + * Copyright (C) 2005-2019 Junjiro R. Okajima
6961 + *
6962 + * This program, aufs is free software; you can redistribute it and/or modify
6963 + * it under the terms of the GNU General Public License as published by
6964 + * the Free Software Foundation; either version 2 of the License, or
6965 + * (at your option) any later version.
6966 + *
6967 + * This program is distributed in the hope that it will be useful,
6968 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6969 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6970 + * GNU General Public License for more details.
6971 + *
6972 + * You should have received a copy of the GNU General Public License
6973 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6974 + */
6975 +
6976 +/*
6977 + * debugfs interface
6978 + */
6979 +
6980 +#ifndef __DBGAUFS_H__
6981 +#define __DBGAUFS_H__
6982 +
6983 +#ifdef __KERNEL__
6984 +
6985 +struct super_block;
6986 +struct au_sbinfo;
6987 +struct au_branch;
6988 +
6989 +#ifdef CONFIG_DEBUG_FS
6990 +/* dbgaufs.c */
6991 +void dbgaufs_xino_del(struct au_branch *br);
6992 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6993 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6994 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6995 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6996 +void dbgaufs_fin(void);
6997 +int __init dbgaufs_init(void);
6998 +#else
6999 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
7000 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
7001 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7002 +          int topdown)
7003 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7004 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7005 +AuStubVoid(dbgaufs_fin, void)
7006 +AuStubInt0(__init dbgaufs_init, void)
7007 +#endif /* CONFIG_DEBUG_FS */
7008 +
7009 +#endif /* __KERNEL__ */
7010 +#endif /* __DBGAUFS_H__ */
7011 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7012 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
7013 +++ linux/fs/aufs/dcsub.c       2019-07-11 15:42:14.462237786 +0200
7014 @@ -0,0 +1,225 @@
7015 +// SPDX-License-Identifier: GPL-2.0
7016 +/*
7017 + * Copyright (C) 2005-2019 Junjiro R. Okajima
7018 + *
7019 + * This program, aufs is free software; you can redistribute it and/or modify
7020 + * it under the terms of the GNU General Public License as published by
7021 + * the Free Software Foundation; either version 2 of the License, or
7022 + * (at your option) any later version.
7023 + *
7024 + * This program is distributed in the hope that it will be useful,
7025 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7026 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7027 + * GNU General Public License for more details.
7028 + *
7029 + * You should have received a copy of the GNU General Public License
7030 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7031 + */
7032 +
7033 +/*
7034 + * sub-routines for dentry cache
7035 + */
7036 +
7037 +#include "aufs.h"
7038 +
7039 +static void au_dpage_free(struct au_dpage *dpage)
7040 +{
7041 +       int i;
7042 +       struct dentry **p;
7043 +
7044 +       p = dpage->dentries;
7045 +       for (i = 0; i < dpage->ndentry; i++)
7046 +               dput(*p++);
7047 +       free_page((unsigned long)dpage->dentries);
7048 +}
7049 +
7050 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7051 +{
7052 +       int err;
7053 +       void *p;
7054 +
7055 +       err = -ENOMEM;
7056 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7057 +       if (unlikely(!dpages->dpages))
7058 +               goto out;
7059 +
7060 +       p = (void *)__get_free_page(gfp);
7061 +       if (unlikely(!p))
7062 +               goto out_dpages;
7063 +
7064 +       dpages->dpages[0].ndentry = 0;
7065 +       dpages->dpages[0].dentries = p;
7066 +       dpages->ndpage = 1;
7067 +       return 0; /* success */
7068 +
7069 +out_dpages:
7070 +       au_kfree_try_rcu(dpages->dpages);
7071 +out:
7072 +       return err;
7073 +}
7074 +
7075 +void au_dpages_free(struct au_dcsub_pages *dpages)
7076 +{
7077 +       int i;
7078 +       struct au_dpage *p;
7079 +
7080 +       p = dpages->dpages;
7081 +       for (i = 0; i < dpages->ndpage; i++)
7082 +               au_dpage_free(p++);
7083 +       au_kfree_try_rcu(dpages->dpages);
7084 +}
7085 +
7086 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7087 +                           struct dentry *dentry, gfp_t gfp)
7088 +{
7089 +       int err, sz;
7090 +       struct au_dpage *dpage;
7091 +       void *p;
7092 +
7093 +       dpage = dpages->dpages + dpages->ndpage - 1;
7094 +       sz = PAGE_SIZE / sizeof(dentry);
7095 +       if (unlikely(dpage->ndentry >= sz)) {
7096 +               AuLabel(new dpage);
7097 +               err = -ENOMEM;
7098 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7099 +               p = au_kzrealloc(dpages->dpages, sz,
7100 +                                sz + sizeof(*dpages->dpages), gfp,
7101 +                                /*may_shrink*/0);
7102 +               if (unlikely(!p))
7103 +                       goto out;
7104 +
7105 +               dpages->dpages = p;
7106 +               dpage = dpages->dpages + dpages->ndpage;
7107 +               p = (void *)__get_free_page(gfp);
7108 +               if (unlikely(!p))
7109 +                       goto out;
7110 +
7111 +               dpage->ndentry = 0;
7112 +               dpage->dentries = p;
7113 +               dpages->ndpage++;
7114 +       }
7115 +
7116 +       AuDebugOn(au_dcount(dentry) <= 0);
7117 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7118 +       return 0; /* success */
7119 +
7120 +out:
7121 +       return err;
7122 +}
7123 +
7124 +/* todo: BAD approach */
7125 +/* copied from linux/fs/dcache.c */
7126 +enum d_walk_ret {
7127 +       D_WALK_CONTINUE,
7128 +       D_WALK_QUIT,
7129 +       D_WALK_NORETRY,
7130 +       D_WALK_SKIP,
7131 +};
7132 +
7133 +extern void d_walk(struct dentry *parent, void *data,
7134 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7135 +
7136 +struct ac_dpages_arg {
7137 +       int err;
7138 +       struct au_dcsub_pages *dpages;
7139 +       struct super_block *sb;
7140 +       au_dpages_test test;
7141 +       void *arg;
7142 +};
7143 +
7144 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7145 +{
7146 +       enum d_walk_ret ret;
7147 +       struct ac_dpages_arg *arg = _arg;
7148 +
7149 +       ret = D_WALK_CONTINUE;
7150 +       if (dentry->d_sb == arg->sb
7151 +           && !IS_ROOT(dentry)
7152 +           && au_dcount(dentry) > 0
7153 +           && au_di(dentry)
7154 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7155 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7156 +               if (unlikely(arg->err))
7157 +                       ret = D_WALK_QUIT;
7158 +       }
7159 +
7160 +       return ret;
7161 +}
7162 +
7163 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7164 +                  au_dpages_test test, void *arg)
7165 +{
7166 +       struct ac_dpages_arg args = {
7167 +               .err    = 0,
7168 +               .dpages = dpages,
7169 +               .sb     = root->d_sb,
7170 +               .test   = test,
7171 +               .arg    = arg
7172 +       };
7173 +
7174 +       d_walk(root, &args, au_call_dpages_append);
7175 +
7176 +       return args.err;
7177 +}
7178 +
7179 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7180 +                      int do_include, au_dpages_test test, void *arg)
7181 +{
7182 +       int err;
7183 +
7184 +       err = 0;
7185 +       write_seqlock(&rename_lock);
7186 +       spin_lock(&dentry->d_lock);
7187 +       if (do_include
7188 +           && au_dcount(dentry) > 0
7189 +           && (!test || test(dentry, arg)))
7190 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7191 +       spin_unlock(&dentry->d_lock);
7192 +       if (unlikely(err))
7193 +               goto out;
7194 +
7195 +       /*
7196 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7197 +        * mount
7198 +        */
7199 +       while (!IS_ROOT(dentry)) {
7200 +               dentry = dentry->d_parent; /* rename_lock is locked */
7201 +               spin_lock(&dentry->d_lock);
7202 +               if (au_dcount(dentry) > 0
7203 +                   && (!test || test(dentry, arg)))
7204 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7205 +               spin_unlock(&dentry->d_lock);
7206 +               if (unlikely(err))
7207 +                       break;
7208 +       }
7209 +
7210 +out:
7211 +       write_sequnlock(&rename_lock);
7212 +       return err;
7213 +}
7214 +
7215 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7216 +{
7217 +       return au_di(dentry) && dentry->d_sb == arg;
7218 +}
7219 +
7220 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7221 +                           struct dentry *dentry, int do_include)
7222 +{
7223 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7224 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7225 +}
7226 +
7227 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7228 +{
7229 +       struct path path[2] = {
7230 +               {
7231 +                       .dentry = d1
7232 +               },
7233 +               {
7234 +                       .dentry = d2
7235 +               }
7236 +       };
7237 +
7238 +       return path_is_under(path + 0, path + 1);
7239 +}
7240 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7241 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7242 +++ linux/fs/aufs/dcsub.h       2019-07-11 15:42:14.462237786 +0200
7243 @@ -0,0 +1,137 @@
7244 +/* SPDX-License-Identifier: GPL-2.0 */
7245 +/*
7246 + * Copyright (C) 2005-2019 Junjiro R. Okajima
7247 + *
7248 + * This program, aufs is free software; you can redistribute it and/or modify
7249 + * it under the terms of the GNU General Public License as published by
7250 + * the Free Software Foundation; either version 2 of the License, or
7251 + * (at your option) any later version.
7252 + *
7253 + * This program is distributed in the hope that it will be useful,
7254 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7255 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7256 + * GNU General Public License for more details.
7257 + *
7258 + * You should have received a copy of the GNU General Public License
7259 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7260 + */
7261 +
7262 +/*
7263 + * sub-routines for dentry cache
7264 + */
7265 +
7266 +#ifndef __AUFS_DCSUB_H__
7267 +#define __AUFS_DCSUB_H__
7268 +
7269 +#ifdef __KERNEL__
7270 +
7271 +#include <linux/dcache.h>
7272 +#include <linux/fs.h>
7273 +
7274 +struct au_dpage {
7275 +       int ndentry;
7276 +       struct dentry **dentries;
7277 +};
7278 +
7279 +struct au_dcsub_pages {
7280 +       int ndpage;
7281 +       struct au_dpage *dpages;
7282 +};
7283 +
7284 +/* ---------------------------------------------------------------------- */
7285 +
7286 +/* dcsub.c */
7287 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7288 +void au_dpages_free(struct au_dcsub_pages *dpages);
7289 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7290 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7291 +                  au_dpages_test test, void *arg);
7292 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7293 +                      int do_include, au_dpages_test test, void *arg);
7294 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7295 +                           struct dentry *dentry, int do_include);
7296 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7297 +
7298 +/* ---------------------------------------------------------------------- */
7299 +
7300 +/*
7301 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7302 + * include/linux/dcache.h. Try them (in the future).
7303 + */
7304 +
7305 +static inline int au_d_hashed_positive(struct dentry *d)
7306 +{
7307 +       int err;
7308 +       struct inode *inode = d_inode(d);
7309 +
7310 +       err = 0;
7311 +       if (unlikely(d_unhashed(d)
7312 +                    || d_is_negative(d)
7313 +                    || !inode->i_nlink))
7314 +               err = -ENOENT;
7315 +       return err;
7316 +}
7317 +
7318 +static inline int au_d_linkable(struct dentry *d)
7319 +{
7320 +       int err;
7321 +       struct inode *inode = d_inode(d);
7322 +
7323 +       err = au_d_hashed_positive(d);
7324 +       if (err
7325 +           && d_is_positive(d)
7326 +           && (inode->i_state & I_LINKABLE))
7327 +               err = 0;
7328 +       return err;
7329 +}
7330 +
7331 +static inline int au_d_alive(struct dentry *d)
7332 +{
7333 +       int err;
7334 +       struct inode *inode;
7335 +
7336 +       err = 0;
7337 +       if (!IS_ROOT(d))
7338 +               err = au_d_hashed_positive(d);
7339 +       else {
7340 +               inode = d_inode(d);
7341 +               if (unlikely(d_unlinked(d)
7342 +                            || d_is_negative(d)
7343 +                            || !inode->i_nlink))
7344 +                       err = -ENOENT;
7345 +       }
7346 +       return err;
7347 +}
7348 +
7349 +static inline int au_alive_dir(struct dentry *d)
7350 +{
7351 +       int err;
7352 +
7353 +       err = au_d_alive(d);
7354 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7355 +               err = -ENOENT;
7356 +       return err;
7357 +}
7358 +
7359 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7360 +{
7361 +       return a->len == b->len
7362 +               && !memcmp(a->name, b->name, a->len);
7363 +}
7364 +
7365 +/*
7366 + * by the commit
7367 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7368 + *                     taking d_lock
7369 + * the type of d_lockref.count became int, but the inlined function d_count()
7370 + * still returns unsigned int.
7371 + * I don't know why. Maybe it is for every d_count() users?
7372 + * Anyway au_dcount() lives on.
7373 + */
7374 +static inline int au_dcount(struct dentry *d)
7375 +{
7376 +       return (int)d_count(d);
7377 +}
7378 +
7379 +#endif /* __KERNEL__ */
7380 +#endif /* __AUFS_DCSUB_H__ */
7381 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7382 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7383 +++ linux/fs/aufs/debug.c       2019-07-11 15:42:14.462237786 +0200
7384 @@ -0,0 +1,441 @@
7385 +// SPDX-License-Identifier: GPL-2.0
7386 +/*
7387 + * Copyright (C) 2005-2019 Junjiro R. Okajima
7388 + *
7389 + * This program, aufs is free software; you can redistribute it and/or modify
7390 + * it under the terms of the GNU General Public License as published by
7391 + * the Free Software Foundation; either version 2 of the License, or
7392 + * (at your option) any later version.
7393 + *
7394 + * This program is distributed in the hope that it will be useful,
7395 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7396 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7397 + * GNU General Public License for more details.
7398 + *
7399 + * You should have received a copy of the GNU General Public License
7400 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7401 + */
7402 +
7403 +/*
7404 + * debug print functions
7405 + */
7406 +
7407 +#include <linux/iversion.h>
7408 +#include "aufs.h"
7409 +
7410 +/* Returns 0, or -errno.  arg is in kp->arg. */
7411 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7412 +{
7413 +       int err, n;
7414 +
7415 +       err = kstrtoint(val, 0, &n);
7416 +       if (!err) {
7417 +               if (n > 0)
7418 +                       au_debug_on();
7419 +               else
7420 +                       au_debug_off();
7421 +       }
7422 +       return err;
7423 +}
7424 +
7425 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7426 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7427 +{
7428 +       atomic_t *a;
7429 +
7430 +       a = kp->arg;
7431 +       return sprintf(buffer, "%d", atomic_read(a));
7432 +}
7433 +
7434 +static struct kernel_param_ops param_ops_atomic_t = {
7435 +       .set = param_atomic_t_set,
7436 +       .get = param_atomic_t_get
7437 +       /* void (*free)(void *arg) */
7438 +};
7439 +
7440 +atomic_t aufs_debug = ATOMIC_INIT(0);
7441 +MODULE_PARM_DESC(debug, "debug print");
7442 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7443 +
7444 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7445 +char *au_plevel = KERN_DEBUG;
7446 +#define dpri(fmt, ...) do {                                    \
7447 +       if ((au_plevel                                          \
7448 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7449 +           || au_debug_test())                                 \
7450 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7451 +} while (0)
7452 +
7453 +/* ---------------------------------------------------------------------- */
7454 +
7455 +void au_dpri_whlist(struct au_nhash *whlist)
7456 +{
7457 +       unsigned long ul, n;
7458 +       struct hlist_head *head;
7459 +       struct au_vdir_wh *pos;
7460 +
7461 +       n = whlist->nh_num;
7462 +       head = whlist->nh_head;
7463 +       for (ul = 0; ul < n; ul++) {
7464 +               hlist_for_each_entry(pos, head, wh_hash)
7465 +                       dpri("b%d, %.*s, %d\n",
7466 +                            pos->wh_bindex,
7467 +                            pos->wh_str.len, pos->wh_str.name,
7468 +                            pos->wh_str.len);
7469 +               head++;
7470 +       }
7471 +}
7472 +
7473 +void au_dpri_vdir(struct au_vdir *vdir)
7474 +{
7475 +       unsigned long ul;
7476 +       union au_vdir_deblk_p p;
7477 +       unsigned char *o;
7478 +
7479 +       if (!vdir || IS_ERR(vdir)) {
7480 +               dpri("err %ld\n", PTR_ERR(vdir));
7481 +               return;
7482 +       }
7483 +
7484 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7485 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7486 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7487 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7488 +               p.deblk = vdir->vd_deblk[ul];
7489 +               o = p.deblk;
7490 +               dpri("[%lu]: %p\n", ul, o);
7491 +       }
7492 +}
7493 +
7494 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7495 +                       struct dentry *wh)
7496 +{
7497 +       char *n = NULL;
7498 +       int l = 0;
7499 +
7500 +       if (!inode || IS_ERR(inode)) {
7501 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7502 +               return -1;
7503 +       }
7504 +
7505 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7506 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7507 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7508 +       if (wh) {
7509 +               n = (void *)wh->d_name.name;
7510 +               l = wh->d_name.len;
7511 +       }
7512 +
7513 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7514 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7515 +            bindex, inode,
7516 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7517 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7518 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7519 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7520 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7521 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7522 +            inode->i_generation,
7523 +            l ? ", wh " : "", l, n);
7524 +       return 0;
7525 +}
7526 +
7527 +void au_dpri_inode(struct inode *inode)
7528 +{
7529 +       struct au_iinfo *iinfo;
7530 +       struct au_hinode *hi;
7531 +       aufs_bindex_t bindex;
7532 +       int err, hn;
7533 +
7534 +       err = do_pri_inode(-1, inode, -1, NULL);
7535 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7536 +               return;
7537 +
7538 +       iinfo = au_ii(inode);
7539 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7540 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7541 +       if (iinfo->ii_btop < 0)
7542 +               return;
7543 +       hn = 0;
7544 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7545 +               hi = au_hinode(iinfo, bindex);
7546 +               hn = !!au_hn(hi);
7547 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7548 +       }
7549 +}
7550 +
7551 +void au_dpri_dalias(struct inode *inode)
7552 +{
7553 +       struct dentry *d;
7554 +
7555 +       spin_lock(&inode->i_lock);
7556 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7557 +               au_dpri_dentry(d);
7558 +       spin_unlock(&inode->i_lock);
7559 +}
7560 +
7561 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7562 +{
7563 +       struct dentry *wh = NULL;
7564 +       int hn;
7565 +       struct inode *inode;
7566 +       struct au_iinfo *iinfo;
7567 +       struct au_hinode *hi;
7568 +
7569 +       if (!dentry || IS_ERR(dentry)) {
7570 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7571 +               return -1;
7572 +       }
7573 +       /* do not call dget_parent() here */
7574 +       /* note: access d_xxx without d_lock */
7575 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7576 +            bindex, dentry, dentry,
7577 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7578 +            au_dcount(dentry), dentry->d_flags,
7579 +            d_unhashed(dentry) ? "un" : "");
7580 +       hn = -1;
7581 +       inode = NULL;
7582 +       if (d_is_positive(dentry))
7583 +               inode = d_inode(dentry);
7584 +       if (inode
7585 +           && au_test_aufs(dentry->d_sb)
7586 +           && bindex >= 0
7587 +           && !au_is_bad_inode(inode)) {
7588 +               iinfo = au_ii(inode);
7589 +               hi = au_hinode(iinfo, bindex);
7590 +               hn = !!au_hn(hi);
7591 +               wh = hi->hi_whdentry;
7592 +       }
7593 +       do_pri_inode(bindex, inode, hn, wh);
7594 +       return 0;
7595 +}
7596 +
7597 +void au_dpri_dentry(struct dentry *dentry)
7598 +{
7599 +       struct au_dinfo *dinfo;
7600 +       aufs_bindex_t bindex;
7601 +       int err;
7602 +
7603 +       err = do_pri_dentry(-1, dentry);
7604 +       if (err || !au_test_aufs(dentry->d_sb))
7605 +               return;
7606 +
7607 +       dinfo = au_di(dentry);
7608 +       if (!dinfo)
7609 +               return;
7610 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7611 +            dinfo->di_btop, dinfo->di_bbot,
7612 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7613 +            dinfo->di_tmpfile);
7614 +       if (dinfo->di_btop < 0)
7615 +               return;
7616 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7617 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7618 +}
7619 +
7620 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7621 +{
7622 +       char a[32];
7623 +
7624 +       if (!file || IS_ERR(file)) {
7625 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7626 +               return -1;
7627 +       }
7628 +       a[0] = 0;
7629 +       if (bindex < 0
7630 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7631 +           && au_test_aufs(file->f_path.dentry->d_sb)
7632 +           && au_fi(file))
7633 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7634 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7635 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7636 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7637 +            file->f_version, file->f_pos, a);
7638 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7639 +               do_pri_dentry(bindex, file->f_path.dentry);
7640 +       return 0;
7641 +}
7642 +
7643 +void au_dpri_file(struct file *file)
7644 +{
7645 +       struct au_finfo *finfo;
7646 +       struct au_fidir *fidir;
7647 +       struct au_hfile *hfile;
7648 +       aufs_bindex_t bindex;
7649 +       int err;
7650 +
7651 +       err = do_pri_file(-1, file);
7652 +       if (err
7653 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7654 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7655 +               return;
7656 +
7657 +       finfo = au_fi(file);
7658 +       if (!finfo)
7659 +               return;
7660 +       if (finfo->fi_btop < 0)
7661 +               return;
7662 +       fidir = finfo->fi_hdir;
7663 +       if (!fidir)
7664 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7665 +       else
7666 +               for (bindex = finfo->fi_btop;
7667 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7668 +                    bindex++) {
7669 +                       hfile = fidir->fd_hfile + bindex;
7670 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7671 +               }
7672 +}
7673 +
7674 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7675 +{
7676 +       struct vfsmount *mnt;
7677 +       struct super_block *sb;
7678 +
7679 +       if (!br || IS_ERR(br))
7680 +               goto out;
7681 +       mnt = au_br_mnt(br);
7682 +       if (!mnt || IS_ERR(mnt))
7683 +               goto out;
7684 +       sb = mnt->mnt_sb;
7685 +       if (!sb || IS_ERR(sb))
7686 +               goto out;
7687 +
7688 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7689 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7690 +            "xino %d\n",
7691 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7692 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7693 +            sb->s_flags, sb->s_count,
7694 +            atomic_read(&sb->s_active),
7695 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7696 +       return 0;
7697 +
7698 +out:
7699 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7700 +       return -1;
7701 +}
7702 +
7703 +void au_dpri_sb(struct super_block *sb)
7704 +{
7705 +       struct au_sbinfo *sbinfo;
7706 +       aufs_bindex_t bindex;
7707 +       int err;
7708 +       /* to reduce stack size */
7709 +       struct {
7710 +               struct vfsmount mnt;
7711 +               struct au_branch fake;
7712 +       } *a;
7713 +
7714 +       /* this function can be called from magic sysrq */
7715 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7716 +       if (unlikely(!a)) {
7717 +               dpri("no memory\n");
7718 +               return;
7719 +       }
7720 +
7721 +       a->mnt.mnt_sb = sb;
7722 +       a->fake.br_path.mnt = &a->mnt;
7723 +       err = do_pri_br(-1, &a->fake);
7724 +       au_kfree_rcu(a);
7725 +       dpri("dev 0x%x\n", sb->s_dev);
7726 +       if (err || !au_test_aufs(sb))
7727 +               return;
7728 +
7729 +       sbinfo = au_sbi(sb);
7730 +       if (!sbinfo)
7731 +               return;
7732 +       dpri("nw %d, gen %u, kobj %d\n",
7733 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7734 +            kref_read(&sbinfo->si_kobj.kref));
7735 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7736 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7737 +}
7738 +
7739 +/* ---------------------------------------------------------------------- */
7740 +
7741 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7742 +{
7743 +       struct inode *h_inode, *inode = d_inode(dentry);
7744 +       struct dentry *h_dentry;
7745 +       aufs_bindex_t bindex, bbot, bi;
7746 +
7747 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7748 +               return;
7749 +
7750 +       bbot = au_dbbot(dentry);
7751 +       bi = au_ibbot(inode);
7752 +       if (bi < bbot)
7753 +               bbot = bi;
7754 +       bindex = au_dbtop(dentry);
7755 +       bi = au_ibtop(inode);
7756 +       if (bi > bindex)
7757 +               bindex = bi;
7758 +
7759 +       for (; bindex <= bbot; bindex++) {
7760 +               h_dentry = au_h_dptr(dentry, bindex);
7761 +               if (!h_dentry)
7762 +                       continue;
7763 +               h_inode = au_h_iptr(inode, bindex);
7764 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7765 +                       au_debug_on();
7766 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7767 +                       AuDbgDentry(dentry);
7768 +                       AuDbgInode(inode);
7769 +                       au_debug_off();
7770 +                       BUG();
7771 +               }
7772 +       }
7773 +}
7774 +
7775 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7776 +{
7777 +       int err, i, j;
7778 +       struct au_dcsub_pages dpages;
7779 +       struct au_dpage *dpage;
7780 +       struct dentry **dentries;
7781 +
7782 +       err = au_dpages_init(&dpages, GFP_NOFS);
7783 +       AuDebugOn(err);
7784 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7785 +       AuDebugOn(err);
7786 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7787 +               dpage = dpages.dpages + i;
7788 +               dentries = dpage->dentries;
7789 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7790 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7791 +       }
7792 +       au_dpages_free(&dpages);
7793 +}
7794 +
7795 +void au_dbg_verify_kthread(void)
7796 +{
7797 +       if (au_wkq_test()) {
7798 +               au_dbg_blocked();
7799 +               /*
7800 +                * It may be recursive, but udba=notify between two aufs mounts,
7801 +                * where a single ro branch is shared, is not a problem.
7802 +                */
7803 +               /* WARN_ON(1); */
7804 +       }
7805 +}
7806 +
7807 +/* ---------------------------------------------------------------------- */
7808 +
7809 +int __init au_debug_init(void)
7810 +{
7811 +       aufs_bindex_t bindex;
7812 +       struct au_vdir_destr destr;
7813 +
7814 +       bindex = -1;
7815 +       AuDebugOn(bindex >= 0);
7816 +
7817 +       destr.len = -1;
7818 +       AuDebugOn(destr.len < NAME_MAX);
7819 +
7820 +#ifdef CONFIG_4KSTACKS
7821 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7822 +#endif
7823 +
7824 +       return 0;
7825 +}
7826 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7827 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7828 +++ linux/fs/aufs/debug.h       2019-07-11 15:42:14.462237786 +0200
7829 @@ -0,0 +1,226 @@
7830 +/* SPDX-License-Identifier: GPL-2.0 */
7831 +/*
7832 + * Copyright (C) 2005-2019 Junjiro R. Okajima
7833 + *
7834 + * This program, aufs is free software; you can redistribute it and/or modify
7835 + * it under the terms of the GNU General Public License as published by
7836 + * the Free Software Foundation; either version 2 of the License, or
7837 + * (at your option) any later version.
7838 + *
7839 + * This program is distributed in the hope that it will be useful,
7840 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7841 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7842 + * GNU General Public License for more details.
7843 + *
7844 + * You should have received a copy of the GNU General Public License
7845 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7846 + */
7847 +
7848 +/*
7849 + * debug print functions
7850 + */
7851 +
7852 +#ifndef __AUFS_DEBUG_H__
7853 +#define __AUFS_DEBUG_H__
7854 +
7855 +#ifdef __KERNEL__
7856 +
7857 +#include <linux/atomic.h>
7858 +#include <linux/module.h>
7859 +#include <linux/kallsyms.h>
7860 +#include <linux/sysrq.h>
7861 +
7862 +#ifdef CONFIG_AUFS_DEBUG
7863 +#define AuDebugOn(a)           BUG_ON(a)
7864 +
7865 +/* module parameter */
7866 +extern atomic_t aufs_debug;
7867 +static inline void au_debug_on(void)
7868 +{
7869 +       atomic_inc(&aufs_debug);
7870 +}
7871 +static inline void au_debug_off(void)
7872 +{
7873 +       atomic_dec_if_positive(&aufs_debug);
7874 +}
7875 +
7876 +static inline int au_debug_test(void)
7877 +{
7878 +       return atomic_read(&aufs_debug) > 0;
7879 +}
7880 +#else
7881 +#define AuDebugOn(a)           do {} while (0)
7882 +AuStubVoid(au_debug_on, void)
7883 +AuStubVoid(au_debug_off, void)
7884 +AuStubInt0(au_debug_test, void)
7885 +#endif /* CONFIG_AUFS_DEBUG */
7886 +
7887 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7888 +
7889 +/* ---------------------------------------------------------------------- */
7890 +
7891 +/* debug print */
7892 +
7893 +#define AuDbg(fmt, ...) do { \
7894 +       if (au_debug_test()) \
7895 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7896 +} while (0)
7897 +#define AuLabel(l)             AuDbg(#l "\n")
7898 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7899 +#define AuWarn1(fmt, ...) do { \
7900 +       static unsigned char _c; \
7901 +       if (!_c++) \
7902 +               pr_warn(fmt, ##__VA_ARGS__); \
7903 +} while (0)
7904 +
7905 +#define AuErr1(fmt, ...) do { \
7906 +       static unsigned char _c; \
7907 +       if (!_c++) \
7908 +               pr_err(fmt, ##__VA_ARGS__); \
7909 +} while (0)
7910 +
7911 +#define AuIOErr1(fmt, ...) do { \
7912 +       static unsigned char _c; \
7913 +       if (!_c++) \
7914 +               AuIOErr(fmt, ##__VA_ARGS__); \
7915 +} while (0)
7916 +
7917 +#define AuUnsupportMsg "This operation is not supported." \
7918 +                       " Please report this application to aufs-users ML."
7919 +#define AuUnsupport(fmt, ...) do { \
7920 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7921 +       dump_stack(); \
7922 +} while (0)
7923 +
7924 +#define AuTraceErr(e) do { \
7925 +       if (unlikely((e) < 0)) \
7926 +               AuDbg("err %d\n", (int)(e)); \
7927 +} while (0)
7928 +
7929 +#define AuTraceErrPtr(p) do { \
7930 +       if (IS_ERR(p)) \
7931 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7932 +} while (0)
7933 +
7934 +/* dirty macros for debug print, use with "%.*s" and caution */
7935 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7936 +
7937 +/* ---------------------------------------------------------------------- */
7938 +
7939 +struct dentry;
7940 +#ifdef CONFIG_AUFS_DEBUG
7941 +extern struct mutex au_dbg_mtx;
7942 +extern char *au_plevel;
7943 +struct au_nhash;
7944 +void au_dpri_whlist(struct au_nhash *whlist);
7945 +struct au_vdir;
7946 +void au_dpri_vdir(struct au_vdir *vdir);
7947 +struct inode;
7948 +void au_dpri_inode(struct inode *inode);
7949 +void au_dpri_dalias(struct inode *inode);
7950 +void au_dpri_dentry(struct dentry *dentry);
7951 +struct file;
7952 +void au_dpri_file(struct file *filp);
7953 +struct super_block;
7954 +void au_dpri_sb(struct super_block *sb);
7955 +
7956 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7957 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7958 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7959 +void au_dbg_verify_kthread(void);
7960 +
7961 +int __init au_debug_init(void);
7962 +
7963 +#define AuDbgWhlist(w) do { \
7964 +       mutex_lock(&au_dbg_mtx); \
7965 +       AuDbg(#w "\n"); \
7966 +       au_dpri_whlist(w); \
7967 +       mutex_unlock(&au_dbg_mtx); \
7968 +} while (0)
7969 +
7970 +#define AuDbgVdir(v) do { \
7971 +       mutex_lock(&au_dbg_mtx); \
7972 +       AuDbg(#v "\n"); \
7973 +       au_dpri_vdir(v); \
7974 +       mutex_unlock(&au_dbg_mtx); \
7975 +} while (0)
7976 +
7977 +#define AuDbgInode(i) do { \
7978 +       mutex_lock(&au_dbg_mtx); \
7979 +       AuDbg(#i "\n"); \
7980 +       au_dpri_inode(i); \
7981 +       mutex_unlock(&au_dbg_mtx); \
7982 +} while (0)
7983 +
7984 +#define AuDbgDAlias(i) do { \
7985 +       mutex_lock(&au_dbg_mtx); \
7986 +       AuDbg(#i "\n"); \
7987 +       au_dpri_dalias(i); \
7988 +       mutex_unlock(&au_dbg_mtx); \
7989 +} while (0)
7990 +
7991 +#define AuDbgDentry(d) do { \
7992 +       mutex_lock(&au_dbg_mtx); \
7993 +       AuDbg(#d "\n"); \
7994 +       au_dpri_dentry(d); \
7995 +       mutex_unlock(&au_dbg_mtx); \
7996 +} while (0)
7997 +
7998 +#define AuDbgFile(f) do { \
7999 +       mutex_lock(&au_dbg_mtx); \
8000 +       AuDbg(#f "\n"); \
8001 +       au_dpri_file(f); \
8002 +       mutex_unlock(&au_dbg_mtx); \
8003 +} while (0)
8004 +
8005 +#define AuDbgSb(sb) do { \
8006 +       mutex_lock(&au_dbg_mtx); \
8007 +       AuDbg(#sb "\n"); \
8008 +       au_dpri_sb(sb); \
8009 +       mutex_unlock(&au_dbg_mtx); \
8010 +} while (0)
8011 +
8012 +#define AuDbgSym(addr) do {                            \
8013 +       char sym[KSYM_SYMBOL_LEN];                      \
8014 +       sprint_symbol(sym, (unsigned long)addr);        \
8015 +       AuDbg("%s\n", sym);                             \
8016 +} while (0)
8017 +#else
8018 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
8019 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8020 +AuStubVoid(au_dbg_verify_kthread, void)
8021 +AuStubInt0(__init au_debug_init, void)
8022 +
8023 +#define AuDbgWhlist(w)         do {} while (0)
8024 +#define AuDbgVdir(v)           do {} while (0)
8025 +#define AuDbgInode(i)          do {} while (0)
8026 +#define AuDbgDAlias(i)         do {} while (0)
8027 +#define AuDbgDentry(d)         do {} while (0)
8028 +#define AuDbgFile(f)           do {} while (0)
8029 +#define AuDbgSb(sb)            do {} while (0)
8030 +#define AuDbgSym(addr)         do {} while (0)
8031 +#endif /* CONFIG_AUFS_DEBUG */
8032 +
8033 +/* ---------------------------------------------------------------------- */
8034 +
8035 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8036 +int __init au_sysrq_init(void);
8037 +void au_sysrq_fin(void);
8038 +
8039 +#ifdef CONFIG_HW_CONSOLE
8040 +#define au_dbg_blocked() do { \
8041 +       WARN_ON(1); \
8042 +       handle_sysrq('w'); \
8043 +} while (0)
8044 +#else
8045 +AuStubVoid(au_dbg_blocked, void)
8046 +#endif
8047 +
8048 +#else
8049 +AuStubInt0(__init au_sysrq_init, void)
8050 +AuStubVoid(au_sysrq_fin, void)
8051 +AuStubVoid(au_dbg_blocked, void)
8052 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8053 +
8054 +#endif /* __KERNEL__ */
8055 +#endif /* __AUFS_DEBUG_H__ */
8056 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8057 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
8058 +++ linux/fs/aufs/dentry.c      2019-07-11 15:42:14.462237786 +0200
8059 @@ -0,0 +1,1154 @@
8060 +// SPDX-License-Identifier: GPL-2.0
8061 +/*
8062 + * Copyright (C) 2005-2019 Junjiro R. Okajima
8063 + *
8064 + * This program, aufs is free software; you can redistribute it and/or modify
8065 + * it under the terms of the GNU General Public License as published by
8066 + * the Free Software Foundation; either version 2 of the License, or
8067 + * (at your option) any later version.
8068 + *
8069 + * This program is distributed in the hope that it will be useful,
8070 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8071 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8072 + * GNU General Public License for more details.
8073 + *
8074 + * You should have received a copy of the GNU General Public License
8075 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8076 + */
8077 +
8078 +/*
8079 + * lookup and dentry operations
8080 + */
8081 +
8082 +#include <linux/iversion.h>
8083 +#include <linux/namei.h>
8084 +#include "aufs.h"
8085 +
8086 +/*
8087 + * returns positive/negative dentry, NULL or an error.
8088 + * NULL means whiteout-ed or not-found.
8089 + */
8090 +static struct dentry*
8091 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8092 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8093 +{
8094 +       struct dentry *h_dentry;
8095 +       struct inode *h_inode;
8096 +       struct au_branch *br;
8097 +       int wh_found, opq;
8098 +       unsigned char wh_able;
8099 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8100 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8101 +                                                         IGNORE_PERM);
8102 +
8103 +       wh_found = 0;
8104 +       br = au_sbr(dentry->d_sb, bindex);
8105 +       wh_able = !!au_br_whable(br->br_perm);
8106 +       if (wh_able)
8107 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8108 +       h_dentry = ERR_PTR(wh_found);
8109 +       if (!wh_found)
8110 +               goto real_lookup;
8111 +       if (unlikely(wh_found < 0))
8112 +               goto out;
8113 +
8114 +       /* We found a whiteout */
8115 +       /* au_set_dbbot(dentry, bindex); */
8116 +       au_set_dbwh(dentry, bindex);
8117 +       if (!allow_neg)
8118 +               return NULL; /* success */
8119 +
8120 +real_lookup:
8121 +       if (!ignore_perm)
8122 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8123 +       else
8124 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8125 +       if (IS_ERR(h_dentry)) {
8126 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8127 +                   && !allow_neg)
8128 +                       h_dentry = NULL;
8129 +               goto out;
8130 +       }
8131 +
8132 +       h_inode = d_inode(h_dentry);
8133 +       if (d_is_negative(h_dentry)) {
8134 +               if (!allow_neg)
8135 +                       goto out_neg;
8136 +       } else if (wh_found
8137 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8138 +               goto out_neg;
8139 +       else if (au_ftest_lkup(args->flags, DIRREN)
8140 +                /* && h_inode */
8141 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8142 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8143 +                     (unsigned long long)h_inode->i_ino);
8144 +               goto out_neg;
8145 +       }
8146 +
8147 +       if (au_dbbot(dentry) <= bindex)
8148 +               au_set_dbbot(dentry, bindex);
8149 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8150 +               au_set_dbtop(dentry, bindex);
8151 +       au_set_h_dptr(dentry, bindex, h_dentry);
8152 +
8153 +       if (!d_is_dir(h_dentry)
8154 +           || !wh_able
8155 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8156 +               goto out; /* success */
8157 +
8158 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8159 +       opq = au_diropq_test(h_dentry);
8160 +       inode_unlock_shared(h_inode);
8161 +       if (opq > 0)
8162 +               au_set_dbdiropq(dentry, bindex);
8163 +       else if (unlikely(opq < 0)) {
8164 +               au_set_h_dptr(dentry, bindex, NULL);
8165 +               h_dentry = ERR_PTR(opq);
8166 +       }
8167 +       goto out;
8168 +
8169 +out_neg:
8170 +       dput(h_dentry);
8171 +       h_dentry = NULL;
8172 +out:
8173 +       return h_dentry;
8174 +}
8175 +
8176 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8177 +{
8178 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8179 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8180 +               return -EPERM;
8181 +       return 0;
8182 +}
8183 +
8184 +/*
8185 + * returns the number of lower positive dentries,
8186 + * otherwise an error.
8187 + * can be called at unlinking with @type is zero.
8188 + */
8189 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8190 +                  unsigned int flags)
8191 +{
8192 +       int npositive, err;
8193 +       aufs_bindex_t bindex, btail, bdiropq;
8194 +       unsigned char isdir, dirperm1, dirren;
8195 +       struct au_do_lookup_args args = {
8196 +               .flags          = flags,
8197 +               .name           = &dentry->d_name
8198 +       };
8199 +       struct dentry *parent;
8200 +       struct super_block *sb;
8201 +
8202 +       sb = dentry->d_sb;
8203 +       err = au_test_shwh(sb, args.name);
8204 +       if (unlikely(err))
8205 +               goto out;
8206 +
8207 +       err = au_wh_name_alloc(&args.whname, args.name);
8208 +       if (unlikely(err))
8209 +               goto out;
8210 +
8211 +       isdir = !!d_is_dir(dentry);
8212 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8213 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8214 +       if (dirren)
8215 +               au_fset_lkup(args.flags, DIRREN);
8216 +
8217 +       npositive = 0;
8218 +       parent = dget_parent(dentry);
8219 +       btail = au_dbtaildir(parent);
8220 +       for (bindex = btop; bindex <= btail; bindex++) {
8221 +               struct dentry *h_parent, *h_dentry;
8222 +               struct inode *h_inode, *h_dir;
8223 +               struct au_branch *br;
8224 +
8225 +               h_dentry = au_h_dptr(dentry, bindex);
8226 +               if (h_dentry) {
8227 +                       if (d_is_positive(h_dentry))
8228 +                               npositive++;
8229 +                       break;
8230 +               }
8231 +               h_parent = au_h_dptr(parent, bindex);
8232 +               if (!h_parent || !d_is_dir(h_parent))
8233 +                       continue;
8234 +
8235 +               if (dirren) {
8236 +                       /* if the inum matches, then use the prepared name */
8237 +                       err = au_dr_lkup_name(&args, bindex);
8238 +                       if (unlikely(err))
8239 +                               goto out_parent;
8240 +               }
8241 +
8242 +               h_dir = d_inode(h_parent);
8243 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8244 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8245 +               inode_unlock_shared(h_dir);
8246 +               err = PTR_ERR(h_dentry);
8247 +               if (IS_ERR(h_dentry))
8248 +                       goto out_parent;
8249 +               if (h_dentry)
8250 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8251 +               if (dirperm1)
8252 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8253 +
8254 +               if (au_dbwh(dentry) == bindex)
8255 +                       break;
8256 +               if (!h_dentry)
8257 +                       continue;
8258 +               if (d_is_negative(h_dentry))
8259 +                       continue;
8260 +               h_inode = d_inode(h_dentry);
8261 +               npositive++;
8262 +               if (!args.type)
8263 +                       args.type = h_inode->i_mode & S_IFMT;
8264 +               if (args.type != S_IFDIR)
8265 +                       break;
8266 +               else if (isdir) {
8267 +                       /* the type of lower may be different */
8268 +                       bdiropq = au_dbdiropq(dentry);
8269 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8270 +                               break;
8271 +               }
8272 +               br = au_sbr(sb, bindex);
8273 +               if (dirren
8274 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8275 +                                          /*add_ent*/NULL)) {
8276 +                       /* prepare next name to lookup */
8277 +                       err = au_dr_lkup(&args, dentry, bindex);
8278 +                       if (unlikely(err))
8279 +                               goto out_parent;
8280 +               }
8281 +       }
8282 +
8283 +       if (npositive) {
8284 +               AuLabel(positive);
8285 +               au_update_dbtop(dentry);
8286 +       }
8287 +       err = npositive;
8288 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8289 +                    && au_dbtop(dentry) < 0)) {
8290 +               err = -EIO;
8291 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8292 +                       dentry, err);
8293 +       }
8294 +
8295 +out_parent:
8296 +       dput(parent);
8297 +       au_kfree_try_rcu(args.whname.name);
8298 +       if (dirren)
8299 +               au_dr_lkup_fin(&args);
8300 +out:
8301 +       return err;
8302 +}
8303 +
8304 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8305 +{
8306 +       struct dentry *dentry;
8307 +       int wkq_err;
8308 +
8309 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8310 +               dentry = vfsub_lkup_one(name, parent);
8311 +       else {
8312 +               struct vfsub_lkup_one_args args = {
8313 +                       .errp   = &dentry,
8314 +                       .name   = name,
8315 +                       .parent = parent
8316 +               };
8317 +
8318 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8319 +               if (unlikely(wkq_err))
8320 +                       dentry = ERR_PTR(wkq_err);
8321 +       }
8322 +
8323 +       return dentry;
8324 +}
8325 +
8326 +/*
8327 + * lookup @dentry on @bindex which should be negative.
8328 + */
8329 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8330 +{
8331 +       int err;
8332 +       struct dentry *parent, *h_parent, *h_dentry;
8333 +       struct au_branch *br;
8334 +
8335 +       parent = dget_parent(dentry);
8336 +       h_parent = au_h_dptr(parent, bindex);
8337 +       br = au_sbr(dentry->d_sb, bindex);
8338 +       if (wh)
8339 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8340 +       else
8341 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8342 +       err = PTR_ERR(h_dentry);
8343 +       if (IS_ERR(h_dentry))
8344 +               goto out;
8345 +       if (unlikely(d_is_positive(h_dentry))) {
8346 +               err = -EIO;
8347 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8348 +               dput(h_dentry);
8349 +               goto out;
8350 +       }
8351 +
8352 +       err = 0;
8353 +       if (bindex < au_dbtop(dentry))
8354 +               au_set_dbtop(dentry, bindex);
8355 +       if (au_dbbot(dentry) < bindex)
8356 +               au_set_dbbot(dentry, bindex);
8357 +       au_set_h_dptr(dentry, bindex, h_dentry);
8358 +
8359 +out:
8360 +       dput(parent);
8361 +       return err;
8362 +}
8363 +
8364 +/* ---------------------------------------------------------------------- */
8365 +
8366 +/* subset of struct inode */
8367 +struct au_iattr {
8368 +       unsigned long           i_ino;
8369 +       /* unsigned int         i_nlink; */
8370 +       kuid_t                  i_uid;
8371 +       kgid_t                  i_gid;
8372 +       u64                     i_version;
8373 +/*
8374 +       loff_t                  i_size;
8375 +       blkcnt_t                i_blocks;
8376 +*/
8377 +       umode_t                 i_mode;
8378 +};
8379 +
8380 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8381 +{
8382 +       ia->i_ino = h_inode->i_ino;
8383 +       /* ia->i_nlink = h_inode->i_nlink; */
8384 +       ia->i_uid = h_inode->i_uid;
8385 +       ia->i_gid = h_inode->i_gid;
8386 +       ia->i_version = inode_query_iversion(h_inode);
8387 +/*
8388 +       ia->i_size = h_inode->i_size;
8389 +       ia->i_blocks = h_inode->i_blocks;
8390 +*/
8391 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8392 +}
8393 +
8394 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8395 +{
8396 +       return ia->i_ino != h_inode->i_ino
8397 +               /* || ia->i_nlink != h_inode->i_nlink */
8398 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8399 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8400 +               || !inode_eq_iversion(h_inode, ia->i_version)
8401 +/*
8402 +               || ia->i_size != h_inode->i_size
8403 +               || ia->i_blocks != h_inode->i_blocks
8404 +*/
8405 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8406 +}
8407 +
8408 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8409 +                             struct au_branch *br)
8410 +{
8411 +       int err;
8412 +       struct au_iattr ia;
8413 +       struct inode *h_inode;
8414 +       struct dentry *h_d;
8415 +       struct super_block *h_sb;
8416 +
8417 +       err = 0;
8418 +       memset(&ia, -1, sizeof(ia));
8419 +       h_sb = h_dentry->d_sb;
8420 +       h_inode = NULL;
8421 +       if (d_is_positive(h_dentry)) {
8422 +               h_inode = d_inode(h_dentry);
8423 +               au_iattr_save(&ia, h_inode);
8424 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8425 +               /* nfs d_revalidate may return 0 for negative dentry */
8426 +               /* fuse d_revalidate always return 0 for negative dentry */
8427 +               goto out;
8428 +
8429 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8430 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8431 +       err = PTR_ERR(h_d);
8432 +       if (IS_ERR(h_d))
8433 +               goto out;
8434 +
8435 +       err = 0;
8436 +       if (unlikely(h_d != h_dentry
8437 +                    || d_inode(h_d) != h_inode
8438 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8439 +               err = au_busy_or_stale();
8440 +       dput(h_d);
8441 +
8442 +out:
8443 +       AuTraceErr(err);
8444 +       return err;
8445 +}
8446 +
8447 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8448 +               struct dentry *h_parent, struct au_branch *br)
8449 +{
8450 +       int err;
8451 +
8452 +       err = 0;
8453 +       if (udba == AuOpt_UDBA_REVAL
8454 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8455 +               IMustLock(h_dir);
8456 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8457 +       } else if (udba != AuOpt_UDBA_NONE)
8458 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8459 +
8460 +       return err;
8461 +}
8462 +
8463 +/* ---------------------------------------------------------------------- */
8464 +
8465 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8466 +{
8467 +       int err;
8468 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8469 +       struct au_hdentry tmp, *p, *q;
8470 +       struct au_dinfo *dinfo;
8471 +       struct super_block *sb;
8472 +
8473 +       DiMustWriteLock(dentry);
8474 +
8475 +       sb = dentry->d_sb;
8476 +       dinfo = au_di(dentry);
8477 +       bbot = dinfo->di_bbot;
8478 +       bwh = dinfo->di_bwh;
8479 +       bdiropq = dinfo->di_bdiropq;
8480 +       bindex = dinfo->di_btop;
8481 +       p = au_hdentry(dinfo, bindex);
8482 +       for (; bindex <= bbot; bindex++, p++) {
8483 +               if (!p->hd_dentry)
8484 +                       continue;
8485 +
8486 +               new_bindex = au_br_index(sb, p->hd_id);
8487 +               if (new_bindex == bindex)
8488 +                       continue;
8489 +
8490 +               if (dinfo->di_bwh == bindex)
8491 +                       bwh = new_bindex;
8492 +               if (dinfo->di_bdiropq == bindex)
8493 +                       bdiropq = new_bindex;
8494 +               if (new_bindex < 0) {
8495 +                       au_hdput(p);
8496 +                       p->hd_dentry = NULL;
8497 +                       continue;
8498 +               }
8499 +
8500 +               /* swap two lower dentries, and loop again */
8501 +               q = au_hdentry(dinfo, new_bindex);
8502 +               tmp = *q;
8503 +               *q = *p;
8504 +               *p = tmp;
8505 +               if (tmp.hd_dentry) {
8506 +                       bindex--;
8507 +                       p--;
8508 +               }
8509 +       }
8510 +
8511 +       dinfo->di_bwh = -1;
8512 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8513 +               dinfo->di_bwh = bwh;
8514 +
8515 +       dinfo->di_bdiropq = -1;
8516 +       if (bdiropq >= 0
8517 +           && bdiropq <= au_sbbot(sb)
8518 +           && au_sbr_whable(sb, bdiropq))
8519 +               dinfo->di_bdiropq = bdiropq;
8520 +
8521 +       err = -EIO;
8522 +       dinfo->di_btop = -1;
8523 +       dinfo->di_bbot = -1;
8524 +       bbot = au_dbbot(parent);
8525 +       bindex = 0;
8526 +       p = au_hdentry(dinfo, bindex);
8527 +       for (; bindex <= bbot; bindex++, p++)
8528 +               if (p->hd_dentry) {
8529 +                       dinfo->di_btop = bindex;
8530 +                       break;
8531 +               }
8532 +
8533 +       if (dinfo->di_btop >= 0) {
8534 +               bindex = bbot;
8535 +               p = au_hdentry(dinfo, bindex);
8536 +               for (; bindex >= 0; bindex--, p--)
8537 +                       if (p->hd_dentry) {
8538 +                               dinfo->di_bbot = bindex;
8539 +                               err = 0;
8540 +                               break;
8541 +                       }
8542 +       }
8543 +
8544 +       return err;
8545 +}
8546 +
8547 +static void au_do_hide(struct dentry *dentry)
8548 +{
8549 +       struct inode *inode;
8550 +
8551 +       if (d_really_is_positive(dentry)) {
8552 +               inode = d_inode(dentry);
8553 +               if (!d_is_dir(dentry)) {
8554 +                       if (inode->i_nlink && !d_unhashed(dentry))
8555 +                               drop_nlink(inode);
8556 +               } else {
8557 +                       clear_nlink(inode);
8558 +                       /* stop next lookup */
8559 +                       inode->i_flags |= S_DEAD;
8560 +               }
8561 +               smp_mb(); /* necessary? */
8562 +       }
8563 +       d_drop(dentry);
8564 +}
8565 +
8566 +static int au_hide_children(struct dentry *parent)
8567 +{
8568 +       int err, i, j, ndentry;
8569 +       struct au_dcsub_pages dpages;
8570 +       struct au_dpage *dpage;
8571 +       struct dentry *dentry;
8572 +
8573 +       err = au_dpages_init(&dpages, GFP_NOFS);
8574 +       if (unlikely(err))
8575 +               goto out;
8576 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8577 +       if (unlikely(err))
8578 +               goto out_dpages;
8579 +
8580 +       /* in reverse order */
8581 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8582 +               dpage = dpages.dpages + i;
8583 +               ndentry = dpage->ndentry;
8584 +               for (j = ndentry - 1; j >= 0; j--) {
8585 +                       dentry = dpage->dentries[j];
8586 +                       if (dentry != parent)
8587 +                               au_do_hide(dentry);
8588 +               }
8589 +       }
8590 +
8591 +out_dpages:
8592 +       au_dpages_free(&dpages);
8593 +out:
8594 +       return err;
8595 +}
8596 +
8597 +static void au_hide(struct dentry *dentry)
8598 +{
8599 +       int err;
8600 +
8601 +       AuDbgDentry(dentry);
8602 +       if (d_is_dir(dentry)) {
8603 +               /* shrink_dcache_parent(dentry); */
8604 +               err = au_hide_children(dentry);
8605 +               if (unlikely(err))
8606 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8607 +                               dentry, err);
8608 +       }
8609 +       au_do_hide(dentry);
8610 +}
8611 +
8612 +/*
8613 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8614 + *
8615 + * a dirty branch is added
8616 + * - on the top of layers
8617 + * - in the middle of layers
8618 + * - to the bottom of layers
8619 + *
8620 + * on the added branch there exists
8621 + * - a whiteout
8622 + * - a diropq
8623 + * - a same named entry
8624 + *   + exist
8625 + *     * negative --> positive
8626 + *     * positive --> positive
8627 + *      - type is unchanged
8628 + *      - type is changed
8629 + *   + doesn't exist
8630 + *     * negative --> negative
8631 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8632 + * - none
8633 + */
8634 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8635 +                              struct au_dinfo *tmp)
8636 +{
8637 +       int err;
8638 +       aufs_bindex_t bindex, bbot;
8639 +       struct {
8640 +               struct dentry *dentry;
8641 +               struct inode *inode;
8642 +               mode_t mode;
8643 +       } orig_h, tmp_h = {
8644 +               .dentry = NULL
8645 +       };
8646 +       struct au_hdentry *hd;
8647 +       struct inode *inode, *h_inode;
8648 +       struct dentry *h_dentry;
8649 +
8650 +       err = 0;
8651 +       AuDebugOn(dinfo->di_btop < 0);
8652 +       orig_h.mode = 0;
8653 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8654 +       orig_h.inode = NULL;
8655 +       if (d_is_positive(orig_h.dentry)) {
8656 +               orig_h.inode = d_inode(orig_h.dentry);
8657 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8658 +       }
8659 +       if (tmp->di_btop >= 0) {
8660 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8661 +               if (d_is_positive(tmp_h.dentry)) {
8662 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8663 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8664 +               }
8665 +       }
8666 +
8667 +       inode = NULL;
8668 +       if (d_really_is_positive(dentry))
8669 +               inode = d_inode(dentry);
8670 +       if (!orig_h.inode) {
8671 +               AuDbg("negative originally\n");
8672 +               if (inode) {
8673 +                       au_hide(dentry);
8674 +                       goto out;
8675 +               }
8676 +               AuDebugOn(inode);
8677 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8678 +               AuDebugOn(dinfo->di_bdiropq != -1);
8679 +
8680 +               if (!tmp_h.inode) {
8681 +                       AuDbg("negative --> negative\n");
8682 +                       /* should have only one negative lower */
8683 +                       if (tmp->di_btop >= 0
8684 +                           && tmp->di_btop < dinfo->di_btop) {
8685 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8686 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8687 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8688 +                               au_di_cp(dinfo, tmp);
8689 +                               hd = au_hdentry(tmp, tmp->di_btop);
8690 +                               au_set_h_dptr(dentry, tmp->di_btop,
8691 +                                             dget(hd->hd_dentry));
8692 +                       }
8693 +                       au_dbg_verify_dinode(dentry);
8694 +               } else {
8695 +                       AuDbg("negative --> positive\n");
8696 +                       /*
8697 +                        * similar to the behaviour of creating with bypassing
8698 +                        * aufs.
8699 +                        * unhash it in order to force an error in the
8700 +                        * succeeding create operation.
8701 +                        * we should not set S_DEAD here.
8702 +                        */
8703 +                       d_drop(dentry);
8704 +                       /* au_di_swap(tmp, dinfo); */
8705 +                       au_dbg_verify_dinode(dentry);
8706 +               }
8707 +       } else {
8708 +               AuDbg("positive originally\n");
8709 +               /* inode may be NULL */
8710 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8711 +               if (!tmp_h.inode) {
8712 +                       AuDbg("positive --> negative\n");
8713 +                       /* or bypassing aufs */
8714 +                       au_hide(dentry);
8715 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8716 +                               dinfo->di_bwh = tmp->di_bwh;
8717 +                       if (inode)
8718 +                               err = au_refresh_hinode_self(inode);
8719 +                       au_dbg_verify_dinode(dentry);
8720 +               } else if (orig_h.mode == tmp_h.mode) {
8721 +                       AuDbg("positive --> positive, same type\n");
8722 +                       if (!S_ISDIR(orig_h.mode)
8723 +                           && dinfo->di_btop > tmp->di_btop) {
8724 +                               /*
8725 +                                * similar to the behaviour of removing and
8726 +                                * creating.
8727 +                                */
8728 +                               au_hide(dentry);
8729 +                               if (inode)
8730 +                                       err = au_refresh_hinode_self(inode);
8731 +                               au_dbg_verify_dinode(dentry);
8732 +                       } else {
8733 +                               /* fill empty slots */
8734 +                               if (dinfo->di_btop > tmp->di_btop)
8735 +                                       dinfo->di_btop = tmp->di_btop;
8736 +                               if (dinfo->di_bbot < tmp->di_bbot)
8737 +                                       dinfo->di_bbot = tmp->di_bbot;
8738 +                               dinfo->di_bwh = tmp->di_bwh;
8739 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8740 +                               bbot = dinfo->di_bbot;
8741 +                               bindex = tmp->di_btop;
8742 +                               hd = au_hdentry(tmp, bindex);
8743 +                               for (; bindex <= bbot; bindex++, hd++) {
8744 +                                       if (au_h_dptr(dentry, bindex))
8745 +                                               continue;
8746 +                                       h_dentry = hd->hd_dentry;
8747 +                                       if (!h_dentry)
8748 +                                               continue;
8749 +                                       AuDebugOn(d_is_negative(h_dentry));
8750 +                                       h_inode = d_inode(h_dentry);
8751 +                                       AuDebugOn(orig_h.mode
8752 +                                                 != (h_inode->i_mode
8753 +                                                     & S_IFMT));
8754 +                                       au_set_h_dptr(dentry, bindex,
8755 +                                                     dget(h_dentry));
8756 +                               }
8757 +                               if (inode)
8758 +                                       err = au_refresh_hinode(inode, dentry);
8759 +                               au_dbg_verify_dinode(dentry);
8760 +                       }
8761 +               } else {
8762 +                       AuDbg("positive --> positive, different type\n");
8763 +                       /* similar to the behaviour of removing and creating */
8764 +                       au_hide(dentry);
8765 +                       if (inode)
8766 +                               err = au_refresh_hinode_self(inode);
8767 +                       au_dbg_verify_dinode(dentry);
8768 +               }
8769 +       }
8770 +
8771 +out:
8772 +       return err;
8773 +}
8774 +
8775 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8776 +{
8777 +       const struct dentry_operations *dop
8778 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8779 +       static const unsigned int mask
8780 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8781 +
8782 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8783 +
8784 +       if (dentry->d_op == dop)
8785 +               return;
8786 +
8787 +       AuDbg("%pd\n", dentry);
8788 +       spin_lock(&dentry->d_lock);
8789 +       if (dop == &aufs_dop)
8790 +               dentry->d_flags |= mask;
8791 +       else
8792 +               dentry->d_flags &= ~mask;
8793 +       dentry->d_op = dop;
8794 +       spin_unlock(&dentry->d_lock);
8795 +}
8796 +
8797 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8798 +{
8799 +       int err, ebrange, nbr;
8800 +       unsigned int sigen;
8801 +       struct au_dinfo *dinfo, *tmp;
8802 +       struct super_block *sb;
8803 +       struct inode *inode;
8804 +
8805 +       DiMustWriteLock(dentry);
8806 +       AuDebugOn(IS_ROOT(dentry));
8807 +       AuDebugOn(d_really_is_negative(parent));
8808 +
8809 +       sb = dentry->d_sb;
8810 +       sigen = au_sigen(sb);
8811 +       err = au_digen_test(parent, sigen);
8812 +       if (unlikely(err))
8813 +               goto out;
8814 +
8815 +       nbr = au_sbbot(sb) + 1;
8816 +       dinfo = au_di(dentry);
8817 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8818 +       if (unlikely(err))
8819 +               goto out;
8820 +       ebrange = au_dbrange_test(dentry);
8821 +       if (!ebrange)
8822 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8823 +
8824 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8825 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8826 +               if (d_really_is_positive(dentry)) {
8827 +                       inode = d_inode(dentry);
8828 +                       err = au_refresh_hinode_self(inode);
8829 +               }
8830 +               au_dbg_verify_dinode(dentry);
8831 +               if (!err)
8832 +                       goto out_dgen; /* success */
8833 +               goto out;
8834 +       }
8835 +
8836 +       /* temporary dinfo */
8837 +       AuDbgDentry(dentry);
8838 +       err = -ENOMEM;
8839 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8840 +       if (unlikely(!tmp))
8841 +               goto out;
8842 +       au_di_swap(tmp, dinfo);
8843 +       /* returns the number of positive dentries */
8844 +       /*
8845 +        * if current working dir is removed, it returns an error.
8846 +        * but the dentry is legal.
8847 +        */
8848 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8849 +       AuDbgDentry(dentry);
8850 +       au_di_swap(tmp, dinfo);
8851 +       if (err == -ENOENT)
8852 +               err = 0;
8853 +       if (err >= 0) {
8854 +               /* compare/refresh by dinfo */
8855 +               AuDbgDentry(dentry);
8856 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8857 +               au_dbg_verify_dinode(dentry);
8858 +               AuTraceErr(err);
8859 +       }
8860 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8861 +       au_rw_write_unlock(&tmp->di_rwsem);
8862 +       au_di_free(tmp);
8863 +       if (unlikely(err))
8864 +               goto out;
8865 +
8866 +out_dgen:
8867 +       au_update_digen(dentry);
8868 +out:
8869 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8870 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8871 +               AuDbgDentry(dentry);
8872 +       }
8873 +       AuTraceErr(err);
8874 +       return err;
8875 +}
8876 +
8877 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8878 +                          struct dentry *dentry, aufs_bindex_t bindex)
8879 +{
8880 +       int err, valid;
8881 +
8882 +       err = 0;
8883 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8884 +               goto out;
8885 +
8886 +       AuDbg("b%d\n", bindex);
8887 +       /*
8888 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8889 +        * due to whiteout and branch permission.
8890 +        */
8891 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8892 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8893 +       /* it may return tri-state */
8894 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8895 +
8896 +       if (unlikely(valid < 0))
8897 +               err = valid;
8898 +       else if (!valid)
8899 +               err = -EINVAL;
8900 +
8901 +out:
8902 +       AuTraceErr(err);
8903 +       return err;
8904 +}
8905 +
8906 +/* todo: remove this */
8907 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8908 +                         unsigned int flags, int do_udba, int dirren)
8909 +{
8910 +       int err;
8911 +       umode_t mode, h_mode;
8912 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8913 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8914 +       struct inode *h_inode, *h_cached_inode;
8915 +       struct dentry *h_dentry;
8916 +       struct qstr *name, *h_name;
8917 +
8918 +       err = 0;
8919 +       plus = 0;
8920 +       mode = 0;
8921 +       ibs = -1;
8922 +       ibe = -1;
8923 +       unhashed = !!d_unhashed(dentry);
8924 +       is_root = !!IS_ROOT(dentry);
8925 +       name = &dentry->d_name;
8926 +       tmpfile = au_di(dentry)->di_tmpfile;
8927 +
8928 +       /*
8929 +        * Theoretically, REVAL test should be unnecessary in case of
8930 +        * {FS,I}NOTIFY.
8931 +        * But {fs,i}notify doesn't fire some necessary events,
8932 +        *      IN_ATTRIB for atime/nlink/pageio
8933 +        * Let's do REVAL test too.
8934 +        */
8935 +       if (do_udba && inode) {
8936 +               mode = (inode->i_mode & S_IFMT);
8937 +               plus = (inode->i_nlink > 0);
8938 +               ibs = au_ibtop(inode);
8939 +               ibe = au_ibbot(inode);
8940 +       }
8941 +
8942 +       btop = au_dbtop(dentry);
8943 +       btail = btop;
8944 +       if (inode && S_ISDIR(inode->i_mode))
8945 +               btail = au_dbtaildir(dentry);
8946 +       for (bindex = btop; bindex <= btail; bindex++) {
8947 +               h_dentry = au_h_dptr(dentry, bindex);
8948 +               if (!h_dentry)
8949 +                       continue;
8950 +
8951 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8952 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8953 +               spin_lock(&h_dentry->d_lock);
8954 +               h_name = &h_dentry->d_name;
8955 +               if (unlikely(do_udba
8956 +                            && !is_root
8957 +                            && ((!h_nfs
8958 +                                 && (unhashed != !!d_unhashed(h_dentry)
8959 +                                     || (!tmpfile && !dirren
8960 +                                         && !au_qstreq(name, h_name))
8961 +                                         ))
8962 +                                || (h_nfs
8963 +                                    && !(flags & LOOKUP_OPEN)
8964 +                                    && (h_dentry->d_flags
8965 +                                        & DCACHE_NFSFS_RENAMED)))
8966 +                           )) {
8967 +                       int h_unhashed;
8968 +
8969 +                       h_unhashed = d_unhashed(h_dentry);
8970 +                       spin_unlock(&h_dentry->d_lock);
8971 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8972 +                             unhashed, h_unhashed, dentry, h_dentry);
8973 +                       goto err;
8974 +               }
8975 +               spin_unlock(&h_dentry->d_lock);
8976 +
8977 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8978 +               if (unlikely(err))
8979 +                       /* do not goto err, to keep the errno */
8980 +                       break;
8981 +
8982 +               /* todo: plink too? */
8983 +               if (!do_udba)
8984 +                       continue;
8985 +
8986 +               /* UDBA tests */
8987 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8988 +                       goto err;
8989 +
8990 +               h_inode = NULL;
8991 +               if (d_is_positive(h_dentry))
8992 +                       h_inode = d_inode(h_dentry);
8993 +               h_plus = plus;
8994 +               h_mode = mode;
8995 +               h_cached_inode = h_inode;
8996 +               if (h_inode) {
8997 +                       h_mode = (h_inode->i_mode & S_IFMT);
8998 +                       h_plus = (h_inode->i_nlink > 0);
8999 +               }
9000 +               if (inode && ibs <= bindex && bindex <= ibe)
9001 +                       h_cached_inode = au_h_iptr(inode, bindex);
9002 +
9003 +               if (!h_nfs) {
9004 +                       if (unlikely(plus != h_plus && !tmpfile))
9005 +                               goto err;
9006 +               } else {
9007 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9008 +                                    && !is_root
9009 +                                    && !IS_ROOT(h_dentry)
9010 +                                    && unhashed != d_unhashed(h_dentry)))
9011 +                               goto err;
9012 +               }
9013 +               if (unlikely(mode != h_mode
9014 +                            || h_cached_inode != h_inode))
9015 +                       goto err;
9016 +               continue;
9017 +
9018 +err:
9019 +               err = -EINVAL;
9020 +               break;
9021 +       }
9022 +
9023 +       AuTraceErr(err);
9024 +       return err;
9025 +}
9026 +
9027 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
9028 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9029 +{
9030 +       int err;
9031 +       struct dentry *parent;
9032 +
9033 +       if (!au_digen_test(dentry, sigen))
9034 +               return 0;
9035 +
9036 +       parent = dget_parent(dentry);
9037 +       di_read_lock_parent(parent, AuLock_IR);
9038 +       AuDebugOn(au_digen_test(parent, sigen));
9039 +       au_dbg_verify_gen(parent, sigen);
9040 +       err = au_refresh_dentry(dentry, parent);
9041 +       di_read_unlock(parent, AuLock_IR);
9042 +       dput(parent);
9043 +       AuTraceErr(err);
9044 +       return err;
9045 +}
9046 +
9047 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9048 +{
9049 +       int err;
9050 +       struct dentry *d, *parent;
9051 +
9052 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9053 +               return simple_reval_dpath(dentry, sigen);
9054 +
9055 +       /* slow loop, keep it simple and stupid */
9056 +       /* cf: au_cpup_dirs() */
9057 +       err = 0;
9058 +       parent = NULL;
9059 +       while (au_digen_test(dentry, sigen)) {
9060 +               d = dentry;
9061 +               while (1) {
9062 +                       dput(parent);
9063 +                       parent = dget_parent(d);
9064 +                       if (!au_digen_test(parent, sigen))
9065 +                               break;
9066 +                       d = parent;
9067 +               }
9068 +
9069 +               if (d != dentry)
9070 +                       di_write_lock_child2(d);
9071 +
9072 +               /* someone might update our dentry while we were sleeping */
9073 +               if (au_digen_test(d, sigen)) {
9074 +                       /*
9075 +                        * todo: consolidate with simple_reval_dpath(),
9076 +                        * do_refresh() and au_reval_for_attr().
9077 +                        */
9078 +                       di_read_lock_parent(parent, AuLock_IR);
9079 +                       err = au_refresh_dentry(d, parent);
9080 +                       di_read_unlock(parent, AuLock_IR);
9081 +               }
9082 +
9083 +               if (d != dentry)
9084 +                       di_write_unlock(d);
9085 +               dput(parent);
9086 +               if (unlikely(err))
9087 +                       break;
9088 +       }
9089 +
9090 +       return err;
9091 +}
9092 +
9093 +/*
9094 + * if valid returns 1, otherwise 0.
9095 + */
9096 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9097 +{
9098 +       int valid, err;
9099 +       unsigned int sigen;
9100 +       unsigned char do_udba, dirren;
9101 +       struct super_block *sb;
9102 +       struct inode *inode;
9103 +
9104 +       /* todo: support rcu-walk? */
9105 +       if (flags & LOOKUP_RCU)
9106 +               return -ECHILD;
9107 +
9108 +       valid = 0;
9109 +       if (unlikely(!au_di(dentry)))
9110 +               goto out;
9111 +
9112 +       valid = 1;
9113 +       sb = dentry->d_sb;
9114 +       /*
9115 +        * todo: very ugly
9116 +        * i_mutex of parent dir may be held,
9117 +        * but we should not return 'invalid' due to busy.
9118 +        */
9119 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9120 +       if (unlikely(err)) {
9121 +               valid = err;
9122 +               AuTraceErr(err);
9123 +               goto out;
9124 +       }
9125 +       inode = NULL;
9126 +       if (d_really_is_positive(dentry))
9127 +               inode = d_inode(dentry);
9128 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9129 +               err = -EINVAL;
9130 +               AuTraceErr(err);
9131 +               goto out_dgrade;
9132 +       }
9133 +       if (unlikely(au_dbrange_test(dentry))) {
9134 +               err = -EINVAL;
9135 +               AuTraceErr(err);
9136 +               goto out_dgrade;
9137 +       }
9138 +
9139 +       sigen = au_sigen(sb);
9140 +       if (au_digen_test(dentry, sigen)) {
9141 +               AuDebugOn(IS_ROOT(dentry));
9142 +               err = au_reval_dpath(dentry, sigen);
9143 +               if (unlikely(err)) {
9144 +                       AuTraceErr(err);
9145 +                       goto out_dgrade;
9146 +               }
9147 +       }
9148 +       di_downgrade_lock(dentry, AuLock_IR);
9149 +
9150 +       err = -EINVAL;
9151 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9152 +           && inode
9153 +           && !(inode->i_state && I_LINKABLE)
9154 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9155 +               AuTraceErr(err);
9156 +               goto out_inval;
9157 +       }
9158 +
9159 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9160 +       if (do_udba && inode) {
9161 +               aufs_bindex_t btop = au_ibtop(inode);
9162 +               struct inode *h_inode;
9163 +
9164 +               if (btop >= 0) {
9165 +                       h_inode = au_h_iptr(inode, btop);
9166 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9167 +                               AuTraceErr(err);
9168 +                               goto out_inval;
9169 +                       }
9170 +               }
9171 +       }
9172 +
9173 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9174 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9175 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9176 +               err = -EIO;
9177 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9178 +                     dentry, err);
9179 +       }
9180 +       goto out_inval;
9181 +
9182 +out_dgrade:
9183 +       di_downgrade_lock(dentry, AuLock_IR);
9184 +out_inval:
9185 +       aufs_read_unlock(dentry, AuLock_IR);
9186 +       AuTraceErr(err);
9187 +       valid = !err;
9188 +out:
9189 +       if (!valid) {
9190 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9191 +               d_drop(dentry);
9192 +       }
9193 +       return valid;
9194 +}
9195 +
9196 +static void aufs_d_release(struct dentry *dentry)
9197 +{
9198 +       if (au_di(dentry)) {
9199 +               au_di_fin(dentry);
9200 +               au_hn_di_reinit(dentry);
9201 +       }
9202 +}
9203 +
9204 +const struct dentry_operations aufs_dop = {
9205 +       .d_revalidate           = aufs_d_revalidate,
9206 +       .d_weak_revalidate      = aufs_d_revalidate,
9207 +       .d_release              = aufs_d_release
9208 +};
9209 +
9210 +/* aufs_dop without d_revalidate */
9211 +const struct dentry_operations aufs_dop_noreval = {
9212 +       .d_release              = aufs_d_release
9213 +};
9214 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9215 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9216 +++ linux/fs/aufs/dentry.h      2019-07-11 15:42:14.462237786 +0200
9217 @@ -0,0 +1,268 @@
9218 +/* SPDX-License-Identifier: GPL-2.0 */
9219 +/*
9220 + * Copyright (C) 2005-2019 Junjiro R. Okajima
9221 + *
9222 + * This program, aufs is free software; you can redistribute it and/or modify
9223 + * it under the terms of the GNU General Public License as published by
9224 + * the Free Software Foundation; either version 2 of the License, or
9225 + * (at your option) any later version.
9226 + *
9227 + * This program is distributed in the hope that it will be useful,
9228 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9229 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9230 + * GNU General Public License for more details.
9231 + *
9232 + * You should have received a copy of the GNU General Public License
9233 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9234 + */
9235 +
9236 +/*
9237 + * lookup and dentry operations
9238 + */
9239 +
9240 +#ifndef __AUFS_DENTRY_H__
9241 +#define __AUFS_DENTRY_H__
9242 +
9243 +#ifdef __KERNEL__
9244 +
9245 +#include <linux/dcache.h>
9246 +#include "dirren.h"
9247 +#include "rwsem.h"
9248 +
9249 +struct au_hdentry {
9250 +       struct dentry           *hd_dentry;
9251 +       aufs_bindex_t           hd_id;
9252 +};
9253 +
9254 +struct au_dinfo {
9255 +       atomic_t                di_generation;
9256 +
9257 +       struct au_rwsem         di_rwsem;
9258 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9259 +       unsigned char           di_tmpfile; /* to allow the different name */
9260 +       struct au_hdentry       *di_hdentry;
9261 +       struct rcu_head         rcu;
9262 +} ____cacheline_aligned_in_smp;
9263 +
9264 +/* ---------------------------------------------------------------------- */
9265 +
9266 +/* flags for au_lkup_dentry() */
9267 +#define AuLkup_ALLOW_NEG       1
9268 +#define AuLkup_IGNORE_PERM     (1 << 1)
9269 +#define AuLkup_DIRREN          (1 << 2)
9270 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9271 +#define au_fset_lkup(flags, name) \
9272 +       do { (flags) |= AuLkup_##name; } while (0)
9273 +#define au_fclr_lkup(flags, name) \
9274 +       do { (flags) &= ~AuLkup_##name; } while (0)
9275 +
9276 +#ifndef CONFIG_AUFS_DIRREN
9277 +#undef AuLkup_DIRREN
9278 +#define AuLkup_DIRREN 0
9279 +#endif
9280 +
9281 +struct au_do_lookup_args {
9282 +       unsigned int            flags;
9283 +       mode_t                  type;
9284 +       struct qstr             whname, *name;
9285 +       struct au_dr_lookup     dirren;
9286 +};
9287 +
9288 +/* ---------------------------------------------------------------------- */
9289 +
9290 +/* dentry.c */
9291 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9292 +struct au_branch;
9293 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9294 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9295 +               struct dentry *h_parent, struct au_branch *br);
9296 +
9297 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9298 +                  unsigned int flags);
9299 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9300 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9301 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9302 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9303 +
9304 +/* dinfo.c */
9305 +void au_di_init_once(void *_di);
9306 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9307 +void au_di_free(struct au_dinfo *dinfo);
9308 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9309 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9310 +int au_di_init(struct dentry *dentry);
9311 +void au_di_fin(struct dentry *dentry);
9312 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9313 +
9314 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9315 +void di_read_unlock(struct dentry *d, int flags);
9316 +void di_downgrade_lock(struct dentry *d, int flags);
9317 +void di_write_lock(struct dentry *d, unsigned int lsc);
9318 +void di_write_unlock(struct dentry *d);
9319 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9320 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9321 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9322 +
9323 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9324 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9325 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9326 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9327 +
9328 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9329 +                  struct dentry *h_dentry);
9330 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9331 +int au_dbrange_test(struct dentry *dentry);
9332 +void au_update_digen(struct dentry *dentry);
9333 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9334 +void au_update_dbtop(struct dentry *dentry);
9335 +void au_update_dbbot(struct dentry *dentry);
9336 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9337 +
9338 +/* ---------------------------------------------------------------------- */
9339 +
9340 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9341 +{
9342 +       return dentry->d_fsdata;
9343 +}
9344 +
9345 +/* ---------------------------------------------------------------------- */
9346 +
9347 +/* lock subclass for dinfo */
9348 +enum {
9349 +       AuLsc_DI_CHILD,         /* child first */
9350 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9351 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9352 +       AuLsc_DI_PARENT,
9353 +       AuLsc_DI_PARENT2,
9354 +       AuLsc_DI_PARENT3,
9355 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9356 +};
9357 +
9358 +/*
9359 + * di_read_lock_child, di_write_lock_child,
9360 + * di_read_lock_child2, di_write_lock_child2,
9361 + * di_read_lock_child3, di_write_lock_child3,
9362 + * di_read_lock_parent, di_write_lock_parent,
9363 + * di_read_lock_parent2, di_write_lock_parent2,
9364 + * di_read_lock_parent3, di_write_lock_parent3,
9365 + */
9366 +#define AuReadLockFunc(name, lsc) \
9367 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9368 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9369 +
9370 +#define AuWriteLockFunc(name, lsc) \
9371 +static inline void di_write_lock_##name(struct dentry *d) \
9372 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9373 +
9374 +#define AuRWLockFuncs(name, lsc) \
9375 +       AuReadLockFunc(name, lsc) \
9376 +       AuWriteLockFunc(name, lsc)
9377 +
9378 +AuRWLockFuncs(child, CHILD);
9379 +AuRWLockFuncs(child2, CHILD2);
9380 +AuRWLockFuncs(child3, CHILD3);
9381 +AuRWLockFuncs(parent, PARENT);
9382 +AuRWLockFuncs(parent2, PARENT2);
9383 +AuRWLockFuncs(parent3, PARENT3);
9384 +
9385 +#undef AuReadLockFunc
9386 +#undef AuWriteLockFunc
9387 +#undef AuRWLockFuncs
9388 +
9389 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9390 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9391 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9392 +
9393 +/* ---------------------------------------------------------------------- */
9394 +
9395 +/* todo: memory barrier? */
9396 +static inline unsigned int au_digen(struct dentry *d)
9397 +{
9398 +       return atomic_read(&au_di(d)->di_generation);
9399 +}
9400 +
9401 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9402 +{
9403 +       hdentry->hd_dentry = NULL;
9404 +}
9405 +
9406 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9407 +                                           aufs_bindex_t bindex)
9408 +{
9409 +       return di->di_hdentry + bindex;
9410 +}
9411 +
9412 +static inline void au_hdput(struct au_hdentry *hd)
9413 +{
9414 +       if (hd)
9415 +               dput(hd->hd_dentry);
9416 +}
9417 +
9418 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9419 +{
9420 +       DiMustAnyLock(dentry);
9421 +       return au_di(dentry)->di_btop;
9422 +}
9423 +
9424 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9425 +{
9426 +       DiMustAnyLock(dentry);
9427 +       return au_di(dentry)->di_bbot;
9428 +}
9429 +
9430 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9431 +{
9432 +       DiMustAnyLock(dentry);
9433 +       return au_di(dentry)->di_bwh;
9434 +}
9435 +
9436 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9437 +{
9438 +       DiMustAnyLock(dentry);
9439 +       return au_di(dentry)->di_bdiropq;
9440 +}
9441 +
9442 +/* todo: hard/soft set? */
9443 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9444 +{
9445 +       DiMustWriteLock(dentry);
9446 +       au_di(dentry)->di_btop = bindex;
9447 +}
9448 +
9449 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9450 +{
9451 +       DiMustWriteLock(dentry);
9452 +       au_di(dentry)->di_bbot = bindex;
9453 +}
9454 +
9455 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9456 +{
9457 +       DiMustWriteLock(dentry);
9458 +       /* dbwh can be outside of btop - bbot range */
9459 +       au_di(dentry)->di_bwh = bindex;
9460 +}
9461 +
9462 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9463 +{
9464 +       DiMustWriteLock(dentry);
9465 +       au_di(dentry)->di_bdiropq = bindex;
9466 +}
9467 +
9468 +/* ---------------------------------------------------------------------- */
9469 +
9470 +#ifdef CONFIG_AUFS_HNOTIFY
9471 +static inline void au_digen_dec(struct dentry *d)
9472 +{
9473 +       atomic_dec(&au_di(d)->di_generation);
9474 +}
9475 +
9476 +static inline void au_hn_di_reinit(struct dentry *dentry)
9477 +{
9478 +       dentry->d_fsdata = NULL;
9479 +}
9480 +#else
9481 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9482 +#endif /* CONFIG_AUFS_HNOTIFY */
9483 +
9484 +#endif /* __KERNEL__ */
9485 +#endif /* __AUFS_DENTRY_H__ */
9486 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9487 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9488 +++ linux/fs/aufs/dinfo.c       2019-07-11 15:42:14.462237786 +0200
9489 @@ -0,0 +1,554 @@
9490 +// SPDX-License-Identifier: GPL-2.0
9491 +/*
9492 + * Copyright (C) 2005-2019 Junjiro R. Okajima
9493 + *
9494 + * This program, aufs is free software; you can redistribute it and/or modify
9495 + * it under the terms of the GNU General Public License as published by
9496 + * the Free Software Foundation; either version 2 of the License, or
9497 + * (at your option) any later version.
9498 + *
9499 + * This program is distributed in the hope that it will be useful,
9500 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9501 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9502 + * GNU General Public License for more details.
9503 + *
9504 + * You should have received a copy of the GNU General Public License
9505 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9506 + */
9507 +
9508 +/*
9509 + * dentry private data
9510 + */
9511 +
9512 +#include "aufs.h"
9513 +
9514 +void au_di_init_once(void *_dinfo)
9515 +{
9516 +       struct au_dinfo *dinfo = _dinfo;
9517 +
9518 +       au_rw_init(&dinfo->di_rwsem);
9519 +}
9520 +
9521 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9522 +{
9523 +       struct au_dinfo *dinfo;
9524 +       int nbr, i;
9525 +
9526 +       dinfo = au_cache_alloc_dinfo();
9527 +       if (unlikely(!dinfo))
9528 +               goto out;
9529 +
9530 +       nbr = au_sbbot(sb) + 1;
9531 +       if (nbr <= 0)
9532 +               nbr = 1;
9533 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9534 +       if (dinfo->di_hdentry) {
9535 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9536 +               dinfo->di_btop = -1;
9537 +               dinfo->di_bbot = -1;
9538 +               dinfo->di_bwh = -1;
9539 +               dinfo->di_bdiropq = -1;
9540 +               dinfo->di_tmpfile = 0;
9541 +               for (i = 0; i < nbr; i++)
9542 +                       dinfo->di_hdentry[i].hd_id = -1;
9543 +               goto out;
9544 +       }
9545 +
9546 +       au_cache_free_dinfo(dinfo);
9547 +       dinfo = NULL;
9548 +
9549 +out:
9550 +       return dinfo;
9551 +}
9552 +
9553 +void au_di_free(struct au_dinfo *dinfo)
9554 +{
9555 +       struct au_hdentry *p;
9556 +       aufs_bindex_t bbot, bindex;
9557 +
9558 +       /* dentry may not be revalidated */
9559 +       bindex = dinfo->di_btop;
9560 +       if (bindex >= 0) {
9561 +               bbot = dinfo->di_bbot;
9562 +               p = au_hdentry(dinfo, bindex);
9563 +               while (bindex++ <= bbot)
9564 +                       au_hdput(p++);
9565 +       }
9566 +       au_kfree_try_rcu(dinfo->di_hdentry);
9567 +       au_cache_free_dinfo(dinfo);
9568 +}
9569 +
9570 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9571 +{
9572 +       struct au_hdentry *p;
9573 +       aufs_bindex_t bi;
9574 +
9575 +       AuRwMustWriteLock(&a->di_rwsem);
9576 +       AuRwMustWriteLock(&b->di_rwsem);
9577 +
9578 +#define DiSwap(v, name)                                \
9579 +       do {                                    \
9580 +               v = a->di_##name;               \
9581 +               a->di_##name = b->di_##name;    \
9582 +               b->di_##name = v;               \
9583 +       } while (0)
9584 +
9585 +       DiSwap(p, hdentry);
9586 +       DiSwap(bi, btop);
9587 +       DiSwap(bi, bbot);
9588 +       DiSwap(bi, bwh);
9589 +       DiSwap(bi, bdiropq);
9590 +       /* smp_mb(); */
9591 +
9592 +#undef DiSwap
9593 +}
9594 +
9595 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9596 +{
9597 +       AuRwMustWriteLock(&dst->di_rwsem);
9598 +       AuRwMustWriteLock(&src->di_rwsem);
9599 +
9600 +       dst->di_btop = src->di_btop;
9601 +       dst->di_bbot = src->di_bbot;
9602 +       dst->di_bwh = src->di_bwh;
9603 +       dst->di_bdiropq = src->di_bdiropq;
9604 +       /* smp_mb(); */
9605 +}
9606 +
9607 +int au_di_init(struct dentry *dentry)
9608 +{
9609 +       int err;
9610 +       struct super_block *sb;
9611 +       struct au_dinfo *dinfo;
9612 +
9613 +       err = 0;
9614 +       sb = dentry->d_sb;
9615 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9616 +       if (dinfo) {
9617 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9618 +               /* smp_mb(); */ /* atomic_set */
9619 +               dentry->d_fsdata = dinfo;
9620 +       } else
9621 +               err = -ENOMEM;
9622 +
9623 +       return err;
9624 +}
9625 +
9626 +void au_di_fin(struct dentry *dentry)
9627 +{
9628 +       struct au_dinfo *dinfo;
9629 +
9630 +       dinfo = au_di(dentry);
9631 +       AuRwDestroy(&dinfo->di_rwsem);
9632 +       au_di_free(dinfo);
9633 +}
9634 +
9635 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9636 +{
9637 +       int err, sz;
9638 +       struct au_hdentry *hdp;
9639 +
9640 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9641 +
9642 +       err = -ENOMEM;
9643 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9644 +       if (!sz)
9645 +               sz = sizeof(*hdp);
9646 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9647 +                          may_shrink);
9648 +       if (hdp) {
9649 +               dinfo->di_hdentry = hdp;
9650 +               err = 0;
9651 +       }
9652 +
9653 +       return err;
9654 +}
9655 +
9656 +/* ---------------------------------------------------------------------- */
9657 +
9658 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9659 +{
9660 +       switch (lsc) {
9661 +       case AuLsc_DI_CHILD:
9662 +               ii_write_lock_child(inode);
9663 +               break;
9664 +       case AuLsc_DI_CHILD2:
9665 +               ii_write_lock_child2(inode);
9666 +               break;
9667 +       case AuLsc_DI_CHILD3:
9668 +               ii_write_lock_child3(inode);
9669 +               break;
9670 +       case AuLsc_DI_PARENT:
9671 +               ii_write_lock_parent(inode);
9672 +               break;
9673 +       case AuLsc_DI_PARENT2:
9674 +               ii_write_lock_parent2(inode);
9675 +               break;
9676 +       case AuLsc_DI_PARENT3:
9677 +               ii_write_lock_parent3(inode);
9678 +               break;
9679 +       default:
9680 +               BUG();
9681 +       }
9682 +}
9683 +
9684 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9685 +{
9686 +       switch (lsc) {
9687 +       case AuLsc_DI_CHILD:
9688 +               ii_read_lock_child(inode);
9689 +               break;
9690 +       case AuLsc_DI_CHILD2:
9691 +               ii_read_lock_child2(inode);
9692 +               break;
9693 +       case AuLsc_DI_CHILD3:
9694 +               ii_read_lock_child3(inode);
9695 +               break;
9696 +       case AuLsc_DI_PARENT:
9697 +               ii_read_lock_parent(inode);
9698 +               break;
9699 +       case AuLsc_DI_PARENT2:
9700 +               ii_read_lock_parent2(inode);
9701 +               break;
9702 +       case AuLsc_DI_PARENT3:
9703 +               ii_read_lock_parent3(inode);
9704 +               break;
9705 +       default:
9706 +               BUG();
9707 +       }
9708 +}
9709 +
9710 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9711 +{
9712 +       struct inode *inode;
9713 +
9714 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9715 +       if (d_really_is_positive(d)) {
9716 +               inode = d_inode(d);
9717 +               if (au_ftest_lock(flags, IW))
9718 +                       do_ii_write_lock(inode, lsc);
9719 +               else if (au_ftest_lock(flags, IR))
9720 +                       do_ii_read_lock(inode, lsc);
9721 +       }
9722 +}
9723 +
9724 +void di_read_unlock(struct dentry *d, int flags)
9725 +{
9726 +       struct inode *inode;
9727 +
9728 +       if (d_really_is_positive(d)) {
9729 +               inode = d_inode(d);
9730 +               if (au_ftest_lock(flags, IW)) {
9731 +                       au_dbg_verify_dinode(d);
9732 +                       ii_write_unlock(inode);
9733 +               } else if (au_ftest_lock(flags, IR)) {
9734 +                       au_dbg_verify_dinode(d);
9735 +                       ii_read_unlock(inode);
9736 +               }
9737 +       }
9738 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9739 +}
9740 +
9741 +void di_downgrade_lock(struct dentry *d, int flags)
9742 +{
9743 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9744 +               ii_downgrade_lock(d_inode(d));
9745 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9746 +}
9747 +
9748 +void di_write_lock(struct dentry *d, unsigned int lsc)
9749 +{
9750 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9751 +       if (d_really_is_positive(d))
9752 +               do_ii_write_lock(d_inode(d), lsc);
9753 +}
9754 +
9755 +void di_write_unlock(struct dentry *d)
9756 +{
9757 +       au_dbg_verify_dinode(d);
9758 +       if (d_really_is_positive(d))
9759 +               ii_write_unlock(d_inode(d));
9760 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9761 +}
9762 +
9763 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9764 +{
9765 +       AuDebugOn(d1 == d2
9766 +                 || d_inode(d1) == d_inode(d2)
9767 +                 || d1->d_sb != d2->d_sb);
9768 +
9769 +       if ((isdir && au_test_subdir(d1, d2))
9770 +           || d1 < d2) {
9771 +               di_write_lock_child(d1);
9772 +               di_write_lock_child2(d2);
9773 +       } else {
9774 +               di_write_lock_child(d2);
9775 +               di_write_lock_child2(d1);
9776 +       }
9777 +}
9778 +
9779 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9780 +{
9781 +       AuDebugOn(d1 == d2
9782 +                 || d_inode(d1) == d_inode(d2)
9783 +                 || d1->d_sb != d2->d_sb);
9784 +
9785 +       if ((isdir && au_test_subdir(d1, d2))
9786 +           || d1 < d2) {
9787 +               di_write_lock_parent(d1);
9788 +               di_write_lock_parent2(d2);
9789 +       } else {
9790 +               di_write_lock_parent(d2);
9791 +               di_write_lock_parent2(d1);
9792 +       }
9793 +}
9794 +
9795 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9796 +{
9797 +       di_write_unlock(d1);
9798 +       if (d_inode(d1) == d_inode(d2))
9799 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9800 +       else
9801 +               di_write_unlock(d2);
9802 +}
9803 +
9804 +/* ---------------------------------------------------------------------- */
9805 +
9806 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9807 +{
9808 +       struct dentry *d;
9809 +
9810 +       DiMustAnyLock(dentry);
9811 +
9812 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9813 +               return NULL;
9814 +       AuDebugOn(bindex < 0);
9815 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9816 +       AuDebugOn(d && au_dcount(d) <= 0);
9817 +       return d;
9818 +}
9819 +
9820 +/*
9821 + * extended version of au_h_dptr().
9822 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9823 + * error.
9824 + */
9825 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9826 +{
9827 +       struct dentry *h_dentry;
9828 +       struct inode *inode, *h_inode;
9829 +
9830 +       AuDebugOn(d_really_is_negative(dentry));
9831 +
9832 +       h_dentry = NULL;
9833 +       if (au_dbtop(dentry) <= bindex
9834 +           && bindex <= au_dbbot(dentry))
9835 +               h_dentry = au_h_dptr(dentry, bindex);
9836 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9837 +               dget(h_dentry);
9838 +               goto out; /* success */
9839 +       }
9840 +
9841 +       inode = d_inode(dentry);
9842 +       AuDebugOn(bindex < au_ibtop(inode));
9843 +       AuDebugOn(au_ibbot(inode) < bindex);
9844 +       h_inode = au_h_iptr(inode, bindex);
9845 +       h_dentry = d_find_alias(h_inode);
9846 +       if (h_dentry) {
9847 +               if (!IS_ERR(h_dentry)) {
9848 +                       if (!au_d_linkable(h_dentry))
9849 +                               goto out; /* success */
9850 +                       dput(h_dentry);
9851 +               } else
9852 +                       goto out;
9853 +       }
9854 +
9855 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9856 +               h_dentry = au_plink_lkup(inode, bindex);
9857 +               AuDebugOn(!h_dentry);
9858 +               if (!IS_ERR(h_dentry)) {
9859 +                       if (!au_d_hashed_positive(h_dentry))
9860 +                               goto out; /* success */
9861 +                       dput(h_dentry);
9862 +                       h_dentry = NULL;
9863 +               }
9864 +       }
9865 +
9866 +out:
9867 +       AuDbgDentry(h_dentry);
9868 +       return h_dentry;
9869 +}
9870 +
9871 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9872 +{
9873 +       aufs_bindex_t bbot, bwh;
9874 +
9875 +       bbot = au_dbbot(dentry);
9876 +       if (0 <= bbot) {
9877 +               bwh = au_dbwh(dentry);
9878 +               if (!bwh)
9879 +                       return bwh;
9880 +               if (0 < bwh && bwh < bbot)
9881 +                       return bwh - 1;
9882 +       }
9883 +       return bbot;
9884 +}
9885 +
9886 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9887 +{
9888 +       aufs_bindex_t bbot, bopq;
9889 +
9890 +       bbot = au_dbtail(dentry);
9891 +       if (0 <= bbot) {
9892 +               bopq = au_dbdiropq(dentry);
9893 +               if (0 <= bopq && bopq < bbot)
9894 +                       bbot = bopq;
9895 +       }
9896 +       return bbot;
9897 +}
9898 +
9899 +/* ---------------------------------------------------------------------- */
9900 +
9901 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9902 +                  struct dentry *h_dentry)
9903 +{
9904 +       struct au_dinfo *dinfo;
9905 +       struct au_hdentry *hd;
9906 +       struct au_branch *br;
9907 +
9908 +       DiMustWriteLock(dentry);
9909 +
9910 +       dinfo = au_di(dentry);
9911 +       hd = au_hdentry(dinfo, bindex);
9912 +       au_hdput(hd);
9913 +       hd->hd_dentry = h_dentry;
9914 +       if (h_dentry) {
9915 +               br = au_sbr(dentry->d_sb, bindex);
9916 +               hd->hd_id = br->br_id;
9917 +       }
9918 +}
9919 +
9920 +int au_dbrange_test(struct dentry *dentry)
9921 +{
9922 +       int err;
9923 +       aufs_bindex_t btop, bbot;
9924 +
9925 +       err = 0;
9926 +       btop = au_dbtop(dentry);
9927 +       bbot = au_dbbot(dentry);
9928 +       if (btop >= 0)
9929 +               AuDebugOn(bbot < 0 && btop > bbot);
9930 +       else {
9931 +               err = -EIO;
9932 +               AuDebugOn(bbot >= 0);
9933 +       }
9934 +
9935 +       return err;
9936 +}
9937 +
9938 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9939 +{
9940 +       int err;
9941 +
9942 +       err = 0;
9943 +       if (unlikely(au_digen(dentry) != sigen
9944 +                    || au_iigen_test(d_inode(dentry), sigen)))
9945 +               err = -EIO;
9946 +
9947 +       return err;
9948 +}
9949 +
9950 +void au_update_digen(struct dentry *dentry)
9951 +{
9952 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9953 +       /* smp_mb(); */ /* atomic_set */
9954 +}
9955 +
9956 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9957 +{
9958 +       struct au_dinfo *dinfo;
9959 +       struct dentry *h_d;
9960 +       struct au_hdentry *hdp;
9961 +       aufs_bindex_t bindex, bbot;
9962 +
9963 +       DiMustWriteLock(dentry);
9964 +
9965 +       dinfo = au_di(dentry);
9966 +       if (!dinfo || dinfo->di_btop < 0)
9967 +               return;
9968 +
9969 +       if (do_put_zero) {
9970 +               bbot = dinfo->di_bbot;
9971 +               bindex = dinfo->di_btop;
9972 +               hdp = au_hdentry(dinfo, bindex);
9973 +               for (; bindex <= bbot; bindex++, hdp++) {
9974 +                       h_d = hdp->hd_dentry;
9975 +                       if (h_d && d_is_negative(h_d))
9976 +                               au_set_h_dptr(dentry, bindex, NULL);
9977 +               }
9978 +       }
9979 +
9980 +       dinfo->di_btop = 0;
9981 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9982 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9983 +               if (hdp->hd_dentry)
9984 +                       break;
9985 +       if (dinfo->di_btop > dinfo->di_bbot) {
9986 +               dinfo->di_btop = -1;
9987 +               dinfo->di_bbot = -1;
9988 +               return;
9989 +       }
9990 +
9991 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9992 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9993 +               if (hdp->hd_dentry)
9994 +                       break;
9995 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9996 +}
9997 +
9998 +void au_update_dbtop(struct dentry *dentry)
9999 +{
10000 +       aufs_bindex_t bindex, bbot;
10001 +       struct dentry *h_dentry;
10002 +
10003 +       bbot = au_dbbot(dentry);
10004 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
10005 +               h_dentry = au_h_dptr(dentry, bindex);
10006 +               if (!h_dentry)
10007 +                       continue;
10008 +               if (d_is_positive(h_dentry)) {
10009 +                       au_set_dbtop(dentry, bindex);
10010 +                       return;
10011 +               }
10012 +               au_set_h_dptr(dentry, bindex, NULL);
10013 +       }
10014 +}
10015 +
10016 +void au_update_dbbot(struct dentry *dentry)
10017 +{
10018 +       aufs_bindex_t bindex, btop;
10019 +       struct dentry *h_dentry;
10020 +
10021 +       btop = au_dbtop(dentry);
10022 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
10023 +               h_dentry = au_h_dptr(dentry, bindex);
10024 +               if (!h_dentry)
10025 +                       continue;
10026 +               if (d_is_positive(h_dentry)) {
10027 +                       au_set_dbbot(dentry, bindex);
10028 +                       return;
10029 +               }
10030 +               au_set_h_dptr(dentry, bindex, NULL);
10031 +       }
10032 +}
10033 +
10034 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10035 +{
10036 +       aufs_bindex_t bindex, bbot;
10037 +
10038 +       bbot = au_dbbot(dentry);
10039 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
10040 +               if (au_h_dptr(dentry, bindex) == h_dentry)
10041 +                       return bindex;
10042 +       return -1;
10043 +}
10044 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10045 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
10046 +++ linux/fs/aufs/dir.c 2019-07-11 15:42:14.462237786 +0200
10047 @@ -0,0 +1,763 @@
10048 +// SPDX-License-Identifier: GPL-2.0
10049 +/*
10050 + * Copyright (C) 2005-2019 Junjiro R. Okajima
10051 + *
10052 + * This program, aufs is free software; you can redistribute it and/or modify
10053 + * it under the terms of the GNU General Public License as published by
10054 + * the Free Software Foundation; either version 2 of the License, or
10055 + * (at your option) any later version.
10056 + *
10057 + * This program is distributed in the hope that it will be useful,
10058 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10059 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10060 + * GNU General Public License for more details.
10061 + *
10062 + * You should have received a copy of the GNU General Public License
10063 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10064 + */
10065 +
10066 +/*
10067 + * directory operations
10068 + */
10069 +
10070 +#include <linux/fs_stack.h>
10071 +#include <linux/iversion.h>
10072 +#include "aufs.h"
10073 +
10074 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10075 +{
10076 +       unsigned int nlink;
10077 +
10078 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10079 +
10080 +       nlink = dir->i_nlink;
10081 +       nlink += h_dir->i_nlink - 2;
10082 +       if (h_dir->i_nlink < 2)
10083 +               nlink += 2;
10084 +       smp_mb(); /* for i_nlink */
10085 +       /* 0 can happen in revaliding */
10086 +       set_nlink(dir, nlink);
10087 +}
10088 +
10089 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10090 +{
10091 +       unsigned int nlink;
10092 +
10093 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10094 +
10095 +       nlink = dir->i_nlink;
10096 +       nlink -= h_dir->i_nlink - 2;
10097 +       if (h_dir->i_nlink < 2)
10098 +               nlink -= 2;
10099 +       smp_mb(); /* for i_nlink */
10100 +       /* nlink == 0 means the branch-fs is broken */
10101 +       set_nlink(dir, nlink);
10102 +}
10103 +
10104 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10105 +{
10106 +       loff_t sz;
10107 +       aufs_bindex_t bindex, bbot;
10108 +       struct file *h_file;
10109 +       struct dentry *h_dentry;
10110 +
10111 +       sz = 0;
10112 +       if (file) {
10113 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10114 +
10115 +               bbot = au_fbbot_dir(file);
10116 +               for (bindex = au_fbtop(file);
10117 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10118 +                    bindex++) {
10119 +                       h_file = au_hf_dir(file, bindex);
10120 +                       if (h_file && file_inode(h_file))
10121 +                               sz += vfsub_f_size_read(h_file);
10122 +               }
10123 +       } else {
10124 +               AuDebugOn(!dentry);
10125 +               AuDebugOn(!d_is_dir(dentry));
10126 +
10127 +               bbot = au_dbtaildir(dentry);
10128 +               for (bindex = au_dbtop(dentry);
10129 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10130 +                    bindex++) {
10131 +                       h_dentry = au_h_dptr(dentry, bindex);
10132 +                       if (h_dentry && d_is_positive(h_dentry))
10133 +                               sz += i_size_read(d_inode(h_dentry));
10134 +               }
10135 +       }
10136 +       if (sz < KMALLOC_MAX_SIZE)
10137 +               sz = roundup_pow_of_two(sz);
10138 +       if (sz > KMALLOC_MAX_SIZE)
10139 +               sz = KMALLOC_MAX_SIZE;
10140 +       else if (sz < NAME_MAX) {
10141 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10142 +               sz = AUFS_RDBLK_DEF;
10143 +       }
10144 +       return sz;
10145 +}
10146 +
10147 +struct au_dir_ts_arg {
10148 +       struct dentry *dentry;
10149 +       aufs_bindex_t brid;
10150 +};
10151 +
10152 +static void au_do_dir_ts(void *arg)
10153 +{
10154 +       struct au_dir_ts_arg *a = arg;
10155 +       struct au_dtime dt;
10156 +       struct path h_path;
10157 +       struct inode *dir, *h_dir;
10158 +       struct super_block *sb;
10159 +       struct au_branch *br;
10160 +       struct au_hinode *hdir;
10161 +       int err;
10162 +       aufs_bindex_t btop, bindex;
10163 +
10164 +       sb = a->dentry->d_sb;
10165 +       if (d_really_is_negative(a->dentry))
10166 +               goto out;
10167 +       /* no dir->i_mutex lock */
10168 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10169 +
10170 +       dir = d_inode(a->dentry);
10171 +       btop = au_ibtop(dir);
10172 +       bindex = au_br_index(sb, a->brid);
10173 +       if (bindex < btop)
10174 +               goto out_unlock;
10175 +
10176 +       br = au_sbr(sb, bindex);
10177 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10178 +       if (!h_path.dentry)
10179 +               goto out_unlock;
10180 +       h_path.mnt = au_br_mnt(br);
10181 +       au_dtime_store(&dt, a->dentry, &h_path);
10182 +
10183 +       br = au_sbr(sb, btop);
10184 +       if (!au_br_writable(br->br_perm))
10185 +               goto out_unlock;
10186 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10187 +       h_path.mnt = au_br_mnt(br);
10188 +       err = vfsub_mnt_want_write(h_path.mnt);
10189 +       if (err)
10190 +               goto out_unlock;
10191 +       hdir = au_hi(dir, btop);
10192 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10193 +       h_dir = au_h_iptr(dir, btop);
10194 +       if (h_dir->i_nlink
10195 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10196 +               dt.dt_h_path = h_path;
10197 +               au_dtime_revert(&dt);
10198 +       }
10199 +       au_hn_inode_unlock(hdir);
10200 +       vfsub_mnt_drop_write(h_path.mnt);
10201 +       au_cpup_attr_timesizes(dir);
10202 +
10203 +out_unlock:
10204 +       aufs_read_unlock(a->dentry, AuLock_DW);
10205 +out:
10206 +       dput(a->dentry);
10207 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10208 +       au_kfree_try_rcu(arg);
10209 +}
10210 +
10211 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10212 +{
10213 +       int perm, wkq_err;
10214 +       aufs_bindex_t btop;
10215 +       struct au_dir_ts_arg *arg;
10216 +       struct dentry *dentry;
10217 +       struct super_block *sb;
10218 +
10219 +       IMustLock(dir);
10220 +
10221 +       dentry = d_find_any_alias(dir);
10222 +       AuDebugOn(!dentry);
10223 +       sb = dentry->d_sb;
10224 +       btop = au_ibtop(dir);
10225 +       if (btop == bindex) {
10226 +               au_cpup_attr_timesizes(dir);
10227 +               goto out;
10228 +       }
10229 +
10230 +       perm = au_sbr_perm(sb, btop);
10231 +       if (!au_br_writable(perm))
10232 +               goto out;
10233 +
10234 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10235 +       if (!arg)
10236 +               goto out;
10237 +
10238 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10239 +       arg->brid = au_sbr_id(sb, bindex);
10240 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10241 +       if (unlikely(wkq_err)) {
10242 +               pr_err("wkq %d\n", wkq_err);
10243 +               dput(dentry);
10244 +               au_kfree_try_rcu(arg);
10245 +       }
10246 +
10247 +out:
10248 +       dput(dentry);
10249 +}
10250 +
10251 +/* ---------------------------------------------------------------------- */
10252 +
10253 +static int reopen_dir(struct file *file)
10254 +{
10255 +       int err;
10256 +       unsigned int flags;
10257 +       aufs_bindex_t bindex, btail, btop;
10258 +       struct dentry *dentry, *h_dentry;
10259 +       struct file *h_file;
10260 +
10261 +       /* open all lower dirs */
10262 +       dentry = file->f_path.dentry;
10263 +       btop = au_dbtop(dentry);
10264 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10265 +               au_set_h_fptr(file, bindex, NULL);
10266 +       au_set_fbtop(file, btop);
10267 +
10268 +       btail = au_dbtaildir(dentry);
10269 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10270 +               au_set_h_fptr(file, bindex, NULL);
10271 +       au_set_fbbot_dir(file, btail);
10272 +
10273 +       flags = vfsub_file_flags(file);
10274 +       for (bindex = btop; bindex <= btail; bindex++) {
10275 +               h_dentry = au_h_dptr(dentry, bindex);
10276 +               if (!h_dentry)
10277 +                       continue;
10278 +               h_file = au_hf_dir(file, bindex);
10279 +               if (h_file)
10280 +                       continue;
10281 +
10282 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10283 +               err = PTR_ERR(h_file);
10284 +               if (IS_ERR(h_file))
10285 +                       goto out; /* close all? */
10286 +               au_set_h_fptr(file, bindex, h_file);
10287 +       }
10288 +       au_update_figen(file);
10289 +       /* todo: necessary? */
10290 +       /* file->f_ra = h_file->f_ra; */
10291 +       err = 0;
10292 +
10293 +out:
10294 +       return err;
10295 +}
10296 +
10297 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10298 +{
10299 +       int err;
10300 +       aufs_bindex_t bindex, btail;
10301 +       struct dentry *dentry, *h_dentry;
10302 +       struct vfsmount *mnt;
10303 +
10304 +       FiMustWriteLock(file);
10305 +       AuDebugOn(h_file);
10306 +
10307 +       err = 0;
10308 +       mnt = file->f_path.mnt;
10309 +       dentry = file->f_path.dentry;
10310 +       file->f_version = inode_query_iversion(d_inode(dentry));
10311 +       bindex = au_dbtop(dentry);
10312 +       au_set_fbtop(file, bindex);
10313 +       btail = au_dbtaildir(dentry);
10314 +       au_set_fbbot_dir(file, btail);
10315 +       for (; !err && bindex <= btail; bindex++) {
10316 +               h_dentry = au_h_dptr(dentry, bindex);
10317 +               if (!h_dentry)
10318 +                       continue;
10319 +
10320 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10321 +               if (unlikely(err))
10322 +                       break;
10323 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10324 +               if (IS_ERR(h_file)) {
10325 +                       err = PTR_ERR(h_file);
10326 +                       break;
10327 +               }
10328 +               au_set_h_fptr(file, bindex, h_file);
10329 +       }
10330 +       au_update_figen(file);
10331 +       /* todo: necessary? */
10332 +       /* file->f_ra = h_file->f_ra; */
10333 +       if (!err)
10334 +               return 0; /* success */
10335 +
10336 +       /* close all */
10337 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10338 +               au_set_h_fptr(file, bindex, NULL);
10339 +       au_set_fbtop(file, -1);
10340 +       au_set_fbbot_dir(file, -1);
10341 +
10342 +       return err;
10343 +}
10344 +
10345 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10346 +                        struct file *file)
10347 +{
10348 +       int err;
10349 +       struct super_block *sb;
10350 +       struct au_fidir *fidir;
10351 +
10352 +       err = -ENOMEM;
10353 +       sb = file->f_path.dentry->d_sb;
10354 +       si_read_lock(sb, AuLock_FLUSH);
10355 +       fidir = au_fidir_alloc(sb);
10356 +       if (fidir) {
10357 +               struct au_do_open_args args = {
10358 +                       .open   = do_open_dir,
10359 +                       .fidir  = fidir
10360 +               };
10361 +               err = au_do_open(file, &args);
10362 +               if (unlikely(err))
10363 +                       au_kfree_rcu(fidir);
10364 +       }
10365 +       si_read_unlock(sb);
10366 +       return err;
10367 +}
10368 +
10369 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10370 +                           struct file *file)
10371 +{
10372 +       struct au_vdir *vdir_cache;
10373 +       struct au_finfo *finfo;
10374 +       struct au_fidir *fidir;
10375 +       struct au_hfile *hf;
10376 +       aufs_bindex_t bindex, bbot;
10377 +
10378 +       finfo = au_fi(file);
10379 +       fidir = finfo->fi_hdir;
10380 +       if (fidir) {
10381 +               au_hbl_del(&finfo->fi_hlist,
10382 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10383 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10384 +               if (vdir_cache)
10385 +                       au_vdir_free(vdir_cache);
10386 +
10387 +               bindex = finfo->fi_btop;
10388 +               if (bindex >= 0) {
10389 +                       hf = fidir->fd_hfile + bindex;
10390 +                       /*
10391 +                        * calls fput() instead of filp_close(),
10392 +                        * since no dnotify or lock for the lower file.
10393 +                        */
10394 +                       bbot = fidir->fd_bbot;
10395 +                       for (; bindex <= bbot; bindex++, hf++)
10396 +                               if (hf->hf_file)
10397 +                                       au_hfput(hf, /*execed*/0);
10398 +               }
10399 +               au_kfree_rcu(fidir);
10400 +               finfo->fi_hdir = NULL;
10401 +       }
10402 +       au_finfo_fin(file);
10403 +       return 0;
10404 +}
10405 +
10406 +/* ---------------------------------------------------------------------- */
10407 +
10408 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10409 +{
10410 +       int err;
10411 +       aufs_bindex_t bindex, bbot;
10412 +       struct file *h_file;
10413 +
10414 +       err = 0;
10415 +       bbot = au_fbbot_dir(file);
10416 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10417 +               h_file = au_hf_dir(file, bindex);
10418 +               if (h_file)
10419 +                       err = vfsub_flush(h_file, id);
10420 +       }
10421 +       return err;
10422 +}
10423 +
10424 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10425 +{
10426 +       return au_do_flush(file, id, au_do_flush_dir);
10427 +}
10428 +
10429 +/* ---------------------------------------------------------------------- */
10430 +
10431 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10432 +{
10433 +       int err;
10434 +       aufs_bindex_t bbot, bindex;
10435 +       struct inode *inode;
10436 +       struct super_block *sb;
10437 +
10438 +       err = 0;
10439 +       sb = dentry->d_sb;
10440 +       inode = d_inode(dentry);
10441 +       IMustLock(inode);
10442 +       bbot = au_dbbot(dentry);
10443 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10444 +               struct path h_path;
10445 +
10446 +               if (au_test_ro(sb, bindex, inode))
10447 +                       continue;
10448 +               h_path.dentry = au_h_dptr(dentry, bindex);
10449 +               if (!h_path.dentry)
10450 +                       continue;
10451 +
10452 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10453 +               err = vfsub_fsync(NULL, &h_path, datasync);
10454 +       }
10455 +
10456 +       return err;
10457 +}
10458 +
10459 +static int au_do_fsync_dir(struct file *file, int datasync)
10460 +{
10461 +       int err;
10462 +       aufs_bindex_t bbot, bindex;
10463 +       struct file *h_file;
10464 +       struct super_block *sb;
10465 +       struct inode *inode;
10466 +
10467 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10468 +       if (unlikely(err))
10469 +               goto out;
10470 +
10471 +       inode = file_inode(file);
10472 +       sb = inode->i_sb;
10473 +       bbot = au_fbbot_dir(file);
10474 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10475 +               h_file = au_hf_dir(file, bindex);
10476 +               if (!h_file || au_test_ro(sb, bindex, inode))
10477 +                       continue;
10478 +
10479 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10480 +       }
10481 +
10482 +out:
10483 +       return err;
10484 +}
10485 +
10486 +/*
10487 + * @file may be NULL
10488 + */
10489 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10490 +                         int datasync)
10491 +{
10492 +       int err;
10493 +       struct dentry *dentry;
10494 +       struct inode *inode;
10495 +       struct super_block *sb;
10496 +
10497 +       err = 0;
10498 +       dentry = file->f_path.dentry;
10499 +       inode = d_inode(dentry);
10500 +       inode_lock(inode);
10501 +       sb = dentry->d_sb;
10502 +       si_noflush_read_lock(sb);
10503 +       if (file)
10504 +               err = au_do_fsync_dir(file, datasync);
10505 +       else {
10506 +               di_write_lock_child(dentry);
10507 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10508 +       }
10509 +       au_cpup_attr_timesizes(inode);
10510 +       di_write_unlock(dentry);
10511 +       if (file)
10512 +               fi_write_unlock(file);
10513 +
10514 +       si_read_unlock(sb);
10515 +       inode_unlock(inode);
10516 +       return err;
10517 +}
10518 +
10519 +/* ---------------------------------------------------------------------- */
10520 +
10521 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10522 +{
10523 +       int err;
10524 +       struct dentry *dentry;
10525 +       struct inode *inode, *h_inode;
10526 +       struct super_block *sb;
10527 +
10528 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10529 +
10530 +       dentry = file->f_path.dentry;
10531 +       inode = d_inode(dentry);
10532 +       IMustLock(inode);
10533 +
10534 +       sb = dentry->d_sb;
10535 +       si_read_lock(sb, AuLock_FLUSH);
10536 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10537 +       if (unlikely(err))
10538 +               goto out;
10539 +       err = au_alive_dir(dentry);
10540 +       if (!err)
10541 +               err = au_vdir_init(file);
10542 +       di_downgrade_lock(dentry, AuLock_IR);
10543 +       if (unlikely(err))
10544 +               goto out_unlock;
10545 +
10546 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10547 +       if (!au_test_nfsd()) {
10548 +               err = au_vdir_fill_de(file, ctx);
10549 +               fsstack_copy_attr_atime(inode, h_inode);
10550 +       } else {
10551 +               /*
10552 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10553 +                * encode_fh() and others.
10554 +                */
10555 +               atomic_inc(&h_inode->i_count);
10556 +               di_read_unlock(dentry, AuLock_IR);
10557 +               si_read_unlock(sb);
10558 +               err = au_vdir_fill_de(file, ctx);
10559 +               fsstack_copy_attr_atime(inode, h_inode);
10560 +               fi_write_unlock(file);
10561 +               iput(h_inode);
10562 +
10563 +               AuTraceErr(err);
10564 +               return err;
10565 +       }
10566 +
10567 +out_unlock:
10568 +       di_read_unlock(dentry, AuLock_IR);
10569 +       fi_write_unlock(file);
10570 +out:
10571 +       si_read_unlock(sb);
10572 +       return err;
10573 +}
10574 +
10575 +/* ---------------------------------------------------------------------- */
10576 +
10577 +#define AuTestEmpty_WHONLY     1
10578 +#define AuTestEmpty_CALLED     (1 << 1)
10579 +#define AuTestEmpty_SHWH       (1 << 2)
10580 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10581 +#define au_fset_testempty(flags, name) \
10582 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10583 +#define au_fclr_testempty(flags, name) \
10584 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10585 +
10586 +#ifndef CONFIG_AUFS_SHWH
10587 +#undef AuTestEmpty_SHWH
10588 +#define AuTestEmpty_SHWH       0
10589 +#endif
10590 +
10591 +struct test_empty_arg {
10592 +       struct dir_context ctx;
10593 +       struct au_nhash *whlist;
10594 +       unsigned int flags;
10595 +       int err;
10596 +       aufs_bindex_t bindex;
10597 +};
10598 +
10599 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10600 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10601 +                        unsigned int d_type)
10602 +{
10603 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10604 +                                                 ctx);
10605 +       char *name = (void *)__name;
10606 +
10607 +       arg->err = 0;
10608 +       au_fset_testempty(arg->flags, CALLED);
10609 +       /* smp_mb(); */
10610 +       if (name[0] == '.'
10611 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10612 +               goto out; /* success */
10613 +
10614 +       if (namelen <= AUFS_WH_PFX_LEN
10615 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10616 +               if (au_ftest_testempty(arg->flags, WHONLY)
10617 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10618 +                       arg->err = -ENOTEMPTY;
10619 +               goto out;
10620 +       }
10621 +
10622 +       name += AUFS_WH_PFX_LEN;
10623 +       namelen -= AUFS_WH_PFX_LEN;
10624 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10625 +               arg->err = au_nhash_append_wh
10626 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10627 +                        au_ftest_testempty(arg->flags, SHWH));
10628 +
10629 +out:
10630 +       /* smp_mb(); */
10631 +       AuTraceErr(arg->err);
10632 +       return arg->err;
10633 +}
10634 +
10635 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10636 +{
10637 +       int err;
10638 +       struct file *h_file;
10639 +       struct au_branch *br;
10640 +
10641 +       h_file = au_h_open(dentry, arg->bindex,
10642 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10643 +                          /*file*/NULL, /*force_wr*/0);
10644 +       err = PTR_ERR(h_file);
10645 +       if (IS_ERR(h_file))
10646 +               goto out;
10647 +
10648 +       err = 0;
10649 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10650 +           && !file_inode(h_file)->i_nlink)
10651 +               goto out_put;
10652 +
10653 +       do {
10654 +               arg->err = 0;
10655 +               au_fclr_testempty(arg->flags, CALLED);
10656 +               /* smp_mb(); */
10657 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10658 +               if (err >= 0)
10659 +                       err = arg->err;
10660 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10661 +
10662 +out_put:
10663 +       fput(h_file);
10664 +       br = au_sbr(dentry->d_sb, arg->bindex);
10665 +       au_lcnt_dec(&br->br_nfiles);
10666 +out:
10667 +       return err;
10668 +}
10669 +
10670 +struct do_test_empty_args {
10671 +       int *errp;
10672 +       struct dentry *dentry;
10673 +       struct test_empty_arg *arg;
10674 +};
10675 +
10676 +static void call_do_test_empty(void *args)
10677 +{
10678 +       struct do_test_empty_args *a = args;
10679 +       *a->errp = do_test_empty(a->dentry, a->arg);
10680 +}
10681 +
10682 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10683 +{
10684 +       int err, wkq_err;
10685 +       struct dentry *h_dentry;
10686 +       struct inode *h_inode;
10687 +
10688 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10689 +       h_inode = d_inode(h_dentry);
10690 +       /* todo: i_mode changes anytime? */
10691 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10692 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10693 +       inode_unlock_shared(h_inode);
10694 +       if (!err)
10695 +               err = do_test_empty(dentry, arg);
10696 +       else {
10697 +               struct do_test_empty_args args = {
10698 +                       .errp   = &err,
10699 +                       .dentry = dentry,
10700 +                       .arg    = arg
10701 +               };
10702 +               unsigned int flags = arg->flags;
10703 +
10704 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10705 +               if (unlikely(wkq_err))
10706 +                       err = wkq_err;
10707 +               arg->flags = flags;
10708 +       }
10709 +
10710 +       return err;
10711 +}
10712 +
10713 +int au_test_empty_lower(struct dentry *dentry)
10714 +{
10715 +       int err;
10716 +       unsigned int rdhash;
10717 +       aufs_bindex_t bindex, btop, btail;
10718 +       struct au_nhash whlist;
10719 +       struct test_empty_arg arg = {
10720 +               .ctx = {
10721 +                       .actor = test_empty_cb
10722 +               }
10723 +       };
10724 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10725 +
10726 +       SiMustAnyLock(dentry->d_sb);
10727 +
10728 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10729 +       if (!rdhash)
10730 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10731 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10732 +       if (unlikely(err))
10733 +               goto out;
10734 +
10735 +       arg.flags = 0;
10736 +       arg.whlist = &whlist;
10737 +       btop = au_dbtop(dentry);
10738 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10739 +               au_fset_testempty(arg.flags, SHWH);
10740 +       test_empty = do_test_empty;
10741 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10742 +               test_empty = sio_test_empty;
10743 +       arg.bindex = btop;
10744 +       err = test_empty(dentry, &arg);
10745 +       if (unlikely(err))
10746 +               goto out_whlist;
10747 +
10748 +       au_fset_testempty(arg.flags, WHONLY);
10749 +       btail = au_dbtaildir(dentry);
10750 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10751 +               struct dentry *h_dentry;
10752 +
10753 +               h_dentry = au_h_dptr(dentry, bindex);
10754 +               if (h_dentry && d_is_positive(h_dentry)) {
10755 +                       arg.bindex = bindex;
10756 +                       err = test_empty(dentry, &arg);
10757 +               }
10758 +       }
10759 +
10760 +out_whlist:
10761 +       au_nhash_wh_free(&whlist);
10762 +out:
10763 +       return err;
10764 +}
10765 +
10766 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10767 +{
10768 +       int err;
10769 +       struct test_empty_arg arg = {
10770 +               .ctx = {
10771 +                       .actor = test_empty_cb
10772 +               }
10773 +       };
10774 +       aufs_bindex_t bindex, btail;
10775 +
10776 +       err = 0;
10777 +       arg.whlist = whlist;
10778 +       arg.flags = AuTestEmpty_WHONLY;
10779 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10780 +               au_fset_testempty(arg.flags, SHWH);
10781 +       btail = au_dbtaildir(dentry);
10782 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10783 +               struct dentry *h_dentry;
10784 +
10785 +               h_dentry = au_h_dptr(dentry, bindex);
10786 +               if (h_dentry && d_is_positive(h_dentry)) {
10787 +                       arg.bindex = bindex;
10788 +                       err = sio_test_empty(dentry, &arg);
10789 +               }
10790 +       }
10791 +
10792 +       return err;
10793 +}
10794 +
10795 +/* ---------------------------------------------------------------------- */
10796 +
10797 +const struct file_operations aufs_dir_fop = {
10798 +       .owner          = THIS_MODULE,
10799 +       .llseek         = default_llseek,
10800 +       .read           = generic_read_dir,
10801 +       .iterate_shared = aufs_iterate_shared,
10802 +       .unlocked_ioctl = aufs_ioctl_dir,
10803 +#ifdef CONFIG_COMPAT
10804 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10805 +#endif
10806 +       .open           = aufs_open_dir,
10807 +       .release        = aufs_release_dir,
10808 +       .flush          = aufs_flush_dir,
10809 +       .fsync          = aufs_fsync_dir
10810 +};
10811 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10812 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10813 +++ linux/fs/aufs/dir.h 2019-07-11 15:42:14.462237786 +0200
10814 @@ -0,0 +1,134 @@
10815 +/* SPDX-License-Identifier: GPL-2.0 */
10816 +/*
10817 + * Copyright (C) 2005-2019 Junjiro R. Okajima
10818 + *
10819 + * This program, aufs is free software; you can redistribute it and/or modify
10820 + * it under the terms of the GNU General Public License as published by
10821 + * the Free Software Foundation; either version 2 of the License, or
10822 + * (at your option) any later version.
10823 + *
10824 + * This program is distributed in the hope that it will be useful,
10825 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10826 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10827 + * GNU General Public License for more details.
10828 + *
10829 + * You should have received a copy of the GNU General Public License
10830 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10831 + */
10832 +
10833 +/*
10834 + * directory operations
10835 + */
10836 +
10837 +#ifndef __AUFS_DIR_H__
10838 +#define __AUFS_DIR_H__
10839 +
10840 +#ifdef __KERNEL__
10841 +
10842 +#include <linux/fs.h>
10843 +
10844 +/* ---------------------------------------------------------------------- */
10845 +
10846 +/* need to be faster and smaller */
10847 +
10848 +struct au_nhash {
10849 +       unsigned int            nh_num;
10850 +       struct hlist_head       *nh_head;
10851 +};
10852 +
10853 +struct au_vdir_destr {
10854 +       unsigned char   len;
10855 +       unsigned char   name[0];
10856 +} __packed;
10857 +
10858 +struct au_vdir_dehstr {
10859 +       struct hlist_node       hash;
10860 +       struct au_vdir_destr    *str;
10861 +       struct rcu_head         rcu;
10862 +} ____cacheline_aligned_in_smp;
10863 +
10864 +struct au_vdir_de {
10865 +       ino_t                   de_ino;
10866 +       unsigned char           de_type;
10867 +       /* caution: packed */
10868 +       struct au_vdir_destr    de_str;
10869 +} __packed;
10870 +
10871 +struct au_vdir_wh {
10872 +       struct hlist_node       wh_hash;
10873 +#ifdef CONFIG_AUFS_SHWH
10874 +       ino_t                   wh_ino;
10875 +       aufs_bindex_t           wh_bindex;
10876 +       unsigned char           wh_type;
10877 +#else
10878 +       aufs_bindex_t           wh_bindex;
10879 +#endif
10880 +       /* caution: packed */
10881 +       struct au_vdir_destr    wh_str;
10882 +} __packed;
10883 +
10884 +union au_vdir_deblk_p {
10885 +       unsigned char           *deblk;
10886 +       struct au_vdir_de       *de;
10887 +};
10888 +
10889 +struct au_vdir {
10890 +       unsigned char   **vd_deblk;
10891 +       unsigned long   vd_nblk;
10892 +       struct {
10893 +               unsigned long           ul;
10894 +               union au_vdir_deblk_p   p;
10895 +       } vd_last;
10896 +
10897 +       u64             vd_version;
10898 +       unsigned int    vd_deblk_sz;
10899 +       unsigned long   vd_jiffy;
10900 +       struct rcu_head rcu;
10901 +} ____cacheline_aligned_in_smp;
10902 +
10903 +/* ---------------------------------------------------------------------- */
10904 +
10905 +/* dir.c */
10906 +extern const struct file_operations aufs_dir_fop;
10907 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10908 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10909 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10910 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10911 +int au_test_empty_lower(struct dentry *dentry);
10912 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10913 +
10914 +/* vdir.c */
10915 +unsigned int au_rdhash_est(loff_t sz);
10916 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10917 +void au_nhash_wh_free(struct au_nhash *whlist);
10918 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10919 +                           int limit);
10920 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10921 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10922 +                      unsigned int d_type, aufs_bindex_t bindex,
10923 +                      unsigned char shwh);
10924 +void au_vdir_free(struct au_vdir *vdir);
10925 +int au_vdir_init(struct file *file);
10926 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10927 +
10928 +/* ioctl.c */
10929 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10930 +
10931 +#ifdef CONFIG_AUFS_RDU
10932 +/* rdu.c */
10933 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10934 +#ifdef CONFIG_COMPAT
10935 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10936 +                        unsigned long arg);
10937 +#endif
10938 +#else
10939 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10940 +       unsigned int cmd, unsigned long arg)
10941 +#ifdef CONFIG_COMPAT
10942 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10943 +       unsigned int cmd, unsigned long arg)
10944 +#endif
10945 +#endif
10946 +
10947 +#endif /* __KERNEL__ */
10948 +#endif /* __AUFS_DIR_H__ */
10949 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10950 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10951 +++ linux/fs/aufs/dirren.c      2019-07-11 15:42:14.462237786 +0200
10952 @@ -0,0 +1,1316 @@
10953 +// SPDX-License-Identifier: GPL-2.0
10954 +/*
10955 + * Copyright (C) 2017-2019 Junjiro R. Okajima
10956 + *
10957 + * This program, aufs is free software; you can redistribute it and/or modify
10958 + * it under the terms of the GNU General Public License as published by
10959 + * the Free Software Foundation; either version 2 of the License, or
10960 + * (at your option) any later version.
10961 + *
10962 + * This program is distributed in the hope that it will be useful,
10963 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10964 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10965 + * GNU General Public License for more details.
10966 + *
10967 + * You should have received a copy of the GNU General Public License
10968 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10969 + */
10970 +
10971 +/*
10972 + * special handling in renaming a directory
10973 + * in order to support looking-up the before-renamed name on the lower readonly
10974 + * branches
10975 + */
10976 +
10977 +#include <linux/byteorder/generic.h>
10978 +#include "aufs.h"
10979 +
10980 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10981 +{
10982 +       int idx;
10983 +
10984 +       idx = au_dr_ihash(ent->dr_h_ino);
10985 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10986 +}
10987 +
10988 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10989 +{
10990 +       int ret, i;
10991 +       struct hlist_bl_head *hbl;
10992 +
10993 +       ret = 1;
10994 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10995 +               hbl = dr->dr_h_ino + i;
10996 +               hlist_bl_lock(hbl);
10997 +               ret &= hlist_bl_empty(hbl);
10998 +               hlist_bl_unlock(hbl);
10999 +       }
11000 +
11001 +       return ret;
11002 +}
11003 +
11004 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11005 +{
11006 +       struct au_dr_hino *found, *ent;
11007 +       struct hlist_bl_head *hbl;
11008 +       struct hlist_bl_node *pos;
11009 +       int idx;
11010 +
11011 +       found = NULL;
11012 +       idx = au_dr_ihash(ino);
11013 +       hbl = dr->dr_h_ino + idx;
11014 +       hlist_bl_lock(hbl);
11015 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11016 +               if (ent->dr_h_ino == ino) {
11017 +                       found = ent;
11018 +                       break;
11019 +               }
11020 +       hlist_bl_unlock(hbl);
11021 +
11022 +       return found;
11023 +}
11024 +
11025 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11026 +                       struct au_dr_hino *add_ent)
11027 +{
11028 +       int found, idx;
11029 +       struct hlist_bl_head *hbl;
11030 +       struct hlist_bl_node *pos;
11031 +       struct au_dr_hino *ent;
11032 +
11033 +       found = 0;
11034 +       idx = au_dr_ihash(ino);
11035 +       hbl = dr->dr_h_ino + idx;
11036 +#if 0
11037 +       {
11038 +               struct hlist_bl_node *tmp;
11039 +
11040 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11041 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11042 +       }
11043 +#endif
11044 +       hlist_bl_lock(hbl);
11045 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11046 +               if (ent->dr_h_ino == ino) {
11047 +                       found = 1;
11048 +                       break;
11049 +               }
11050 +       if (!found && add_ent)
11051 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11052 +       hlist_bl_unlock(hbl);
11053 +
11054 +       if (!found && add_ent)
11055 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11056 +
11057 +       return found;
11058 +}
11059 +
11060 +void au_dr_hino_free(struct au_dr_br *dr)
11061 +{
11062 +       int i;
11063 +       struct hlist_bl_head *hbl;
11064 +       struct hlist_bl_node *pos, *tmp;
11065 +       struct au_dr_hino *ent;
11066 +
11067 +       /* SiMustWriteLock(sb); */
11068 +
11069 +       for (i = 0; i < AuDirren_NHASH; i++) {
11070 +               hbl = dr->dr_h_ino + i;
11071 +               /* no spinlock since sbinfo must be write-locked */
11072 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11073 +                       au_kfree_rcu(ent);
11074 +               INIT_HLIST_BL_HEAD(hbl);
11075 +       }
11076 +}
11077 +
11078 +/* returns the number of inodes or an error */
11079 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11080 +                           struct file *hinofile)
11081 +{
11082 +       int err, i;
11083 +       ssize_t ssz;
11084 +       loff_t pos, oldsize;
11085 +       __be64 u64;
11086 +       struct inode *hinoinode;
11087 +       struct hlist_bl_head *hbl;
11088 +       struct hlist_bl_node *n1, *n2;
11089 +       struct au_dr_hino *ent;
11090 +
11091 +       SiMustWriteLock(sb);
11092 +       AuDebugOn(!au_br_writable(br->br_perm));
11093 +
11094 +       hinoinode = file_inode(hinofile);
11095 +       oldsize = i_size_read(hinoinode);
11096 +
11097 +       err = 0;
11098 +       pos = 0;
11099 +       hbl = br->br_dirren.dr_h_ino;
11100 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11101 +               /* no bit-lock since sbinfo must be write-locked */
11102 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11103 +                       AuDbg("hi%llu, %pD2\n",
11104 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11105 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11106 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11107 +                       if (ssz == sizeof(u64))
11108 +                               continue;
11109 +
11110 +                       /* write error */
11111 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11112 +                       err = -ENOSPC;
11113 +                       if (ssz < 0)
11114 +                               err = ssz;
11115 +                       break;
11116 +               }
11117 +       }
11118 +       /* regardless the error */
11119 +       if (pos < oldsize) {
11120 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11121 +               AuTraceErr(err);
11122 +       }
11123 +
11124 +       AuTraceErr(err);
11125 +       return err;
11126 +}
11127 +
11128 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11129 +{
11130 +       int err, hidx;
11131 +       ssize_t ssz;
11132 +       size_t sz, n;
11133 +       loff_t pos;
11134 +       uint64_t u64;
11135 +       struct au_dr_hino *ent;
11136 +       struct inode *hinoinode;
11137 +       struct hlist_bl_head *hbl;
11138 +
11139 +       err = 0;
11140 +       pos = 0;
11141 +       hbl = dr->dr_h_ino;
11142 +       hinoinode = file_inode(hinofile);
11143 +       sz = i_size_read(hinoinode);
11144 +       AuDebugOn(sz % sizeof(u64));
11145 +       n = sz / sizeof(u64);
11146 +       while (n--) {
11147 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11148 +               if (unlikely(ssz != sizeof(u64))) {
11149 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11150 +                       err = -EINVAL;
11151 +                       if (ssz < 0)
11152 +                               err = ssz;
11153 +                       goto out_free;
11154 +               }
11155 +
11156 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11157 +               if (!ent) {
11158 +                       err = -ENOMEM;
11159 +                       AuTraceErr(err);
11160 +                       goto out_free;
11161 +               }
11162 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11163 +               AuDbg("hi%llu, %pD2\n",
11164 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11165 +               hidx = au_dr_ihash(ent->dr_h_ino);
11166 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11167 +       }
11168 +       goto out; /* success */
11169 +
11170 +out_free:
11171 +       au_dr_hino_free(dr);
11172 +out:
11173 +       AuTraceErr(err);
11174 +       return err;
11175 +}
11176 +
11177 +/*
11178 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11179 + * @path is a switch to distinguish load and store.
11180 + */
11181 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11182 +                     struct au_branch *br, const struct path *path)
11183 +{
11184 +       int err, flags;
11185 +       unsigned char load, suspend;
11186 +       struct file *hinofile;
11187 +       struct au_hinode *hdir;
11188 +       struct inode *dir, *delegated;
11189 +       struct path hinopath;
11190 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11191 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11192 +
11193 +       AuDebugOn(bindex < 0 && !br);
11194 +       AuDebugOn(bindex >= 0 && br);
11195 +
11196 +       err = -EINVAL;
11197 +       suspend = !br;
11198 +       if (suspend)
11199 +               br = au_sbr(sb, bindex);
11200 +       load = !!path;
11201 +       if (!load) {
11202 +               path = &br->br_path;
11203 +               AuDebugOn(!au_br_writable(br->br_perm));
11204 +               if (unlikely(!au_br_writable(br->br_perm)))
11205 +                       goto out;
11206 +       }
11207 +
11208 +       hdir = NULL;
11209 +       if (suspend) {
11210 +               dir = d_inode(sb->s_root);
11211 +               hdir = au_hinode(au_ii(dir), bindex);
11212 +               dir = hdir->hi_inode;
11213 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11214 +       } else {
11215 +               dir = d_inode(path->dentry);
11216 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11217 +       }
11218 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11219 +       err = PTR_ERR(hinopath.dentry);
11220 +       if (IS_ERR(hinopath.dentry))
11221 +               goto out_unlock;
11222 +
11223 +       err = 0;
11224 +       flags = O_RDONLY;
11225 +       if (load) {
11226 +               if (d_is_negative(hinopath.dentry))
11227 +                       goto out_dput; /* success */
11228 +       } else {
11229 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11230 +                       if (d_is_positive(hinopath.dentry)) {
11231 +                               delegated = NULL;
11232 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11233 +                                                  /*force*/0);
11234 +                               AuTraceErr(err);
11235 +                               if (unlikely(err))
11236 +                                       pr_err("ignored err %d, %pd2\n",
11237 +                                              err, hinopath.dentry);
11238 +                               if (unlikely(err == -EWOULDBLOCK))
11239 +                                       iput(delegated);
11240 +                               err = 0;
11241 +                       }
11242 +                       goto out_dput;
11243 +               } else if (!d_is_positive(hinopath.dentry)) {
11244 +                       err = vfsub_create(dir, &hinopath, 0600,
11245 +                                          /*want_excl*/false);
11246 +                       AuTraceErr(err);
11247 +                       if (unlikely(err))
11248 +                               goto out_dput;
11249 +               }
11250 +               flags = O_WRONLY;
11251 +       }
11252 +       hinopath.mnt = path->mnt;
11253 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11254 +       if (suspend)
11255 +               au_hn_inode_unlock(hdir);
11256 +       else
11257 +               inode_unlock(dir);
11258 +       dput(hinopath.dentry);
11259 +       AuTraceErrPtr(hinofile);
11260 +       if (IS_ERR(hinofile)) {
11261 +               err = PTR_ERR(hinofile);
11262 +               goto out;
11263 +       }
11264 +
11265 +       if (load)
11266 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11267 +       else
11268 +               err = au_dr_hino_store(sb, br, hinofile);
11269 +       fput(hinofile);
11270 +       goto out;
11271 +
11272 +out_dput:
11273 +       dput(hinopath.dentry);
11274 +out_unlock:
11275 +       if (suspend)
11276 +               au_hn_inode_unlock(hdir);
11277 +       else
11278 +               inode_unlock(dir);
11279 +out:
11280 +       AuTraceErr(err);
11281 +       return err;
11282 +}
11283 +
11284 +/* ---------------------------------------------------------------------- */
11285 +
11286 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11287 +{
11288 +       int err;
11289 +       struct kstatfs kstfs;
11290 +       dev_t dev;
11291 +       struct dentry *dentry;
11292 +       struct super_block *sb;
11293 +
11294 +       err = vfs_statfs((void *)path, &kstfs);
11295 +       AuTraceErr(err);
11296 +       if (unlikely(err))
11297 +               goto out;
11298 +
11299 +       /* todo: support for UUID */
11300 +
11301 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11302 +               brid->type = AuBrid_FSID;
11303 +               brid->fsid = kstfs.f_fsid;
11304 +       } else {
11305 +               dentry = path->dentry;
11306 +               sb = dentry->d_sb;
11307 +               dev = sb->s_dev;
11308 +               if (dev) {
11309 +                       brid->type = AuBrid_DEV;
11310 +                       brid->dev = dev;
11311 +               }
11312 +       }
11313 +
11314 +out:
11315 +       return err;
11316 +}
11317 +
11318 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11319 +                 const struct path *path)
11320 +{
11321 +       int err, i;
11322 +       struct au_dr_br *dr;
11323 +       struct hlist_bl_head *hbl;
11324 +
11325 +       dr = &br->br_dirren;
11326 +       hbl = dr->dr_h_ino;
11327 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11328 +               INIT_HLIST_BL_HEAD(hbl);
11329 +
11330 +       err = au_dr_brid_init(&dr->dr_brid, path);
11331 +       if (unlikely(err))
11332 +               goto out;
11333 +
11334 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11335 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11336 +
11337 +out:
11338 +       AuTraceErr(err);
11339 +       return err;
11340 +}
11341 +
11342 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11343 +{
11344 +       int err;
11345 +
11346 +       err = 0;
11347 +       if (au_br_writable(br->br_perm))
11348 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11349 +       if (!err)
11350 +               au_dr_hino_free(&br->br_dirren);
11351 +
11352 +       return err;
11353 +}
11354 +
11355 +/* ---------------------------------------------------------------------- */
11356 +
11357 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11358 +                      char *buf, size_t sz)
11359 +{
11360 +       int err;
11361 +       unsigned int major, minor;
11362 +       char *p;
11363 +
11364 +       p = buf;
11365 +       err = snprintf(p, sz, "%d_", brid->type);
11366 +       AuDebugOn(err > sz);
11367 +       p += err;
11368 +       sz -= err;
11369 +       switch (brid->type) {
11370 +       case AuBrid_Unset:
11371 +               return -EINVAL;
11372 +       case AuBrid_UUID:
11373 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11374 +               break;
11375 +       case AuBrid_FSID:
11376 +               err = snprintf(p, sz, "%08x-%08x",
11377 +                              brid->fsid.val[0], brid->fsid.val[1]);
11378 +               break;
11379 +       case AuBrid_DEV:
11380 +               major = MAJOR(brid->dev);
11381 +               minor = MINOR(brid->dev);
11382 +               if (major <= 0xff && minor <= 0xff)
11383 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11384 +               else
11385 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11386 +               break;
11387 +       }
11388 +       AuDebugOn(err > sz);
11389 +       p += err;
11390 +       sz -= err;
11391 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11392 +       AuDebugOn(err > sz);
11393 +       p += err;
11394 +       sz -= err;
11395 +
11396 +       return p - buf;
11397 +}
11398 +
11399 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11400 +{
11401 +       int rlen;
11402 +       struct dentry *br_dentry;
11403 +       struct inode *br_inode;
11404 +
11405 +       br_dentry = au_br_dentry(br);
11406 +       br_inode = d_inode(br_dentry);
11407 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11408 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11409 +       AuDebugOn(rlen > len);
11410 +
11411 +       return rlen;
11412 +}
11413 +
11414 +/* ---------------------------------------------------------------------- */
11415 +
11416 +/*
11417 + * from the given @h_dentry, construct drinfo at @*fdata.
11418 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11419 + * @allocated.
11420 + */
11421 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11422 +                              struct dentry *h_dentry,
11423 +                              unsigned char *allocated)
11424 +{
11425 +       int err, v;
11426 +       struct au_drinfo_fdata *f, *p;
11427 +       struct au_drinfo *drinfo;
11428 +       struct inode *h_inode;
11429 +       struct qstr *qname;
11430 +
11431 +       err = 0;
11432 +       f = *fdata;
11433 +       h_inode = d_inode(h_dentry);
11434 +       qname = &h_dentry->d_name;
11435 +       drinfo = &f->drinfo;
11436 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11437 +       drinfo->oldnamelen = qname->len;
11438 +       if (*allocated < sizeof(*f) + qname->len) {
11439 +               v = roundup_pow_of_two(*allocated + qname->len);
11440 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11441 +               if (unlikely(!p)) {
11442 +                       err = -ENOMEM;
11443 +                       AuTraceErr(err);
11444 +                       goto out;
11445 +               }
11446 +               f = p;
11447 +               *fdata = f;
11448 +               *allocated = v;
11449 +               drinfo = &f->drinfo;
11450 +       }
11451 +       memcpy(drinfo->oldname, qname->name, qname->len);
11452 +       AuDbg("i%llu, %.*s\n",
11453 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11454 +             drinfo->oldname);
11455 +
11456 +out:
11457 +       AuTraceErr(err);
11458 +       return err;
11459 +}
11460 +
11461 +/* callers have to free the return value */
11462 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11463 +{
11464 +       struct au_drinfo *ret, *drinfo;
11465 +       struct au_drinfo_fdata fdata;
11466 +       int len;
11467 +       loff_t pos;
11468 +       ssize_t ssz;
11469 +
11470 +       ret = ERR_PTR(-EIO);
11471 +       pos = 0;
11472 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11473 +       if (unlikely(ssz != sizeof(fdata))) {
11474 +               AuIOErr("ssz %zd, %u, %pD2\n",
11475 +                       ssz, (unsigned int)sizeof(fdata), file);
11476 +               goto out;
11477 +       }
11478 +
11479 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11480 +       switch (fdata.magic) {
11481 +       case AUFS_DRINFO_MAGIC_V1:
11482 +               break;
11483 +       default:
11484 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11485 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11486 +               goto out;
11487 +       }
11488 +
11489 +       drinfo = &fdata.drinfo;
11490 +       len = drinfo->oldnamelen;
11491 +       if (!len) {
11492 +               AuIOErr("broken drinfo %pD2\n", file);
11493 +               goto out;
11494 +       }
11495 +
11496 +       ret = NULL;
11497 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11498 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11499 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11500 +                     (unsigned long long)drinfo->ino,
11501 +                     (unsigned long long)h_ino, file);
11502 +               goto out; /* success */
11503 +       }
11504 +
11505 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11506 +       if (unlikely(!ret)) {
11507 +               ret = ERR_PTR(-ENOMEM);
11508 +               AuTraceErrPtr(ret);
11509 +               goto out;
11510 +       }
11511 +
11512 +       *ret = *drinfo;
11513 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11514 +       if (unlikely(ssz != len)) {
11515 +               au_kfree_rcu(ret);
11516 +               ret = ERR_PTR(-EIO);
11517 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11518 +               goto out;
11519 +       }
11520 +
11521 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11522 +
11523 +out:
11524 +       return ret;
11525 +}
11526 +
11527 +/* ---------------------------------------------------------------------- */
11528 +
11529 +/* in order to be revertible */
11530 +struct au_drinfo_rev_elm {
11531 +       int                     created;
11532 +       struct dentry           *info_dentry;
11533 +       struct au_drinfo        *info_last;
11534 +};
11535 +
11536 +struct au_drinfo_rev {
11537 +       unsigned char                   already;
11538 +       aufs_bindex_t                   nelm;
11539 +       struct au_drinfo_rev_elm        elm[0];
11540 +};
11541 +
11542 +/* todo: isn't it too large? */
11543 +struct au_drinfo_store {
11544 +       struct path h_ppath;
11545 +       struct dentry *h_dentry;
11546 +       struct au_drinfo_fdata *fdata;
11547 +       char *infoname;                 /* inside of whname, just after PFX */
11548 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11549 +       aufs_bindex_t btgt, btail;
11550 +       unsigned char no_sio,
11551 +               allocated,              /* current size of *fdata */
11552 +               infonamelen,            /* room size for p */
11553 +               whnamelen,              /* length of the generated name */
11554 +               renameback;             /* renamed back */
11555 +};
11556 +
11557 +/* on rename(2) error, the caller should revert it using @elm */
11558 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11559 +                             struct au_drinfo_rev_elm *elm)
11560 +{
11561 +       int err, len;
11562 +       ssize_t ssz;
11563 +       loff_t pos;
11564 +       struct path infopath = {
11565 +               .mnt = w->h_ppath.mnt
11566 +       };
11567 +       struct inode *h_dir, *h_inode, *delegated;
11568 +       struct file *infofile;
11569 +       struct qstr *qname;
11570 +
11571 +       AuDebugOn(elm
11572 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11573 +
11574 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11575 +                                              w->whnamelen);
11576 +       AuTraceErrPtr(infopath.dentry);
11577 +       if (IS_ERR(infopath.dentry)) {
11578 +               err = PTR_ERR(infopath.dentry);
11579 +               goto out;
11580 +       }
11581 +
11582 +       err = 0;
11583 +       h_dir = d_inode(w->h_ppath.dentry);
11584 +       if (elm && d_is_negative(infopath.dentry)) {
11585 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11586 +               AuTraceErr(err);
11587 +               if (unlikely(err))
11588 +                       goto out_dput;
11589 +               elm->created = 1;
11590 +               elm->info_dentry = dget(infopath.dentry);
11591 +       }
11592 +
11593 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11594 +       AuTraceErrPtr(infofile);
11595 +       if (IS_ERR(infofile)) {
11596 +               err = PTR_ERR(infofile);
11597 +               goto out_dput;
11598 +       }
11599 +
11600 +       h_inode = d_inode(infopath.dentry);
11601 +       if (elm && i_size_read(h_inode)) {
11602 +               h_inode = d_inode(w->h_dentry);
11603 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11604 +               AuTraceErrPtr(elm->info_last);
11605 +               if (IS_ERR(elm->info_last)) {
11606 +                       err = PTR_ERR(elm->info_last);
11607 +                       elm->info_last = NULL;
11608 +                       AuDebugOn(elm->info_dentry);
11609 +                       goto out_fput;
11610 +               }
11611 +       }
11612 +
11613 +       if (elm && w->renameback) {
11614 +               delegated = NULL;
11615 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11616 +               AuTraceErr(err);
11617 +               if (unlikely(err == -EWOULDBLOCK))
11618 +                       iput(delegated);
11619 +               goto out_fput;
11620 +       }
11621 +
11622 +       pos = 0;
11623 +       qname = &w->h_dentry->d_name;
11624 +       len = sizeof(*w->fdata) + qname->len;
11625 +       if (!elm)
11626 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11627 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11628 +       if (ssz == len) {
11629 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11630 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11631 +               goto out_fput; /* success */
11632 +       } else {
11633 +               err = -EIO;
11634 +               if (ssz < 0)
11635 +                       err = ssz;
11636 +               /* the caller should revert it using @elm */
11637 +       }
11638 +
11639 +out_fput:
11640 +       fput(infofile);
11641 +out_dput:
11642 +       dput(infopath.dentry);
11643 +out:
11644 +       AuTraceErr(err);
11645 +       return err;
11646 +}
11647 +
11648 +struct au_call_drinfo_do_store_args {
11649 +       int *errp;
11650 +       struct au_drinfo_store *w;
11651 +       struct au_drinfo_rev_elm *elm;
11652 +};
11653 +
11654 +static void au_call_drinfo_do_store(void *args)
11655 +{
11656 +       struct au_call_drinfo_do_store_args *a = args;
11657 +
11658 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11659 +}
11660 +
11661 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11662 +                              struct au_drinfo_rev_elm *elm)
11663 +{
11664 +       int err, wkq_err;
11665 +
11666 +       if (w->no_sio)
11667 +               err = au_drinfo_do_store(w, elm);
11668 +       else {
11669 +               struct au_call_drinfo_do_store_args a = {
11670 +                       .errp   = &err,
11671 +                       .w      = w,
11672 +                       .elm    = elm
11673 +               };
11674 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11675 +               if (unlikely(wkq_err))
11676 +                       err = wkq_err;
11677 +       }
11678 +       AuTraceErr(err);
11679 +
11680 +       return err;
11681 +}
11682 +
11683 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11684 +                                    aufs_bindex_t btgt)
11685 +{
11686 +       int err;
11687 +
11688 +       memset(w, 0, sizeof(*w));
11689 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11690 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11691 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11692 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11693 +       w->btgt = btgt;
11694 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11695 +
11696 +       err = -ENOMEM;
11697 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11698 +       if (unlikely(!w->fdata)) {
11699 +               AuTraceErr(err);
11700 +               goto out;
11701 +       }
11702 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11703 +       err = 0;
11704 +
11705 +out:
11706 +       return err;
11707 +}
11708 +
11709 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11710 +{
11711 +       au_kfree_rcu(w->fdata);
11712 +}
11713 +
11714 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11715 +                               struct au_drinfo_store *w)
11716 +{
11717 +       struct au_drinfo_rev_elm *elm;
11718 +       struct inode *h_dir, *delegated;
11719 +       int err, nelm;
11720 +       struct path infopath = {
11721 +               .mnt = w->h_ppath.mnt
11722 +       };
11723 +
11724 +       h_dir = d_inode(w->h_ppath.dentry);
11725 +       IMustLock(h_dir);
11726 +
11727 +       err = 0;
11728 +       elm = rev->elm;
11729 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11730 +               AuDebugOn(elm->created && elm->info_last);
11731 +               if (elm->created) {
11732 +                       AuDbg("here\n");
11733 +                       delegated = NULL;
11734 +                       infopath.dentry = elm->info_dentry;
11735 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11736 +                                          !w->no_sio);
11737 +                       AuTraceErr(err);
11738 +                       if (unlikely(err == -EWOULDBLOCK))
11739 +                               iput(delegated);
11740 +                       dput(elm->info_dentry);
11741 +               } else if (elm->info_last) {
11742 +                       AuDbg("here\n");
11743 +                       w->fdata->drinfo = *elm->info_last;
11744 +                       memcpy(w->fdata->drinfo.oldname,
11745 +                              elm->info_last->oldname,
11746 +                              elm->info_last->oldnamelen);
11747 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11748 +                       au_kfree_rcu(elm->info_last);
11749 +               }
11750 +               if (unlikely(err))
11751 +                       AuIOErr("%d, %s\n", err, w->whname);
11752 +               /* go on even if err */
11753 +       }
11754 +}
11755 +
11756 +/* caller has to call au_dr_rename_fin() later */
11757 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11758 +                          struct qstr *dst_name, void *_rev)
11759 +{
11760 +       int err, sz, nelm;
11761 +       aufs_bindex_t bindex, btail;
11762 +       struct au_drinfo_store work;
11763 +       struct au_drinfo_rev *rev, **p;
11764 +       struct au_drinfo_rev_elm *elm;
11765 +       struct super_block *sb;
11766 +       struct au_branch *br;
11767 +       struct au_hinode *hdir;
11768 +
11769 +       err = au_drinfo_store_work_init(&work, btgt);
11770 +       AuTraceErr(err);
11771 +       if (unlikely(err))
11772 +               goto out;
11773 +
11774 +       err = -ENOMEM;
11775 +       btail = au_dbtaildir(dentry);
11776 +       nelm = btail - btgt;
11777 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11778 +       rev = kcalloc(1, sz, GFP_NOFS);
11779 +       if (unlikely(!rev)) {
11780 +               AuTraceErr(err);
11781 +               goto out_args;
11782 +       }
11783 +       rev->nelm = nelm;
11784 +       elm = rev->elm;
11785 +       p = _rev;
11786 +       *p = rev;
11787 +
11788 +       err = 0;
11789 +       sb = dentry->d_sb;
11790 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11791 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11792 +       hdir = au_hi(d_inode(dentry), btgt);
11793 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11794 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11795 +               work.h_dentry = au_h_dptr(dentry, bindex);
11796 +               if (!work.h_dentry)
11797 +                       continue;
11798 +
11799 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11800 +                                         &work.allocated);
11801 +               AuTraceErr(err);
11802 +               if (unlikely(err))
11803 +                       break;
11804 +
11805 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11806 +               br = au_sbr(sb, bindex);
11807 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11808 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11809 +                                                work.infonamelen);
11810 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11811 +                     work.whnamelen, work.whname,
11812 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11813 +                     work.fdata->drinfo.oldnamelen,
11814 +                     work.fdata->drinfo.oldname);
11815 +
11816 +               err = au_drinfo_store_sio(&work, elm);
11817 +               AuTraceErr(err);
11818 +               if (unlikely(err))
11819 +                       break;
11820 +       }
11821 +       if (unlikely(err)) {
11822 +               /* revert all drinfo */
11823 +               au_drinfo_store_rev(rev, &work);
11824 +               au_kfree_try_rcu(rev);
11825 +               *p = NULL;
11826 +       }
11827 +       au_hn_inode_unlock(hdir);
11828 +
11829 +out_args:
11830 +       au_drinfo_store_work_fin(&work);
11831 +out:
11832 +       return err;
11833 +}
11834 +
11835 +/* ---------------------------------------------------------------------- */
11836 +
11837 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11838 +                struct qstr *dst_name, void *_rev)
11839 +{
11840 +       int err, already;
11841 +       ino_t ino;
11842 +       struct super_block *sb;
11843 +       struct au_branch *br;
11844 +       struct au_dr_br *dr;
11845 +       struct dentry *h_dentry;
11846 +       struct inode *h_inode;
11847 +       struct au_dr_hino *ent;
11848 +       struct au_drinfo_rev *rev, **p;
11849 +
11850 +       AuDbg("bindex %d\n", bindex);
11851 +
11852 +       err = -ENOMEM;
11853 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11854 +       if (unlikely(!ent))
11855 +               goto out;
11856 +
11857 +       sb = src->d_sb;
11858 +       br = au_sbr(sb, bindex);
11859 +       dr = &br->br_dirren;
11860 +       h_dentry = au_h_dptr(src, bindex);
11861 +       h_inode = d_inode(h_dentry);
11862 +       ino = h_inode->i_ino;
11863 +       ent->dr_h_ino = ino;
11864 +       already = au_dr_hino_test_add(dr, ino, ent);
11865 +       AuDbg("b%d, hi%llu, already %d\n",
11866 +             bindex, (unsigned long long)ino, already);
11867 +
11868 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11869 +       AuTraceErr(err);
11870 +       if (!err) {
11871 +               p = _rev;
11872 +               rev = *p;
11873 +               rev->already = already;
11874 +               goto out; /* success */
11875 +       }
11876 +
11877 +       /* revert */
11878 +       if (!already)
11879 +               au_dr_hino_del(dr, ent);
11880 +       au_kfree_rcu(ent);
11881 +
11882 +out:
11883 +       AuTraceErr(err);
11884 +       return err;
11885 +}
11886 +
11887 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11888 +{
11889 +       struct au_drinfo_rev *rev;
11890 +       struct au_drinfo_rev_elm *elm;
11891 +       int nelm;
11892 +
11893 +       rev = _rev;
11894 +       elm = rev->elm;
11895 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11896 +               dput(elm->info_dentry);
11897 +               au_kfree_rcu(elm->info_last);
11898 +       }
11899 +       au_kfree_try_rcu(rev);
11900 +}
11901 +
11902 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11903 +{
11904 +       int err;
11905 +       struct au_drinfo_store work;
11906 +       struct au_drinfo_rev *rev = _rev;
11907 +       struct super_block *sb;
11908 +       struct au_branch *br;
11909 +       struct inode *h_inode;
11910 +       struct au_dr_br *dr;
11911 +       struct au_dr_hino *ent;
11912 +
11913 +       err = au_drinfo_store_work_init(&work, btgt);
11914 +       if (unlikely(err))
11915 +               goto out;
11916 +
11917 +       sb = src->d_sb;
11918 +       br = au_sbr(sb, btgt);
11919 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11920 +       work.h_ppath.mnt = au_br_mnt(br);
11921 +       au_drinfo_store_rev(rev, &work);
11922 +       au_drinfo_store_work_fin(&work);
11923 +       if (rev->already)
11924 +               goto out;
11925 +
11926 +       dr = &br->br_dirren;
11927 +       h_inode = d_inode(work.h_ppath.dentry);
11928 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11929 +       BUG_ON(!ent);
11930 +       au_dr_hino_del(dr, ent);
11931 +       au_kfree_rcu(ent);
11932 +
11933 +out:
11934 +       au_kfree_try_rcu(rev);
11935 +       if (unlikely(err))
11936 +               pr_err("failed to remove dirren info\n");
11937 +}
11938 +
11939 +/* ---------------------------------------------------------------------- */
11940 +
11941 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11942 +                                          char *whname, int whnamelen,
11943 +                                          struct dentry **info_dentry)
11944 +{
11945 +       struct au_drinfo *drinfo;
11946 +       struct file *f;
11947 +       struct inode *h_dir;
11948 +       struct path infopath;
11949 +       int unlocked;
11950 +
11951 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11952 +
11953 +       *info_dentry = NULL;
11954 +       drinfo = NULL;
11955 +       unlocked = 0;
11956 +       h_dir = d_inode(h_ppath->dentry);
11957 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11958 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11959 +                                              whnamelen);
11960 +       if (IS_ERR(infopath.dentry)) {
11961 +               drinfo = (void *)infopath.dentry;
11962 +               goto out;
11963 +       }
11964 +
11965 +       if (d_is_negative(infopath.dentry))
11966 +               goto out_dput; /* success */
11967 +
11968 +       infopath.mnt = h_ppath->mnt;
11969 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11970 +       inode_unlock_shared(h_dir);
11971 +       unlocked = 1;
11972 +       if (IS_ERR(f)) {
11973 +               drinfo = (void *)f;
11974 +               goto out_dput;
11975 +       }
11976 +
11977 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11978 +       if (IS_ERR_OR_NULL(drinfo))
11979 +               goto out_fput;
11980 +
11981 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11982 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11983 +
11984 +out_fput:
11985 +       fput(f);
11986 +out_dput:
11987 +       dput(infopath.dentry);
11988 +out:
11989 +       if (!unlocked)
11990 +               inode_unlock_shared(h_dir);
11991 +       AuTraceErrPtr(drinfo);
11992 +       return drinfo;
11993 +}
11994 +
11995 +struct au_drinfo_do_load_args {
11996 +       struct au_drinfo **drinfop;
11997 +       struct path *h_ppath;
11998 +       char *whname;
11999 +       int whnamelen;
12000 +       struct dentry **info_dentry;
12001 +};
12002 +
12003 +static void au_call_drinfo_do_load(void *args)
12004 +{
12005 +       struct au_drinfo_do_load_args *a = args;
12006 +
12007 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12008 +                                       a->info_dentry);
12009 +}
12010 +
12011 +struct au_drinfo_load {
12012 +       struct path h_ppath;
12013 +       struct qstr *qname;
12014 +       unsigned char no_sio;
12015 +
12016 +       aufs_bindex_t ninfo;
12017 +       struct au_drinfo **drinfo;
12018 +};
12019 +
12020 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12021 +                         struct au_branch *br)
12022 +{
12023 +       int err, wkq_err, whnamelen, e;
12024 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12025 +               = AUFS_WH_DR_INFO_PFX;
12026 +       struct au_drinfo *drinfo;
12027 +       struct qstr oldname;
12028 +       struct inode *h_dir, *delegated;
12029 +       struct dentry *info_dentry;
12030 +       struct path infopath;
12031 +
12032 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12033 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
12034 +                                   sizeof(whname) - whnamelen);
12035 +       if (w->no_sio)
12036 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12037 +                                          &info_dentry);
12038 +       else {
12039 +               struct au_drinfo_do_load_args args = {
12040 +                       .drinfop        = &drinfo,
12041 +                       .h_ppath        = &w->h_ppath,
12042 +                       .whname         = whname,
12043 +                       .whnamelen      = whnamelen,
12044 +                       .info_dentry    = &info_dentry
12045 +               };
12046 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12047 +               if (unlikely(wkq_err))
12048 +                       drinfo = ERR_PTR(wkq_err);
12049 +       }
12050 +       err = PTR_ERR(drinfo);
12051 +       if (IS_ERR_OR_NULL(drinfo))
12052 +               goto out;
12053 +
12054 +       err = 0;
12055 +       oldname.len = drinfo->oldnamelen;
12056 +       oldname.name = drinfo->oldname;
12057 +       if (au_qstreq(w->qname, &oldname)) {
12058 +               /* the name is renamed back */
12059 +               au_kfree_rcu(drinfo);
12060 +               drinfo = NULL;
12061 +
12062 +               infopath.dentry = info_dentry;
12063 +               infopath.mnt = w->h_ppath.mnt;
12064 +               h_dir = d_inode(w->h_ppath.dentry);
12065 +               delegated = NULL;
12066 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12067 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12068 +               inode_unlock(h_dir);
12069 +               if (unlikely(e))
12070 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12071 +               if (unlikely(e == -EWOULDBLOCK))
12072 +                       iput(delegated);
12073 +       }
12074 +       au_kfree_rcu(w->drinfo[bindex]);
12075 +       w->drinfo[bindex] = drinfo;
12076 +       dput(info_dentry);
12077 +
12078 +out:
12079 +       AuTraceErr(err);
12080 +       return err;
12081 +}
12082 +
12083 +/* ---------------------------------------------------------------------- */
12084 +
12085 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12086 +{
12087 +       struct au_drinfo **p = drinfo;
12088 +
12089 +       while (n-- > 0)
12090 +               au_kfree_rcu(*drinfo++);
12091 +       au_kfree_try_rcu(p);
12092 +}
12093 +
12094 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12095 +              aufs_bindex_t btgt)
12096 +{
12097 +       int err, ninfo;
12098 +       struct au_drinfo_load w;
12099 +       aufs_bindex_t bindex, bbot;
12100 +       struct au_branch *br;
12101 +       struct inode *h_dir;
12102 +       struct au_dr_hino *ent;
12103 +       struct super_block *sb;
12104 +
12105 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12106 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12107 +             AuLNPair(&lkup->whname), btgt);
12108 +
12109 +       sb = dentry->d_sb;
12110 +       bbot = au_sbbot(sb);
12111 +       w.ninfo = bbot + 1;
12112 +       if (!lkup->dirren.drinfo) {
12113 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12114 +                                             sizeof(*lkup->dirren.drinfo),
12115 +                                             GFP_NOFS);
12116 +               if (unlikely(!lkup->dirren.drinfo)) {
12117 +                       err = -ENOMEM;
12118 +                       goto out;
12119 +               }
12120 +               lkup->dirren.ninfo = w.ninfo;
12121 +       }
12122 +       w.drinfo = lkup->dirren.drinfo;
12123 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12124 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12125 +       AuDebugOn(!w.h_ppath.dentry);
12126 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12127 +       w.qname = &dentry->d_name;
12128 +
12129 +       ninfo = 0;
12130 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12131 +               br = au_sbr(sb, bindex);
12132 +               err = au_drinfo_load(&w, bindex, br);
12133 +               if (unlikely(err))
12134 +                       goto out_free;
12135 +               if (w.drinfo[bindex])
12136 +                       ninfo++;
12137 +       }
12138 +       if (!ninfo) {
12139 +               br = au_sbr(sb, btgt);
12140 +               h_dir = d_inode(w.h_ppath.dentry);
12141 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12142 +               AuDebugOn(!ent);
12143 +               au_dr_hino_del(&br->br_dirren, ent);
12144 +               au_kfree_rcu(ent);
12145 +       }
12146 +       goto out; /* success */
12147 +
12148 +out_free:
12149 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12150 +       lkup->dirren.ninfo = 0;
12151 +       lkup->dirren.drinfo = NULL;
12152 +out:
12153 +       AuTraceErr(err);
12154 +       return err;
12155 +}
12156 +
12157 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12158 +{
12159 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12160 +}
12161 +
12162 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12163 +{
12164 +       int err;
12165 +       struct au_drinfo *drinfo;
12166 +
12167 +       err = 0;
12168 +       if (!lkup->dirren.drinfo)
12169 +               goto out;
12170 +       AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12171 +       drinfo = lkup->dirren.drinfo[btgt + 1];
12172 +       if (!drinfo)
12173 +               goto out;
12174 +
12175 +       au_kfree_try_rcu(lkup->whname.name);
12176 +       lkup->whname.name = NULL;
12177 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12178 +       lkup->dirren.dr_name.name = drinfo->oldname;
12179 +       lkup->name = &lkup->dirren.dr_name;
12180 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12181 +       if (!err)
12182 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12183 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12184 +                     btgt);
12185 +
12186 +out:
12187 +       AuTraceErr(err);
12188 +       return err;
12189 +}
12190 +
12191 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12192 +                    ino_t h_ino)
12193 +{
12194 +       int match;
12195 +       struct au_drinfo *drinfo;
12196 +
12197 +       match = 1;
12198 +       if (!lkup->dirren.drinfo)
12199 +               goto out;
12200 +       AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12201 +       drinfo = lkup->dirren.drinfo[bindex + 1];
12202 +       if (!drinfo)
12203 +               goto out;
12204 +
12205 +       match = (drinfo->ino == h_ino);
12206 +       AuDbg("match %d\n", match);
12207 +
12208 +out:
12209 +       return match;
12210 +}
12211 +
12212 +/* ---------------------------------------------------------------------- */
12213 +
12214 +int au_dr_opt_set(struct super_block *sb)
12215 +{
12216 +       int err;
12217 +       aufs_bindex_t bindex, bbot;
12218 +       struct au_branch *br;
12219 +
12220 +       err = 0;
12221 +       bbot = au_sbbot(sb);
12222 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12223 +               br = au_sbr(sb, bindex);
12224 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12225 +       }
12226 +
12227 +       return err;
12228 +}
12229 +
12230 +int au_dr_opt_flush(struct super_block *sb)
12231 +{
12232 +       int err;
12233 +       aufs_bindex_t bindex, bbot;
12234 +       struct au_branch *br;
12235 +
12236 +       err = 0;
12237 +       bbot = au_sbbot(sb);
12238 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12239 +               br = au_sbr(sb, bindex);
12240 +               if (au_br_writable(br->br_perm))
12241 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12242 +       }
12243 +
12244 +       return err;
12245 +}
12246 +
12247 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12248 +{
12249 +       int err;
12250 +       aufs_bindex_t bindex, bbot;
12251 +       struct au_branch *br;
12252 +
12253 +       err = 0;
12254 +       if (!no_flush) {
12255 +               err = au_dr_opt_flush(sb);
12256 +               if (unlikely(err))
12257 +                       goto out;
12258 +       }
12259 +
12260 +       bbot = au_sbbot(sb);
12261 +       for (bindex = 0; bindex <= bbot; bindex++) {
12262 +               br = au_sbr(sb, bindex);
12263 +               au_dr_hino_free(&br->br_dirren);
12264 +       }
12265 +
12266 +out:
12267 +       return err;
12268 +}
12269 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12270 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12271 +++ linux/fs/aufs/dirren.h      2019-07-11 15:42:14.462237786 +0200
12272 @@ -0,0 +1,140 @@
12273 +/* SPDX-License-Identifier: GPL-2.0 */
12274 +/*
12275 + * Copyright (C) 2017-2019 Junjiro R. Okajima
12276 + *
12277 + * This program, aufs is free software; you can redistribute it and/or modify
12278 + * it under the terms of the GNU General Public License as published by
12279 + * the Free Software Foundation; either version 2 of the License, or
12280 + * (at your option) any later version.
12281 + *
12282 + * This program is distributed in the hope that it will be useful,
12283 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12284 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12285 + * GNU General Public License for more details.
12286 + *
12287 + * You should have received a copy of the GNU General Public License
12288 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12289 + */
12290 +
12291 +/*
12292 + * renamed dir info
12293 + */
12294 +
12295 +#ifndef __AUFS_DIRREN_H__
12296 +#define __AUFS_DIRREN_H__
12297 +
12298 +#ifdef __KERNEL__
12299 +
12300 +#include <linux/dcache.h>
12301 +#include <linux/statfs.h>
12302 +#include <linux/uuid.h>
12303 +#include "hbl.h"
12304 +
12305 +#define AuDirren_NHASH 100
12306 +
12307 +#ifdef CONFIG_AUFS_DIRREN
12308 +enum au_brid_type {
12309 +       AuBrid_Unset,
12310 +       AuBrid_UUID,
12311 +       AuBrid_FSID,
12312 +       AuBrid_DEV
12313 +};
12314 +
12315 +struct au_dr_brid {
12316 +       enum au_brid_type       type;
12317 +       union {
12318 +               uuid_t  uuid;   /* unimplemented yet */
12319 +               fsid_t  fsid;
12320 +               dev_t   dev;
12321 +       };
12322 +};
12323 +
12324 +/* 20 is the max digits length of ulong 64 */
12325 +/* brid-type "_" uuid "_" inum */
12326 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12327 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12328 +
12329 +struct au_dr_hino {
12330 +       struct hlist_bl_node    dr_hnode;
12331 +       ino_t                   dr_h_ino;
12332 +};
12333 +
12334 +struct au_dr_br {
12335 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12336 +       struct au_dr_brid       dr_brid;
12337 +};
12338 +
12339 +struct au_dr_lookup {
12340 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12341 +       struct qstr             dr_name; /* subset of dr_info */
12342 +       aufs_bindex_t           ninfo;
12343 +       struct au_drinfo        **drinfo;
12344 +};
12345 +#else
12346 +struct au_dr_hino;
12347 +/* empty */
12348 +struct au_dr_br { };
12349 +struct au_dr_lookup { };
12350 +#endif
12351 +
12352 +/* ---------------------------------------------------------------------- */
12353 +
12354 +struct au_branch;
12355 +struct au_do_lookup_args;
12356 +struct au_hinode;
12357 +#ifdef CONFIG_AUFS_DIRREN
12358 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12359 +                       struct au_dr_hino *add_ent);
12360 +void au_dr_hino_free(struct au_dr_br *dr);
12361 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12362 +                 const struct path *path);
12363 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12364 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12365 +                struct qstr *dst_name, void *_rev);
12366 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12367 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12368 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12369 +              aufs_bindex_t bindex);
12370 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12371 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12372 +                    ino_t h_ino);
12373 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12374 +int au_dr_opt_set(struct super_block *sb);
12375 +int au_dr_opt_flush(struct super_block *sb);
12376 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12377 +#else
12378 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12379 +          struct au_dr_hino *add_ent);
12380 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12381 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12382 +          const struct path *path);
12383 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12384 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12385 +          struct qstr *dst_name, void *_rev);
12386 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12387 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12388 +          void *rev);
12389 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12390 +          aufs_bindex_t bindex);
12391 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12392 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12393 +          aufs_bindex_t bindex, ino_t h_ino);
12394 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12395 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12396 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12397 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12398 +#endif
12399 +
12400 +/* ---------------------------------------------------------------------- */
12401 +
12402 +#ifdef CONFIG_AUFS_DIRREN
12403 +static inline int au_dr_ihash(ino_t h_ino)
12404 +{
12405 +       return h_ino % AuDirren_NHASH;
12406 +}
12407 +#else
12408 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12409 +#endif
12410 +
12411 +#endif /* __KERNEL__ */
12412 +#endif /* __AUFS_DIRREN_H__ */
12413 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12414 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12415 +++ linux/fs/aufs/dynop.c       2019-07-11 15:42:14.465571210 +0200
12416 @@ -0,0 +1,367 @@
12417 +// SPDX-License-Identifier: GPL-2.0
12418 +/*
12419 + * Copyright (C) 2010-2019 Junjiro R. Okajima
12420 + *
12421 + * This program, aufs is free software; you can redistribute it and/or modify
12422 + * it under the terms of the GNU General Public License as published by
12423 + * the Free Software Foundation; either version 2 of the License, or
12424 + * (at your option) any later version.
12425 + *
12426 + * This program is distributed in the hope that it will be useful,
12427 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12428 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12429 + * GNU General Public License for more details.
12430 + *
12431 + * You should have received a copy of the GNU General Public License
12432 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12433 + */
12434 +
12435 +/*
12436 + * dynamically customizable operations for regular files
12437 + */
12438 +
12439 +#include "aufs.h"
12440 +
12441 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12442 +
12443 +/*
12444 + * How large will these lists be?
12445 + * Usually just a few elements, 20-30 at most for each, I guess.
12446 + */
12447 +static struct hlist_bl_head dynop[AuDyLast];
12448 +
12449 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12450 +                                    const void *h_op)
12451 +{
12452 +       struct au_dykey *key, *tmp;
12453 +       struct hlist_bl_node *pos;
12454 +
12455 +       key = NULL;
12456 +       hlist_bl_lock(hbl);
12457 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12458 +               if (tmp->dk_op.dy_hop == h_op) {
12459 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12460 +                               key = tmp;
12461 +                       break;
12462 +               }
12463 +       hlist_bl_unlock(hbl);
12464 +
12465 +       return key;
12466 +}
12467 +
12468 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12469 +{
12470 +       struct au_dykey **k, *found;
12471 +       const void *h_op = key->dk_op.dy_hop;
12472 +       int i;
12473 +
12474 +       found = NULL;
12475 +       k = br->br_dykey;
12476 +       for (i = 0; i < AuBrDynOp; i++)
12477 +               if (k[i]) {
12478 +                       if (k[i]->dk_op.dy_hop == h_op) {
12479 +                               found = k[i];
12480 +                               break;
12481 +                       }
12482 +               } else
12483 +                       break;
12484 +       if (!found) {
12485 +               spin_lock(&br->br_dykey_lock);
12486 +               for (; i < AuBrDynOp; i++)
12487 +                       if (k[i]) {
12488 +                               if (k[i]->dk_op.dy_hop == h_op) {
12489 +                                       found = k[i];
12490 +                                       break;
12491 +                               }
12492 +                       } else {
12493 +                               k[i] = key;
12494 +                               break;
12495 +                       }
12496 +               spin_unlock(&br->br_dykey_lock);
12497 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12498 +       }
12499 +
12500 +       return found;
12501 +}
12502 +
12503 +/* kref_get() if @key is already added */
12504 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12505 +{
12506 +       struct au_dykey *tmp, *found;
12507 +       struct hlist_bl_node *pos;
12508 +       const void *h_op = key->dk_op.dy_hop;
12509 +
12510 +       found = NULL;
12511 +       hlist_bl_lock(hbl);
12512 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12513 +               if (tmp->dk_op.dy_hop == h_op) {
12514 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12515 +                               found = tmp;
12516 +                       break;
12517 +               }
12518 +       if (!found)
12519 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12520 +       hlist_bl_unlock(hbl);
12521 +
12522 +       if (!found)
12523 +               DyPrSym(key);
12524 +       return found;
12525 +}
12526 +
12527 +static void dy_free_rcu(struct rcu_head *rcu)
12528 +{
12529 +       struct au_dykey *key;
12530 +
12531 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12532 +       DyPrSym(key);
12533 +       kfree(key);
12534 +}
12535 +
12536 +static void dy_free(struct kref *kref)
12537 +{
12538 +       struct au_dykey *key;
12539 +       struct hlist_bl_head *hbl;
12540 +
12541 +       key = container_of(kref, struct au_dykey, dk_kref);
12542 +       hbl = dynop + key->dk_op.dy_type;
12543 +       au_hbl_del(&key->dk_hnode, hbl);
12544 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12545 +}
12546 +
12547 +void au_dy_put(struct au_dykey *key)
12548 +{
12549 +       kref_put(&key->dk_kref, dy_free);
12550 +}
12551 +
12552 +/* ---------------------------------------------------------------------- */
12553 +
12554 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12555 +
12556 +#ifdef CONFIG_AUFS_DEBUG
12557 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12558 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12559 +#else
12560 +#define DyDbgDeclare(cnt)      do {} while (0)
12561 +#define DyDbgInc(cnt)          do {} while (0)
12562 +#endif
12563 +
12564 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12565 +       DyDbgInc(cnt);                                                  \
12566 +       if (h_op->func) {                                               \
12567 +               if (src.func)                                           \
12568 +                       dst.func = src.func;                            \
12569 +               else                                                    \
12570 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12571 +       }                                                               \
12572 +} while (0)
12573 +
12574 +#define DySetForce(func, dst, src) do {                \
12575 +       AuDebugOn(!src.func);                   \
12576 +       DyDbgInc(cnt);                          \
12577 +       dst.func = src.func;                    \
12578 +} while (0)
12579 +
12580 +#define DySetAop(func) \
12581 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12582 +#define DySetAopForce(func) \
12583 +       DySetForce(func, dyaop->da_op, aufs_aop)
12584 +
12585 +static void dy_aop(struct au_dykey *key, const void *h_op,
12586 +                  struct super_block *h_sb __maybe_unused)
12587 +{
12588 +       struct au_dyaop *dyaop = (void *)key;
12589 +       const struct address_space_operations *h_aop = h_op;
12590 +       DyDbgDeclare(cnt);
12591 +
12592 +       AuDbg("%s\n", au_sbtype(h_sb));
12593 +
12594 +       DySetAop(writepage);
12595 +       DySetAopForce(readpage);        /* force */
12596 +       DySetAop(writepages);
12597 +       DySetAop(set_page_dirty);
12598 +       DySetAop(readpages);
12599 +       DySetAop(write_begin);
12600 +       DySetAop(write_end);
12601 +       DySetAop(bmap);
12602 +       DySetAop(invalidatepage);
12603 +       DySetAop(releasepage);
12604 +       DySetAop(freepage);
12605 +       /* this one will be changed according to an aufs mount option */
12606 +       DySetAop(direct_IO);
12607 +       DySetAop(migratepage);
12608 +       DySetAop(isolate_page);
12609 +       DySetAop(putback_page);
12610 +       DySetAop(launder_page);
12611 +       DySetAop(is_partially_uptodate);
12612 +       DySetAop(is_dirty_writeback);
12613 +       DySetAop(error_remove_page);
12614 +       DySetAop(swap_activate);
12615 +       DySetAop(swap_deactivate);
12616 +
12617 +       DyDbgSize(cnt, *h_aop);
12618 +}
12619 +
12620 +/* ---------------------------------------------------------------------- */
12621 +
12622 +static void dy_bug(struct kref *kref)
12623 +{
12624 +       BUG();
12625 +}
12626 +
12627 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12628 +{
12629 +       struct au_dykey *key, *old;
12630 +       struct hlist_bl_head *hbl;
12631 +       struct op {
12632 +               unsigned int sz;
12633 +               void (*set)(struct au_dykey *key, const void *h_op,
12634 +                           struct super_block *h_sb __maybe_unused);
12635 +       };
12636 +       static const struct op a[] = {
12637 +               [AuDy_AOP] = {
12638 +                       .sz     = sizeof(struct au_dyaop),
12639 +                       .set    = dy_aop
12640 +               }
12641 +       };
12642 +       const struct op *p;
12643 +
12644 +       hbl = dynop + op->dy_type;
12645 +       key = dy_gfind_get(hbl, op->dy_hop);
12646 +       if (key)
12647 +               goto out_add; /* success */
12648 +
12649 +       p = a + op->dy_type;
12650 +       key = kzalloc(p->sz, GFP_NOFS);
12651 +       if (unlikely(!key)) {
12652 +               key = ERR_PTR(-ENOMEM);
12653 +               goto out;
12654 +       }
12655 +
12656 +       key->dk_op.dy_hop = op->dy_hop;
12657 +       kref_init(&key->dk_kref);
12658 +       p->set(key, op->dy_hop, au_br_sb(br));
12659 +       old = dy_gadd(hbl, key);
12660 +       if (old) {
12661 +               au_kfree_rcu(key);
12662 +               key = old;
12663 +       }
12664 +
12665 +out_add:
12666 +       old = dy_bradd(br, key);
12667 +       if (old)
12668 +               /* its ref-count should never be zero here */
12669 +               kref_put(&key->dk_kref, dy_bug);
12670 +out:
12671 +       return key;
12672 +}
12673 +
12674 +/* ---------------------------------------------------------------------- */
12675 +/*
12676 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12677 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12678 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12679 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12680 + * See the aufs manual in detail.
12681 + */
12682 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12683 +{
12684 +       if (!do_dx)
12685 +               dyaop->da_op.direct_IO = NULL;
12686 +       else
12687 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12688 +}
12689 +
12690 +static struct au_dyaop *dy_aget(struct au_branch *br,
12691 +                               const struct address_space_operations *h_aop,
12692 +                               int do_dx)
12693 +{
12694 +       struct au_dyaop *dyaop;
12695 +       struct au_dynop op;
12696 +
12697 +       op.dy_type = AuDy_AOP;
12698 +       op.dy_haop = h_aop;
12699 +       dyaop = (void *)dy_get(&op, br);
12700 +       if (IS_ERR(dyaop))
12701 +               goto out;
12702 +       dy_adx(dyaop, do_dx);
12703 +
12704 +out:
12705 +       return dyaop;
12706 +}
12707 +
12708 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12709 +               struct inode *h_inode)
12710 +{
12711 +       int err, do_dx;
12712 +       struct super_block *sb;
12713 +       struct au_branch *br;
12714 +       struct au_dyaop *dyaop;
12715 +
12716 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12717 +       IiMustWriteLock(inode);
12718 +
12719 +       sb = inode->i_sb;
12720 +       br = au_sbr(sb, bindex);
12721 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12722 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12723 +       err = PTR_ERR(dyaop);
12724 +       if (IS_ERR(dyaop))
12725 +               /* unnecessary to call dy_fput() */
12726 +               goto out;
12727 +
12728 +       err = 0;
12729 +       inode->i_mapping->a_ops = &dyaop->da_op;
12730 +
12731 +out:
12732 +       return err;
12733 +}
12734 +
12735 +/*
12736 + * Is it safe to replace a_ops during the inode/file is in operation?
12737 + * Yes, I hope so.
12738 + */
12739 +int au_dy_irefresh(struct inode *inode)
12740 +{
12741 +       int err;
12742 +       aufs_bindex_t btop;
12743 +       struct inode *h_inode;
12744 +
12745 +       err = 0;
12746 +       if (S_ISREG(inode->i_mode)) {
12747 +               btop = au_ibtop(inode);
12748 +               h_inode = au_h_iptr(inode, btop);
12749 +               err = au_dy_iaop(inode, btop, h_inode);
12750 +       }
12751 +       return err;
12752 +}
12753 +
12754 +void au_dy_arefresh(int do_dx)
12755 +{
12756 +       struct hlist_bl_head *hbl;
12757 +       struct hlist_bl_node *pos;
12758 +       struct au_dykey *key;
12759 +
12760 +       hbl = dynop + AuDy_AOP;
12761 +       hlist_bl_lock(hbl);
12762 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12763 +               dy_adx((void *)key, do_dx);
12764 +       hlist_bl_unlock(hbl);
12765 +}
12766 +
12767 +/* ---------------------------------------------------------------------- */
12768 +
12769 +void __init au_dy_init(void)
12770 +{
12771 +       int i;
12772 +
12773 +       for (i = 0; i < AuDyLast; i++)
12774 +               INIT_HLIST_BL_HEAD(dynop + i);
12775 +}
12776 +
12777 +void au_dy_fin(void)
12778 +{
12779 +       int i;
12780 +
12781 +       for (i = 0; i < AuDyLast; i++)
12782 +               WARN_ON(!hlist_bl_empty(dynop + i));
12783 +}
12784 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12785 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12786 +++ linux/fs/aufs/dynop.h       2019-07-11 15:42:14.465571210 +0200
12787 @@ -0,0 +1,77 @@
12788 +/* SPDX-License-Identifier: GPL-2.0 */
12789 +/*
12790 + * Copyright (C) 2010-2019 Junjiro R. Okajima
12791 + *
12792 + * This program, aufs is free software; you can redistribute it and/or modify
12793 + * it under the terms of the GNU General Public License as published by
12794 + * the Free Software Foundation; either version 2 of the License, or
12795 + * (at your option) any later version.
12796 + *
12797 + * This program is distributed in the hope that it will be useful,
12798 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12799 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12800 + * GNU General Public License for more details.
12801 + *
12802 + * You should have received a copy of the GNU General Public License
12803 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12804 + */
12805 +
12806 +/*
12807 + * dynamically customizable operations (for regular files only)
12808 + */
12809 +
12810 +#ifndef __AUFS_DYNOP_H__
12811 +#define __AUFS_DYNOP_H__
12812 +
12813 +#ifdef __KERNEL__
12814 +
12815 +#include <linux/fs.h>
12816 +#include <linux/kref.h>
12817 +
12818 +enum {AuDy_AOP, AuDyLast};
12819 +
12820 +struct au_dynop {
12821 +       int                                             dy_type;
12822 +       union {
12823 +               const void                              *dy_hop;
12824 +               const struct address_space_operations   *dy_haop;
12825 +       };
12826 +};
12827 +
12828 +struct au_dykey {
12829 +       union {
12830 +               struct hlist_bl_node    dk_hnode;
12831 +               struct rcu_head         dk_rcu;
12832 +       };
12833 +       struct au_dynop         dk_op;
12834 +
12835 +       /*
12836 +        * during I am in the branch local array, kref is gotten. when the
12837 +        * branch is removed, kref is put.
12838 +        */
12839 +       struct kref             dk_kref;
12840 +};
12841 +
12842 +/* stop unioning since their sizes are very different from each other */
12843 +struct au_dyaop {
12844 +       struct au_dykey                 da_key;
12845 +       struct address_space_operations da_op; /* not const */
12846 +};
12847 +/* make sure that 'struct au_dykey *' can be any type */
12848 +static_assert(!offsetof(struct au_dyaop, da_key));
12849 +
12850 +/* ---------------------------------------------------------------------- */
12851 +
12852 +/* dynop.c */
12853 +struct au_branch;
12854 +void au_dy_put(struct au_dykey *key);
12855 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12856 +               struct inode *h_inode);
12857 +int au_dy_irefresh(struct inode *inode);
12858 +void au_dy_arefresh(int do_dio);
12859 +
12860 +void __init au_dy_init(void);
12861 +void au_dy_fin(void);
12862 +
12863 +#endif /* __KERNEL__ */
12864 +#endif /* __AUFS_DYNOP_H__ */
12865 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12866 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12867 +++ linux/fs/aufs/export.c      2019-07-11 15:42:14.465571210 +0200
12868 @@ -0,0 +1,838 @@
12869 +// SPDX-License-Identifier: GPL-2.0
12870 +/*
12871 + * Copyright (C) 2005-2019 Junjiro R. Okajima
12872 + *
12873 + * This program, aufs is free software; you can redistribute it and/or modify
12874 + * it under the terms of the GNU General Public License as published by
12875 + * the Free Software Foundation; either version 2 of the License, or
12876 + * (at your option) any later version.
12877 + *
12878 + * This program is distributed in the hope that it will be useful,
12879 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12880 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12881 + * GNU General Public License for more details.
12882 + *
12883 + * You should have received a copy of the GNU General Public License
12884 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12885 + */
12886 +
12887 +/*
12888 + * export via nfs
12889 + */
12890 +
12891 +#include <linux/exportfs.h>
12892 +#include <linux/fs_struct.h>
12893 +#include <linux/namei.h>
12894 +#include <linux/nsproxy.h>
12895 +#include <linux/random.h>
12896 +#include <linux/writeback.h>
12897 +#include "aufs.h"
12898 +
12899 +union conv {
12900 +#ifdef CONFIG_AUFS_INO_T_64
12901 +       __u32 a[2];
12902 +#else
12903 +       __u32 a[1];
12904 +#endif
12905 +       ino_t ino;
12906 +};
12907 +
12908 +static ino_t decode_ino(__u32 *a)
12909 +{
12910 +       union conv u;
12911 +
12912 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12913 +       u.a[0] = a[0];
12914 +#ifdef CONFIG_AUFS_INO_T_64
12915 +       u.a[1] = a[1];
12916 +#endif
12917 +       return u.ino;
12918 +}
12919 +
12920 +static void encode_ino(__u32 *a, ino_t ino)
12921 +{
12922 +       union conv u;
12923 +
12924 +       u.ino = ino;
12925 +       a[0] = u.a[0];
12926 +#ifdef CONFIG_AUFS_INO_T_64
12927 +       a[1] = u.a[1];
12928 +#endif
12929 +}
12930 +
12931 +/* NFS file handle */
12932 +enum {
12933 +       Fh_br_id,
12934 +       Fh_sigen,
12935 +#ifdef CONFIG_AUFS_INO_T_64
12936 +       /* support 64bit inode number */
12937 +       Fh_ino1,
12938 +       Fh_ino2,
12939 +       Fh_dir_ino1,
12940 +       Fh_dir_ino2,
12941 +#else
12942 +       Fh_ino1,
12943 +       Fh_dir_ino1,
12944 +#endif
12945 +       Fh_igen,
12946 +       Fh_h_type,
12947 +       Fh_tail,
12948 +
12949 +       Fh_ino = Fh_ino1,
12950 +       Fh_dir_ino = Fh_dir_ino1
12951 +};
12952 +
12953 +static int au_test_anon(struct dentry *dentry)
12954 +{
12955 +       /* note: read d_flags without d_lock */
12956 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12957 +}
12958 +
12959 +int au_test_nfsd(void)
12960 +{
12961 +       int ret;
12962 +       struct task_struct *tsk = current;
12963 +       char comm[sizeof(tsk->comm)];
12964 +
12965 +       ret = 0;
12966 +       if (tsk->flags & PF_KTHREAD) {
12967 +               get_task_comm(comm, tsk);
12968 +               ret = !strcmp(comm, "nfsd");
12969 +       }
12970 +
12971 +       return ret;
12972 +}
12973 +
12974 +/* ---------------------------------------------------------------------- */
12975 +/* inode generation external table */
12976 +
12977 +void au_xigen_inc(struct inode *inode)
12978 +{
12979 +       loff_t pos;
12980 +       ssize_t sz;
12981 +       __u32 igen;
12982 +       struct super_block *sb;
12983 +       struct au_sbinfo *sbinfo;
12984 +
12985 +       sb = inode->i_sb;
12986 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12987 +
12988 +       sbinfo = au_sbi(sb);
12989 +       pos = inode->i_ino;
12990 +       pos *= sizeof(igen);
12991 +       igen = inode->i_generation + 1;
12992 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12993 +                        sizeof(igen), &pos);
12994 +       if (sz == sizeof(igen))
12995 +               return; /* success */
12996 +
12997 +       if (unlikely(sz >= 0))
12998 +               AuIOErr("xigen error (%zd)\n", sz);
12999 +}
13000 +
13001 +int au_xigen_new(struct inode *inode)
13002 +{
13003 +       int err;
13004 +       loff_t pos;
13005 +       ssize_t sz;
13006 +       struct super_block *sb;
13007 +       struct au_sbinfo *sbinfo;
13008 +       struct file *file;
13009 +
13010 +       err = 0;
13011 +       /* todo: dirty, at mount time */
13012 +       if (inode->i_ino == AUFS_ROOT_INO)
13013 +               goto out;
13014 +       sb = inode->i_sb;
13015 +       SiMustAnyLock(sb);
13016 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13017 +               goto out;
13018 +
13019 +       err = -EFBIG;
13020 +       pos = inode->i_ino;
13021 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13022 +               AuIOErr1("too large i%lld\n", pos);
13023 +               goto out;
13024 +       }
13025 +       pos *= sizeof(inode->i_generation);
13026 +
13027 +       err = 0;
13028 +       sbinfo = au_sbi(sb);
13029 +       file = sbinfo->si_xigen;
13030 +       BUG_ON(!file);
13031 +
13032 +       if (vfsub_f_size_read(file)
13033 +           < pos + sizeof(inode->i_generation)) {
13034 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13035 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13036 +                                sizeof(inode->i_generation), &pos);
13037 +       } else
13038 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13039 +                               sizeof(inode->i_generation), &pos);
13040 +       if (sz == sizeof(inode->i_generation))
13041 +               goto out; /* success */
13042 +
13043 +       err = sz;
13044 +       if (unlikely(sz >= 0)) {
13045 +               err = -EIO;
13046 +               AuIOErr("xigen error (%zd)\n", sz);
13047 +       }
13048 +
13049 +out:
13050 +       return err;
13051 +}
13052 +
13053 +int au_xigen_set(struct super_block *sb, struct path *path)
13054 +{
13055 +       int err;
13056 +       struct au_sbinfo *sbinfo;
13057 +       struct file *file;
13058 +
13059 +       SiMustWriteLock(sb);
13060 +
13061 +       sbinfo = au_sbi(sb);
13062 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13063 +       err = PTR_ERR(file);
13064 +       if (IS_ERR(file))
13065 +               goto out;
13066 +       err = 0;
13067 +       if (sbinfo->si_xigen)
13068 +               fput(sbinfo->si_xigen);
13069 +       sbinfo->si_xigen = file;
13070 +
13071 +out:
13072 +       AuTraceErr(err);
13073 +       return err;
13074 +}
13075 +
13076 +void au_xigen_clr(struct super_block *sb)
13077 +{
13078 +       struct au_sbinfo *sbinfo;
13079 +
13080 +       SiMustWriteLock(sb);
13081 +
13082 +       sbinfo = au_sbi(sb);
13083 +       if (sbinfo->si_xigen) {
13084 +               fput(sbinfo->si_xigen);
13085 +               sbinfo->si_xigen = NULL;
13086 +       }
13087 +}
13088 +
13089 +/* ---------------------------------------------------------------------- */
13090 +
13091 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13092 +                                   ino_t dir_ino)
13093 +{
13094 +       struct dentry *dentry, *d;
13095 +       struct inode *inode;
13096 +       unsigned int sigen;
13097 +
13098 +       dentry = NULL;
13099 +       inode = ilookup(sb, ino);
13100 +       if (!inode)
13101 +               goto out;
13102 +
13103 +       dentry = ERR_PTR(-ESTALE);
13104 +       sigen = au_sigen(sb);
13105 +       if (unlikely(au_is_bad_inode(inode)
13106 +                    || IS_DEADDIR(inode)
13107 +                    || sigen != au_iigen(inode, NULL)))
13108 +               goto out_iput;
13109 +
13110 +       dentry = NULL;
13111 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13112 +               dentry = d_find_alias(inode);
13113 +       else {
13114 +               spin_lock(&inode->i_lock);
13115 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13116 +                       spin_lock(&d->d_lock);
13117 +                       if (!au_test_anon(d)
13118 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13119 +                               dentry = dget_dlock(d);
13120 +                               spin_unlock(&d->d_lock);
13121 +                               break;
13122 +                       }
13123 +                       spin_unlock(&d->d_lock);
13124 +               }
13125 +               spin_unlock(&inode->i_lock);
13126 +       }
13127 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13128 +               /* need to refresh */
13129 +               dput(dentry);
13130 +               dentry = NULL;
13131 +       }
13132 +
13133 +out_iput:
13134 +       iput(inode);
13135 +out:
13136 +       AuTraceErrPtr(dentry);
13137 +       return dentry;
13138 +}
13139 +
13140 +/* ---------------------------------------------------------------------- */
13141 +
13142 +/* todo: dirty? */
13143 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13144 +
13145 +struct au_compare_mnt_args {
13146 +       /* input */
13147 +       struct super_block *sb;
13148 +
13149 +       /* output */
13150 +       struct vfsmount *mnt;
13151 +};
13152 +
13153 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13154 +{
13155 +       struct au_compare_mnt_args *a = arg;
13156 +
13157 +       if (mnt->mnt_sb != a->sb)
13158 +               return 0;
13159 +       a->mnt = mntget(mnt);
13160 +       return 1;
13161 +}
13162 +
13163 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13164 +{
13165 +       int err;
13166 +       struct path root;
13167 +       struct au_compare_mnt_args args = {
13168 +               .sb = sb
13169 +       };
13170 +
13171 +       get_fs_root(current->fs, &root);
13172 +       rcu_read_lock();
13173 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13174 +       rcu_read_unlock();
13175 +       path_put(&root);
13176 +       AuDebugOn(!err);
13177 +       AuDebugOn(!args.mnt);
13178 +       return args.mnt;
13179 +}
13180 +
13181 +struct au_nfsd_si_lock {
13182 +       unsigned int sigen;
13183 +       aufs_bindex_t bindex, br_id;
13184 +       unsigned char force_lock;
13185 +};
13186 +
13187 +static int si_nfsd_read_lock(struct super_block *sb,
13188 +                            struct au_nfsd_si_lock *nsi_lock)
13189 +{
13190 +       int err;
13191 +       aufs_bindex_t bindex;
13192 +
13193 +       si_read_lock(sb, AuLock_FLUSH);
13194 +
13195 +       /* branch id may be wrapped around */
13196 +       err = 0;
13197 +       bindex = au_br_index(sb, nsi_lock->br_id);
13198 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13199 +               goto out; /* success */
13200 +
13201 +       err = -ESTALE;
13202 +       bindex = -1;
13203 +       if (!nsi_lock->force_lock)
13204 +               si_read_unlock(sb);
13205 +
13206 +out:
13207 +       nsi_lock->bindex = bindex;
13208 +       return err;
13209 +}
13210 +
13211 +struct find_name_by_ino {
13212 +       struct dir_context ctx;
13213 +       int called, found;
13214 +       ino_t ino;
13215 +       char *name;
13216 +       int namelen;
13217 +};
13218 +
13219 +static int
13220 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13221 +                loff_t offset, u64 ino, unsigned int d_type)
13222 +{
13223 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13224 +                                                 ctx);
13225 +
13226 +       a->called++;
13227 +       if (a->ino != ino)
13228 +               return 0;
13229 +
13230 +       memcpy(a->name, name, namelen);
13231 +       a->namelen = namelen;
13232 +       a->found = 1;
13233 +       return 1;
13234 +}
13235 +
13236 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13237 +                                    struct au_nfsd_si_lock *nsi_lock)
13238 +{
13239 +       struct dentry *dentry, *parent;
13240 +       struct file *file;
13241 +       struct inode *dir;
13242 +       struct find_name_by_ino arg = {
13243 +               .ctx = {
13244 +                       .actor = find_name_by_ino
13245 +               }
13246 +       };
13247 +       int err;
13248 +
13249 +       parent = path->dentry;
13250 +       if (nsi_lock)
13251 +               si_read_unlock(parent->d_sb);
13252 +       file = vfsub_dentry_open(path, au_dir_roflags);
13253 +       dentry = (void *)file;
13254 +       if (IS_ERR(file))
13255 +               goto out;
13256 +
13257 +       dentry = ERR_PTR(-ENOMEM);
13258 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13259 +       if (unlikely(!arg.name))
13260 +               goto out_file;
13261 +       arg.ino = ino;
13262 +       arg.found = 0;
13263 +       do {
13264 +               arg.called = 0;
13265 +               /* smp_mb(); */
13266 +               err = vfsub_iterate_dir(file, &arg.ctx);
13267 +       } while (!err && !arg.found && arg.called);
13268 +       dentry = ERR_PTR(err);
13269 +       if (unlikely(err))
13270 +               goto out_name;
13271 +       /* instead of ENOENT */
13272 +       dentry = ERR_PTR(-ESTALE);
13273 +       if (!arg.found)
13274 +               goto out_name;
13275 +
13276 +       /* do not call vfsub_lkup_one() */
13277 +       dir = d_inode(parent);
13278 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13279 +       AuTraceErrPtr(dentry);
13280 +       if (IS_ERR(dentry))
13281 +               goto out_name;
13282 +       AuDebugOn(au_test_anon(dentry));
13283 +       if (unlikely(d_really_is_negative(dentry))) {
13284 +               dput(dentry);
13285 +               dentry = ERR_PTR(-ENOENT);
13286 +       }
13287 +
13288 +out_name:
13289 +       free_page((unsigned long)arg.name);
13290 +out_file:
13291 +       fput(file);
13292 +out:
13293 +       if (unlikely(nsi_lock
13294 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13295 +               if (!IS_ERR(dentry)) {
13296 +                       dput(dentry);
13297 +                       dentry = ERR_PTR(-ESTALE);
13298 +               }
13299 +       AuTraceErrPtr(dentry);
13300 +       return dentry;
13301 +}
13302 +
13303 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13304 +                                       ino_t dir_ino,
13305 +                                       struct au_nfsd_si_lock *nsi_lock)
13306 +{
13307 +       struct dentry *dentry;
13308 +       struct path path;
13309 +
13310 +       if (dir_ino != AUFS_ROOT_INO) {
13311 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13312 +               dentry = path.dentry;
13313 +               if (!path.dentry || IS_ERR(path.dentry))
13314 +                       goto out;
13315 +               AuDebugOn(au_test_anon(path.dentry));
13316 +       } else
13317 +               path.dentry = dget(sb->s_root);
13318 +
13319 +       path.mnt = au_mnt_get(sb);
13320 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13321 +       path_put(&path);
13322 +
13323 +out:
13324 +       AuTraceErrPtr(dentry);
13325 +       return dentry;
13326 +}
13327 +
13328 +/* ---------------------------------------------------------------------- */
13329 +
13330 +static int h_acceptable(void *expv, struct dentry *dentry)
13331 +{
13332 +       return 1;
13333 +}
13334 +
13335 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13336 +                          char *buf, int len, struct super_block *sb)
13337 +{
13338 +       char *p;
13339 +       int n;
13340 +       struct path path;
13341 +
13342 +       p = d_path(h_rootpath, buf, len);
13343 +       if (IS_ERR(p))
13344 +               goto out;
13345 +       n = strlen(p);
13346 +
13347 +       path.mnt = h_rootpath->mnt;
13348 +       path.dentry = h_parent;
13349 +       p = d_path(&path, buf, len);
13350 +       if (IS_ERR(p))
13351 +               goto out;
13352 +       if (n != 1)
13353 +               p += n;
13354 +
13355 +       path.mnt = au_mnt_get(sb);
13356 +       path.dentry = sb->s_root;
13357 +       p = d_path(&path, buf, len - strlen(p));
13358 +       mntput(path.mnt);
13359 +       if (IS_ERR(p))
13360 +               goto out;
13361 +       if (n != 1)
13362 +               p[strlen(p)] = '/';
13363 +
13364 +out:
13365 +       AuTraceErrPtr(p);
13366 +       return p;
13367 +}
13368 +
13369 +static
13370 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13371 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13372 +{
13373 +       struct dentry *dentry, *h_parent, *root;
13374 +       struct super_block *h_sb;
13375 +       char *pathname, *p;
13376 +       struct vfsmount *h_mnt;
13377 +       struct au_branch *br;
13378 +       int err;
13379 +       struct path path;
13380 +
13381 +       br = au_sbr(sb, nsi_lock->bindex);
13382 +       h_mnt = au_br_mnt(br);
13383 +       h_sb = h_mnt->mnt_sb;
13384 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13385 +       lockdep_off();
13386 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13387 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13388 +                                     h_acceptable, /*context*/NULL);
13389 +       lockdep_on();
13390 +       dentry = h_parent;
13391 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13392 +               AuWarn1("%s decode_fh failed, %ld\n",
13393 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13394 +               goto out;
13395 +       }
13396 +       dentry = NULL;
13397 +       if (unlikely(au_test_anon(h_parent))) {
13398 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13399 +                       au_sbtype(h_sb));
13400 +               goto out_h_parent;
13401 +       }
13402 +
13403 +       dentry = ERR_PTR(-ENOMEM);
13404 +       pathname = (void *)__get_free_page(GFP_NOFS);
13405 +       if (unlikely(!pathname))
13406 +               goto out_h_parent;
13407 +
13408 +       root = sb->s_root;
13409 +       path.mnt = h_mnt;
13410 +       di_read_lock_parent(root, !AuLock_IR);
13411 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13412 +       di_read_unlock(root, !AuLock_IR);
13413 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13414 +       dentry = (void *)p;
13415 +       if (IS_ERR(p))
13416 +               goto out_pathname;
13417 +
13418 +       si_read_unlock(sb);
13419 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13420 +       dentry = ERR_PTR(err);
13421 +       if (unlikely(err))
13422 +               goto out_relock;
13423 +
13424 +       dentry = ERR_PTR(-ENOENT);
13425 +       AuDebugOn(au_test_anon(path.dentry));
13426 +       if (unlikely(d_really_is_negative(path.dentry)))
13427 +               goto out_path;
13428 +
13429 +       if (ino != d_inode(path.dentry)->i_ino)
13430 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13431 +       else
13432 +               dentry = dget(path.dentry);
13433 +
13434 +out_path:
13435 +       path_put(&path);
13436 +out_relock:
13437 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13438 +               if (!IS_ERR(dentry)) {
13439 +                       dput(dentry);
13440 +                       dentry = ERR_PTR(-ESTALE);
13441 +               }
13442 +out_pathname:
13443 +       free_page((unsigned long)pathname);
13444 +out_h_parent:
13445 +       dput(h_parent);
13446 +out:
13447 +       AuTraceErrPtr(dentry);
13448 +       return dentry;
13449 +}
13450 +
13451 +/* ---------------------------------------------------------------------- */
13452 +
13453 +static struct dentry *
13454 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13455 +                 int fh_type)
13456 +{
13457 +       struct dentry *dentry;
13458 +       __u32 *fh = fid->raw;
13459 +       struct au_branch *br;
13460 +       ino_t ino, dir_ino;
13461 +       struct au_nfsd_si_lock nsi_lock = {
13462 +               .force_lock     = 0
13463 +       };
13464 +
13465 +       dentry = ERR_PTR(-ESTALE);
13466 +       /* it should never happen, but the file handle is unreliable */
13467 +       if (unlikely(fh_len < Fh_tail))
13468 +               goto out;
13469 +       nsi_lock.sigen = fh[Fh_sigen];
13470 +       nsi_lock.br_id = fh[Fh_br_id];
13471 +
13472 +       /* branch id may be wrapped around */
13473 +       br = NULL;
13474 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13475 +               goto out;
13476 +       nsi_lock.force_lock = 1;
13477 +
13478 +       /* is this inode still cached? */
13479 +       ino = decode_ino(fh + Fh_ino);
13480 +       /* it should never happen */
13481 +       if (unlikely(ino == AUFS_ROOT_INO))
13482 +               goto out_unlock;
13483 +
13484 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13485 +       dentry = decode_by_ino(sb, ino, dir_ino);
13486 +       if (IS_ERR(dentry))
13487 +               goto out_unlock;
13488 +       if (dentry)
13489 +               goto accept;
13490 +
13491 +       /* is the parent dir cached? */
13492 +       br = au_sbr(sb, nsi_lock.bindex);
13493 +       au_lcnt_inc(&br->br_nfiles);
13494 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13495 +       if (IS_ERR(dentry))
13496 +               goto out_unlock;
13497 +       if (dentry)
13498 +               goto accept;
13499 +
13500 +       /* lookup path */
13501 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13502 +       if (IS_ERR(dentry))
13503 +               goto out_unlock;
13504 +       if (unlikely(!dentry))
13505 +               /* todo?: make it ESTALE */
13506 +               goto out_unlock;
13507 +
13508 +accept:
13509 +       if (!au_digen_test(dentry, au_sigen(sb))
13510 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13511 +               goto out_unlock; /* success */
13512 +
13513 +       dput(dentry);
13514 +       dentry = ERR_PTR(-ESTALE);
13515 +out_unlock:
13516 +       if (br)
13517 +               au_lcnt_dec(&br->br_nfiles);
13518 +       si_read_unlock(sb);
13519 +out:
13520 +       AuTraceErrPtr(dentry);
13521 +       return dentry;
13522 +}
13523 +
13524 +#if 0 /* reserved for future use */
13525 +/* support subtreecheck option */
13526 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13527 +                                       int fh_len, int fh_type)
13528 +{
13529 +       struct dentry *parent;
13530 +       __u32 *fh = fid->raw;
13531 +       ino_t dir_ino;
13532 +
13533 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13534 +       parent = decode_by_ino(sb, dir_ino, 0);
13535 +       if (IS_ERR(parent))
13536 +               goto out;
13537 +       if (!parent)
13538 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13539 +                                       dir_ino, fh, fh_len);
13540 +
13541 +out:
13542 +       AuTraceErrPtr(parent);
13543 +       return parent;
13544 +}
13545 +#endif
13546 +
13547 +/* ---------------------------------------------------------------------- */
13548 +
13549 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13550 +                         struct inode *dir)
13551 +{
13552 +       int err;
13553 +       aufs_bindex_t bindex;
13554 +       struct super_block *sb, *h_sb;
13555 +       struct dentry *dentry, *parent, *h_parent;
13556 +       struct inode *h_dir;
13557 +       struct au_branch *br;
13558 +
13559 +       err = -ENOSPC;
13560 +       if (unlikely(*max_len <= Fh_tail)) {
13561 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13562 +               goto out;
13563 +       }
13564 +
13565 +       err = FILEID_ROOT;
13566 +       if (inode->i_ino == AUFS_ROOT_INO) {
13567 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13568 +               goto out;
13569 +       }
13570 +
13571 +       h_parent = NULL;
13572 +       sb = inode->i_sb;
13573 +       err = si_read_lock(sb, AuLock_FLUSH);
13574 +       if (unlikely(err))
13575 +               goto out;
13576 +
13577 +#ifdef CONFIG_AUFS_DEBUG
13578 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13579 +               AuWarn1("NFS-exporting requires xino\n");
13580 +#endif
13581 +       err = -EIO;
13582 +       parent = NULL;
13583 +       ii_read_lock_child(inode);
13584 +       bindex = au_ibtop(inode);
13585 +       if (!dir) {
13586 +               dentry = d_find_any_alias(inode);
13587 +               if (unlikely(!dentry))
13588 +                       goto out_unlock;
13589 +               AuDebugOn(au_test_anon(dentry));
13590 +               parent = dget_parent(dentry);
13591 +               dput(dentry);
13592 +               if (unlikely(!parent))
13593 +                       goto out_unlock;
13594 +               if (d_really_is_positive(parent))
13595 +                       dir = d_inode(parent);
13596 +       }
13597 +
13598 +       ii_read_lock_parent(dir);
13599 +       h_dir = au_h_iptr(dir, bindex);
13600 +       ii_read_unlock(dir);
13601 +       if (unlikely(!h_dir))
13602 +               goto out_parent;
13603 +       h_parent = d_find_any_alias(h_dir);
13604 +       if (unlikely(!h_parent))
13605 +               goto out_hparent;
13606 +
13607 +       err = -EPERM;
13608 +       br = au_sbr(sb, bindex);
13609 +       h_sb = au_br_sb(br);
13610 +       if (unlikely(!h_sb->s_export_op)) {
13611 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13612 +               goto out_hparent;
13613 +       }
13614 +
13615 +       fh[Fh_br_id] = br->br_id;
13616 +       fh[Fh_sigen] = au_sigen(sb);
13617 +       encode_ino(fh + Fh_ino, inode->i_ino);
13618 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13619 +       fh[Fh_igen] = inode->i_generation;
13620 +
13621 +       *max_len -= Fh_tail;
13622 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13623 +                                          max_len,
13624 +                                          /*connectable or subtreecheck*/0);
13625 +       err = fh[Fh_h_type];
13626 +       *max_len += Fh_tail;
13627 +       /* todo: macros? */
13628 +       if (err != FILEID_INVALID)
13629 +               err = 99;
13630 +       else
13631 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13632 +
13633 +out_hparent:
13634 +       dput(h_parent);
13635 +out_parent:
13636 +       dput(parent);
13637 +out_unlock:
13638 +       ii_read_unlock(inode);
13639 +       si_read_unlock(sb);
13640 +out:
13641 +       if (unlikely(err < 0))
13642 +               err = FILEID_INVALID;
13643 +       return err;
13644 +}
13645 +
13646 +/* ---------------------------------------------------------------------- */
13647 +
13648 +static int aufs_commit_metadata(struct inode *inode)
13649 +{
13650 +       int err;
13651 +       aufs_bindex_t bindex;
13652 +       struct super_block *sb;
13653 +       struct inode *h_inode;
13654 +       int (*f)(struct inode *inode);
13655 +
13656 +       sb = inode->i_sb;
13657 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13658 +       ii_write_lock_child(inode);
13659 +       bindex = au_ibtop(inode);
13660 +       AuDebugOn(bindex < 0);
13661 +       h_inode = au_h_iptr(inode, bindex);
13662 +
13663 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13664 +       if (f)
13665 +               err = f(h_inode);
13666 +       else {
13667 +               struct writeback_control wbc = {
13668 +                       .sync_mode      = WB_SYNC_ALL,
13669 +                       .nr_to_write    = 0 /* metadata only */
13670 +               };
13671 +
13672 +               err = sync_inode(h_inode, &wbc);
13673 +       }
13674 +
13675 +       au_cpup_attr_timesizes(inode);
13676 +       ii_write_unlock(inode);
13677 +       si_read_unlock(sb);
13678 +       return err;
13679 +}
13680 +
13681 +/* ---------------------------------------------------------------------- */
13682 +
13683 +static struct export_operations aufs_export_op = {
13684 +       .fh_to_dentry           = aufs_fh_to_dentry,
13685 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13686 +       .encode_fh              = aufs_encode_fh,
13687 +       .commit_metadata        = aufs_commit_metadata
13688 +};
13689 +
13690 +void au_export_init(struct super_block *sb)
13691 +{
13692 +       struct au_sbinfo *sbinfo;
13693 +       __u32 u;
13694 +
13695 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13696 +                        && IS_MODULE(CONFIG_EXPORTFS),
13697 +                        AUFS_NAME ": unsupported configuration "
13698 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13699 +
13700 +       sb->s_export_op = &aufs_export_op;
13701 +       sbinfo = au_sbi(sb);
13702 +       sbinfo->si_xigen = NULL;
13703 +       get_random_bytes(&u, sizeof(u));
13704 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13705 +       atomic_set(&sbinfo->si_xigen_next, u);
13706 +}
13707 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13708 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13709 +++ linux/fs/aufs/fhsm.c        2019-07-11 15:42:14.465571210 +0200
13710 @@ -0,0 +1,427 @@
13711 +// SPDX-License-Identifier: GPL-2.0
13712 +/*
13713 + * Copyright (C) 2011-2019 Junjiro R. Okajima
13714 + *
13715 + * This program, aufs is free software; you can redistribute it and/or modify
13716 + * it under the terms of the GNU General Public License as published by
13717 + * the Free Software Foundation; either version 2 of the License, or
13718 + * (at your option) any later version.
13719 + *
13720 + * This program is distributed in the hope that it will be useful,
13721 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13722 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13723 + * GNU General Public License for more details.
13724 + *
13725 + * You should have received a copy of the GNU General Public License
13726 + * along with this program; if not, write to the Free Software
13727 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13728 + */
13729 +
13730 +/*
13731 + * File-based Hierarchy Storage Management
13732 + */
13733 +
13734 +#include <linux/anon_inodes.h>
13735 +#include <linux/poll.h>
13736 +#include <linux/seq_file.h>
13737 +#include <linux/statfs.h>
13738 +#include "aufs.h"
13739 +
13740 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13741 +{
13742 +       struct au_sbinfo *sbinfo;
13743 +       struct au_fhsm *fhsm;
13744 +
13745 +       SiMustAnyLock(sb);
13746 +
13747 +       sbinfo = au_sbi(sb);
13748 +       fhsm = &sbinfo->si_fhsm;
13749 +       AuDebugOn(!fhsm);
13750 +       return fhsm->fhsm_bottom;
13751 +}
13752 +
13753 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13754 +{
13755 +       struct au_sbinfo *sbinfo;
13756 +       struct au_fhsm *fhsm;
13757 +
13758 +       SiMustWriteLock(sb);
13759 +
13760 +       sbinfo = au_sbi(sb);
13761 +       fhsm = &sbinfo->si_fhsm;
13762 +       AuDebugOn(!fhsm);
13763 +       fhsm->fhsm_bottom = bindex;
13764 +}
13765 +
13766 +/* ---------------------------------------------------------------------- */
13767 +
13768 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13769 +{
13770 +       struct au_br_fhsm *bf;
13771 +
13772 +       bf = br->br_fhsm;
13773 +       MtxMustLock(&bf->bf_lock);
13774 +
13775 +       return !bf->bf_readable
13776 +               || time_after(jiffies,
13777 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13778 +}
13779 +
13780 +/* ---------------------------------------------------------------------- */
13781 +
13782 +static void au_fhsm_notify(struct super_block *sb, int val)
13783 +{
13784 +       struct au_sbinfo *sbinfo;
13785 +       struct au_fhsm *fhsm;
13786 +
13787 +       SiMustAnyLock(sb);
13788 +
13789 +       sbinfo = au_sbi(sb);
13790 +       fhsm = &sbinfo->si_fhsm;
13791 +       if (au_fhsm_pid(fhsm)
13792 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13793 +               atomic_set(&fhsm->fhsm_readable, val);
13794 +               if (val)
13795 +                       wake_up(&fhsm->fhsm_wqh);
13796 +       }
13797 +}
13798 +
13799 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13800 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13801 +{
13802 +       int err;
13803 +       struct au_branch *br;
13804 +       struct au_br_fhsm *bf;
13805 +
13806 +       br = au_sbr(sb, bindex);
13807 +       AuDebugOn(au_br_rdonly(br));
13808 +       bf = br->br_fhsm;
13809 +       AuDebugOn(!bf);
13810 +
13811 +       if (do_lock)
13812 +               mutex_lock(&bf->bf_lock);
13813 +       else
13814 +               MtxMustLock(&bf->bf_lock);
13815 +
13816 +       /* sb->s_root for NFS is unreliable */
13817 +       err = au_br_stfs(br, &bf->bf_stfs);
13818 +       if (unlikely(err)) {
13819 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13820 +               goto out;
13821 +       }
13822 +
13823 +       bf->bf_jiffy = jiffies;
13824 +       bf->bf_readable = 1;
13825 +       if (do_notify)
13826 +               au_fhsm_notify(sb, /*val*/1);
13827 +       if (rstfs)
13828 +               *rstfs = bf->bf_stfs;
13829 +
13830 +out:
13831 +       if (do_lock)
13832 +               mutex_unlock(&bf->bf_lock);
13833 +       au_fhsm_notify(sb, /*val*/1);
13834 +
13835 +       return err;
13836 +}
13837 +
13838 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13839 +{
13840 +       int err;
13841 +       struct au_sbinfo *sbinfo;
13842 +       struct au_fhsm *fhsm;
13843 +       struct au_branch *br;
13844 +       struct au_br_fhsm *bf;
13845 +
13846 +       AuDbg("b%d, force %d\n", bindex, force);
13847 +       SiMustAnyLock(sb);
13848 +
13849 +       sbinfo = au_sbi(sb);
13850 +       fhsm = &sbinfo->si_fhsm;
13851 +       if (!au_ftest_si(sbinfo, FHSM)
13852 +           || fhsm->fhsm_bottom == bindex)
13853 +               return;
13854 +
13855 +       br = au_sbr(sb, bindex);
13856 +       bf = br->br_fhsm;
13857 +       AuDebugOn(!bf);
13858 +       mutex_lock(&bf->bf_lock);
13859 +       if (force
13860 +           || au_fhsm_pid(fhsm)
13861 +           || au_fhsm_test_jiffy(sbinfo, br))
13862 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13863 +                                 /*do_notify*/1);
13864 +       mutex_unlock(&bf->bf_lock);
13865 +}
13866 +
13867 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13868 +{
13869 +       aufs_bindex_t bindex, bbot;
13870 +       struct au_branch *br;
13871 +
13872 +       /* exclude the bottom */
13873 +       bbot = au_fhsm_bottom(sb);
13874 +       for (bindex = 0; bindex < bbot; bindex++) {
13875 +               br = au_sbr(sb, bindex);
13876 +               if (au_br_fhsm(br->br_perm))
13877 +                       au_fhsm_wrote(sb, bindex, force);
13878 +       }
13879 +}
13880 +
13881 +/* ---------------------------------------------------------------------- */
13882 +
13883 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13884 +{
13885 +       __poll_t mask;
13886 +       struct au_sbinfo *sbinfo;
13887 +       struct au_fhsm *fhsm;
13888 +
13889 +       mask = 0;
13890 +       sbinfo = file->private_data;
13891 +       fhsm = &sbinfo->si_fhsm;
13892 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13893 +       if (atomic_read(&fhsm->fhsm_readable))
13894 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13895 +
13896 +       if (!mask)
13897 +               AuDbg("mask 0x%x\n", mask);
13898 +       return mask;
13899 +}
13900 +
13901 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13902 +                             struct aufs_stfs *stfs, __s16 brid)
13903 +{
13904 +       int err;
13905 +
13906 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13907 +       if (!err)
13908 +               err = __put_user(brid, &stbr->brid);
13909 +       if (unlikely(err))
13910 +               err = -EFAULT;
13911 +
13912 +       return err;
13913 +}
13914 +
13915 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13916 +                              struct aufs_stbr __user *stbr, size_t count)
13917 +{
13918 +       ssize_t err;
13919 +       int nstbr;
13920 +       aufs_bindex_t bindex, bbot;
13921 +       struct au_branch *br;
13922 +       struct au_br_fhsm *bf;
13923 +
13924 +       /* except the bottom branch */
13925 +       err = 0;
13926 +       nstbr = 0;
13927 +       bbot = au_fhsm_bottom(sb);
13928 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13929 +               br = au_sbr(sb, bindex);
13930 +               if (!au_br_fhsm(br->br_perm))
13931 +                       continue;
13932 +
13933 +               bf = br->br_fhsm;
13934 +               mutex_lock(&bf->bf_lock);
13935 +               if (bf->bf_readable) {
13936 +                       err = -EFAULT;
13937 +                       if (count >= sizeof(*stbr))
13938 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13939 +                                                         br->br_id);
13940 +                       if (!err) {
13941 +                               bf->bf_readable = 0;
13942 +                               count -= sizeof(*stbr);
13943 +                               nstbr++;
13944 +                       }
13945 +               }
13946 +               mutex_unlock(&bf->bf_lock);
13947 +       }
13948 +       if (!err)
13949 +               err = sizeof(*stbr) * nstbr;
13950 +
13951 +       return err;
13952 +}
13953 +
13954 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13955 +                          loff_t *pos)
13956 +{
13957 +       ssize_t err;
13958 +       int readable;
13959 +       aufs_bindex_t nfhsm, bindex, bbot;
13960 +       struct au_sbinfo *sbinfo;
13961 +       struct au_fhsm *fhsm;
13962 +       struct au_branch *br;
13963 +       struct super_block *sb;
13964 +
13965 +       err = 0;
13966 +       sbinfo = file->private_data;
13967 +       fhsm = &sbinfo->si_fhsm;
13968 +need_data:
13969 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13970 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13971 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13972 +                       err = -EAGAIN;
13973 +               else
13974 +                       err = wait_event_interruptible_locked_irq
13975 +                               (fhsm->fhsm_wqh,
13976 +                                atomic_read(&fhsm->fhsm_readable));
13977 +       }
13978 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13979 +       if (unlikely(err))
13980 +               goto out;
13981 +
13982 +       /* sb may already be dead */
13983 +       au_rw_read_lock(&sbinfo->si_rwsem);
13984 +       readable = atomic_read(&fhsm->fhsm_readable);
13985 +       if (readable > 0) {
13986 +               sb = sbinfo->si_sb;
13987 +               AuDebugOn(!sb);
13988 +               /* exclude the bottom branch */
13989 +               nfhsm = 0;
13990 +               bbot = au_fhsm_bottom(sb);
13991 +               for (bindex = 0; bindex < bbot; bindex++) {
13992 +                       br = au_sbr(sb, bindex);
13993 +                       if (au_br_fhsm(br->br_perm))
13994 +                               nfhsm++;
13995 +               }
13996 +               err = -EMSGSIZE;
13997 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13998 +                       atomic_set(&fhsm->fhsm_readable, 0);
13999 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14000 +                                            count);
14001 +               }
14002 +       }
14003 +       au_rw_read_unlock(&sbinfo->si_rwsem);
14004 +       if (!readable)
14005 +               goto need_data;
14006 +
14007 +out:
14008 +       return err;
14009 +}
14010 +
14011 +static int au_fhsm_release(struct inode *inode, struct file *file)
14012 +{
14013 +       struct au_sbinfo *sbinfo;
14014 +       struct au_fhsm *fhsm;
14015 +
14016 +       /* sb may already be dead */
14017 +       sbinfo = file->private_data;
14018 +       fhsm = &sbinfo->si_fhsm;
14019 +       spin_lock(&fhsm->fhsm_spin);
14020 +       fhsm->fhsm_pid = 0;
14021 +       spin_unlock(&fhsm->fhsm_spin);
14022 +       kobject_put(&sbinfo->si_kobj);
14023 +
14024 +       return 0;
14025 +}
14026 +
14027 +static const struct file_operations au_fhsm_fops = {
14028 +       .owner          = THIS_MODULE,
14029 +       .llseek         = noop_llseek,
14030 +       .read           = au_fhsm_read,
14031 +       .poll           = au_fhsm_poll,
14032 +       .release        = au_fhsm_release
14033 +};
14034 +
14035 +int au_fhsm_fd(struct super_block *sb, int oflags)
14036 +{
14037 +       int err, fd;
14038 +       struct au_sbinfo *sbinfo;
14039 +       struct au_fhsm *fhsm;
14040 +
14041 +       err = -EPERM;
14042 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
14043 +               goto out;
14044 +
14045 +       err = -EINVAL;
14046 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14047 +               goto out;
14048 +
14049 +       err = 0;
14050 +       sbinfo = au_sbi(sb);
14051 +       fhsm = &sbinfo->si_fhsm;
14052 +       spin_lock(&fhsm->fhsm_spin);
14053 +       if (!fhsm->fhsm_pid)
14054 +               fhsm->fhsm_pid = current->pid;
14055 +       else
14056 +               err = -EBUSY;
14057 +       spin_unlock(&fhsm->fhsm_spin);
14058 +       if (unlikely(err))
14059 +               goto out;
14060 +
14061 +       oflags |= O_RDONLY;
14062 +       /* oflags |= FMODE_NONOTIFY; */
14063 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14064 +       err = fd;
14065 +       if (unlikely(fd < 0))
14066 +               goto out_pid;
14067 +
14068 +       /* succeed regardless 'fhsm' status */
14069 +       kobject_get(&sbinfo->si_kobj);
14070 +       si_noflush_read_lock(sb);
14071 +       if (au_ftest_si(sbinfo, FHSM))
14072 +               au_fhsm_wrote_all(sb, /*force*/0);
14073 +       si_read_unlock(sb);
14074 +       goto out; /* success */
14075 +
14076 +out_pid:
14077 +       spin_lock(&fhsm->fhsm_spin);
14078 +       fhsm->fhsm_pid = 0;
14079 +       spin_unlock(&fhsm->fhsm_spin);
14080 +out:
14081 +       AuTraceErr(err);
14082 +       return err;
14083 +}
14084 +
14085 +/* ---------------------------------------------------------------------- */
14086 +
14087 +int au_fhsm_br_alloc(struct au_branch *br)
14088 +{
14089 +       int err;
14090 +
14091 +       err = 0;
14092 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14093 +       if (br->br_fhsm)
14094 +               au_br_fhsm_init(br->br_fhsm);
14095 +       else
14096 +               err = -ENOMEM;
14097 +
14098 +       return err;
14099 +}
14100 +
14101 +/* ---------------------------------------------------------------------- */
14102 +
14103 +void au_fhsm_fin(struct super_block *sb)
14104 +{
14105 +       au_fhsm_notify(sb, /*val*/-1);
14106 +}
14107 +
14108 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14109 +{
14110 +       struct au_fhsm *fhsm;
14111 +
14112 +       fhsm = &sbinfo->si_fhsm;
14113 +       spin_lock_init(&fhsm->fhsm_spin);
14114 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14115 +       atomic_set(&fhsm->fhsm_readable, 0);
14116 +       fhsm->fhsm_expire
14117 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14118 +       fhsm->fhsm_bottom = -1;
14119 +}
14120 +
14121 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14122 +{
14123 +       sbinfo->si_fhsm.fhsm_expire
14124 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14125 +}
14126 +
14127 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14128 +{
14129 +       unsigned int u;
14130 +
14131 +       if (!au_ftest_si(sbinfo, FHSM))
14132 +               return;
14133 +
14134 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14135 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14136 +               seq_printf(seq, ",fhsm_sec=%u", u);
14137 +}
14138 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14139 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14140 +++ linux/fs/aufs/file.c        2019-07-11 15:42:14.465571210 +0200
14141 @@ -0,0 +1,863 @@
14142 +// SPDX-License-Identifier: GPL-2.0
14143 +/*
14144 + * Copyright (C) 2005-2019 Junjiro R. Okajima
14145 + *
14146 + * This program, aufs is free software; you can redistribute it and/or modify
14147 + * it under the terms of the GNU General Public License as published by
14148 + * the Free Software Foundation; either version 2 of the License, or
14149 + * (at your option) any later version.
14150 + *
14151 + * This program is distributed in the hope that it will be useful,
14152 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14153 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14154 + * GNU General Public License for more details.
14155 + *
14156 + * You should have received a copy of the GNU General Public License
14157 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14158 + */
14159 +
14160 +/*
14161 + * handling file/dir, and address_space operation
14162 + */
14163 +
14164 +#ifdef CONFIG_AUFS_DEBUG
14165 +#include <linux/migrate.h>
14166 +#endif
14167 +#include <linux/pagemap.h>
14168 +#include "aufs.h"
14169 +
14170 +/* drop flags for writing */
14171 +unsigned int au_file_roflags(unsigned int flags)
14172 +{
14173 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14174 +       flags |= O_RDONLY | O_NOATIME;
14175 +       return flags;
14176 +}
14177 +
14178 +/* common functions to regular file and dir */
14179 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14180 +                      struct file *file, int force_wr)
14181 +{
14182 +       struct file *h_file;
14183 +       struct dentry *h_dentry;
14184 +       struct inode *h_inode;
14185 +       struct super_block *sb;
14186 +       struct au_branch *br;
14187 +       struct path h_path;
14188 +       int err;
14189 +
14190 +       /* a race condition can happen between open and unlink/rmdir */
14191 +       h_file = ERR_PTR(-ENOENT);
14192 +       h_dentry = au_h_dptr(dentry, bindex);
14193 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14194 +               goto out;
14195 +       h_inode = d_inode(h_dentry);
14196 +       spin_lock(&h_dentry->d_lock);
14197 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14198 +               /* || !d_inode(dentry)->i_nlink */
14199 +               ;
14200 +       spin_unlock(&h_dentry->d_lock);
14201 +       if (unlikely(err))
14202 +               goto out;
14203 +
14204 +       sb = dentry->d_sb;
14205 +       br = au_sbr(sb, bindex);
14206 +       err = au_br_test_oflag(flags, br);
14207 +       h_file = ERR_PTR(err);
14208 +       if (unlikely(err))
14209 +               goto out;
14210 +
14211 +       /* drop flags for writing */
14212 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14213 +               if (force_wr && !(flags & O_WRONLY))
14214 +                       force_wr = 0;
14215 +               flags = au_file_roflags(flags);
14216 +               if (force_wr) {
14217 +                       h_file = ERR_PTR(-EROFS);
14218 +                       flags = au_file_roflags(flags);
14219 +                       if (unlikely(vfsub_native_ro(h_inode)
14220 +                                    || IS_APPEND(h_inode)))
14221 +                               goto out;
14222 +                       flags &= ~O_ACCMODE;
14223 +                       flags |= O_WRONLY;
14224 +               }
14225 +       }
14226 +       flags &= ~O_CREAT;
14227 +       au_lcnt_inc(&br->br_nfiles);
14228 +       h_path.dentry = h_dentry;
14229 +       h_path.mnt = au_br_mnt(br);
14230 +       h_file = vfsub_dentry_open(&h_path, flags);
14231 +       if (IS_ERR(h_file))
14232 +               goto out_br;
14233 +
14234 +       if (flags & __FMODE_EXEC) {
14235 +               err = deny_write_access(h_file);
14236 +               if (unlikely(err)) {
14237 +                       fput(h_file);
14238 +                       h_file = ERR_PTR(err);
14239 +                       goto out_br;
14240 +               }
14241 +       }
14242 +       fsnotify_open(h_file);
14243 +       goto out; /* success */
14244 +
14245 +out_br:
14246 +       au_lcnt_dec(&br->br_nfiles);
14247 +out:
14248 +       return h_file;
14249 +}
14250 +
14251 +static int au_cmoo(struct dentry *dentry)
14252 +{
14253 +       int err, cmoo, matched;
14254 +       unsigned int udba;
14255 +       struct path h_path;
14256 +       struct au_pin pin;
14257 +       struct au_cp_generic cpg = {
14258 +               .dentry = dentry,
14259 +               .bdst   = -1,
14260 +               .bsrc   = -1,
14261 +               .len    = -1,
14262 +               .pin    = &pin,
14263 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14264 +       };
14265 +       struct inode *delegated;
14266 +       struct super_block *sb;
14267 +       struct au_sbinfo *sbinfo;
14268 +       struct au_fhsm *fhsm;
14269 +       pid_t pid;
14270 +       struct au_branch *br;
14271 +       struct dentry *parent;
14272 +       struct au_hinode *hdir;
14273 +
14274 +       DiMustWriteLock(dentry);
14275 +       IiMustWriteLock(d_inode(dentry));
14276 +
14277 +       err = 0;
14278 +       if (IS_ROOT(dentry))
14279 +               goto out;
14280 +       cpg.bsrc = au_dbtop(dentry);
14281 +       if (!cpg.bsrc)
14282 +               goto out;
14283 +
14284 +       sb = dentry->d_sb;
14285 +       sbinfo = au_sbi(sb);
14286 +       fhsm = &sbinfo->si_fhsm;
14287 +       pid = au_fhsm_pid(fhsm);
14288 +       rcu_read_lock();
14289 +       matched = (pid
14290 +                  && (current->pid == pid
14291 +                      || rcu_dereference(current->real_parent)->pid == pid));
14292 +       rcu_read_unlock();
14293 +       if (matched)
14294 +               goto out;
14295 +
14296 +       br = au_sbr(sb, cpg.bsrc);
14297 +       cmoo = au_br_cmoo(br->br_perm);
14298 +       if (!cmoo)
14299 +               goto out;
14300 +       if (!d_is_reg(dentry))
14301 +               cmoo &= AuBrAttr_COO_ALL;
14302 +       if (!cmoo)
14303 +               goto out;
14304 +
14305 +       parent = dget_parent(dentry);
14306 +       di_write_lock_parent(parent);
14307 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14308 +       cpg.bdst = err;
14309 +       if (unlikely(err < 0)) {
14310 +               err = 0;        /* there is no upper writable branch */
14311 +               goto out_dgrade;
14312 +       }
14313 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14314 +
14315 +       /* do not respect the coo attrib for the target branch */
14316 +       err = au_cpup_dirs(dentry, cpg.bdst);
14317 +       if (unlikely(err))
14318 +               goto out_dgrade;
14319 +
14320 +       di_downgrade_lock(parent, AuLock_IR);
14321 +       udba = au_opt_udba(sb);
14322 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14323 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14324 +       if (unlikely(err))
14325 +               goto out_parent;
14326 +
14327 +       err = au_sio_cpup_simple(&cpg);
14328 +       au_unpin(&pin);
14329 +       if (unlikely(err))
14330 +               goto out_parent;
14331 +       if (!(cmoo & AuBrWAttr_MOO))
14332 +               goto out_parent; /* success */
14333 +
14334 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14335 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14336 +       if (unlikely(err))
14337 +               goto out_parent;
14338 +
14339 +       h_path.mnt = au_br_mnt(br);
14340 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14341 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14342 +       delegated = NULL;
14343 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14344 +       au_unpin(&pin);
14345 +       /* todo: keep h_dentry or not? */
14346 +       if (unlikely(err == -EWOULDBLOCK)) {
14347 +               pr_warn("cannot retry for NFSv4 delegation"
14348 +                       " for an internal unlink\n");
14349 +               iput(delegated);
14350 +       }
14351 +       if (unlikely(err)) {
14352 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14353 +                      dentry, err);
14354 +               err = 0;
14355 +       }
14356 +       goto out_parent; /* success */
14357 +
14358 +out_dgrade:
14359 +       di_downgrade_lock(parent, AuLock_IR);
14360 +out_parent:
14361 +       di_read_unlock(parent, AuLock_IR);
14362 +       dput(parent);
14363 +out:
14364 +       AuTraceErr(err);
14365 +       return err;
14366 +}
14367 +
14368 +int au_do_open(struct file *file, struct au_do_open_args *args)
14369 +{
14370 +       int err, aopen = args->aopen;
14371 +       struct dentry *dentry;
14372 +       struct au_finfo *finfo;
14373 +
14374 +       if (!aopen)
14375 +               err = au_finfo_init(file, args->fidir);
14376 +       else {
14377 +               lockdep_off();
14378 +               err = au_finfo_init(file, args->fidir);
14379 +               lockdep_on();
14380 +       }
14381 +       if (unlikely(err))
14382 +               goto out;
14383 +
14384 +       dentry = file->f_path.dentry;
14385 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14386 +       di_write_lock_child(dentry);
14387 +       err = au_cmoo(dentry);
14388 +       di_downgrade_lock(dentry, AuLock_IR);
14389 +       if (!err) {
14390 +               if (!aopen)
14391 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14392 +               else {
14393 +                       lockdep_off();
14394 +                       err = args->open(file, vfsub_file_flags(file),
14395 +                                        args->h_file);
14396 +                       lockdep_on();
14397 +               }
14398 +       }
14399 +       di_read_unlock(dentry, AuLock_IR);
14400 +
14401 +       finfo = au_fi(file);
14402 +       if (!err) {
14403 +               finfo->fi_file = file;
14404 +               au_hbl_add(&finfo->fi_hlist,
14405 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14406 +       }
14407 +       if (!aopen)
14408 +               fi_write_unlock(file);
14409 +       else {
14410 +               lockdep_off();
14411 +               fi_write_unlock(file);
14412 +               lockdep_on();
14413 +       }
14414 +       if (unlikely(err)) {
14415 +               finfo->fi_hdir = NULL;
14416 +               au_finfo_fin(file);
14417 +       }
14418 +
14419 +out:
14420 +       AuTraceErr(err);
14421 +       return err;
14422 +}
14423 +
14424 +int au_reopen_nondir(struct file *file)
14425 +{
14426 +       int err;
14427 +       aufs_bindex_t btop;
14428 +       struct dentry *dentry;
14429 +       struct au_branch *br;
14430 +       struct file *h_file, *h_file_tmp;
14431 +
14432 +       dentry = file->f_path.dentry;
14433 +       btop = au_dbtop(dentry);
14434 +       br = au_sbr(dentry->d_sb, btop);
14435 +       h_file_tmp = NULL;
14436 +       if (au_fbtop(file) == btop) {
14437 +               h_file = au_hf_top(file);
14438 +               if (file->f_mode == h_file->f_mode)
14439 +                       return 0; /* success */
14440 +               h_file_tmp = h_file;
14441 +               get_file(h_file_tmp);
14442 +               au_lcnt_inc(&br->br_nfiles);
14443 +               au_set_h_fptr(file, btop, NULL);
14444 +       }
14445 +       AuDebugOn(au_fi(file)->fi_hdir);
14446 +       /*
14447 +        * it can happen
14448 +        * file exists on both of rw and ro
14449 +        * open --> dbtop and fbtop are both 0
14450 +        * prepend a branch as rw, "rw" become ro
14451 +        * remove rw/file
14452 +        * delete the top branch, "rw" becomes rw again
14453 +        *      --> dbtop is 1, fbtop is still 0
14454 +        * write --> fbtop is 0 but dbtop is 1
14455 +        */
14456 +       /* AuDebugOn(au_fbtop(file) < btop); */
14457 +
14458 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14459 +                          file, /*force_wr*/0);
14460 +       err = PTR_ERR(h_file);
14461 +       if (IS_ERR(h_file)) {
14462 +               if (h_file_tmp) {
14463 +                       /* revert */
14464 +                       au_set_h_fptr(file, btop, h_file_tmp);
14465 +                       h_file_tmp = NULL;
14466 +               }
14467 +               goto out; /* todo: close all? */
14468 +       }
14469 +
14470 +       err = 0;
14471 +       au_set_fbtop(file, btop);
14472 +       au_set_h_fptr(file, btop, h_file);
14473 +       au_update_figen(file);
14474 +       /* todo: necessary? */
14475 +       /* file->f_ra = h_file->f_ra; */
14476 +
14477 +out:
14478 +       if (h_file_tmp) {
14479 +               fput(h_file_tmp);
14480 +               au_lcnt_dec(&br->br_nfiles);
14481 +       }
14482 +       return err;
14483 +}
14484 +
14485 +/* ---------------------------------------------------------------------- */
14486 +
14487 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14488 +                       struct dentry *hi_wh)
14489 +{
14490 +       int err;
14491 +       aufs_bindex_t btop;
14492 +       struct au_dinfo *dinfo;
14493 +       struct dentry *h_dentry;
14494 +       struct au_hdentry *hdp;
14495 +
14496 +       dinfo = au_di(file->f_path.dentry);
14497 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14498 +
14499 +       btop = dinfo->di_btop;
14500 +       dinfo->di_btop = btgt;
14501 +       hdp = au_hdentry(dinfo, btgt);
14502 +       h_dentry = hdp->hd_dentry;
14503 +       hdp->hd_dentry = hi_wh;
14504 +       err = au_reopen_nondir(file);
14505 +       hdp->hd_dentry = h_dentry;
14506 +       dinfo->di_btop = btop;
14507 +
14508 +       return err;
14509 +}
14510 +
14511 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14512 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14513 +{
14514 +       int err;
14515 +       struct inode *inode, *h_inode;
14516 +       struct dentry *h_dentry, *hi_wh;
14517 +       struct au_cp_generic cpg = {
14518 +               .dentry = file->f_path.dentry,
14519 +               .bdst   = bcpup,
14520 +               .bsrc   = -1,
14521 +               .len    = len,
14522 +               .pin    = pin
14523 +       };
14524 +
14525 +       au_update_dbtop(cpg.dentry);
14526 +       inode = d_inode(cpg.dentry);
14527 +       h_inode = NULL;
14528 +       if (au_dbtop(cpg.dentry) <= bcpup
14529 +           && au_dbbot(cpg.dentry) >= bcpup) {
14530 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14531 +               if (h_dentry && d_is_positive(h_dentry))
14532 +                       h_inode = d_inode(h_dentry);
14533 +       }
14534 +       hi_wh = au_hi_wh(inode, bcpup);
14535 +       if (!hi_wh && !h_inode)
14536 +               err = au_sio_cpup_wh(&cpg, file);
14537 +       else
14538 +               /* already copied-up after unlink */
14539 +               err = au_reopen_wh(file, bcpup, hi_wh);
14540 +
14541 +       if (!err
14542 +           && (inode->i_nlink > 1
14543 +               || (inode->i_state & I_LINKABLE))
14544 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14545 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14546 +
14547 +       return err;
14548 +}
14549 +
14550 +/*
14551 + * prepare the @file for writing.
14552 + */
14553 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14554 +{
14555 +       int err;
14556 +       aufs_bindex_t dbtop;
14557 +       struct dentry *parent;
14558 +       struct inode *inode;
14559 +       struct super_block *sb;
14560 +       struct file *h_file;
14561 +       struct au_cp_generic cpg = {
14562 +               .dentry = file->f_path.dentry,
14563 +               .bdst   = -1,
14564 +               .bsrc   = -1,
14565 +               .len    = len,
14566 +               .pin    = pin,
14567 +               .flags  = AuCpup_DTIME
14568 +       };
14569 +
14570 +       sb = cpg.dentry->d_sb;
14571 +       inode = d_inode(cpg.dentry);
14572 +       cpg.bsrc = au_fbtop(file);
14573 +       err = au_test_ro(sb, cpg.bsrc, inode);
14574 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14575 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14576 +                            /*flags*/0);
14577 +               goto out;
14578 +       }
14579 +
14580 +       /* need to cpup or reopen */
14581 +       parent = dget_parent(cpg.dentry);
14582 +       di_write_lock_parent(parent);
14583 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14584 +       cpg.bdst = err;
14585 +       if (unlikely(err < 0))
14586 +               goto out_dgrade;
14587 +       err = 0;
14588 +
14589 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14590 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14591 +               if (unlikely(err))
14592 +                       goto out_dgrade;
14593 +       }
14594 +
14595 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14596 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14597 +       if (unlikely(err))
14598 +               goto out_dgrade;
14599 +
14600 +       dbtop = au_dbtop(cpg.dentry);
14601 +       if (dbtop <= cpg.bdst)
14602 +               cpg.bsrc = cpg.bdst;
14603 +
14604 +       if (dbtop <= cpg.bdst           /* just reopen */
14605 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14606 +               ) {
14607 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14608 +               if (IS_ERR(h_file))
14609 +                       err = PTR_ERR(h_file);
14610 +               else {
14611 +                       di_downgrade_lock(parent, AuLock_IR);
14612 +                       if (dbtop > cpg.bdst)
14613 +                               err = au_sio_cpup_simple(&cpg);
14614 +                       if (!err)
14615 +                               err = au_reopen_nondir(file);
14616 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14617 +               }
14618 +       } else {                        /* copyup as wh and reopen */
14619 +               /*
14620 +                * since writable hfsplus branch is not supported,
14621 +                * h_open_pre/post() are unnecessary.
14622 +                */
14623 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14624 +               di_downgrade_lock(parent, AuLock_IR);
14625 +       }
14626 +
14627 +       if (!err) {
14628 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14629 +               goto out_dput; /* success */
14630 +       }
14631 +       au_unpin(pin);
14632 +       goto out_unlock;
14633 +
14634 +out_dgrade:
14635 +       di_downgrade_lock(parent, AuLock_IR);
14636 +out_unlock:
14637 +       di_read_unlock(parent, AuLock_IR);
14638 +out_dput:
14639 +       dput(parent);
14640 +out:
14641 +       return err;
14642 +}
14643 +
14644 +/* ---------------------------------------------------------------------- */
14645 +
14646 +int au_do_flush(struct file *file, fl_owner_t id,
14647 +               int (*flush)(struct file *file, fl_owner_t id))
14648 +{
14649 +       int err;
14650 +       struct super_block *sb;
14651 +       struct inode *inode;
14652 +
14653 +       inode = file_inode(file);
14654 +       sb = inode->i_sb;
14655 +       si_noflush_read_lock(sb);
14656 +       fi_read_lock(file);
14657 +       ii_read_lock_child(inode);
14658 +
14659 +       err = flush(file, id);
14660 +       au_cpup_attr_timesizes(inode);
14661 +
14662 +       ii_read_unlock(inode);
14663 +       fi_read_unlock(file);
14664 +       si_read_unlock(sb);
14665 +       return err;
14666 +}
14667 +
14668 +/* ---------------------------------------------------------------------- */
14669 +
14670 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14671 +{
14672 +       int err;
14673 +       struct au_pin pin;
14674 +       struct au_finfo *finfo;
14675 +       struct dentry *parent, *hi_wh;
14676 +       struct inode *inode;
14677 +       struct super_block *sb;
14678 +       struct au_cp_generic cpg = {
14679 +               .dentry = file->f_path.dentry,
14680 +               .bdst   = -1,
14681 +               .bsrc   = -1,
14682 +               .len    = -1,
14683 +               .pin    = &pin,
14684 +               .flags  = AuCpup_DTIME
14685 +       };
14686 +
14687 +       FiMustWriteLock(file);
14688 +
14689 +       err = 0;
14690 +       finfo = au_fi(file);
14691 +       sb = cpg.dentry->d_sb;
14692 +       inode = d_inode(cpg.dentry);
14693 +       cpg.bdst = au_ibtop(inode);
14694 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14695 +               goto out;
14696 +
14697 +       parent = dget_parent(cpg.dentry);
14698 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14699 +               di_read_lock_parent(parent, !AuLock_IR);
14700 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14701 +               cpg.bdst = err;
14702 +               di_read_unlock(parent, !AuLock_IR);
14703 +               if (unlikely(err < 0))
14704 +                       goto out_parent;
14705 +               err = 0;
14706 +       }
14707 +
14708 +       di_read_lock_parent(parent, AuLock_IR);
14709 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14710 +       if (!S_ISDIR(inode->i_mode)
14711 +           && au_opt_test(au_mntflags(sb), PLINK)
14712 +           && au_plink_test(inode)
14713 +           && !d_unhashed(cpg.dentry)
14714 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14715 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14716 +               if (unlikely(err))
14717 +                       goto out_unlock;
14718 +
14719 +               /* always superio. */
14720 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14721 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14722 +               if (!err) {
14723 +                       err = au_sio_cpup_simple(&cpg);
14724 +                       au_unpin(&pin);
14725 +               }
14726 +       } else if (hi_wh) {
14727 +               /* already copied-up after unlink */
14728 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14729 +               *need_reopen = 0;
14730 +       }
14731 +
14732 +out_unlock:
14733 +       di_read_unlock(parent, AuLock_IR);
14734 +out_parent:
14735 +       dput(parent);
14736 +out:
14737 +       return err;
14738 +}
14739 +
14740 +static void au_do_refresh_dir(struct file *file)
14741 +{
14742 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14743 +       struct au_hfile *p, tmp, *q;
14744 +       struct au_finfo *finfo;
14745 +       struct super_block *sb;
14746 +       struct au_fidir *fidir;
14747 +
14748 +       FiMustWriteLock(file);
14749 +
14750 +       sb = file->f_path.dentry->d_sb;
14751 +       finfo = au_fi(file);
14752 +       fidir = finfo->fi_hdir;
14753 +       AuDebugOn(!fidir);
14754 +       p = fidir->fd_hfile + finfo->fi_btop;
14755 +       brid = p->hf_br->br_id;
14756 +       bbot = fidir->fd_bbot;
14757 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14758 +               if (!p->hf_file)
14759 +                       continue;
14760 +
14761 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14762 +               if (new_bindex == bindex)
14763 +                       continue;
14764 +               if (new_bindex < 0) {
14765 +                       au_set_h_fptr(file, bindex, NULL);
14766 +                       continue;
14767 +               }
14768 +
14769 +               /* swap two lower inode, and loop again */
14770 +               q = fidir->fd_hfile + new_bindex;
14771 +               tmp = *q;
14772 +               *q = *p;
14773 +               *p = tmp;
14774 +               if (tmp.hf_file) {
14775 +                       bindex--;
14776 +                       p--;
14777 +               }
14778 +       }
14779 +
14780 +       p = fidir->fd_hfile;
14781 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14782 +               bbot = au_sbbot(sb);
14783 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14784 +                    finfo->fi_btop++, p++)
14785 +                       if (p->hf_file) {
14786 +                               if (file_inode(p->hf_file))
14787 +                                       break;
14788 +                               au_hfput(p, /*execed*/0);
14789 +                       }
14790 +       } else {
14791 +               bbot = au_br_index(sb, brid);
14792 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14793 +                    finfo->fi_btop++, p++)
14794 +                       if (p->hf_file)
14795 +                               au_hfput(p, /*execed*/0);
14796 +               bbot = au_sbbot(sb);
14797 +       }
14798 +
14799 +       p = fidir->fd_hfile + bbot;
14800 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14801 +            fidir->fd_bbot--, p--)
14802 +               if (p->hf_file) {
14803 +                       if (file_inode(p->hf_file))
14804 +                               break;
14805 +                       au_hfput(p, /*execed*/0);
14806 +               }
14807 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14808 +}
14809 +
14810 +/*
14811 + * after branch manipulating, refresh the file.
14812 + */
14813 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14814 +{
14815 +       int err, need_reopen, nbr;
14816 +       aufs_bindex_t bbot, bindex;
14817 +       struct dentry *dentry;
14818 +       struct super_block *sb;
14819 +       struct au_finfo *finfo;
14820 +       struct au_hfile *hfile;
14821 +
14822 +       dentry = file->f_path.dentry;
14823 +       sb = dentry->d_sb;
14824 +       nbr = au_sbbot(sb) + 1;
14825 +       finfo = au_fi(file);
14826 +       if (!finfo->fi_hdir) {
14827 +               hfile = &finfo->fi_htop;
14828 +               AuDebugOn(!hfile->hf_file);
14829 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14830 +               AuDebugOn(bindex < 0);
14831 +               if (bindex != finfo->fi_btop)
14832 +                       au_set_fbtop(file, bindex);
14833 +       } else {
14834 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14835 +               if (unlikely(err))
14836 +                       goto out;
14837 +               au_do_refresh_dir(file);
14838 +       }
14839 +
14840 +       err = 0;
14841 +       need_reopen = 1;
14842 +       if (!au_test_mmapped(file))
14843 +               err = au_file_refresh_by_inode(file, &need_reopen);
14844 +       if (finfo->fi_hdir)
14845 +               /* harmless if err */
14846 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14847 +       if (!err && need_reopen && !d_unlinked(dentry))
14848 +               err = reopen(file);
14849 +       if (!err) {
14850 +               au_update_figen(file);
14851 +               goto out; /* success */
14852 +       }
14853 +
14854 +       /* error, close all lower files */
14855 +       if (finfo->fi_hdir) {
14856 +               bbot = au_fbbot_dir(file);
14857 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14858 +                       au_set_h_fptr(file, bindex, NULL);
14859 +       }
14860 +
14861 +out:
14862 +       return err;
14863 +}
14864 +
14865 +/* common function to regular file and dir */
14866 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14867 +                         int wlock, unsigned int fi_lsc)
14868 +{
14869 +       int err;
14870 +       unsigned int sigen, figen;
14871 +       aufs_bindex_t btop;
14872 +       unsigned char pseudo_link;
14873 +       struct dentry *dentry;
14874 +       struct inode *inode;
14875 +
14876 +       err = 0;
14877 +       dentry = file->f_path.dentry;
14878 +       inode = d_inode(dentry);
14879 +       sigen = au_sigen(dentry->d_sb);
14880 +       fi_write_lock_nested(file, fi_lsc);
14881 +       figen = au_figen(file);
14882 +       if (!fi_lsc)
14883 +               di_write_lock_child(dentry);
14884 +       else
14885 +               di_write_lock_child2(dentry);
14886 +       btop = au_dbtop(dentry);
14887 +       pseudo_link = (btop != au_ibtop(inode));
14888 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14889 +               if (!wlock) {
14890 +                       di_downgrade_lock(dentry, AuLock_IR);
14891 +                       fi_downgrade_lock(file);
14892 +               }
14893 +               goto out; /* success */
14894 +       }
14895 +
14896 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14897 +       if (au_digen_test(dentry, sigen)) {
14898 +               err = au_reval_dpath(dentry, sigen);
14899 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14900 +       }
14901 +
14902 +       if (!err)
14903 +               err = refresh_file(file, reopen);
14904 +       if (!err) {
14905 +               if (!wlock) {
14906 +                       di_downgrade_lock(dentry, AuLock_IR);
14907 +                       fi_downgrade_lock(file);
14908 +               }
14909 +       } else {
14910 +               di_write_unlock(dentry);
14911 +               fi_write_unlock(file);
14912 +       }
14913 +
14914 +out:
14915 +       return err;
14916 +}
14917 +
14918 +/* ---------------------------------------------------------------------- */
14919 +
14920 +/* cf. aufs_nopage() */
14921 +/* for madvise(2) */
14922 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14923 +{
14924 +       unlock_page(page);
14925 +       return 0;
14926 +}
14927 +
14928 +/* it will never be called, but necessary to support O_DIRECT */
14929 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14930 +{ BUG(); return 0; }
14931 +
14932 +/* they will never be called. */
14933 +#ifdef CONFIG_AUFS_DEBUG
14934 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14935 +                           loff_t pos, unsigned len, unsigned flags,
14936 +                           struct page **pagep, void **fsdata)
14937 +{ AuUnsupport(); return 0; }
14938 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14939 +                         loff_t pos, unsigned len, unsigned copied,
14940 +                         struct page *page, void *fsdata)
14941 +{ AuUnsupport(); return 0; }
14942 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14943 +{ AuUnsupport(); return 0; }
14944 +
14945 +static int aufs_set_page_dirty(struct page *page)
14946 +{ AuUnsupport(); return 0; }
14947 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14948 +                               unsigned int length)
14949 +{ AuUnsupport(); }
14950 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14951 +{ AuUnsupport(); return 0; }
14952 +#if 0 /* called by memory compaction regardless file */
14953 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14954 +                           struct page *page, enum migrate_mode mode)
14955 +{ AuUnsupport(); return 0; }
14956 +#endif
14957 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14958 +{ AuUnsupport(); return true; }
14959 +static void aufs_putback_page(struct page *page)
14960 +{ AuUnsupport(); }
14961 +static int aufs_launder_page(struct page *page)
14962 +{ AuUnsupport(); return 0; }
14963 +static int aufs_is_partially_uptodate(struct page *page,
14964 +                                     unsigned long from,
14965 +                                     unsigned long count)
14966 +{ AuUnsupport(); return 0; }
14967 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14968 +                                   bool *writeback)
14969 +{ AuUnsupport(); }
14970 +static int aufs_error_remove_page(struct address_space *mapping,
14971 +                                 struct page *page)
14972 +{ AuUnsupport(); return 0; }
14973 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14974 +                             sector_t *span)
14975 +{ AuUnsupport(); return 0; }
14976 +static void aufs_swap_deactivate(struct file *file)
14977 +{ AuUnsupport(); }
14978 +#endif /* CONFIG_AUFS_DEBUG */
14979 +
14980 +const struct address_space_operations aufs_aop = {
14981 +       .readpage               = aufs_readpage,
14982 +       .direct_IO              = aufs_direct_IO,
14983 +#ifdef CONFIG_AUFS_DEBUG
14984 +       .writepage              = aufs_writepage,
14985 +       /* no writepages, because of writepage */
14986 +       .set_page_dirty         = aufs_set_page_dirty,
14987 +       /* no readpages, because of readpage */
14988 +       .write_begin            = aufs_write_begin,
14989 +       .write_end              = aufs_write_end,
14990 +       /* no bmap, no block device */
14991 +       .invalidatepage         = aufs_invalidatepage,
14992 +       .releasepage            = aufs_releasepage,
14993 +       /* is fallback_migrate_page ok? */
14994 +       /* .migratepage         = aufs_migratepage, */
14995 +       .isolate_page           = aufs_isolate_page,
14996 +       .putback_page           = aufs_putback_page,
14997 +       .launder_page           = aufs_launder_page,
14998 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14999 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
15000 +       .error_remove_page      = aufs_error_remove_page,
15001 +       .swap_activate          = aufs_swap_activate,
15002 +       .swap_deactivate        = aufs_swap_deactivate
15003 +#endif /* CONFIG_AUFS_DEBUG */
15004 +};
15005 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15006 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
15007 +++ linux/fs/aufs/file.h        2019-07-11 15:42:14.465571210 +0200
15008 @@ -0,0 +1,342 @@
15009 +/* SPDX-License-Identifier: GPL-2.0 */
15010 +/*
15011 + * Copyright (C) 2005-2019 Junjiro R. Okajima
15012 + *
15013 + * This program, aufs is free software; you can redistribute it and/or modify
15014 + * it under the terms of the GNU General Public License as published by
15015 + * the Free Software Foundation; either version 2 of the License, or
15016 + * (at your option) any later version.
15017 + *
15018 + * This program is distributed in the hope that it will be useful,
15019 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15020 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15021 + * GNU General Public License for more details.
15022 + *
15023 + * You should have received a copy of the GNU General Public License
15024 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15025 + */
15026 +
15027 +/*
15028 + * file operations
15029 + */
15030 +
15031 +#ifndef __AUFS_FILE_H__
15032 +#define __AUFS_FILE_H__
15033 +
15034 +#ifdef __KERNEL__
15035 +
15036 +#include <linux/file.h>
15037 +#include <linux/fs.h>
15038 +#include <linux/mm_types.h>
15039 +#include <linux/poll.h>
15040 +#include "rwsem.h"
15041 +
15042 +struct au_branch;
15043 +struct au_hfile {
15044 +       struct file             *hf_file;
15045 +       struct au_branch        *hf_br;
15046 +};
15047 +
15048 +struct au_vdir;
15049 +struct au_fidir {
15050 +       aufs_bindex_t           fd_bbot;
15051 +       aufs_bindex_t           fd_nent;
15052 +       struct au_vdir          *fd_vdir_cache;
15053 +       struct au_hfile         fd_hfile[];
15054 +};
15055 +
15056 +static inline int au_fidir_sz(int nent)
15057 +{
15058 +       AuDebugOn(nent < 0);
15059 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15060 +}
15061 +
15062 +struct au_finfo {
15063 +       atomic_t                fi_generation;
15064 +
15065 +       struct au_rwsem         fi_rwsem;
15066 +       aufs_bindex_t           fi_btop;
15067 +
15068 +       /* do not union them */
15069 +       struct {                                /* for non-dir */
15070 +               struct au_hfile                 fi_htop;
15071 +               atomic_t                        fi_mmapped;
15072 +       };
15073 +       struct au_fidir         *fi_hdir;       /* for dir only */
15074 +
15075 +       struct hlist_bl_node    fi_hlist;
15076 +       struct file             *fi_file;       /* very ugly */
15077 +       struct rcu_head         rcu;
15078 +} ____cacheline_aligned_in_smp;
15079 +
15080 +/* ---------------------------------------------------------------------- */
15081 +
15082 +/* file.c */
15083 +extern const struct address_space_operations aufs_aop;
15084 +unsigned int au_file_roflags(unsigned int flags);
15085 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15086 +                      struct file *file, int force_wr);
15087 +struct au_do_open_args {
15088 +       int             aopen;
15089 +       int             (*open)(struct file *file, int flags,
15090 +                               struct file *h_file);
15091 +       struct au_fidir *fidir;
15092 +       struct file     *h_file;
15093 +};
15094 +int au_do_open(struct file *file, struct au_do_open_args *args);
15095 +int au_reopen_nondir(struct file *file);
15096 +struct au_pin;
15097 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15098 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15099 +                         int wlock, unsigned int fi_lsc);
15100 +int au_do_flush(struct file *file, fl_owner_t id,
15101 +               int (*flush)(struct file *file, fl_owner_t id));
15102 +
15103 +/* poll.c */
15104 +#ifdef CONFIG_AUFS_POLL
15105 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15106 +#endif
15107 +
15108 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15109 +/* hfsplus.c */
15110 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15111 +                          int force_wr);
15112 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15113 +                   struct file *h_file);
15114 +#else
15115 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15116 +       aufs_bindex_t bindex, int force_wr)
15117 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15118 +          struct file *h_file);
15119 +#endif
15120 +
15121 +/* f_op.c */
15122 +extern const struct file_operations aufs_file_fop;
15123 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15124 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15125 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15126 +
15127 +/* finfo.c */
15128 +void au_hfput(struct au_hfile *hf, int execed);
15129 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15130 +                  struct file *h_file);
15131 +
15132 +void au_update_figen(struct file *file);
15133 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15134 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15135 +
15136 +void au_fi_init_once(void *_fi);
15137 +void au_finfo_fin(struct file *file);
15138 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15139 +
15140 +/* ioctl.c */
15141 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15142 +#ifdef CONFIG_COMPAT
15143 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15144 +                          unsigned long arg);
15145 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15146 +                             unsigned long arg);
15147 +#endif
15148 +
15149 +/* ---------------------------------------------------------------------- */
15150 +
15151 +static inline struct au_finfo *au_fi(struct file *file)
15152 +{
15153 +       return file->private_data;
15154 +}
15155 +
15156 +/* ---------------------------------------------------------------------- */
15157 +
15158 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15159 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15160 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15161 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15162 +/*
15163 +#define fi_read_trylock_nested(f) \
15164 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15165 +#define fi_write_trylock_nested(f) \
15166 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15167 +*/
15168 +
15169 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15170 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15171 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15172 +
15173 +/* lock subclass for finfo */
15174 +enum {
15175 +       AuLsc_FI_1,
15176 +       AuLsc_FI_2
15177 +};
15178 +
15179 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15180 +{
15181 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15182 +}
15183 +
15184 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15185 +{
15186 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15187 +}
15188 +
15189 +/*
15190 + * fi_read_lock_1, fi_write_lock_1,
15191 + * fi_read_lock_2, fi_write_lock_2
15192 + */
15193 +#define AuReadLockFunc(name) \
15194 +static inline void fi_read_lock_##name(struct file *f) \
15195 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15196 +
15197 +#define AuWriteLockFunc(name) \
15198 +static inline void fi_write_lock_##name(struct file *f) \
15199 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15200 +
15201 +#define AuRWLockFuncs(name) \
15202 +       AuReadLockFunc(name) \
15203 +       AuWriteLockFunc(name)
15204 +
15205 +AuRWLockFuncs(1);
15206 +AuRWLockFuncs(2);
15207 +
15208 +#undef AuReadLockFunc
15209 +#undef AuWriteLockFunc
15210 +#undef AuRWLockFuncs
15211 +
15212 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15213 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15214 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15215 +
15216 +/* ---------------------------------------------------------------------- */
15217 +
15218 +/* todo: hard/soft set? */
15219 +static inline aufs_bindex_t au_fbtop(struct file *file)
15220 +{
15221 +       FiMustAnyLock(file);
15222 +       return au_fi(file)->fi_btop;
15223 +}
15224 +
15225 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15226 +{
15227 +       FiMustAnyLock(file);
15228 +       AuDebugOn(!au_fi(file)->fi_hdir);
15229 +       return au_fi(file)->fi_hdir->fd_bbot;
15230 +}
15231 +
15232 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15233 +{
15234 +       FiMustAnyLock(file);
15235 +       AuDebugOn(!au_fi(file)->fi_hdir);
15236 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15237 +}
15238 +
15239 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15240 +{
15241 +       FiMustWriteLock(file);
15242 +       au_fi(file)->fi_btop = bindex;
15243 +}
15244 +
15245 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15246 +{
15247 +       FiMustWriteLock(file);
15248 +       AuDebugOn(!au_fi(file)->fi_hdir);
15249 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15250 +}
15251 +
15252 +static inline void au_set_fvdir_cache(struct file *file,
15253 +                                     struct au_vdir *vdir_cache)
15254 +{
15255 +       FiMustWriteLock(file);
15256 +       AuDebugOn(!au_fi(file)->fi_hdir);
15257 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15258 +}
15259 +
15260 +static inline struct file *au_hf_top(struct file *file)
15261 +{
15262 +       FiMustAnyLock(file);
15263 +       AuDebugOn(au_fi(file)->fi_hdir);
15264 +       return au_fi(file)->fi_htop.hf_file;
15265 +}
15266 +
15267 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15268 +{
15269 +       FiMustAnyLock(file);
15270 +       AuDebugOn(!au_fi(file)->fi_hdir);
15271 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15272 +}
15273 +
15274 +/* todo: memory barrier? */
15275 +static inline unsigned int au_figen(struct file *f)
15276 +{
15277 +       return atomic_read(&au_fi(f)->fi_generation);
15278 +}
15279 +
15280 +static inline void au_set_mmapped(struct file *f)
15281 +{
15282 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15283 +               return;
15284 +       pr_warn("fi_mmapped wrapped around\n");
15285 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15286 +               ;
15287 +}
15288 +
15289 +static inline void au_unset_mmapped(struct file *f)
15290 +{
15291 +       atomic_dec(&au_fi(f)->fi_mmapped);
15292 +}
15293 +
15294 +static inline int au_test_mmapped(struct file *f)
15295 +{
15296 +       return atomic_read(&au_fi(f)->fi_mmapped);
15297 +}
15298 +
15299 +/* customize vma->vm_file */
15300 +
15301 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15302 +                                      struct file *file)
15303 +{
15304 +       struct file *f;
15305 +
15306 +       f = vma->vm_file;
15307 +       get_file(file);
15308 +       vma->vm_file = file;
15309 +       fput(f);
15310 +}
15311 +
15312 +#ifdef CONFIG_MMU
15313 +#define AuDbgVmRegion(file, vma) do {} while (0)
15314 +
15315 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15316 +                                   struct file *file)
15317 +{
15318 +       au_do_vm_file_reset(vma, file);
15319 +}
15320 +#else
15321 +#define AuDbgVmRegion(file, vma) \
15322 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15323 +
15324 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15325 +                                   struct file *file)
15326 +{
15327 +       struct file *f;
15328 +
15329 +       au_do_vm_file_reset(vma, file);
15330 +       f = vma->vm_region->vm_file;
15331 +       get_file(file);
15332 +       vma->vm_region->vm_file = file;
15333 +       fput(f);
15334 +}
15335 +#endif /* CONFIG_MMU */
15336 +
15337 +/* handle vma->vm_prfile */
15338 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15339 +                                   struct file *file)
15340 +{
15341 +       get_file(file);
15342 +       vma->vm_prfile = file;
15343 +#ifndef CONFIG_MMU
15344 +       get_file(file);
15345 +       vma->vm_region->vm_prfile = file;
15346 +#endif
15347 +}
15348 +
15349 +#endif /* __KERNEL__ */
15350 +#endif /* __AUFS_FILE_H__ */
15351 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15352 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15353 +++ linux/fs/aufs/finfo.c       2019-07-11 15:42:14.465571210 +0200
15354 @@ -0,0 +1,149 @@
15355 +// SPDX-License-Identifier: GPL-2.0
15356 +/*
15357 + * Copyright (C) 2005-2019 Junjiro R. Okajima
15358 + *
15359 + * This program, aufs is free software; you can redistribute it and/or modify
15360 + * it under the terms of the GNU General Public License as published by
15361 + * the Free Software Foundation; either version 2 of the License, or
15362 + * (at your option) any later version.
15363 + *
15364 + * This program is distributed in the hope that it will be useful,
15365 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15366 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15367 + * GNU General Public License for more details.
15368 + *
15369 + * You should have received a copy of the GNU General Public License
15370 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15371 + */
15372 +
15373 +/*
15374 + * file private data
15375 + */
15376 +
15377 +#include "aufs.h"
15378 +
15379 +void au_hfput(struct au_hfile *hf, int execed)
15380 +{
15381 +       if (execed)
15382 +               allow_write_access(hf->hf_file);
15383 +       fput(hf->hf_file);
15384 +       hf->hf_file = NULL;
15385 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15386 +       hf->hf_br = NULL;
15387 +}
15388 +
15389 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15390 +{
15391 +       struct au_finfo *finfo = au_fi(file);
15392 +       struct au_hfile *hf;
15393 +       struct au_fidir *fidir;
15394 +
15395 +       fidir = finfo->fi_hdir;
15396 +       if (!fidir) {
15397 +               AuDebugOn(finfo->fi_btop != bindex);
15398 +               hf = &finfo->fi_htop;
15399 +       } else
15400 +               hf = fidir->fd_hfile + bindex;
15401 +
15402 +       if (hf && hf->hf_file)
15403 +               au_hfput(hf, vfsub_file_execed(file));
15404 +       if (val) {
15405 +               FiMustWriteLock(file);
15406 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15407 +               hf->hf_file = val;
15408 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15409 +       }
15410 +}
15411 +
15412 +void au_update_figen(struct file *file)
15413 +{
15414 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15415 +       /* smp_mb(); */ /* atomic_set */
15416 +}
15417 +
15418 +/* ---------------------------------------------------------------------- */
15419 +
15420 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15421 +{
15422 +       struct au_fidir *fidir;
15423 +       int nbr;
15424 +
15425 +       nbr = au_sbbot(sb) + 1;
15426 +       if (nbr < 2)
15427 +               nbr = 2; /* initial allocate for 2 branches */
15428 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15429 +       if (fidir) {
15430 +               fidir->fd_bbot = -1;
15431 +               fidir->fd_nent = nbr;
15432 +       }
15433 +
15434 +       return fidir;
15435 +}
15436 +
15437 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15438 +{
15439 +       int err;
15440 +       struct au_fidir *fidir, *p;
15441 +
15442 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15443 +       fidir = finfo->fi_hdir;
15444 +       AuDebugOn(!fidir);
15445 +
15446 +       err = -ENOMEM;
15447 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15448 +                        GFP_NOFS, may_shrink);
15449 +       if (p) {
15450 +               p->fd_nent = nbr;
15451 +               finfo->fi_hdir = p;
15452 +               err = 0;
15453 +       }
15454 +
15455 +       return err;
15456 +}
15457 +
15458 +/* ---------------------------------------------------------------------- */
15459 +
15460 +void au_finfo_fin(struct file *file)
15461 +{
15462 +       struct au_finfo *finfo;
15463 +
15464 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15465 +
15466 +       finfo = au_fi(file);
15467 +       AuDebugOn(finfo->fi_hdir);
15468 +       AuRwDestroy(&finfo->fi_rwsem);
15469 +       au_cache_free_finfo(finfo);
15470 +}
15471 +
15472 +void au_fi_init_once(void *_finfo)
15473 +{
15474 +       struct au_finfo *finfo = _finfo;
15475 +
15476 +       au_rw_init(&finfo->fi_rwsem);
15477 +}
15478 +
15479 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15480 +{
15481 +       int err;
15482 +       struct au_finfo *finfo;
15483 +       struct dentry *dentry;
15484 +
15485 +       err = -ENOMEM;
15486 +       dentry = file->f_path.dentry;
15487 +       finfo = au_cache_alloc_finfo();
15488 +       if (unlikely(!finfo))
15489 +               goto out;
15490 +
15491 +       err = 0;
15492 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15493 +       au_rw_write_lock(&finfo->fi_rwsem);
15494 +       finfo->fi_btop = -1;
15495 +       finfo->fi_hdir = fidir;
15496 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15497 +       /* smp_mb(); */ /* atomic_set */
15498 +
15499 +       file->private_data = finfo;
15500 +
15501 +out:
15502 +       return err;
15503 +}
15504 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15505 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15506 +++ linux/fs/aufs/f_op.c        2019-07-11 15:42:14.465571210 +0200
15507 @@ -0,0 +1,819 @@
15508 +// SPDX-License-Identifier: GPL-2.0
15509 +/*
15510 + * Copyright (C) 2005-2019 Junjiro R. Okajima
15511 + *
15512 + * This program, aufs is free software; you can redistribute it and/or modify
15513 + * it under the terms of the GNU General Public License as published by
15514 + * the Free Software Foundation; either version 2 of the License, or
15515 + * (at your option) any later version.
15516 + *
15517 + * This program is distributed in the hope that it will be useful,
15518 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15519 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15520 + * GNU General Public License for more details.
15521 + *
15522 + * You should have received a copy of the GNU General Public License
15523 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15524 + */
15525 +
15526 +/*
15527 + * file and vm operations
15528 + */
15529 +
15530 +#include <linux/aio.h>
15531 +#include <linux/fs_stack.h>
15532 +#include <linux/mman.h>
15533 +#include <linux/security.h>
15534 +#include "aufs.h"
15535 +
15536 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15537 +{
15538 +       int err;
15539 +       aufs_bindex_t bindex;
15540 +       struct dentry *dentry, *h_dentry;
15541 +       struct au_finfo *finfo;
15542 +       struct inode *h_inode;
15543 +
15544 +       FiMustWriteLock(file);
15545 +
15546 +       err = 0;
15547 +       dentry = file->f_path.dentry;
15548 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15549 +       finfo = au_fi(file);
15550 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15551 +       atomic_set(&finfo->fi_mmapped, 0);
15552 +       bindex = au_dbtop(dentry);
15553 +       if (!h_file) {
15554 +               h_dentry = au_h_dptr(dentry, bindex);
15555 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15556 +               if (unlikely(err))
15557 +                       goto out;
15558 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15559 +               if (IS_ERR(h_file)) {
15560 +                       err = PTR_ERR(h_file);
15561 +                       goto out;
15562 +               }
15563 +       } else {
15564 +               h_dentry = h_file->f_path.dentry;
15565 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15566 +               if (unlikely(err))
15567 +                       goto out;
15568 +               /* br ref is already inc-ed */
15569 +       }
15570 +
15571 +       if ((flags & __O_TMPFILE)
15572 +           && !(flags & O_EXCL)) {
15573 +               h_inode = file_inode(h_file);
15574 +               spin_lock(&h_inode->i_lock);
15575 +               h_inode->i_state |= I_LINKABLE;
15576 +               spin_unlock(&h_inode->i_lock);
15577 +       }
15578 +       au_set_fbtop(file, bindex);
15579 +       au_set_h_fptr(file, bindex, h_file);
15580 +       au_update_figen(file);
15581 +       /* todo: necessary? */
15582 +       /* file->f_ra = h_file->f_ra; */
15583 +
15584 +out:
15585 +       return err;
15586 +}
15587 +
15588 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15589 +                           struct file *file)
15590 +{
15591 +       int err;
15592 +       struct super_block *sb;
15593 +       struct au_do_open_args args = {
15594 +               .open   = au_do_open_nondir
15595 +       };
15596 +
15597 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15598 +             file, vfsub_file_flags(file), file->f_mode);
15599 +
15600 +       sb = file->f_path.dentry->d_sb;
15601 +       si_read_lock(sb, AuLock_FLUSH);
15602 +       err = au_do_open(file, &args);
15603 +       si_read_unlock(sb);
15604 +       return err;
15605 +}
15606 +
15607 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15608 +{
15609 +       struct au_finfo *finfo;
15610 +       aufs_bindex_t bindex;
15611 +
15612 +       finfo = au_fi(file);
15613 +       au_hbl_del(&finfo->fi_hlist,
15614 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15615 +       bindex = finfo->fi_btop;
15616 +       if (bindex >= 0)
15617 +               au_set_h_fptr(file, bindex, NULL);
15618 +
15619 +       au_finfo_fin(file);
15620 +       return 0;
15621 +}
15622 +
15623 +/* ---------------------------------------------------------------------- */
15624 +
15625 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15626 +{
15627 +       int err;
15628 +       struct file *h_file;
15629 +
15630 +       err = 0;
15631 +       h_file = au_hf_top(file);
15632 +       if (h_file)
15633 +               err = vfsub_flush(h_file, id);
15634 +       return err;
15635 +}
15636 +
15637 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15638 +{
15639 +       return au_do_flush(file, id, au_do_flush_nondir);
15640 +}
15641 +
15642 +/* ---------------------------------------------------------------------- */
15643 +/*
15644 + * read and write functions acquire [fdi]_rwsem once, but release before
15645 + * mmap_sem. This is because to stop a race condition between mmap(2).
15646 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15647 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15648 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15649 + */
15650 +
15651 +/* Callers should call au_read_post() or fput() in the end */
15652 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15653 +{
15654 +       struct file *h_file;
15655 +       int err;
15656 +
15657 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15658 +       if (!err) {
15659 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15660 +               h_file = au_hf_top(file);
15661 +               get_file(h_file);
15662 +               if (!keep_fi)
15663 +                       fi_read_unlock(file);
15664 +       } else
15665 +               h_file = ERR_PTR(err);
15666 +
15667 +       return h_file;
15668 +}
15669 +
15670 +static void au_read_post(struct inode *inode, struct file *h_file)
15671 +{
15672 +       /* update without lock, I don't think it a problem */
15673 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15674 +       fput(h_file);
15675 +}
15676 +
15677 +struct au_write_pre {
15678 +       /* input */
15679 +       unsigned int lsc;
15680 +
15681 +       /* output */
15682 +       blkcnt_t blks;
15683 +       aufs_bindex_t btop;
15684 +};
15685 +
15686 +/*
15687 + * return with iinfo is write-locked
15688 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15689 + * end
15690 + */
15691 +static struct file *au_write_pre(struct file *file, int do_ready,
15692 +                                struct au_write_pre *wpre)
15693 +{
15694 +       struct file *h_file;
15695 +       struct dentry *dentry;
15696 +       int err;
15697 +       unsigned int lsc;
15698 +       struct au_pin pin;
15699 +
15700 +       lsc = 0;
15701 +       if (wpre)
15702 +               lsc = wpre->lsc;
15703 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15704 +       h_file = ERR_PTR(err);
15705 +       if (unlikely(err))
15706 +               goto out;
15707 +
15708 +       dentry = file->f_path.dentry;
15709 +       if (do_ready) {
15710 +               err = au_ready_to_write(file, -1, &pin);
15711 +               if (unlikely(err)) {
15712 +                       h_file = ERR_PTR(err);
15713 +                       di_write_unlock(dentry);
15714 +                       goto out_fi;
15715 +               }
15716 +       }
15717 +
15718 +       di_downgrade_lock(dentry, /*flags*/0);
15719 +       if (wpre)
15720 +               wpre->btop = au_fbtop(file);
15721 +       h_file = au_hf_top(file);
15722 +       get_file(h_file);
15723 +       if (wpre)
15724 +               wpre->blks = file_inode(h_file)->i_blocks;
15725 +       if (do_ready)
15726 +               au_unpin(&pin);
15727 +       di_read_unlock(dentry, /*flags*/0);
15728 +
15729 +out_fi:
15730 +       fi_write_unlock(file);
15731 +out:
15732 +       return h_file;
15733 +}
15734 +
15735 +static void au_write_post(struct inode *inode, struct file *h_file,
15736 +                         struct au_write_pre *wpre, ssize_t written)
15737 +{
15738 +       struct inode *h_inode;
15739 +
15740 +       au_cpup_attr_timesizes(inode);
15741 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15742 +       h_inode = file_inode(h_file);
15743 +       inode->i_mode = h_inode->i_mode;
15744 +       ii_write_unlock(inode);
15745 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15746 +       if (written > 0)
15747 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15748 +                             /*force*/h_inode->i_blocks > wpre->blks);
15749 +       fput(h_file);
15750 +}
15751 +
15752 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15753 +                        loff_t *ppos)
15754 +{
15755 +       ssize_t err;
15756 +       struct inode *inode;
15757 +       struct file *h_file;
15758 +       struct super_block *sb;
15759 +
15760 +       inode = file_inode(file);
15761 +       sb = inode->i_sb;
15762 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15763 +
15764 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15765 +       err = PTR_ERR(h_file);
15766 +       if (IS_ERR(h_file))
15767 +               goto out;
15768 +
15769 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
15770 +       err = vfsub_read_u(h_file, buf, count, ppos);
15771 +       /* todo: necessary? */
15772 +       /* file->f_ra = h_file->f_ra; */
15773 +       au_read_post(inode, h_file);
15774 +
15775 +out:
15776 +       si_read_unlock(sb);
15777 +       return err;
15778 +}
15779 +
15780 +/*
15781 + * todo: very ugly
15782 + * it locks both of i_mutex and si_rwsem for read in safe.
15783 + * if the plink maintenance mode continues forever (that is the problem),
15784 + * may loop forever.
15785 + */
15786 +static void au_mtx_and_read_lock(struct inode *inode)
15787 +{
15788 +       int err;
15789 +       struct super_block *sb = inode->i_sb;
15790 +
15791 +       while (1) {
15792 +               inode_lock(inode);
15793 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15794 +               if (!err)
15795 +                       break;
15796 +               inode_unlock(inode);
15797 +               si_read_lock(sb, AuLock_NOPLMW);
15798 +               si_read_unlock(sb);
15799 +       }
15800 +}
15801 +
15802 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15803 +                         size_t count, loff_t *ppos)
15804 +{
15805 +       ssize_t err;
15806 +       struct au_write_pre wpre;
15807 +       struct inode *inode;
15808 +       struct file *h_file;
15809 +       char __user *buf = (char __user *)ubuf;
15810 +
15811 +       inode = file_inode(file);
15812 +       au_mtx_and_read_lock(inode);
15813 +
15814 +       wpre.lsc = 0;
15815 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15816 +       err = PTR_ERR(h_file);
15817 +       if (IS_ERR(h_file))
15818 +               goto out;
15819 +
15820 +       err = vfsub_write_u(h_file, buf, count, ppos);
15821 +       au_write_post(inode, h_file, &wpre, err);
15822 +
15823 +out:
15824 +       si_read_unlock(inode->i_sb);
15825 +       inode_unlock(inode);
15826 +       return err;
15827 +}
15828 +
15829 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15830 +                         struct iov_iter *iov_iter)
15831 +{
15832 +       ssize_t err;
15833 +       struct file *file;
15834 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15835 +
15836 +       err = security_file_permission(h_file, rw);
15837 +       if (unlikely(err))
15838 +               goto out;
15839 +
15840 +       err = -ENOSYS;
15841 +       iter = NULL;
15842 +       if (rw == MAY_READ)
15843 +               iter = h_file->f_op->read_iter;
15844 +       else if (rw == MAY_WRITE)
15845 +               iter = h_file->f_op->write_iter;
15846 +
15847 +       file = kio->ki_filp;
15848 +       kio->ki_filp = h_file;
15849 +       if (iter) {
15850 +               lockdep_off();
15851 +               err = iter(kio, iov_iter);
15852 +               lockdep_on();
15853 +       } else
15854 +               /* currently there is no such fs */
15855 +               WARN_ON_ONCE(1);
15856 +       kio->ki_filp = file;
15857 +
15858 +out:
15859 +       return err;
15860 +}
15861 +
15862 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15863 +{
15864 +       ssize_t err;
15865 +       struct file *file, *h_file;
15866 +       struct inode *inode;
15867 +       struct super_block *sb;
15868 +
15869 +       file = kio->ki_filp;
15870 +       inode = file_inode(file);
15871 +       sb = inode->i_sb;
15872 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15873 +
15874 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15875 +       err = PTR_ERR(h_file);
15876 +       if (IS_ERR(h_file))
15877 +               goto out;
15878 +
15879 +       if (au_test_loopback_kthread()) {
15880 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15881 +               if (file->f_mapping != h_file->f_mapping) {
15882 +                       file->f_mapping = h_file->f_mapping;
15883 +                       smp_mb(); /* unnecessary? */
15884 +               }
15885 +       }
15886 +       fi_read_unlock(file);
15887 +
15888 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15889 +       /* todo: necessary? */
15890 +       /* file->f_ra = h_file->f_ra; */
15891 +       au_read_post(inode, h_file);
15892 +
15893 +out:
15894 +       si_read_unlock(sb);
15895 +       return err;
15896 +}
15897 +
15898 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15899 +{
15900 +       ssize_t err;
15901 +       struct au_write_pre wpre;
15902 +       struct inode *inode;
15903 +       struct file *file, *h_file;
15904 +
15905 +       file = kio->ki_filp;
15906 +       inode = file_inode(file);
15907 +       au_mtx_and_read_lock(inode);
15908 +
15909 +       wpre.lsc = 0;
15910 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15911 +       err = PTR_ERR(h_file);
15912 +       if (IS_ERR(h_file))
15913 +               goto out;
15914 +
15915 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15916 +       au_write_post(inode, h_file, &wpre, err);
15917 +
15918 +out:
15919 +       si_read_unlock(inode->i_sb);
15920 +       inode_unlock(inode);
15921 +       return err;
15922 +}
15923 +
15924 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15925 +                               struct pipe_inode_info *pipe, size_t len,
15926 +                               unsigned int flags)
15927 +{
15928 +       ssize_t err;
15929 +       struct file *h_file;
15930 +       struct inode *inode;
15931 +       struct super_block *sb;
15932 +
15933 +       inode = file_inode(file);
15934 +       sb = inode->i_sb;
15935 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15936 +
15937 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15938 +       err = PTR_ERR(h_file);
15939 +       if (IS_ERR(h_file))
15940 +               goto out;
15941 +
15942 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15943 +       /* todo: necessary? */
15944 +       /* file->f_ra = h_file->f_ra; */
15945 +       au_read_post(inode, h_file);
15946 +
15947 +out:
15948 +       si_read_unlock(sb);
15949 +       return err;
15950 +}
15951 +
15952 +static ssize_t
15953 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15954 +                 size_t len, unsigned int flags)
15955 +{
15956 +       ssize_t err;
15957 +       struct au_write_pre wpre;
15958 +       struct inode *inode;
15959 +       struct file *h_file;
15960 +
15961 +       inode = file_inode(file);
15962 +       au_mtx_and_read_lock(inode);
15963 +
15964 +       wpre.lsc = 0;
15965 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15966 +       err = PTR_ERR(h_file);
15967 +       if (IS_ERR(h_file))
15968 +               goto out;
15969 +
15970 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15971 +       au_write_post(inode, h_file, &wpre, err);
15972 +
15973 +out:
15974 +       si_read_unlock(inode->i_sb);
15975 +       inode_unlock(inode);
15976 +       return err;
15977 +}
15978 +
15979 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15980 +                          loff_t len)
15981 +{
15982 +       long err;
15983 +       struct au_write_pre wpre;
15984 +       struct inode *inode;
15985 +       struct file *h_file;
15986 +
15987 +       inode = file_inode(file);
15988 +       au_mtx_and_read_lock(inode);
15989 +
15990 +       wpre.lsc = 0;
15991 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15992 +       err = PTR_ERR(h_file);
15993 +       if (IS_ERR(h_file))
15994 +               goto out;
15995 +
15996 +       lockdep_off();
15997 +       err = vfs_fallocate(h_file, mode, offset, len);
15998 +       lockdep_on();
15999 +       au_write_post(inode, h_file, &wpre, /*written*/1);
16000 +
16001 +out:
16002 +       si_read_unlock(inode->i_sb);
16003 +       inode_unlock(inode);
16004 +       return err;
16005 +}
16006 +
16007 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16008 +                                   struct file *dst, loff_t dst_pos,
16009 +                                   size_t len, unsigned int flags)
16010 +{
16011 +       ssize_t err;
16012 +       struct au_write_pre wpre;
16013 +       enum { SRC, DST };
16014 +       struct {
16015 +               struct inode *inode;
16016 +               struct file *h_file;
16017 +               struct super_block *h_sb;
16018 +       } a[2];
16019 +#define a_src  a[SRC]
16020 +#define a_dst  a[DST]
16021 +
16022 +       err = -EINVAL;
16023 +       a_src.inode = file_inode(src);
16024 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16025 +               goto out;
16026 +       a_dst.inode = file_inode(dst);
16027 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16028 +               goto out;
16029 +
16030 +       au_mtx_and_read_lock(a_dst.inode);
16031 +       /*
16032 +        * in order to match the order in di_write_lock2_{child,parent}(),
16033 +        * use f_path.dentry for this comparison.
16034 +        */
16035 +       if (src->f_path.dentry < dst->f_path.dentry) {
16036 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16037 +               err = PTR_ERR(a_src.h_file);
16038 +               if (IS_ERR(a_src.h_file))
16039 +                       goto out_si;
16040 +
16041 +               wpre.lsc = AuLsc_FI_2;
16042 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16043 +               err = PTR_ERR(a_dst.h_file);
16044 +               if (IS_ERR(a_dst.h_file)) {
16045 +                       au_read_post(a_src.inode, a_src.h_file);
16046 +                       goto out_si;
16047 +               }
16048 +       } else {
16049 +               wpre.lsc = AuLsc_FI_1;
16050 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16051 +               err = PTR_ERR(a_dst.h_file);
16052 +               if (IS_ERR(a_dst.h_file))
16053 +                       goto out_si;
16054 +
16055 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16056 +               err = PTR_ERR(a_src.h_file);
16057 +               if (IS_ERR(a_src.h_file)) {
16058 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16059 +                                     /*written*/0);
16060 +                       goto out_si;
16061 +               }
16062 +       }
16063 +
16064 +       err = -EXDEV;
16065 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16066 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16067 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16068 +               AuDbgFile(src);
16069 +               AuDbgFile(dst);
16070 +               goto out_file;
16071 +       }
16072 +
16073 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16074 +                                   dst_pos, len, flags);
16075 +
16076 +out_file:
16077 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16078 +       fi_read_unlock(src);
16079 +       au_read_post(a_src.inode, a_src.h_file);
16080 +out_si:
16081 +       si_read_unlock(a_dst.inode->i_sb);
16082 +       inode_unlock(a_dst.inode);
16083 +out:
16084 +       return err;
16085 +#undef a_src
16086 +#undef a_dst
16087 +}
16088 +
16089 +/* ---------------------------------------------------------------------- */
16090 +
16091 +/*
16092 + * The locking order around current->mmap_sem.
16093 + * - in most and regular cases
16094 + *   file I/O syscall -- aufs_read() or something
16095 + *     -- si_rwsem for read -- mmap_sem
16096 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
16097 + * - in mmap case
16098 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16099 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16100 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16101 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16102 + * It means that when aufs acquires si_rwsem for write, the process should never
16103 + * acquire mmap_sem.
16104 + *
16105 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16106 + * problem either since any directory is not able to be mmap-ed.
16107 + * The similar scenario is applied to aufs_readlink() too.
16108 + */
16109 +
16110 +#if 0 /* stop calling security_file_mmap() */
16111 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16112 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16113 +
16114 +static unsigned long au_arch_prot_conv(unsigned long flags)
16115 +{
16116 +       /* currently ppc64 only */
16117 +#ifdef CONFIG_PPC64
16118 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16119 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16120 +       return AuConv_VM_PROT(flags, SAO);
16121 +#else
16122 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16123 +       return 0;
16124 +#endif
16125 +}
16126 +
16127 +static unsigned long au_prot_conv(unsigned long flags)
16128 +{
16129 +       return AuConv_VM_PROT(flags, READ)
16130 +               | AuConv_VM_PROT(flags, WRITE)
16131 +               | AuConv_VM_PROT(flags, EXEC)
16132 +               | au_arch_prot_conv(flags);
16133 +}
16134 +
16135 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16136 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16137 +
16138 +static unsigned long au_flag_conv(unsigned long flags)
16139 +{
16140 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16141 +               | AuConv_VM_MAP(flags, DENYWRITE)
16142 +               | AuConv_VM_MAP(flags, LOCKED);
16143 +}
16144 +#endif
16145 +
16146 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16147 +{
16148 +       int err;
16149 +       const unsigned char wlock
16150 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16151 +       struct super_block *sb;
16152 +       struct file *h_file;
16153 +       struct inode *inode;
16154 +
16155 +       AuDbgVmRegion(file, vma);
16156 +
16157 +       inode = file_inode(file);
16158 +       sb = inode->i_sb;
16159 +       lockdep_off();
16160 +       si_read_lock(sb, AuLock_NOPLMW);
16161 +
16162 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16163 +       lockdep_on();
16164 +       err = PTR_ERR(h_file);
16165 +       if (IS_ERR(h_file))
16166 +               goto out;
16167 +
16168 +       err = 0;
16169 +       au_set_mmapped(file);
16170 +       au_vm_file_reset(vma, h_file);
16171 +       /*
16172 +        * we cannot call security_mmap_file() here since it may acquire
16173 +        * mmap_sem or i_mutex.
16174 +        *
16175 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16176 +        *                       au_flag_conv(vma->vm_flags));
16177 +        */
16178 +       if (!err)
16179 +               err = call_mmap(h_file, vma);
16180 +       if (!err) {
16181 +               au_vm_prfile_set(vma, file);
16182 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16183 +               goto out_fput; /* success */
16184 +       }
16185 +       au_unset_mmapped(file);
16186 +       au_vm_file_reset(vma, file);
16187 +
16188 +out_fput:
16189 +       lockdep_off();
16190 +       ii_write_unlock(inode);
16191 +       lockdep_on();
16192 +       fput(h_file);
16193 +out:
16194 +       lockdep_off();
16195 +       si_read_unlock(sb);
16196 +       lockdep_on();
16197 +       AuTraceErr(err);
16198 +       return err;
16199 +}
16200 +
16201 +/* ---------------------------------------------------------------------- */
16202 +
16203 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16204 +                            int datasync)
16205 +{
16206 +       int err;
16207 +       struct au_write_pre wpre;
16208 +       struct inode *inode;
16209 +       struct file *h_file;
16210 +
16211 +       err = 0; /* -EBADF; */ /* posix? */
16212 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16213 +               goto out;
16214 +
16215 +       inode = file_inode(file);
16216 +       au_mtx_and_read_lock(inode);
16217 +
16218 +       wpre.lsc = 0;
16219 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16220 +       err = PTR_ERR(h_file);
16221 +       if (IS_ERR(h_file))
16222 +               goto out_unlock;
16223 +
16224 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16225 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16226 +
16227 +out_unlock:
16228 +       si_read_unlock(inode->i_sb);
16229 +       inode_unlock(inode);
16230 +out:
16231 +       return err;
16232 +}
16233 +
16234 +static int aufs_fasync(int fd, struct file *file, int flag)
16235 +{
16236 +       int err;
16237 +       struct file *h_file;
16238 +       struct super_block *sb;
16239 +
16240 +       sb = file->f_path.dentry->d_sb;
16241 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16242 +
16243 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16244 +       err = PTR_ERR(h_file);
16245 +       if (IS_ERR(h_file))
16246 +               goto out;
16247 +
16248 +       if (h_file->f_op->fasync)
16249 +               err = h_file->f_op->fasync(fd, h_file, flag);
16250 +       fput(h_file); /* instead of au_read_post() */
16251 +
16252 +out:
16253 +       si_read_unlock(sb);
16254 +       return err;
16255 +}
16256 +
16257 +static int aufs_setfl(struct file *file, unsigned long arg)
16258 +{
16259 +       int err;
16260 +       struct file *h_file;
16261 +       struct super_block *sb;
16262 +
16263 +       sb = file->f_path.dentry->d_sb;
16264 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16265 +
16266 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16267 +       err = PTR_ERR(h_file);
16268 +       if (IS_ERR(h_file))
16269 +               goto out;
16270 +
16271 +       /* stop calling h_file->fasync */
16272 +       arg |= vfsub_file_flags(file) & FASYNC;
16273 +       err = setfl(/*unused fd*/-1, h_file, arg);
16274 +       fput(h_file); /* instead of au_read_post() */
16275 +
16276 +out:
16277 +       si_read_unlock(sb);
16278 +       return err;
16279 +}
16280 +
16281 +/* ---------------------------------------------------------------------- */
16282 +
16283 +/* no one supports this operation, currently */
16284 +#if 0
16285 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16286 +                            size_t len, loff_t *pos, int more)
16287 +{
16288 +}
16289 +#endif
16290 +
16291 +/* ---------------------------------------------------------------------- */
16292 +
16293 +const struct file_operations aufs_file_fop = {
16294 +       .owner          = THIS_MODULE,
16295 +
16296 +       .llseek         = default_llseek,
16297 +
16298 +       .read           = aufs_read,
16299 +       .write          = aufs_write,
16300 +       .read_iter      = aufs_read_iter,
16301 +       .write_iter     = aufs_write_iter,
16302 +
16303 +#ifdef CONFIG_AUFS_POLL
16304 +       .poll           = aufs_poll,
16305 +#endif
16306 +       .unlocked_ioctl = aufs_ioctl_nondir,
16307 +#ifdef CONFIG_COMPAT
16308 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16309 +#endif
16310 +       .mmap           = aufs_mmap,
16311 +       .open           = aufs_open_nondir,
16312 +       .flush          = aufs_flush_nondir,
16313 +       .release        = aufs_release_nondir,
16314 +       .fsync          = aufs_fsync_nondir,
16315 +       .fasync         = aufs_fasync,
16316 +       /* .sendpage    = aufs_sendpage, */
16317 +       .setfl          = aufs_setfl,
16318 +       .splice_write   = aufs_splice_write,
16319 +       .splice_read    = aufs_splice_read,
16320 +#if 0
16321 +       .aio_splice_write = aufs_aio_splice_write,
16322 +       .aio_splice_read  = aufs_aio_splice_read,
16323 +#endif
16324 +       .fallocate      = aufs_fallocate,
16325 +       .copy_file_range = aufs_copy_file_range
16326 +};
16327 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16328 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16329 +++ linux/fs/aufs/fstype.h      2019-07-11 15:42:14.465571210 +0200
16330 @@ -0,0 +1,401 @@
16331 +/* SPDX-License-Identifier: GPL-2.0 */
16332 +/*
16333 + * Copyright (C) 2005-2019 Junjiro R. Okajima
16334 + *
16335 + * This program, aufs is free software; you can redistribute it and/or modify
16336 + * it under the terms of the GNU General Public License as published by
16337 + * the Free Software Foundation; either version 2 of the License, or
16338 + * (at your option) any later version.
16339 + *
16340 + * This program is distributed in the hope that it will be useful,
16341 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16342 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16343 + * GNU General Public License for more details.
16344 + *
16345 + * You should have received a copy of the GNU General Public License
16346 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16347 + */
16348 +
16349 +/*
16350 + * judging filesystem type
16351 + */
16352 +
16353 +#ifndef __AUFS_FSTYPE_H__
16354 +#define __AUFS_FSTYPE_H__
16355 +
16356 +#ifdef __KERNEL__
16357 +
16358 +#include <linux/fs.h>
16359 +#include <linux/magic.h>
16360 +#include <linux/nfs_fs.h>
16361 +#include <linux/romfs_fs.h>
16362 +
16363 +static inline int au_test_aufs(struct super_block *sb)
16364 +{
16365 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16366 +}
16367 +
16368 +static inline const char *au_sbtype(struct super_block *sb)
16369 +{
16370 +       return sb->s_type->name;
16371 +}
16372 +
16373 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16374 +{
16375 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16376 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16377 +#else
16378 +       return 0;
16379 +#endif
16380 +}
16381 +
16382 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16383 +{
16384 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16385 +       return sb->s_magic == ROMFS_MAGIC;
16386 +#else
16387 +       return 0;
16388 +#endif
16389 +}
16390 +
16391 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16392 +{
16393 +#if IS_ENABLED(CONFIG_CRAMFS)
16394 +       return sb->s_magic == CRAMFS_MAGIC;
16395 +#endif
16396 +       return 0;
16397 +}
16398 +
16399 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16400 +{
16401 +#if IS_ENABLED(CONFIG_NFS_FS)
16402 +       return sb->s_magic == NFS_SUPER_MAGIC;
16403 +#else
16404 +       return 0;
16405 +#endif
16406 +}
16407 +
16408 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16409 +{
16410 +#if IS_ENABLED(CONFIG_FUSE_FS)
16411 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16412 +#else
16413 +       return 0;
16414 +#endif
16415 +}
16416 +
16417 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16418 +{
16419 +#if IS_ENABLED(CONFIG_XFS_FS)
16420 +       return sb->s_magic == XFS_SB_MAGIC;
16421 +#else
16422 +       return 0;
16423 +#endif
16424 +}
16425 +
16426 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16427 +{
16428 +#ifdef CONFIG_TMPFS
16429 +       return sb->s_magic == TMPFS_MAGIC;
16430 +#else
16431 +       return 0;
16432 +#endif
16433 +}
16434 +
16435 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16436 +{
16437 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16438 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16439 +#else
16440 +       return 0;
16441 +#endif
16442 +}
16443 +
16444 +static inline int au_test_ramfs(struct super_block *sb)
16445 +{
16446 +       return sb->s_magic == RAMFS_MAGIC;
16447 +}
16448 +
16449 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16450 +{
16451 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16452 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16453 +#else
16454 +       return 0;
16455 +#endif
16456 +}
16457 +
16458 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16459 +{
16460 +#ifdef CONFIG_PROC_FS
16461 +       return sb->s_magic == PROC_SUPER_MAGIC;
16462 +#else
16463 +       return 0;
16464 +#endif
16465 +}
16466 +
16467 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16468 +{
16469 +#ifdef CONFIG_SYSFS
16470 +       return sb->s_magic == SYSFS_MAGIC;
16471 +#else
16472 +       return 0;
16473 +#endif
16474 +}
16475 +
16476 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16477 +{
16478 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16479 +       return sb->s_magic == CONFIGFS_MAGIC;
16480 +#else
16481 +       return 0;
16482 +#endif
16483 +}
16484 +
16485 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16486 +{
16487 +#if IS_ENABLED(CONFIG_MINIX_FS)
16488 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16489 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16490 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16491 +               || sb->s_magic == MINIX_SUPER_MAGIC
16492 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16493 +#else
16494 +       return 0;
16495 +#endif
16496 +}
16497 +
16498 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16499 +{
16500 +#if IS_ENABLED(CONFIG_FAT_FS)
16501 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16502 +#else
16503 +       return 0;
16504 +#endif
16505 +}
16506 +
16507 +static inline int au_test_msdos(struct super_block *sb)
16508 +{
16509 +       return au_test_fat(sb);
16510 +}
16511 +
16512 +static inline int au_test_vfat(struct super_block *sb)
16513 +{
16514 +       return au_test_fat(sb);
16515 +}
16516 +
16517 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16518 +{
16519 +#ifdef CONFIG_SECURITYFS
16520 +       return sb->s_magic == SECURITYFS_MAGIC;
16521 +#else
16522 +       return 0;
16523 +#endif
16524 +}
16525 +
16526 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16527 +{
16528 +#if IS_ENABLED(CONFIG_SQUASHFS)
16529 +       return sb->s_magic == SQUASHFS_MAGIC;
16530 +#else
16531 +       return 0;
16532 +#endif
16533 +}
16534 +
16535 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16536 +{
16537 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16538 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16539 +#else
16540 +       return 0;
16541 +#endif
16542 +}
16543 +
16544 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16545 +{
16546 +#if IS_ENABLED(CONFIG_XENFS)
16547 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16548 +#else
16549 +       return 0;
16550 +#endif
16551 +}
16552 +
16553 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16554 +{
16555 +#ifdef CONFIG_DEBUG_FS
16556 +       return sb->s_magic == DEBUGFS_MAGIC;
16557 +#else
16558 +       return 0;
16559 +#endif
16560 +}
16561 +
16562 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16563 +{
16564 +#if IS_ENABLED(CONFIG_NILFS)
16565 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16566 +#else
16567 +       return 0;
16568 +#endif
16569 +}
16570 +
16571 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16572 +{
16573 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16574 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16575 +#else
16576 +       return 0;
16577 +#endif
16578 +}
16579 +
16580 +/* ---------------------------------------------------------------------- */
16581 +/*
16582 + * they can't be an aufs branch.
16583 + */
16584 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16585 +{
16586 +       return
16587 +#ifndef CONFIG_AUFS_BR_RAMFS
16588 +               au_test_ramfs(sb) ||
16589 +#endif
16590 +               au_test_procfs(sb)
16591 +               || au_test_sysfs(sb)
16592 +               || au_test_configfs(sb)
16593 +               || au_test_debugfs(sb)
16594 +               || au_test_securityfs(sb)
16595 +               || au_test_xenfs(sb)
16596 +               || au_test_ecryptfs(sb)
16597 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16598 +               || au_test_aufs(sb); /* will be supported in next version */
16599 +}
16600 +
16601 +static inline int au_test_fs_remote(struct super_block *sb)
16602 +{
16603 +       return !au_test_tmpfs(sb)
16604 +#ifdef CONFIG_AUFS_BR_RAMFS
16605 +               && !au_test_ramfs(sb)
16606 +#endif
16607 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16608 +}
16609 +
16610 +/* ---------------------------------------------------------------------- */
16611 +
16612 +/*
16613 + * Note: these functions (below) are created after reading ->getattr() in all
16614 + * filesystems under linux/fs. it means we have to do so in every update...
16615 + */
16616 +
16617 +/*
16618 + * some filesystems require getattr to refresh the inode attributes before
16619 + * referencing.
16620 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16621 + * and leave the work for d_revalidate()
16622 + */
16623 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16624 +{
16625 +       return au_test_nfs(sb)
16626 +               || au_test_fuse(sb)
16627 +               /* || au_test_btrfs(sb) */      /* untested */
16628 +               ;
16629 +}
16630 +
16631 +/*
16632 + * filesystems which don't maintain i_size or i_blocks.
16633 + */
16634 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16635 +{
16636 +       return au_test_xfs(sb)
16637 +               || au_test_btrfs(sb)
16638 +               || au_test_ubifs(sb)
16639 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16640 +               /* || au_test_minix(sb) */      /* untested */
16641 +               ;
16642 +}
16643 +
16644 +/*
16645 + * filesystems which don't store the correct value in some of their inode
16646 + * attributes.
16647 + */
16648 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16649 +{
16650 +       return au_test_fs_bad_iattr_size(sb)
16651 +               || au_test_fat(sb)
16652 +               || au_test_msdos(sb)
16653 +               || au_test_vfat(sb);
16654 +}
16655 +
16656 +/* they don't check i_nlink in link(2) */
16657 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16658 +{
16659 +       return au_test_tmpfs(sb)
16660 +#ifdef CONFIG_AUFS_BR_RAMFS
16661 +               || au_test_ramfs(sb)
16662 +#endif
16663 +               || au_test_ubifs(sb)
16664 +               || au_test_hfsplus(sb);
16665 +}
16666 +
16667 +/*
16668 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16669 + */
16670 +static inline int au_test_fs_notime(struct super_block *sb)
16671 +{
16672 +       return au_test_nfs(sb)
16673 +               || au_test_fuse(sb)
16674 +               || au_test_ubifs(sb)
16675 +               ;
16676 +}
16677 +
16678 +/* temporary support for i#1 in cramfs */
16679 +static inline int au_test_fs_unique_ino(struct inode *inode)
16680 +{
16681 +       if (au_test_cramfs(inode->i_sb))
16682 +               return inode->i_ino != 1;
16683 +       return 1;
16684 +}
16685 +
16686 +/* ---------------------------------------------------------------------- */
16687 +
16688 +/*
16689 + * the filesystem where the xino files placed must support i/o after unlink and
16690 + * maintain i_size and i_blocks.
16691 + */
16692 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16693 +{
16694 +       return au_test_fs_remote(sb)
16695 +               || au_test_fs_bad_iattr_size(sb)
16696 +               /* don't want unnecessary work for xino */
16697 +               || au_test_aufs(sb)
16698 +               || au_test_ecryptfs(sb)
16699 +               || au_test_nilfs(sb);
16700 +}
16701 +
16702 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16703 +{
16704 +       return au_test_tmpfs(sb)
16705 +               || au_test_ramfs(sb);
16706 +}
16707 +
16708 +/*
16709 + * test if the @sb is real-readonly.
16710 + */
16711 +static inline int au_test_fs_rr(struct super_block *sb)
16712 +{
16713 +       return au_test_squashfs(sb)
16714 +               || au_test_iso9660(sb)
16715 +               || au_test_cramfs(sb)
16716 +               || au_test_romfs(sb);
16717 +}
16718 +
16719 +/*
16720 + * test if the @inode is nfs with 'noacl' option
16721 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16722 + */
16723 +static inline int au_test_nfs_noacl(struct inode *inode)
16724 +{
16725 +       return au_test_nfs(inode->i_sb)
16726 +               /* && IS_POSIXACL(inode) */
16727 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16728 +}
16729 +
16730 +#endif /* __KERNEL__ */
16731 +#endif /* __AUFS_FSTYPE_H__ */
16732 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16733 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16734 +++ linux/fs/aufs/hbl.h 2019-07-11 15:42:14.465571210 +0200
16735 @@ -0,0 +1,65 @@
16736 +/* SPDX-License-Identifier: GPL-2.0 */
16737 +/*
16738 + * Copyright (C) 2017-2019 Junjiro R. Okajima
16739 + *
16740 + * This program, aufs is free software; you can redistribute it and/or modify
16741 + * it under the terms of the GNU General Public License as published by
16742 + * the Free Software Foundation; either version 2 of the License, or
16743 + * (at your option) any later version.
16744 + *
16745 + * This program is distributed in the hope that it will be useful,
16746 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16747 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16748 + * GNU General Public License for more details.
16749 + *
16750 + * You should have received a copy of the GNU General Public License
16751 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16752 + */
16753 +
16754 +/*
16755 + * helpers for hlist_bl.h
16756 + */
16757 +
16758 +#ifndef __AUFS_HBL_H__
16759 +#define __AUFS_HBL_H__
16760 +
16761 +#ifdef __KERNEL__
16762 +
16763 +#include <linux/list_bl.h>
16764 +
16765 +static inline void au_hbl_add(struct hlist_bl_node *node,
16766 +                             struct hlist_bl_head *hbl)
16767 +{
16768 +       hlist_bl_lock(hbl);
16769 +       hlist_bl_add_head(node, hbl);
16770 +       hlist_bl_unlock(hbl);
16771 +}
16772 +
16773 +static inline void au_hbl_del(struct hlist_bl_node *node,
16774 +                             struct hlist_bl_head *hbl)
16775 +{
16776 +       hlist_bl_lock(hbl);
16777 +       hlist_bl_del(node);
16778 +       hlist_bl_unlock(hbl);
16779 +}
16780 +
16781 +#define au_hbl_for_each(pos, head)                                     \
16782 +       for (pos = hlist_bl_first(head);                                \
16783 +            pos;                                                       \
16784 +            pos = pos->next)
16785 +
16786 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16787 +{
16788 +       unsigned long cnt;
16789 +       struct hlist_bl_node *pos;
16790 +
16791 +       cnt = 0;
16792 +       hlist_bl_lock(hbl);
16793 +       au_hbl_for_each(pos, hbl)
16794 +               cnt++;
16795 +       hlist_bl_unlock(hbl);
16796 +       return cnt;
16797 +}
16798 +
16799 +#endif /* __KERNEL__ */
16800 +#endif /* __AUFS_HBL_H__ */
16801 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16802 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16803 +++ linux/fs/aufs/hfsnotify.c   2019-07-11 15:42:14.465571210 +0200
16804 @@ -0,0 +1,288 @@
16805 +// SPDX-License-Identifier: GPL-2.0
16806 +/*
16807 + * Copyright (C) 2005-2019 Junjiro R. Okajima
16808 + *
16809 + * This program, aufs is free software; you can redistribute it and/or modify
16810 + * it under the terms of the GNU General Public License as published by
16811 + * the Free Software Foundation; either version 2 of the License, or
16812 + * (at your option) any later version.
16813 + *
16814 + * This program is distributed in the hope that it will be useful,
16815 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16816 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16817 + * GNU General Public License for more details.
16818 + *
16819 + * You should have received a copy of the GNU General Public License
16820 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16821 + */
16822 +
16823 +/*
16824 + * fsnotify for the lower directories
16825 + */
16826 +
16827 +#include "aufs.h"
16828 +
16829 +/* FS_IN_IGNORED is unnecessary */
16830 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16831 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16832 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16833 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16834 +
16835 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16836 +{
16837 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16838 +                                            hn_mark);
16839 +       /* AuDbg("here\n"); */
16840 +       au_cache_free_hnotify(hn);
16841 +       smp_mb__before_atomic(); /* for atomic64_dec */
16842 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16843 +               wake_up(&au_hfsn_wq);
16844 +}
16845 +
16846 +static int au_hfsn_alloc(struct au_hinode *hinode)
16847 +{
16848 +       int err;
16849 +       struct au_hnotify *hn;
16850 +       struct super_block *sb;
16851 +       struct au_branch *br;
16852 +       struct fsnotify_mark *mark;
16853 +       aufs_bindex_t bindex;
16854 +
16855 +       hn = hinode->hi_notify;
16856 +       sb = hn->hn_aufs_inode->i_sb;
16857 +       bindex = au_br_index(sb, hinode->hi_id);
16858 +       br = au_sbr(sb, bindex);
16859 +       AuDebugOn(!br->br_hfsn);
16860 +
16861 +       mark = &hn->hn_mark;
16862 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16863 +       mark->mask = AuHfsnMask;
16864 +       /*
16865 +        * by udba rename or rmdir, aufs assign a new inode to the known
16866 +        * h_inode, so specify 1 to allow dups.
16867 +        */
16868 +       lockdep_off();
16869 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16870 +       lockdep_on();
16871 +
16872 +       return err;
16873 +}
16874 +
16875 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16876 +{
16877 +       struct fsnotify_mark *mark;
16878 +       unsigned long long ull;
16879 +       struct fsnotify_group *group;
16880 +
16881 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16882 +       BUG_ON(!ull);
16883 +
16884 +       mark = &hn->hn_mark;
16885 +       spin_lock(&mark->lock);
16886 +       group = mark->group;
16887 +       fsnotify_get_group(group);
16888 +       spin_unlock(&mark->lock);
16889 +       lockdep_off();
16890 +       fsnotify_destroy_mark(mark, group);
16891 +       fsnotify_put_mark(mark);
16892 +       fsnotify_put_group(group);
16893 +       lockdep_on();
16894 +
16895 +       /* free hn by myself */
16896 +       return 0;
16897 +}
16898 +
16899 +/* ---------------------------------------------------------------------- */
16900 +
16901 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16902 +{
16903 +       struct fsnotify_mark *mark;
16904 +
16905 +       mark = &hinode->hi_notify->hn_mark;
16906 +       spin_lock(&mark->lock);
16907 +       if (do_set) {
16908 +               AuDebugOn(mark->mask & AuHfsnMask);
16909 +               mark->mask |= AuHfsnMask;
16910 +       } else {
16911 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16912 +               mark->mask &= ~AuHfsnMask;
16913 +       }
16914 +       spin_unlock(&mark->lock);
16915 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16916 +}
16917 +
16918 +/* ---------------------------------------------------------------------- */
16919 +
16920 +/* #define AuDbgHnotify */
16921 +#ifdef AuDbgHnotify
16922 +static char *au_hfsn_name(u32 mask)
16923 +{
16924 +#ifdef CONFIG_AUFS_DEBUG
16925 +#define test_ret(flag)                         \
16926 +       do {                                    \
16927 +               if (mask & flag)                \
16928 +                       return #flag;           \
16929 +       } while (0)
16930 +       test_ret(FS_ACCESS);
16931 +       test_ret(FS_MODIFY);
16932 +       test_ret(FS_ATTRIB);
16933 +       test_ret(FS_CLOSE_WRITE);
16934 +       test_ret(FS_CLOSE_NOWRITE);
16935 +       test_ret(FS_OPEN);
16936 +       test_ret(FS_MOVED_FROM);
16937 +       test_ret(FS_MOVED_TO);
16938 +       test_ret(FS_CREATE);
16939 +       test_ret(FS_DELETE);
16940 +       test_ret(FS_DELETE_SELF);
16941 +       test_ret(FS_MOVE_SELF);
16942 +       test_ret(FS_UNMOUNT);
16943 +       test_ret(FS_Q_OVERFLOW);
16944 +       test_ret(FS_IN_IGNORED);
16945 +       test_ret(FS_ISDIR);
16946 +       test_ret(FS_IN_ONESHOT);
16947 +       test_ret(FS_EVENT_ON_CHILD);
16948 +       return "";
16949 +#undef test_ret
16950 +#else
16951 +       return "??";
16952 +#endif
16953 +}
16954 +#endif
16955 +
16956 +/* ---------------------------------------------------------------------- */
16957 +
16958 +static void au_hfsn_free_group(struct fsnotify_group *group)
16959 +{
16960 +       struct au_br_hfsnotify *hfsn = group->private;
16961 +
16962 +       /* AuDbg("here\n"); */
16963 +       au_kfree_try_rcu(hfsn);
16964 +}
16965 +
16966 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16967 +                               struct inode *inode,
16968 +                               u32 mask, const void *data, int data_type,
16969 +                               const struct qstr *file_name, u32 cookie,
16970 +                               struct fsnotify_iter_info *iter_info)
16971 +{
16972 +       int err;
16973 +       struct au_hnotify *hnotify;
16974 +       struct inode *h_dir, *h_inode;
16975 +       struct fsnotify_mark *inode_mark;
16976 +
16977 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16978 +
16979 +       err = 0;
16980 +       /* if FS_UNMOUNT happens, there must be another bug */
16981 +       AuDebugOn(mask & FS_UNMOUNT);
16982 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16983 +               goto out;
16984 +
16985 +       h_dir = inode;
16986 +       h_inode = NULL;
16987 +#ifdef AuDbgHnotify
16988 +       au_debug_on();
16989 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16990 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16991 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16992 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16993 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16994 +               /* WARN_ON(1); */
16995 +       }
16996 +       au_debug_off();
16997 +#endif
16998 +
16999 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
17000 +       AuDebugOn(!inode_mark);
17001 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17002 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
17003 +
17004 +out:
17005 +       return err;
17006 +}
17007 +
17008 +static struct fsnotify_ops au_hfsn_ops = {
17009 +       .handle_event           = au_hfsn_handle_event,
17010 +       .free_group_priv        = au_hfsn_free_group,
17011 +       .free_mark              = au_hfsn_free_mark
17012 +};
17013 +
17014 +/* ---------------------------------------------------------------------- */
17015 +
17016 +static void au_hfsn_fin_br(struct au_branch *br)
17017 +{
17018 +       struct au_br_hfsnotify *hfsn;
17019 +
17020 +       hfsn = br->br_hfsn;
17021 +       if (hfsn) {
17022 +               lockdep_off();
17023 +               fsnotify_put_group(hfsn->hfsn_group);
17024 +               lockdep_on();
17025 +       }
17026 +}
17027 +
17028 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17029 +{
17030 +       int err;
17031 +       struct fsnotify_group *group;
17032 +       struct au_br_hfsnotify *hfsn;
17033 +
17034 +       err = 0;
17035 +       br->br_hfsn = NULL;
17036 +       if (!au_br_hnotifyable(perm))
17037 +               goto out;
17038 +
17039 +       err = -ENOMEM;
17040 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17041 +       if (unlikely(!hfsn))
17042 +               goto out;
17043 +
17044 +       err = 0;
17045 +       group = fsnotify_alloc_group(&au_hfsn_ops);
17046 +       if (IS_ERR(group)) {
17047 +               err = PTR_ERR(group);
17048 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
17049 +               goto out_hfsn;
17050 +       }
17051 +
17052 +       group->private = hfsn;
17053 +       hfsn->hfsn_group = group;
17054 +       br->br_hfsn = hfsn;
17055 +       goto out; /* success */
17056 +
17057 +out_hfsn:
17058 +       au_kfree_try_rcu(hfsn);
17059 +out:
17060 +       return err;
17061 +}
17062 +
17063 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17064 +{
17065 +       int err;
17066 +
17067 +       err = 0;
17068 +       if (!br->br_hfsn)
17069 +               err = au_hfsn_init_br(br, perm);
17070 +
17071 +       return err;
17072 +}
17073 +
17074 +/* ---------------------------------------------------------------------- */
17075 +
17076 +static void au_hfsn_fin(void)
17077 +{
17078 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17079 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17080 +}
17081 +
17082 +const struct au_hnotify_op au_hnotify_op = {
17083 +       .ctl            = au_hfsn_ctl,
17084 +       .alloc          = au_hfsn_alloc,
17085 +       .free           = au_hfsn_free,
17086 +
17087 +       .fin            = au_hfsn_fin,
17088 +
17089 +       .reset_br       = au_hfsn_reset_br,
17090 +       .fin_br         = au_hfsn_fin_br,
17091 +       .init_br        = au_hfsn_init_br
17092 +};
17093 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17094 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
17095 +++ linux/fs/aufs/hfsplus.c     2019-07-11 15:42:14.465571210 +0200
17096 @@ -0,0 +1,60 @@
17097 +// SPDX-License-Identifier: GPL-2.0
17098 +/*
17099 + * Copyright (C) 2010-2019 Junjiro R. Okajima
17100 + *
17101 + * This program, aufs is free software; you can redistribute it and/or modify
17102 + * it under the terms of the GNU General Public License as published by
17103 + * the Free Software Foundation; either version 2 of the License, or
17104 + * (at your option) any later version.
17105 + *
17106 + * This program is distributed in the hope that it will be useful,
17107 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17108 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17109 + * GNU General Public License for more details.
17110 + *
17111 + * You should have received a copy of the GNU General Public License
17112 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17113 + */
17114 +
17115 +/*
17116 + * special support for filesystems which acquires an inode mutex
17117 + * at final closing a file, eg, hfsplus.
17118 + *
17119 + * This trick is very simple and stupid, just to open the file before really
17120 + * necessary open to tell hfsplus that this is not the final closing.
17121 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17122 + * and au_h_open_post() after releasing it.
17123 + */
17124 +
17125 +#include "aufs.h"
17126 +
17127 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17128 +                          int force_wr)
17129 +{
17130 +       struct file *h_file;
17131 +       struct dentry *h_dentry;
17132 +
17133 +       h_dentry = au_h_dptr(dentry, bindex);
17134 +       AuDebugOn(!h_dentry);
17135 +       AuDebugOn(d_is_negative(h_dentry));
17136 +
17137 +       h_file = NULL;
17138 +       if (au_test_hfsplus(h_dentry->d_sb)
17139 +           && d_is_reg(h_dentry))
17140 +               h_file = au_h_open(dentry, bindex,
17141 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17142 +                                  /*file*/NULL, force_wr);
17143 +       return h_file;
17144 +}
17145 +
17146 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17147 +                   struct file *h_file)
17148 +{
17149 +       struct au_branch *br;
17150 +
17151 +       if (h_file) {
17152 +               fput(h_file);
17153 +               br = au_sbr(dentry->d_sb, bindex);
17154 +               au_lcnt_dec(&br->br_nfiles);
17155 +       }
17156 +}
17157 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17158 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17159 +++ linux/fs/aufs/hnotify.c     2019-07-11 15:42:14.465571210 +0200
17160 @@ -0,0 +1,721 @@
17161 +// SPDX-License-Identifier: GPL-2.0
17162 +/*
17163 + * Copyright (C) 2005-2019 Junjiro R. Okajima
17164 + *
17165 + * This program, aufs is free software; you can redistribute it and/or modify
17166 + * it under the terms of the GNU General Public License as published by
17167 + * the Free Software Foundation; either version 2 of the License, or
17168 + * (at your option) any later version.
17169 + *
17170 + * This program is distributed in the hope that it will be useful,
17171 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17172 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17173 + * GNU General Public License for more details.
17174 + *
17175 + * You should have received a copy of the GNU General Public License
17176 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17177 + */
17178 +
17179 +/*
17180 + * abstraction to notify the direct changes on lower directories
17181 + */
17182 +
17183 +/* #include <linux/iversion.h> */
17184 +#include "aufs.h"
17185 +
17186 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17187 +{
17188 +       int err;
17189 +       struct au_hnotify *hn;
17190 +
17191 +       err = -ENOMEM;
17192 +       hn = au_cache_alloc_hnotify();
17193 +       if (hn) {
17194 +               hn->hn_aufs_inode = inode;
17195 +               hinode->hi_notify = hn;
17196 +               err = au_hnotify_op.alloc(hinode);
17197 +               AuTraceErr(err);
17198 +               if (unlikely(err)) {
17199 +                       hinode->hi_notify = NULL;
17200 +                       au_cache_free_hnotify(hn);
17201 +                       /*
17202 +                        * The upper dir was removed by udba, but the same named
17203 +                        * dir left. In this case, aufs assigns a new inode
17204 +                        * number and set the monitor again.
17205 +                        * For the lower dir, the old monitor is still left.
17206 +                        */
17207 +                       if (err == -EEXIST)
17208 +                               err = 0;
17209 +               }
17210 +       }
17211 +
17212 +       AuTraceErr(err);
17213 +       return err;
17214 +}
17215 +
17216 +void au_hn_free(struct au_hinode *hinode)
17217 +{
17218 +       struct au_hnotify *hn;
17219 +
17220 +       hn = hinode->hi_notify;
17221 +       if (hn) {
17222 +               hinode->hi_notify = NULL;
17223 +               if (au_hnotify_op.free(hinode, hn))
17224 +                       au_cache_free_hnotify(hn);
17225 +       }
17226 +}
17227 +
17228 +/* ---------------------------------------------------------------------- */
17229 +
17230 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17231 +{
17232 +       if (hinode->hi_notify)
17233 +               au_hnotify_op.ctl(hinode, do_set);
17234 +}
17235 +
17236 +void au_hn_reset(struct inode *inode, unsigned int flags)
17237 +{
17238 +       aufs_bindex_t bindex, bbot;
17239 +       struct inode *hi;
17240 +       struct dentry *iwhdentry;
17241 +
17242 +       bbot = au_ibbot(inode);
17243 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17244 +               hi = au_h_iptr(inode, bindex);
17245 +               if (!hi)
17246 +                       continue;
17247 +
17248 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17249 +               iwhdentry = au_hi_wh(inode, bindex);
17250 +               if (iwhdentry)
17251 +                       dget(iwhdentry);
17252 +               au_igrab(hi);
17253 +               au_set_h_iptr(inode, bindex, NULL, 0);
17254 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17255 +                             flags & ~AuHi_XINO);
17256 +               iput(hi);
17257 +               dput(iwhdentry);
17258 +               /* inode_unlock(hi); */
17259 +       }
17260 +}
17261 +
17262 +/* ---------------------------------------------------------------------- */
17263 +
17264 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17265 +{
17266 +       int err;
17267 +       aufs_bindex_t bindex, bbot, bfound, btop;
17268 +       struct inode *h_i;
17269 +
17270 +       err = 0;
17271 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17272 +               pr_warn("branch root dir was changed\n");
17273 +               goto out;
17274 +       }
17275 +
17276 +       bfound = -1;
17277 +       bbot = au_ibbot(inode);
17278 +       btop = au_ibtop(inode);
17279 +#if 0 /* reserved for future use */
17280 +       if (bindex == bbot) {
17281 +               /* keep this ino in rename case */
17282 +               goto out;
17283 +       }
17284 +#endif
17285 +       for (bindex = btop; bindex <= bbot; bindex++)
17286 +               if (au_h_iptr(inode, bindex) == h_inode) {
17287 +                       bfound = bindex;
17288 +                       break;
17289 +               }
17290 +       if (bfound < 0)
17291 +               goto out;
17292 +
17293 +       for (bindex = btop; bindex <= bbot; bindex++) {
17294 +               h_i = au_h_iptr(inode, bindex);
17295 +               if (!h_i)
17296 +                       continue;
17297 +
17298 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17299 +               /* ignore this error */
17300 +               /* bad action? */
17301 +       }
17302 +
17303 +       /* children inode number will be broken */
17304 +
17305 +out:
17306 +       AuTraceErr(err);
17307 +       return err;
17308 +}
17309 +
17310 +static int hn_gen_tree(struct dentry *dentry)
17311 +{
17312 +       int err, i, j, ndentry;
17313 +       struct au_dcsub_pages dpages;
17314 +       struct au_dpage *dpage;
17315 +       struct dentry **dentries;
17316 +
17317 +       err = au_dpages_init(&dpages, GFP_NOFS);
17318 +       if (unlikely(err))
17319 +               goto out;
17320 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17321 +       if (unlikely(err))
17322 +               goto out_dpages;
17323 +
17324 +       for (i = 0; i < dpages.ndpage; i++) {
17325 +               dpage = dpages.dpages + i;
17326 +               dentries = dpage->dentries;
17327 +               ndentry = dpage->ndentry;
17328 +               for (j = 0; j < ndentry; j++) {
17329 +                       struct dentry *d;
17330 +
17331 +                       d = dentries[j];
17332 +                       if (IS_ROOT(d))
17333 +                               continue;
17334 +
17335 +                       au_digen_dec(d);
17336 +                       if (d_really_is_positive(d))
17337 +                               /* todo: reset children xino?
17338 +                                  cached children only? */
17339 +                               au_iigen_dec(d_inode(d));
17340 +               }
17341 +       }
17342 +
17343 +out_dpages:
17344 +       au_dpages_free(&dpages);
17345 +
17346 +#if 0
17347 +       /* discard children */
17348 +       dentry_unhash(dentry);
17349 +       dput(dentry);
17350 +#endif
17351 +out:
17352 +       return err;
17353 +}
17354 +
17355 +/*
17356 + * return 0 if processed.
17357 + */
17358 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17359 +                          const unsigned int isdir)
17360 +{
17361 +       int err;
17362 +       struct dentry *d;
17363 +       struct qstr *dname;
17364 +
17365 +       err = 1;
17366 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17367 +               pr_warn("branch root dir was changed\n");
17368 +               err = 0;
17369 +               goto out;
17370 +       }
17371 +
17372 +       if (!isdir) {
17373 +               AuDebugOn(!name);
17374 +               au_iigen_dec(inode);
17375 +               spin_lock(&inode->i_lock);
17376 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17377 +                       spin_lock(&d->d_lock);
17378 +                       dname = &d->d_name;
17379 +                       if (dname->len != nlen
17380 +                           && memcmp(dname->name, name, nlen)) {
17381 +                               spin_unlock(&d->d_lock);
17382 +                               continue;
17383 +                       }
17384 +                       err = 0;
17385 +                       au_digen_dec(d);
17386 +                       spin_unlock(&d->d_lock);
17387 +                       break;
17388 +               }
17389 +               spin_unlock(&inode->i_lock);
17390 +       } else {
17391 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17392 +               d = d_find_any_alias(inode);
17393 +               if (!d) {
17394 +                       au_iigen_dec(inode);
17395 +                       goto out;
17396 +               }
17397 +
17398 +               spin_lock(&d->d_lock);
17399 +               dname = &d->d_name;
17400 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17401 +                       spin_unlock(&d->d_lock);
17402 +                       err = hn_gen_tree(d);
17403 +                       spin_lock(&d->d_lock);
17404 +               }
17405 +               spin_unlock(&d->d_lock);
17406 +               dput(d);
17407 +       }
17408 +
17409 +out:
17410 +       AuTraceErr(err);
17411 +       return err;
17412 +}
17413 +
17414 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17415 +{
17416 +       int err;
17417 +
17418 +       if (IS_ROOT(dentry)) {
17419 +               pr_warn("branch root dir was changed\n");
17420 +               return 0;
17421 +       }
17422 +
17423 +       err = 0;
17424 +       if (!isdir) {
17425 +               au_digen_dec(dentry);
17426 +               if (d_really_is_positive(dentry))
17427 +                       au_iigen_dec(d_inode(dentry));
17428 +       } else {
17429 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17430 +               if (d_really_is_positive(dentry))
17431 +                       err = hn_gen_tree(dentry);
17432 +       }
17433 +
17434 +       AuTraceErr(err);
17435 +       return err;
17436 +}
17437 +
17438 +/* ---------------------------------------------------------------------- */
17439 +
17440 +/* hnotify job flags */
17441 +#define AuHnJob_XINO0          1
17442 +#define AuHnJob_GEN            (1 << 1)
17443 +#define AuHnJob_DIRENT         (1 << 2)
17444 +#define AuHnJob_ISDIR          (1 << 3)
17445 +#define AuHnJob_TRYXINO0       (1 << 4)
17446 +#define AuHnJob_MNTPNT         (1 << 5)
17447 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17448 +#define au_fset_hnjob(flags, name) \
17449 +       do { (flags) |= AuHnJob_##name; } while (0)
17450 +#define au_fclr_hnjob(flags, name) \
17451 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17452 +
17453 +enum {
17454 +       AuHn_CHILD,
17455 +       AuHn_PARENT,
17456 +       AuHnLast
17457 +};
17458 +
17459 +struct au_hnotify_args {
17460 +       struct inode *h_dir, *dir, *h_child_inode;
17461 +       u32 mask;
17462 +       unsigned int flags[AuHnLast];
17463 +       unsigned int h_child_nlen;
17464 +       char h_child_name[];
17465 +};
17466 +
17467 +struct hn_job_args {
17468 +       unsigned int flags;
17469 +       struct inode *inode, *h_inode, *dir, *h_dir;
17470 +       struct dentry *dentry;
17471 +       char *h_name;
17472 +       int h_nlen;
17473 +};
17474 +
17475 +static int hn_job(struct hn_job_args *a)
17476 +{
17477 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17478 +       int e;
17479 +
17480 +       /* reset xino */
17481 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17482 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17483 +
17484 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17485 +           && a->inode
17486 +           && a->h_inode) {
17487 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17488 +               if (!a->h_inode->i_nlink
17489 +                   && !(a->h_inode->i_state & I_LINKABLE))
17490 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17491 +               inode_unlock_shared(a->h_inode);
17492 +       }
17493 +
17494 +       /* make the generation obsolete */
17495 +       if (au_ftest_hnjob(a->flags, GEN)) {
17496 +               e = -1;
17497 +               if (a->inode)
17498 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17499 +                                             isdir);
17500 +               if (e && a->dentry)
17501 +                       hn_gen_by_name(a->dentry, isdir);
17502 +               /* ignore this error */
17503 +       }
17504 +
17505 +       /* make dir entries obsolete */
17506 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17507 +               struct au_vdir *vdir;
17508 +
17509 +               vdir = au_ivdir(a->inode);
17510 +               if (vdir)
17511 +                       vdir->vd_jiffy = 0;
17512 +               /* IMustLock(a->inode); */
17513 +               /* inode_inc_iversion(a->inode); */
17514 +       }
17515 +
17516 +       /* can do nothing but warn */
17517 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17518 +           && a->dentry
17519 +           && d_mountpoint(a->dentry))
17520 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17521 +
17522 +       return 0;
17523 +}
17524 +
17525 +/* ---------------------------------------------------------------------- */
17526 +
17527 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17528 +                                          struct inode *dir)
17529 +{
17530 +       struct dentry *dentry, *d, *parent;
17531 +       struct qstr *dname;
17532 +
17533 +       parent = d_find_any_alias(dir);
17534 +       if (!parent)
17535 +               return NULL;
17536 +
17537 +       dentry = NULL;
17538 +       spin_lock(&parent->d_lock);
17539 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17540 +               /* AuDbg("%pd\n", d); */
17541 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17542 +               dname = &d->d_name;
17543 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17544 +                       goto cont_unlock;
17545 +               if (au_di(d))
17546 +                       au_digen_dec(d);
17547 +               else
17548 +                       goto cont_unlock;
17549 +               if (au_dcount(d) > 0) {
17550 +                       dentry = dget_dlock(d);
17551 +                       spin_unlock(&d->d_lock);
17552 +                       break;
17553 +               }
17554 +
17555 +cont_unlock:
17556 +               spin_unlock(&d->d_lock);
17557 +       }
17558 +       spin_unlock(&parent->d_lock);
17559 +       dput(parent);
17560 +
17561 +       if (dentry)
17562 +               di_write_lock_child(dentry);
17563 +
17564 +       return dentry;
17565 +}
17566 +
17567 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17568 +                                        aufs_bindex_t bindex, ino_t h_ino)
17569 +{
17570 +       struct inode *inode;
17571 +       ino_t ino;
17572 +       int err;
17573 +
17574 +       inode = NULL;
17575 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17576 +       if (!err && ino)
17577 +               inode = ilookup(sb, ino);
17578 +       if (!inode)
17579 +               goto out;
17580 +
17581 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17582 +               pr_warn("wrong root branch\n");
17583 +               iput(inode);
17584 +               inode = NULL;
17585 +               goto out;
17586 +       }
17587 +
17588 +       ii_write_lock_child(inode);
17589 +
17590 +out:
17591 +       return inode;
17592 +}
17593 +
17594 +static void au_hn_bh(void *_args)
17595 +{
17596 +       struct au_hnotify_args *a = _args;
17597 +       struct super_block *sb;
17598 +       aufs_bindex_t bindex, bbot, bfound;
17599 +       unsigned char xino, try_iput;
17600 +       int err;
17601 +       struct inode *inode;
17602 +       ino_t h_ino;
17603 +       struct hn_job_args args;
17604 +       struct dentry *dentry;
17605 +       struct au_sbinfo *sbinfo;
17606 +
17607 +       AuDebugOn(!_args);
17608 +       AuDebugOn(!a->h_dir);
17609 +       AuDebugOn(!a->dir);
17610 +       AuDebugOn(!a->mask);
17611 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17612 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17613 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17614 +
17615 +       inode = NULL;
17616 +       dentry = NULL;
17617 +       /*
17618 +        * do not lock a->dir->i_mutex here
17619 +        * because of d_revalidate() may cause a deadlock.
17620 +        */
17621 +       sb = a->dir->i_sb;
17622 +       AuDebugOn(!sb);
17623 +       sbinfo = au_sbi(sb);
17624 +       AuDebugOn(!sbinfo);
17625 +       si_write_lock(sb, AuLock_NOPLMW);
17626 +
17627 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17628 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17629 +               case FS_MOVED_FROM:
17630 +               case FS_MOVED_TO:
17631 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17632 +                               "for the direct rename(2)\n");
17633 +               }
17634 +
17635 +       ii_read_lock_parent(a->dir);
17636 +       bfound = -1;
17637 +       bbot = au_ibbot(a->dir);
17638 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17639 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17640 +                       bfound = bindex;
17641 +                       break;
17642 +               }
17643 +       ii_read_unlock(a->dir);
17644 +       if (unlikely(bfound < 0))
17645 +               goto out;
17646 +
17647 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17648 +       h_ino = 0;
17649 +       if (a->h_child_inode)
17650 +               h_ino = a->h_child_inode->i_ino;
17651 +
17652 +       if (a->h_child_nlen
17653 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17654 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17655 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17656 +                                             a->dir);
17657 +       try_iput = 0;
17658 +       if (dentry && d_really_is_positive(dentry))
17659 +               inode = d_inode(dentry);
17660 +       if (xino && !inode && h_ino
17661 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17662 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17663 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17664 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17665 +               try_iput = 1;
17666 +       }
17667 +
17668 +       args.flags = a->flags[AuHn_CHILD];
17669 +       args.dentry = dentry;
17670 +       args.inode = inode;
17671 +       args.h_inode = a->h_child_inode;
17672 +       args.dir = a->dir;
17673 +       args.h_dir = a->h_dir;
17674 +       args.h_name = a->h_child_name;
17675 +       args.h_nlen = a->h_child_nlen;
17676 +       err = hn_job(&args);
17677 +       if (dentry) {
17678 +               if (au_di(dentry))
17679 +                       di_write_unlock(dentry);
17680 +               dput(dentry);
17681 +       }
17682 +       if (inode && try_iput) {
17683 +               ii_write_unlock(inode);
17684 +               iput(inode);
17685 +       }
17686 +
17687 +       ii_write_lock_parent(a->dir);
17688 +       args.flags = a->flags[AuHn_PARENT];
17689 +       args.dentry = NULL;
17690 +       args.inode = a->dir;
17691 +       args.h_inode = a->h_dir;
17692 +       args.dir = NULL;
17693 +       args.h_dir = NULL;
17694 +       args.h_name = NULL;
17695 +       args.h_nlen = 0;
17696 +       err = hn_job(&args);
17697 +       ii_write_unlock(a->dir);
17698 +
17699 +out:
17700 +       iput(a->h_child_inode);
17701 +       iput(a->h_dir);
17702 +       iput(a->dir);
17703 +       si_write_unlock(sb);
17704 +       au_nwt_done(&sbinfo->si_nowait);
17705 +       au_kfree_rcu(a);
17706 +}
17707 +
17708 +/* ---------------------------------------------------------------------- */
17709 +
17710 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17711 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17712 +{
17713 +       int err, len;
17714 +       unsigned int flags[AuHnLast], f;
17715 +       unsigned char isdir, isroot, wh;
17716 +       struct inode *dir;
17717 +       struct au_hnotify_args *args;
17718 +       char *p, *h_child_name;
17719 +
17720 +       err = 0;
17721 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17722 +       dir = igrab(hnotify->hn_aufs_inode);
17723 +       if (!dir)
17724 +               goto out;
17725 +
17726 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17727 +       wh = 0;
17728 +       h_child_name = (void *)h_child_qstr->name;
17729 +       len = h_child_qstr->len;
17730 +       if (h_child_name) {
17731 +               if (len > AUFS_WH_PFX_LEN
17732 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17733 +                       h_child_name += AUFS_WH_PFX_LEN;
17734 +                       len -= AUFS_WH_PFX_LEN;
17735 +                       wh = 1;
17736 +               }
17737 +       }
17738 +
17739 +       isdir = 0;
17740 +       if (h_child_inode)
17741 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17742 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17743 +       flags[AuHn_CHILD] = 0;
17744 +       if (isdir)
17745 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17746 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17747 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17748 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17749 +       case FS_MOVED_FROM:
17750 +       case FS_MOVED_TO:
17751 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17752 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17753 +               /*FALLTHROUGH*/
17754 +       case FS_CREATE:
17755 +               AuDebugOn(!h_child_name);
17756 +               break;
17757 +
17758 +       case FS_DELETE:
17759 +               /*
17760 +                * aufs never be able to get this child inode.
17761 +                * revalidation should be in d_revalidate()
17762 +                * by checking i_nlink, i_generation or d_unhashed().
17763 +                */
17764 +               AuDebugOn(!h_child_name);
17765 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17766 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17767 +               break;
17768 +
17769 +       default:
17770 +               AuDebugOn(1);
17771 +       }
17772 +
17773 +       if (wh)
17774 +               h_child_inode = NULL;
17775 +
17776 +       err = -ENOMEM;
17777 +       /* iput() and kfree() will be called in au_hnotify() */
17778 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17779 +       if (unlikely(!args)) {
17780 +               AuErr1("no memory\n");
17781 +               iput(dir);
17782 +               goto out;
17783 +       }
17784 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17785 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17786 +       args->mask = mask;
17787 +       args->dir = dir;
17788 +       args->h_dir = igrab(h_dir);
17789 +       if (h_child_inode)
17790 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17791 +       args->h_child_inode = h_child_inode;
17792 +       args->h_child_nlen = len;
17793 +       if (len) {
17794 +               p = (void *)args;
17795 +               p += sizeof(*args);
17796 +               memcpy(p, h_child_name, len);
17797 +               p[len] = 0;
17798 +       }
17799 +
17800 +       /* NFS fires the event for silly-renamed one from kworker */
17801 +       f = 0;
17802 +       if (!dir->i_nlink
17803 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17804 +               f = AuWkq_NEST;
17805 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17806 +       if (unlikely(err)) {
17807 +               pr_err("wkq %d\n", err);
17808 +               iput(args->h_child_inode);
17809 +               iput(args->h_dir);
17810 +               iput(args->dir);
17811 +               au_kfree_rcu(args);
17812 +       }
17813 +
17814 +out:
17815 +       return err;
17816 +}
17817 +
17818 +/* ---------------------------------------------------------------------- */
17819 +
17820 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17821 +{
17822 +       int err;
17823 +
17824 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17825 +
17826 +       err = 0;
17827 +       if (au_hnotify_op.reset_br)
17828 +               err = au_hnotify_op.reset_br(udba, br, perm);
17829 +
17830 +       return err;
17831 +}
17832 +
17833 +int au_hnotify_init_br(struct au_branch *br, int perm)
17834 +{
17835 +       int err;
17836 +
17837 +       err = 0;
17838 +       if (au_hnotify_op.init_br)
17839 +               err = au_hnotify_op.init_br(br, perm);
17840 +
17841 +       return err;
17842 +}
17843 +
17844 +void au_hnotify_fin_br(struct au_branch *br)
17845 +{
17846 +       if (au_hnotify_op.fin_br)
17847 +               au_hnotify_op.fin_br(br);
17848 +}
17849 +
17850 +static void au_hn_destroy_cache(void)
17851 +{
17852 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17853 +       au_cache[AuCache_HNOTIFY] = NULL;
17854 +}
17855 +
17856 +int __init au_hnotify_init(void)
17857 +{
17858 +       int err;
17859 +
17860 +       err = -ENOMEM;
17861 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17862 +       if (au_cache[AuCache_HNOTIFY]) {
17863 +               err = 0;
17864 +               if (au_hnotify_op.init)
17865 +                       err = au_hnotify_op.init();
17866 +               if (unlikely(err))
17867 +                       au_hn_destroy_cache();
17868 +       }
17869 +       AuTraceErr(err);
17870 +       return err;
17871 +}
17872 +
17873 +void au_hnotify_fin(void)
17874 +{
17875 +       if (au_hnotify_op.fin)
17876 +               au_hnotify_op.fin();
17877 +
17878 +       /* cf. au_cache_fin() */
17879 +       if (au_cache[AuCache_HNOTIFY])
17880 +               au_hn_destroy_cache();
17881 +}
17882 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17883 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17884 +++ linux/fs/aufs/iinfo.c       2019-07-11 15:42:14.468904634 +0200
17885 @@ -0,0 +1,286 @@
17886 +// SPDX-License-Identifier: GPL-2.0
17887 +/*
17888 + * Copyright (C) 2005-2019 Junjiro R. Okajima
17889 + *
17890 + * This program, aufs is free software; you can redistribute it and/or modify
17891 + * it under the terms of the GNU General Public License as published by
17892 + * the Free Software Foundation; either version 2 of the License, or
17893 + * (at your option) any later version.
17894 + *
17895 + * This program is distributed in the hope that it will be useful,
17896 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17897 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17898 + * GNU General Public License for more details.
17899 + *
17900 + * You should have received a copy of the GNU General Public License
17901 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17902 + */
17903 +
17904 +/*
17905 + * inode private data
17906 + */
17907 +
17908 +#include "aufs.h"
17909 +
17910 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17911 +{
17912 +       struct inode *h_inode;
17913 +       struct au_hinode *hinode;
17914 +
17915 +       IiMustAnyLock(inode);
17916 +
17917 +       hinode = au_hinode(au_ii(inode), bindex);
17918 +       h_inode = hinode->hi_inode;
17919 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17920 +       return h_inode;
17921 +}
17922 +
17923 +/* todo: hard/soft set? */
17924 +void au_hiput(struct au_hinode *hinode)
17925 +{
17926 +       au_hn_free(hinode);
17927 +       dput(hinode->hi_whdentry);
17928 +       iput(hinode->hi_inode);
17929 +}
17930 +
17931 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17932 +{
17933 +       unsigned int flags;
17934 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17935 +
17936 +       flags = 0;
17937 +       if (au_opt_test(mnt_flags, XINO))
17938 +               au_fset_hi(flags, XINO);
17939 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17940 +               au_fset_hi(flags, HNOTIFY);
17941 +       return flags;
17942 +}
17943 +
17944 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17945 +                  struct inode *h_inode, unsigned int flags)
17946 +{
17947 +       struct au_hinode *hinode;
17948 +       struct inode *hi;
17949 +       struct au_iinfo *iinfo = au_ii(inode);
17950 +
17951 +       IiMustWriteLock(inode);
17952 +
17953 +       hinode = au_hinode(iinfo, bindex);
17954 +       hi = hinode->hi_inode;
17955 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17956 +
17957 +       if (hi)
17958 +               au_hiput(hinode);
17959 +       hinode->hi_inode = h_inode;
17960 +       if (h_inode) {
17961 +               int err;
17962 +               struct super_block *sb = inode->i_sb;
17963 +               struct au_branch *br;
17964 +
17965 +               AuDebugOn(inode->i_mode
17966 +                         && (h_inode->i_mode & S_IFMT)
17967 +                         != (inode->i_mode & S_IFMT));
17968 +               if (bindex == iinfo->ii_btop)
17969 +                       au_cpup_igen(inode, h_inode);
17970 +               br = au_sbr(sb, bindex);
17971 +               hinode->hi_id = br->br_id;
17972 +               if (au_ftest_hi(flags, XINO)) {
17973 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17974 +                                           inode->i_ino);
17975 +                       if (unlikely(err))
17976 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17977 +               }
17978 +
17979 +               if (au_ftest_hi(flags, HNOTIFY)
17980 +                   && au_br_hnotifyable(br->br_perm)) {
17981 +                       err = au_hn_alloc(hinode, inode);
17982 +                       if (unlikely(err))
17983 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17984 +               }
17985 +       }
17986 +}
17987 +
17988 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17989 +                 struct dentry *h_wh)
17990 +{
17991 +       struct au_hinode *hinode;
17992 +
17993 +       IiMustWriteLock(inode);
17994 +
17995 +       hinode = au_hinode(au_ii(inode), bindex);
17996 +       AuDebugOn(hinode->hi_whdentry);
17997 +       hinode->hi_whdentry = h_wh;
17998 +}
17999 +
18000 +void au_update_iigen(struct inode *inode, int half)
18001 +{
18002 +       struct au_iinfo *iinfo;
18003 +       struct au_iigen *iigen;
18004 +       unsigned int sigen;
18005 +
18006 +       sigen = au_sigen(inode->i_sb);
18007 +       iinfo = au_ii(inode);
18008 +       iigen = &iinfo->ii_generation;
18009 +       spin_lock(&iigen->ig_spin);
18010 +       iigen->ig_generation = sigen;
18011 +       if (half)
18012 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18013 +       else
18014 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
18015 +       spin_unlock(&iigen->ig_spin);
18016 +}
18017 +
18018 +/* it may be called at remount time, too */
18019 +void au_update_ibrange(struct inode *inode, int do_put_zero)
18020 +{
18021 +       struct au_iinfo *iinfo;
18022 +       aufs_bindex_t bindex, bbot;
18023 +
18024 +       AuDebugOn(au_is_bad_inode(inode));
18025 +       IiMustWriteLock(inode);
18026 +
18027 +       iinfo = au_ii(inode);
18028 +       if (do_put_zero && iinfo->ii_btop >= 0) {
18029 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18030 +                    bindex++) {
18031 +                       struct inode *h_i;
18032 +
18033 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
18034 +                       if (h_i
18035 +                           && !h_i->i_nlink
18036 +                           && !(h_i->i_state & I_LINKABLE))
18037 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18038 +               }
18039 +       }
18040 +
18041 +       iinfo->ii_btop = -1;
18042 +       iinfo->ii_bbot = -1;
18043 +       bbot = au_sbbot(inode->i_sb);
18044 +       for (bindex = 0; bindex <= bbot; bindex++)
18045 +               if (au_hinode(iinfo, bindex)->hi_inode) {
18046 +                       iinfo->ii_btop = bindex;
18047 +                       break;
18048 +               }
18049 +       if (iinfo->ii_btop >= 0)
18050 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18051 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
18052 +                               iinfo->ii_bbot = bindex;
18053 +                               break;
18054 +                       }
18055 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
18056 +}
18057 +
18058 +/* ---------------------------------------------------------------------- */
18059 +
18060 +void au_icntnr_init_once(void *_c)
18061 +{
18062 +       struct au_icntnr *c = _c;
18063 +       struct au_iinfo *iinfo = &c->iinfo;
18064 +
18065 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
18066 +       au_rw_init(&iinfo->ii_rwsem);
18067 +       inode_init_once(&c->vfs_inode);
18068 +}
18069 +
18070 +void au_hinode_init(struct au_hinode *hinode)
18071 +{
18072 +       hinode->hi_inode = NULL;
18073 +       hinode->hi_id = -1;
18074 +       au_hn_init(hinode);
18075 +       hinode->hi_whdentry = NULL;
18076 +}
18077 +
18078 +int au_iinfo_init(struct inode *inode)
18079 +{
18080 +       struct au_iinfo *iinfo;
18081 +       struct super_block *sb;
18082 +       struct au_hinode *hi;
18083 +       int nbr, i;
18084 +
18085 +       sb = inode->i_sb;
18086 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18087 +       nbr = au_sbbot(sb) + 1;
18088 +       if (unlikely(nbr <= 0))
18089 +               nbr = 1;
18090 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18091 +       if (hi) {
18092 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
18093 +
18094 +               iinfo->ii_hinode = hi;
18095 +               for (i = 0; i < nbr; i++, hi++)
18096 +                       au_hinode_init(hi);
18097 +
18098 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
18099 +               iinfo->ii_btop = -1;
18100 +               iinfo->ii_bbot = -1;
18101 +               iinfo->ii_vdir = NULL;
18102 +               return 0;
18103 +       }
18104 +       return -ENOMEM;
18105 +}
18106 +
18107 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
18108 +{
18109 +       int err, i;
18110 +       struct au_hinode *hip;
18111 +
18112 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18113 +
18114 +       err = -ENOMEM;
18115 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18116 +                         may_shrink);
18117 +       if (hip) {
18118 +               iinfo->ii_hinode = hip;
18119 +               i = iinfo->ii_bbot + 1;
18120 +               hip += i;
18121 +               for (; i < nbr; i++, hip++)
18122 +                       au_hinode_init(hip);
18123 +               err = 0;
18124 +       }
18125 +
18126 +       return err;
18127 +}
18128 +
18129 +void au_iinfo_fin(struct inode *inode)
18130 +{
18131 +       struct au_iinfo *iinfo;
18132 +       struct au_hinode *hi;
18133 +       struct super_block *sb;
18134 +       aufs_bindex_t bindex, bbot;
18135 +       const unsigned char unlinked = !inode->i_nlink;
18136 +
18137 +       AuDebugOn(au_is_bad_inode(inode));
18138 +
18139 +       sb = inode->i_sb;
18140 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18141 +       if (si_pid_test(sb))
18142 +               au_xino_delete_inode(inode, unlinked);
18143 +       else {
18144 +               /*
18145 +                * it is safe to hide the dependency between sbinfo and
18146 +                * sb->s_umount.
18147 +                */
18148 +               lockdep_off();
18149 +               si_noflush_read_lock(sb);
18150 +               au_xino_delete_inode(inode, unlinked);
18151 +               si_read_unlock(sb);
18152 +               lockdep_on();
18153 +       }
18154 +
18155 +       iinfo = au_ii(inode);
18156 +       if (iinfo->ii_vdir)
18157 +               au_vdir_free(iinfo->ii_vdir);
18158 +
18159 +       bindex = iinfo->ii_btop;
18160 +       if (bindex >= 0) {
18161 +               hi = au_hinode(iinfo, bindex);
18162 +               bbot = iinfo->ii_bbot;
18163 +               while (bindex++ <= bbot) {
18164 +                       if (hi->hi_inode)
18165 +                               au_hiput(hi);
18166 +                       hi++;
18167 +               }
18168 +       }
18169 +       au_kfree_rcu(iinfo->ii_hinode);
18170 +       AuRwDestroy(&iinfo->ii_rwsem);
18171 +}
18172 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18173 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18174 +++ linux/fs/aufs/inode.c       2019-07-11 15:42:14.468904634 +0200
18175 @@ -0,0 +1,529 @@
18176 +// SPDX-License-Identifier: GPL-2.0
18177 +/*
18178 + * Copyright (C) 2005-2019 Junjiro R. Okajima
18179 + *
18180 + * This program, aufs is free software; you can redistribute it and/or modify
18181 + * it under the terms of the GNU General Public License as published by
18182 + * the Free Software Foundation; either version 2 of the License, or
18183 + * (at your option) any later version.
18184 + *
18185 + * This program is distributed in the hope that it will be useful,
18186 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18187 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18188 + * GNU General Public License for more details.
18189 + *
18190 + * You should have received a copy of the GNU General Public License
18191 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18192 + */
18193 +
18194 +/*
18195 + * inode functions
18196 + */
18197 +
18198 +#include <linux/iversion.h>
18199 +#include "aufs.h"
18200 +
18201 +struct inode *au_igrab(struct inode *inode)
18202 +{
18203 +       if (inode) {
18204 +               AuDebugOn(!atomic_read(&inode->i_count));
18205 +               ihold(inode);
18206 +       }
18207 +       return inode;
18208 +}
18209 +
18210 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18211 +{
18212 +       au_cpup_attr_all(inode, /*force*/0);
18213 +       au_update_iigen(inode, /*half*/1);
18214 +       if (do_version)
18215 +               inode_inc_iversion(inode);
18216 +}
18217 +
18218 +static int au_ii_refresh(struct inode *inode, int *update)
18219 +{
18220 +       int err, e, nbr;
18221 +       umode_t type;
18222 +       aufs_bindex_t bindex, new_bindex;
18223 +       struct super_block *sb;
18224 +       struct au_iinfo *iinfo;
18225 +       struct au_hinode *p, *q, tmp;
18226 +
18227 +       AuDebugOn(au_is_bad_inode(inode));
18228 +       IiMustWriteLock(inode);
18229 +
18230 +       *update = 0;
18231 +       sb = inode->i_sb;
18232 +       nbr = au_sbbot(sb) + 1;
18233 +       type = inode->i_mode & S_IFMT;
18234 +       iinfo = au_ii(inode);
18235 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18236 +       if (unlikely(err))
18237 +               goto out;
18238 +
18239 +       AuDebugOn(iinfo->ii_btop < 0);
18240 +       p = au_hinode(iinfo, iinfo->ii_btop);
18241 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18242 +            bindex++, p++) {
18243 +               if (!p->hi_inode)
18244 +                       continue;
18245 +
18246 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18247 +               new_bindex = au_br_index(sb, p->hi_id);
18248 +               if (new_bindex == bindex)
18249 +                       continue;
18250 +
18251 +               if (new_bindex < 0) {
18252 +                       *update = 1;
18253 +                       au_hiput(p);
18254 +                       p->hi_inode = NULL;
18255 +                       continue;
18256 +               }
18257 +
18258 +               if (new_bindex < iinfo->ii_btop)
18259 +                       iinfo->ii_btop = new_bindex;
18260 +               if (iinfo->ii_bbot < new_bindex)
18261 +                       iinfo->ii_bbot = new_bindex;
18262 +               /* swap two lower inode, and loop again */
18263 +               q = au_hinode(iinfo, new_bindex);
18264 +               tmp = *q;
18265 +               *q = *p;
18266 +               *p = tmp;
18267 +               if (tmp.hi_inode) {
18268 +                       bindex--;
18269 +                       p--;
18270 +               }
18271 +       }
18272 +       au_update_ibrange(inode, /*do_put_zero*/0);
18273 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18274 +       e = au_dy_irefresh(inode);
18275 +       if (unlikely(e && !err))
18276 +               err = e;
18277 +
18278 +out:
18279 +       AuTraceErr(err);
18280 +       return err;
18281 +}
18282 +
18283 +void au_refresh_iop(struct inode *inode, int force_getattr)
18284 +{
18285 +       int type;
18286 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18287 +       const struct inode_operations *iop
18288 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18289 +
18290 +       if (inode->i_op == iop)
18291 +               return;
18292 +
18293 +       switch (inode->i_mode & S_IFMT) {
18294 +       case S_IFDIR:
18295 +               type = AuIop_DIR;
18296 +               break;
18297 +       case S_IFLNK:
18298 +               type = AuIop_SYMLINK;
18299 +               break;
18300 +       default:
18301 +               type = AuIop_OTHER;
18302 +               break;
18303 +       }
18304 +
18305 +       inode->i_op = iop + type;
18306 +       /* unnecessary smp_wmb() */
18307 +}
18308 +
18309 +int au_refresh_hinode_self(struct inode *inode)
18310 +{
18311 +       int err, update;
18312 +
18313 +       err = au_ii_refresh(inode, &update);
18314 +       if (!err)
18315 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18316 +
18317 +       AuTraceErr(err);
18318 +       return err;
18319 +}
18320 +
18321 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18322 +{
18323 +       int err, e, update;
18324 +       unsigned int flags;
18325 +       umode_t mode;
18326 +       aufs_bindex_t bindex, bbot;
18327 +       unsigned char isdir;
18328 +       struct au_hinode *p;
18329 +       struct au_iinfo *iinfo;
18330 +
18331 +       err = au_ii_refresh(inode, &update);
18332 +       if (unlikely(err))
18333 +               goto out;
18334 +
18335 +       update = 0;
18336 +       iinfo = au_ii(inode);
18337 +       p = au_hinode(iinfo, iinfo->ii_btop);
18338 +       mode = (inode->i_mode & S_IFMT);
18339 +       isdir = S_ISDIR(mode);
18340 +       flags = au_hi_flags(inode, isdir);
18341 +       bbot = au_dbbot(dentry);
18342 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18343 +               struct inode *h_i, *h_inode;
18344 +               struct dentry *h_d;
18345 +
18346 +               h_d = au_h_dptr(dentry, bindex);
18347 +               if (!h_d || d_is_negative(h_d))
18348 +                       continue;
18349 +
18350 +               h_inode = d_inode(h_d);
18351 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18352 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18353 +                       h_i = au_h_iptr(inode, bindex);
18354 +                       if (h_i) {
18355 +                               if (h_i == h_inode)
18356 +                                       continue;
18357 +                               err = -EIO;
18358 +                               break;
18359 +                       }
18360 +               }
18361 +               if (bindex < iinfo->ii_btop)
18362 +                       iinfo->ii_btop = bindex;
18363 +               if (iinfo->ii_bbot < bindex)
18364 +                       iinfo->ii_bbot = bindex;
18365 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18366 +               update = 1;
18367 +       }
18368 +       au_update_ibrange(inode, /*do_put_zero*/0);
18369 +       e = au_dy_irefresh(inode);
18370 +       if (unlikely(e && !err))
18371 +               err = e;
18372 +       if (!err)
18373 +               au_refresh_hinode_attr(inode, update && isdir);
18374 +
18375 +out:
18376 +       AuTraceErr(err);
18377 +       return err;
18378 +}
18379 +
18380 +static int set_inode(struct inode *inode, struct dentry *dentry)
18381 +{
18382 +       int err;
18383 +       unsigned int flags;
18384 +       umode_t mode;
18385 +       aufs_bindex_t bindex, btop, btail;
18386 +       unsigned char isdir;
18387 +       struct dentry *h_dentry;
18388 +       struct inode *h_inode;
18389 +       struct au_iinfo *iinfo;
18390 +       struct inode_operations *iop;
18391 +
18392 +       IiMustWriteLock(inode);
18393 +
18394 +       err = 0;
18395 +       isdir = 0;
18396 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18397 +       btop = au_dbtop(dentry);
18398 +       h_dentry = au_h_dptr(dentry, btop);
18399 +       h_inode = d_inode(h_dentry);
18400 +       mode = h_inode->i_mode;
18401 +       switch (mode & S_IFMT) {
18402 +       case S_IFREG:
18403 +               btail = au_dbtail(dentry);
18404 +               inode->i_op = iop + AuIop_OTHER;
18405 +               inode->i_fop = &aufs_file_fop;
18406 +               err = au_dy_iaop(inode, btop, h_inode);
18407 +               if (unlikely(err))
18408 +                       goto out;
18409 +               break;
18410 +       case S_IFDIR:
18411 +               isdir = 1;
18412 +               btail = au_dbtaildir(dentry);
18413 +               inode->i_op = iop + AuIop_DIR;
18414 +               inode->i_fop = &aufs_dir_fop;
18415 +               break;
18416 +       case S_IFLNK:
18417 +               btail = au_dbtail(dentry);
18418 +               inode->i_op = iop + AuIop_SYMLINK;
18419 +               break;
18420 +       case S_IFBLK:
18421 +       case S_IFCHR:
18422 +       case S_IFIFO:
18423 +       case S_IFSOCK:
18424 +               btail = au_dbtail(dentry);
18425 +               inode->i_op = iop + AuIop_OTHER;
18426 +               init_special_inode(inode, mode, h_inode->i_rdev);
18427 +               break;
18428 +       default:
18429 +               AuIOErr("Unknown file type 0%o\n", mode);
18430 +               err = -EIO;
18431 +               goto out;
18432 +       }
18433 +
18434 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18435 +       flags = au_hi_flags(inode, isdir);
18436 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18437 +           && au_ftest_hi(flags, HNOTIFY)
18438 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18439 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18440 +               au_fclr_hi(flags, HNOTIFY);
18441 +       iinfo = au_ii(inode);
18442 +       iinfo->ii_btop = btop;
18443 +       iinfo->ii_bbot = btail;
18444 +       for (bindex = btop; bindex <= btail; bindex++) {
18445 +               h_dentry = au_h_dptr(dentry, bindex);
18446 +               if (h_dentry)
18447 +                       au_set_h_iptr(inode, bindex,
18448 +                                     au_igrab(d_inode(h_dentry)), flags);
18449 +       }
18450 +       au_cpup_attr_all(inode, /*force*/1);
18451 +       /*
18452 +        * to force calling aufs_get_acl() every time,
18453 +        * do not call cache_no_acl() for aufs inode.
18454 +        */
18455 +
18456 +out:
18457 +       return err;
18458 +}
18459 +
18460 +/*
18461 + * successful returns with iinfo write_locked
18462 + * minus: errno
18463 + * zero: success, matched
18464 + * plus: no error, but unmatched
18465 + */
18466 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18467 +{
18468 +       int err;
18469 +       unsigned int gen, igflags;
18470 +       aufs_bindex_t bindex, bbot;
18471 +       struct inode *h_inode, *h_dinode;
18472 +       struct dentry *h_dentry;
18473 +
18474 +       /*
18475 +        * before this function, if aufs got any iinfo lock, it must be only
18476 +        * one, the parent dir.
18477 +        * it can happen by UDBA and the obsoleted inode number.
18478 +        */
18479 +       err = -EIO;
18480 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18481 +               goto out;
18482 +
18483 +       err = 1;
18484 +       ii_write_lock_new_child(inode);
18485 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18486 +       h_dinode = d_inode(h_dentry);
18487 +       bbot = au_ibbot(inode);
18488 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18489 +               h_inode = au_h_iptr(inode, bindex);
18490 +               if (!h_inode || h_inode != h_dinode)
18491 +                       continue;
18492 +
18493 +               err = 0;
18494 +               gen = au_iigen(inode, &igflags);
18495 +               if (gen == au_digen(dentry)
18496 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18497 +                       break;
18498 +
18499 +               /* fully refresh inode using dentry */
18500 +               err = au_refresh_hinode(inode, dentry);
18501 +               if (!err)
18502 +                       au_update_iigen(inode, /*half*/0);
18503 +               break;
18504 +       }
18505 +
18506 +       if (unlikely(err))
18507 +               ii_write_unlock(inode);
18508 +out:
18509 +       return err;
18510 +}
18511 +
18512 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18513 +          unsigned int d_type, ino_t *ino)
18514 +{
18515 +       int err, idx;
18516 +       const int isnondir = d_type != DT_DIR;
18517 +
18518 +       /* prevent hardlinked inode number from race condition */
18519 +       if (isnondir) {
18520 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18521 +               if (unlikely(err))
18522 +                       goto out;
18523 +       }
18524 +
18525 +       err = au_xino_read(sb, bindex, h_ino, ino);
18526 +       if (unlikely(err))
18527 +               goto out_xinondir;
18528 +
18529 +       if (!*ino) {
18530 +               err = -EIO;
18531 +               *ino = au_xino_new_ino(sb);
18532 +               if (unlikely(!*ino))
18533 +                       goto out_xinondir;
18534 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18535 +               if (unlikely(err))
18536 +                       goto out_xinondir;
18537 +       }
18538 +
18539 +out_xinondir:
18540 +       if (isnondir && idx >= 0)
18541 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18542 +out:
18543 +       return err;
18544 +}
18545 +
18546 +/* successful returns with iinfo write_locked */
18547 +/* todo: return with unlocked? */
18548 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18549 +{
18550 +       struct inode *inode, *h_inode;
18551 +       struct dentry *h_dentry;
18552 +       struct super_block *sb;
18553 +       ino_t h_ino, ino;
18554 +       int err, idx, hlinked;
18555 +       aufs_bindex_t btop;
18556 +
18557 +       sb = dentry->d_sb;
18558 +       btop = au_dbtop(dentry);
18559 +       h_dentry = au_h_dptr(dentry, btop);
18560 +       h_inode = d_inode(h_dentry);
18561 +       h_ino = h_inode->i_ino;
18562 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18563 +
18564 +new_ino:
18565 +       /*
18566 +        * stop 'race'-ing between hardlinks under different
18567 +        * parents.
18568 +        */
18569 +       if (hlinked) {
18570 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18571 +               inode = ERR_PTR(err);
18572 +               if (unlikely(err))
18573 +                       goto out;
18574 +       }
18575 +
18576 +       err = au_xino_read(sb, btop, h_ino, &ino);
18577 +       inode = ERR_PTR(err);
18578 +       if (unlikely(err))
18579 +               goto out_xinondir;
18580 +
18581 +       if (!ino) {
18582 +               ino = au_xino_new_ino(sb);
18583 +               if (unlikely(!ino)) {
18584 +                       inode = ERR_PTR(-EIO);
18585 +                       goto out_xinondir;
18586 +               }
18587 +       }
18588 +
18589 +       AuDbg("i%lu\n", (unsigned long)ino);
18590 +       inode = au_iget_locked(sb, ino);
18591 +       err = PTR_ERR(inode);
18592 +       if (IS_ERR(inode))
18593 +               goto out_xinondir;
18594 +
18595 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18596 +       if (inode->i_state & I_NEW) {
18597 +               ii_write_lock_new_child(inode);
18598 +               err = set_inode(inode, dentry);
18599 +               if (!err) {
18600 +                       unlock_new_inode(inode);
18601 +                       goto out_xinondir; /* success */
18602 +               }
18603 +
18604 +               /*
18605 +                * iget_failed() calls iput(), but we need to call
18606 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18607 +                * i_count.
18608 +                */
18609 +               atomic_inc(&inode->i_count);
18610 +               iget_failed(inode);
18611 +               ii_write_unlock(inode);
18612 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18613 +               /* ignore this error */
18614 +               goto out_iput;
18615 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18616 +               /*
18617 +                * horrible race condition between lookup, readdir and copyup
18618 +                * (or something).
18619 +                */
18620 +               if (hlinked && idx >= 0)
18621 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18622 +               err = reval_inode(inode, dentry);
18623 +               if (unlikely(err < 0)) {
18624 +                       hlinked = 0;
18625 +                       goto out_iput;
18626 +               }
18627 +               if (!err)
18628 +                       goto out; /* success */
18629 +               else if (hlinked && idx >= 0) {
18630 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18631 +                       if (unlikely(err)) {
18632 +                               iput(inode);
18633 +                               inode = ERR_PTR(err);
18634 +                               goto out;
18635 +                       }
18636 +               }
18637 +       }
18638 +
18639 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18640 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18641 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18642 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18643 +                       (unsigned long)h_ino, (unsigned long)ino);
18644 +       ino = 0;
18645 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18646 +       if (!err) {
18647 +               iput(inode);
18648 +               if (hlinked && idx >= 0)
18649 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18650 +               goto new_ino;
18651 +       }
18652 +
18653 +out_iput:
18654 +       iput(inode);
18655 +       inode = ERR_PTR(err);
18656 +out_xinondir:
18657 +       if (hlinked && idx >= 0)
18658 +               au_xinondir_leave(sb, btop, h_ino, idx);
18659 +out:
18660 +       return inode;
18661 +}
18662 +
18663 +/* ---------------------------------------------------------------------- */
18664 +
18665 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18666 +              struct inode *inode)
18667 +{
18668 +       int err;
18669 +       struct inode *hi;
18670 +
18671 +       err = au_br_rdonly(au_sbr(sb, bindex));
18672 +
18673 +       /* pseudo-link after flushed may happen out of bounds */
18674 +       if (!err
18675 +           && inode
18676 +           && au_ibtop(inode) <= bindex
18677 +           && bindex <= au_ibbot(inode)) {
18678 +               /*
18679 +                * permission check is unnecessary since vfsub routine
18680 +                * will be called later
18681 +                */
18682 +               hi = au_h_iptr(inode, bindex);
18683 +               if (hi)
18684 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18685 +       }
18686 +
18687 +       return err;
18688 +}
18689 +
18690 +int au_test_h_perm(struct inode *h_inode, int mask)
18691 +{
18692 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18693 +               return 0;
18694 +       return inode_permission(h_inode, mask);
18695 +}
18696 +
18697 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18698 +{
18699 +       if (au_test_nfs(h_inode->i_sb)
18700 +           && (mask & MAY_WRITE)
18701 +           && S_ISDIR(h_inode->i_mode))
18702 +               mask |= MAY_READ; /* force permission check */
18703 +       return au_test_h_perm(h_inode, mask);
18704 +}
18705 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18706 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18707 +++ linux/fs/aufs/inode.h       2019-07-11 15:42:14.468904634 +0200
18708 @@ -0,0 +1,698 @@
18709 +/* SPDX-License-Identifier: GPL-2.0 */
18710 +/*
18711 + * Copyright (C) 2005-2019 Junjiro R. Okajima
18712 + *
18713 + * This program, aufs is free software; you can redistribute it and/or modify
18714 + * it under the terms of the GNU General Public License as published by
18715 + * the Free Software Foundation; either version 2 of the License, or
18716 + * (at your option) any later version.
18717 + *
18718 + * This program is distributed in the hope that it will be useful,
18719 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18720 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18721 + * GNU General Public License for more details.
18722 + *
18723 + * You should have received a copy of the GNU General Public License
18724 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18725 + */
18726 +
18727 +/*
18728 + * inode operations
18729 + */
18730 +
18731 +#ifndef __AUFS_INODE_H__
18732 +#define __AUFS_INODE_H__
18733 +
18734 +#ifdef __KERNEL__
18735 +
18736 +#include <linux/fsnotify.h>
18737 +#include "rwsem.h"
18738 +
18739 +struct vfsmount;
18740 +
18741 +struct au_hnotify {
18742 +#ifdef CONFIG_AUFS_HNOTIFY
18743 +#ifdef CONFIG_AUFS_HFSNOTIFY
18744 +       /* never use fsnotify_add_vfsmount_mark() */
18745 +       struct fsnotify_mark            hn_mark;
18746 +#endif
18747 +       struct inode            *hn_aufs_inode; /* no get/put */
18748 +       struct rcu_head         rcu;
18749 +#endif
18750 +} ____cacheline_aligned_in_smp;
18751 +
18752 +struct au_hinode {
18753 +       struct inode            *hi_inode;
18754 +       aufs_bindex_t           hi_id;
18755 +#ifdef CONFIG_AUFS_HNOTIFY
18756 +       struct au_hnotify       *hi_notify;
18757 +#endif
18758 +
18759 +       /* reference to the copied-up whiteout with get/put */
18760 +       struct dentry           *hi_whdentry;
18761 +};
18762 +
18763 +/* ig_flags */
18764 +#define AuIG_HALF_REFRESHED            1
18765 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18766 +#define au_ig_fset(flags, name) \
18767 +       do { (flags) |= AuIG_##name; } while (0)
18768 +#define au_ig_fclr(flags, name) \
18769 +       do { (flags) &= ~AuIG_##name; } while (0)
18770 +
18771 +struct au_iigen {
18772 +       spinlock_t      ig_spin;
18773 +       __u32           ig_generation, ig_flags;
18774 +};
18775 +
18776 +struct au_vdir;
18777 +struct au_iinfo {
18778 +       struct au_iigen         ii_generation;
18779 +       struct super_block      *ii_hsb1;       /* no get/put */
18780 +
18781 +       struct au_rwsem         ii_rwsem;
18782 +       aufs_bindex_t           ii_btop, ii_bbot;
18783 +       __u32                   ii_higen;
18784 +       struct au_hinode        *ii_hinode;
18785 +       struct au_vdir          *ii_vdir;
18786 +};
18787 +
18788 +struct au_icntnr {
18789 +       struct au_iinfo         iinfo;
18790 +       struct inode            vfs_inode;
18791 +       struct hlist_bl_node    plink;
18792 +       struct rcu_head         rcu;
18793 +} ____cacheline_aligned_in_smp;
18794 +
18795 +/* au_pin flags */
18796 +#define AuPin_DI_LOCKED                1
18797 +#define AuPin_MNT_WRITE                (1 << 1)
18798 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18799 +#define au_fset_pin(flags, name) \
18800 +       do { (flags) |= AuPin_##name; } while (0)
18801 +#define au_fclr_pin(flags, name) \
18802 +       do { (flags) &= ~AuPin_##name; } while (0)
18803 +
18804 +struct au_pin {
18805 +       /* input */
18806 +       struct dentry *dentry;
18807 +       unsigned int udba;
18808 +       unsigned char lsc_di, lsc_hi, flags;
18809 +       aufs_bindex_t bindex;
18810 +
18811 +       /* output */
18812 +       struct dentry *parent;
18813 +       struct au_hinode *hdir;
18814 +       struct vfsmount *h_mnt;
18815 +
18816 +       /* temporary unlock/relock for copyup */
18817 +       struct dentry *h_dentry, *h_parent;
18818 +       struct au_branch *br;
18819 +       struct task_struct *task;
18820 +};
18821 +
18822 +void au_pin_hdir_unlock(struct au_pin *p);
18823 +int au_pin_hdir_lock(struct au_pin *p);
18824 +int au_pin_hdir_relock(struct au_pin *p);
18825 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18826 +void au_pin_hdir_release(struct au_pin *p);
18827 +
18828 +/* ---------------------------------------------------------------------- */
18829 +
18830 +static inline struct au_iinfo *au_ii(struct inode *inode)
18831 +{
18832 +       BUG_ON(is_bad_inode(inode));
18833 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18834 +}
18835 +
18836 +/* ---------------------------------------------------------------------- */
18837 +
18838 +/* inode.c */
18839 +struct inode *au_igrab(struct inode *inode);
18840 +void au_refresh_iop(struct inode *inode, int force_getattr);
18841 +int au_refresh_hinode_self(struct inode *inode);
18842 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18843 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18844 +          unsigned int d_type, ino_t *ino);
18845 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18846 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18847 +              struct inode *inode);
18848 +int au_test_h_perm(struct inode *h_inode, int mask);
18849 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18850 +
18851 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18852 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18853 +{
18854 +#ifdef CONFIG_AUFS_SHWH
18855 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18856 +#else
18857 +       return 0;
18858 +#endif
18859 +}
18860 +
18861 +/* i_op.c */
18862 +enum {
18863 +       AuIop_SYMLINK,
18864 +       AuIop_DIR,
18865 +       AuIop_OTHER,
18866 +       AuIop_Last
18867 +};
18868 +extern struct inode_operations aufs_iop[AuIop_Last],
18869 +       aufs_iop_nogetattr[AuIop_Last];
18870 +
18871 +/* au_wr_dir flags */
18872 +#define AuWrDir_ADD_ENTRY      1
18873 +#define AuWrDir_ISDIR          (1 << 1)
18874 +#define AuWrDir_TMPFILE                (1 << 2)
18875 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18876 +#define au_fset_wrdir(flags, name) \
18877 +       do { (flags) |= AuWrDir_##name; } while (0)
18878 +#define au_fclr_wrdir(flags, name) \
18879 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18880 +
18881 +struct au_wr_dir_args {
18882 +       aufs_bindex_t force_btgt;
18883 +       unsigned char flags;
18884 +};
18885 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18886 +             struct au_wr_dir_args *args);
18887 +
18888 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18889 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18890 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18891 +                unsigned int udba, unsigned char flags);
18892 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18893 +          unsigned int udba, unsigned char flags) __must_check;
18894 +int au_do_pin(struct au_pin *pin) __must_check;
18895 +void au_unpin(struct au_pin *pin);
18896 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18897 +
18898 +#define AuIcpup_DID_CPUP       1
18899 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18900 +#define au_fset_icpup(flags, name) \
18901 +       do { (flags) |= AuIcpup_##name; } while (0)
18902 +#define au_fclr_icpup(flags, name) \
18903 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18904 +
18905 +struct au_icpup_args {
18906 +       unsigned char flags;
18907 +       unsigned char pin_flags;
18908 +       aufs_bindex_t btgt;
18909 +       unsigned int udba;
18910 +       struct au_pin pin;
18911 +       struct path h_path;
18912 +       struct inode *h_inode;
18913 +};
18914 +
18915 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18916 +                    struct au_icpup_args *a);
18917 +
18918 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18919 +                     int locked);
18920 +
18921 +/* i_op_add.c */
18922 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18923 +              struct dentry *h_parent, int isdir);
18924 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18925 +              dev_t dev);
18926 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18927 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18928 +               bool want_excl);
18929 +struct vfsub_aopen_args;
18930 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18931 +                      struct vfsub_aopen_args *args);
18932 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18933 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18934 +             struct dentry *dentry);
18935 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18936 +
18937 +/* i_op_del.c */
18938 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18939 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18940 +              struct dentry *h_parent, int isdir);
18941 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18942 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18943 +
18944 +/* i_op_ren.c */
18945 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18946 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18947 +               struct inode *dir, struct dentry *dentry,
18948 +               unsigned int flags);
18949 +
18950 +/* iinfo.c */
18951 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18952 +void au_hiput(struct au_hinode *hinode);
18953 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18954 +                 struct dentry *h_wh);
18955 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18956 +
18957 +/* hinode flags */
18958 +#define AuHi_XINO      1
18959 +#define AuHi_HNOTIFY   (1 << 1)
18960 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18961 +#define au_fset_hi(flags, name) \
18962 +       do { (flags) |= AuHi_##name; } while (0)
18963 +#define au_fclr_hi(flags, name) \
18964 +       do { (flags) &= ~AuHi_##name; } while (0)
18965 +
18966 +#ifndef CONFIG_AUFS_HNOTIFY
18967 +#undef AuHi_HNOTIFY
18968 +#define AuHi_HNOTIFY   0
18969 +#endif
18970 +
18971 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18972 +                  struct inode *h_inode, unsigned int flags);
18973 +
18974 +void au_update_iigen(struct inode *inode, int half);
18975 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18976 +
18977 +void au_icntnr_init_once(void *_c);
18978 +void au_hinode_init(struct au_hinode *hinode);
18979 +int au_iinfo_init(struct inode *inode);
18980 +void au_iinfo_fin(struct inode *inode);
18981 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18982 +
18983 +#ifdef CONFIG_PROC_FS
18984 +/* plink.c */
18985 +int au_plink_maint(struct super_block *sb, int flags);
18986 +struct au_sbinfo;
18987 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18988 +int au_plink_maint_enter(struct super_block *sb);
18989 +#ifdef CONFIG_AUFS_DEBUG
18990 +void au_plink_list(struct super_block *sb);
18991 +#else
18992 +AuStubVoid(au_plink_list, struct super_block *sb)
18993 +#endif
18994 +int au_plink_test(struct inode *inode);
18995 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18996 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18997 +                    struct dentry *h_dentry);
18998 +void au_plink_put(struct super_block *sb, int verbose);
18999 +void au_plink_clean(struct super_block *sb, int verbose);
19000 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
19001 +#else
19002 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19003 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19004 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19005 +AuStubVoid(au_plink_list, struct super_block *sb);
19006 +AuStubInt0(au_plink_test, struct inode *inode);
19007 +AuStub(struct dentry *, au_plink_lkup, return NULL,
19008 +       struct inode *inode, aufs_bindex_t bindex);
19009 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19010 +          struct dentry *h_dentry);
19011 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19012 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19013 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19014 +#endif /* CONFIG_PROC_FS */
19015 +
19016 +#ifdef CONFIG_AUFS_XATTR
19017 +/* xattr.c */
19018 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19019 +                 unsigned int verbose);
19020 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
19021 +void au_xattr_init(struct super_block *sb);
19022 +#else
19023 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
19024 +          int ignore_flags, unsigned int verbose);
19025 +AuStubVoid(au_xattr_init, struct super_block *sb);
19026 +#endif
19027 +
19028 +#ifdef CONFIG_FS_POSIX_ACL
19029 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19030 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19031 +#endif
19032 +
19033 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19034 +enum {
19035 +       AU_XATTR_SET,
19036 +       AU_ACL_SET
19037 +};
19038 +
19039 +struct au_sxattr {
19040 +       int type;
19041 +       union {
19042 +               struct {
19043 +                       const char      *name;
19044 +                       const void      *value;
19045 +                       size_t          size;
19046 +                       int             flags;
19047 +               } set;
19048 +               struct {
19049 +                       struct posix_acl *acl;
19050 +                       int             type;
19051 +               } acl_set;
19052 +       } u;
19053 +};
19054 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19055 +                 struct au_sxattr *arg);
19056 +#endif
19057 +
19058 +/* ---------------------------------------------------------------------- */
19059 +
19060 +/* lock subclass for iinfo */
19061 +enum {
19062 +       AuLsc_II_CHILD,         /* child first */
19063 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
19064 +       AuLsc_II_CHILD3,        /* copyup dirs */
19065 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
19066 +       AuLsc_II_PARENT2,
19067 +       AuLsc_II_PARENT3,       /* copyup dirs */
19068 +       AuLsc_II_NEW_CHILD
19069 +};
19070 +
19071 +/*
19072 + * ii_read_lock_child, ii_write_lock_child,
19073 + * ii_read_lock_child2, ii_write_lock_child2,
19074 + * ii_read_lock_child3, ii_write_lock_child3,
19075 + * ii_read_lock_parent, ii_write_lock_parent,
19076 + * ii_read_lock_parent2, ii_write_lock_parent2,
19077 + * ii_read_lock_parent3, ii_write_lock_parent3,
19078 + * ii_read_lock_new_child, ii_write_lock_new_child,
19079 + */
19080 +#define AuReadLockFunc(name, lsc) \
19081 +static inline void ii_read_lock_##name(struct inode *i) \
19082 +{ \
19083 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19084 +}
19085 +
19086 +#define AuWriteLockFunc(name, lsc) \
19087 +static inline void ii_write_lock_##name(struct inode *i) \
19088 +{ \
19089 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19090 +}
19091 +
19092 +#define AuRWLockFuncs(name, lsc) \
19093 +       AuReadLockFunc(name, lsc) \
19094 +       AuWriteLockFunc(name, lsc)
19095 +
19096 +AuRWLockFuncs(child, CHILD);
19097 +AuRWLockFuncs(child2, CHILD2);
19098 +AuRWLockFuncs(child3, CHILD3);
19099 +AuRWLockFuncs(parent, PARENT);
19100 +AuRWLockFuncs(parent2, PARENT2);
19101 +AuRWLockFuncs(parent3, PARENT3);
19102 +AuRWLockFuncs(new_child, NEW_CHILD);
19103 +
19104 +#undef AuReadLockFunc
19105 +#undef AuWriteLockFunc
19106 +#undef AuRWLockFuncs
19107 +
19108 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19109 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19110 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19111 +
19112 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19113 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19114 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19115 +
19116 +/* ---------------------------------------------------------------------- */
19117 +
19118 +static inline void au_icntnr_init(struct au_icntnr *c)
19119 +{
19120 +#ifdef CONFIG_AUFS_DEBUG
19121 +       c->vfs_inode.i_mode = 0;
19122 +#endif
19123 +}
19124 +
19125 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19126 +{
19127 +       unsigned int gen;
19128 +       struct au_iinfo *iinfo;
19129 +       struct au_iigen *iigen;
19130 +
19131 +       iinfo = au_ii(inode);
19132 +       iigen = &iinfo->ii_generation;
19133 +       spin_lock(&iigen->ig_spin);
19134 +       if (igflags)
19135 +               *igflags = iigen->ig_flags;
19136 +       gen = iigen->ig_generation;
19137 +       spin_unlock(&iigen->ig_spin);
19138 +
19139 +       return gen;
19140 +}
19141 +
19142 +/* tiny test for inode number */
19143 +/* tmpfs generation is too rough */
19144 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19145 +{
19146 +       struct au_iinfo *iinfo;
19147 +
19148 +       iinfo = au_ii(inode);
19149 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19150 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19151 +                && iinfo->ii_higen == h_inode->i_generation);
19152 +}
19153 +
19154 +static inline void au_iigen_dec(struct inode *inode)
19155 +{
19156 +       struct au_iinfo *iinfo;
19157 +       struct au_iigen *iigen;
19158 +
19159 +       iinfo = au_ii(inode);
19160 +       iigen = &iinfo->ii_generation;
19161 +       spin_lock(&iigen->ig_spin);
19162 +       iigen->ig_generation--;
19163 +       spin_unlock(&iigen->ig_spin);
19164 +}
19165 +
19166 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19167 +{
19168 +       int err;
19169 +
19170 +       err = 0;
19171 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19172 +               err = -EIO;
19173 +
19174 +       return err;
19175 +}
19176 +
19177 +/* ---------------------------------------------------------------------- */
19178 +
19179 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19180 +                                         aufs_bindex_t bindex)
19181 +{
19182 +       return iinfo->ii_hinode + bindex;
19183 +}
19184 +
19185 +static inline int au_is_bad_inode(struct inode *inode)
19186 +{
19187 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19188 +}
19189 +
19190 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19191 +                                       aufs_bindex_t bindex)
19192 +{
19193 +       IiMustAnyLock(inode);
19194 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19195 +}
19196 +
19197 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19198 +{
19199 +       IiMustAnyLock(inode);
19200 +       return au_ii(inode)->ii_btop;
19201 +}
19202 +
19203 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19204 +{
19205 +       IiMustAnyLock(inode);
19206 +       return au_ii(inode)->ii_bbot;
19207 +}
19208 +
19209 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19210 +{
19211 +       IiMustAnyLock(inode);
19212 +       return au_ii(inode)->ii_vdir;
19213 +}
19214 +
19215 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19216 +{
19217 +       IiMustAnyLock(inode);
19218 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19219 +}
19220 +
19221 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19222 +{
19223 +       IiMustWriteLock(inode);
19224 +       au_ii(inode)->ii_btop = bindex;
19225 +}
19226 +
19227 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19228 +{
19229 +       IiMustWriteLock(inode);
19230 +       au_ii(inode)->ii_bbot = bindex;
19231 +}
19232 +
19233 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19234 +{
19235 +       IiMustWriteLock(inode);
19236 +       au_ii(inode)->ii_vdir = vdir;
19237 +}
19238 +
19239 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19240 +{
19241 +       IiMustAnyLock(inode);
19242 +       return au_hinode(au_ii(inode), bindex);
19243 +}
19244 +
19245 +/* ---------------------------------------------------------------------- */
19246 +
19247 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19248 +{
19249 +       if (pin)
19250 +               return pin->parent;
19251 +       return NULL;
19252 +}
19253 +
19254 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19255 +{
19256 +       if (pin && pin->hdir)
19257 +               return pin->hdir->hi_inode;
19258 +       return NULL;
19259 +}
19260 +
19261 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19262 +{
19263 +       if (pin)
19264 +               return pin->hdir;
19265 +       return NULL;
19266 +}
19267 +
19268 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19269 +{
19270 +       if (pin)
19271 +               pin->dentry = dentry;
19272 +}
19273 +
19274 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19275 +                                          unsigned char lflag)
19276 +{
19277 +       if (pin) {
19278 +               if (lflag)
19279 +                       au_fset_pin(pin->flags, DI_LOCKED);
19280 +               else
19281 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19282 +       }
19283 +}
19284 +
19285 +#if 0 /* reserved */
19286 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19287 +{
19288 +       if (pin) {
19289 +               dput(pin->parent);
19290 +               pin->parent = dget(parent);
19291 +       }
19292 +}
19293 +#endif
19294 +
19295 +/* ---------------------------------------------------------------------- */
19296 +
19297 +struct au_branch;
19298 +#ifdef CONFIG_AUFS_HNOTIFY
19299 +struct au_hnotify_op {
19300 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19301 +       int (*alloc)(struct au_hinode *hinode);
19302 +
19303 +       /*
19304 +        * if it returns true, the the caller should free hinode->hi_notify,
19305 +        * otherwise ->free() frees it.
19306 +        */
19307 +       int (*free)(struct au_hinode *hinode,
19308 +                   struct au_hnotify *hn) __must_check;
19309 +
19310 +       void (*fin)(void);
19311 +       int (*init)(void);
19312 +
19313 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19314 +       void (*fin_br)(struct au_branch *br);
19315 +       int (*init_br)(struct au_branch *br, int perm);
19316 +};
19317 +
19318 +/* hnotify.c */
19319 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19320 +void au_hn_free(struct au_hinode *hinode);
19321 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19322 +void au_hn_reset(struct inode *inode, unsigned int flags);
19323 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19324 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19325 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19326 +int au_hnotify_init_br(struct au_branch *br, int perm);
19327 +void au_hnotify_fin_br(struct au_branch *br);
19328 +int __init au_hnotify_init(void);
19329 +void au_hnotify_fin(void);
19330 +
19331 +/* hfsnotify.c */
19332 +extern const struct au_hnotify_op au_hnotify_op;
19333 +
19334 +static inline
19335 +void au_hn_init(struct au_hinode *hinode)
19336 +{
19337 +       hinode->hi_notify = NULL;
19338 +}
19339 +
19340 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19341 +{
19342 +       return hinode->hi_notify;
19343 +}
19344 +
19345 +#else
19346 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19347 +       struct au_hinode *hinode __maybe_unused,
19348 +       struct inode *inode __maybe_unused)
19349 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19350 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19351 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19352 +          int do_set __maybe_unused)
19353 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19354 +          unsigned int flags __maybe_unused)
19355 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19356 +          struct au_branch *br __maybe_unused,
19357 +          int perm __maybe_unused)
19358 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19359 +          int perm __maybe_unused)
19360 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19361 +AuStubInt0(__init au_hnotify_init, void)
19362 +AuStubVoid(au_hnotify_fin, void)
19363 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19364 +#endif /* CONFIG_AUFS_HNOTIFY */
19365 +
19366 +static inline void au_hn_suspend(struct au_hinode *hdir)
19367 +{
19368 +       au_hn_ctl(hdir, /*do_set*/0);
19369 +}
19370 +
19371 +static inline void au_hn_resume(struct au_hinode *hdir)
19372 +{
19373 +       au_hn_ctl(hdir, /*do_set*/1);
19374 +}
19375 +
19376 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19377 +{
19378 +       inode_lock(hdir->hi_inode);
19379 +       au_hn_suspend(hdir);
19380 +}
19381 +
19382 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19383 +                                         unsigned int sc __maybe_unused)
19384 +{
19385 +       inode_lock_nested(hdir->hi_inode, sc);
19386 +       au_hn_suspend(hdir);
19387 +}
19388 +
19389 +#if 0 /* unused */
19390 +#include "vfsub.h"
19391 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19392 +                                                 unsigned int sc)
19393 +{
19394 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19395 +       au_hn_suspend(hdir);
19396 +}
19397 +#endif
19398 +
19399 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19400 +{
19401 +       au_hn_resume(hdir);
19402 +       inode_unlock(hdir->hi_inode);
19403 +}
19404 +
19405 +#endif /* __KERNEL__ */
19406 +#endif /* __AUFS_INODE_H__ */
19407 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19408 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19409 +++ linux/fs/aufs/ioctl.c       2019-07-11 15:42:14.468904634 +0200
19410 @@ -0,0 +1,220 @@
19411 +// SPDX-License-Identifier: GPL-2.0
19412 +/*
19413 + * Copyright (C) 2005-2019 Junjiro R. Okajima
19414 + *
19415 + * This program, aufs is free software; you can redistribute it and/or modify
19416 + * it under the terms of the GNU General Public License as published by
19417 + * the Free Software Foundation; either version 2 of the License, or
19418 + * (at your option) any later version.
19419 + *
19420 + * This program is distributed in the hope that it will be useful,
19421 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19422 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19423 + * GNU General Public License for more details.
19424 + *
19425 + * You should have received a copy of the GNU General Public License
19426 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19427 + */
19428 +
19429 +/*
19430 + * ioctl
19431 + * plink-management and readdir in userspace.
19432 + * assist the pathconf(3) wrapper library.
19433 + * move-down
19434 + * File-based Hierarchical Storage Management.
19435 + */
19436 +
19437 +#include <linux/compat.h>
19438 +#include <linux/file.h>
19439 +#include "aufs.h"
19440 +
19441 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19442 +{
19443 +       int err, fd;
19444 +       aufs_bindex_t wbi, bindex, bbot;
19445 +       struct file *h_file;
19446 +       struct super_block *sb;
19447 +       struct dentry *root;
19448 +       struct au_branch *br;
19449 +       struct aufs_wbr_fd wbrfd = {
19450 +               .oflags = au_dir_roflags,
19451 +               .brid   = -1
19452 +       };
19453 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19454 +               | O_NOATIME | O_CLOEXEC;
19455 +
19456 +       AuDebugOn(wbrfd.oflags & ~valid);
19457 +
19458 +       if (arg) {
19459 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19460 +               if (unlikely(err)) {
19461 +                       err = -EFAULT;
19462 +                       goto out;
19463 +               }
19464 +
19465 +               err = -EINVAL;
19466 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19467 +               wbrfd.oflags |= au_dir_roflags;
19468 +               AuDbg("0%o\n", wbrfd.oflags);
19469 +               if (unlikely(wbrfd.oflags & ~valid))
19470 +                       goto out;
19471 +       }
19472 +
19473 +       fd = get_unused_fd_flags(0);
19474 +       err = fd;
19475 +       if (unlikely(fd < 0))
19476 +               goto out;
19477 +
19478 +       h_file = ERR_PTR(-EINVAL);
19479 +       wbi = 0;
19480 +       br = NULL;
19481 +       sb = path->dentry->d_sb;
19482 +       root = sb->s_root;
19483 +       aufs_read_lock(root, AuLock_IR);
19484 +       bbot = au_sbbot(sb);
19485 +       if (wbrfd.brid >= 0) {
19486 +               wbi = au_br_index(sb, wbrfd.brid);
19487 +               if (unlikely(wbi < 0 || wbi > bbot))
19488 +                       goto out_unlock;
19489 +       }
19490 +
19491 +       h_file = ERR_PTR(-ENOENT);
19492 +       br = au_sbr(sb, wbi);
19493 +       if (!au_br_writable(br->br_perm)) {
19494 +               if (arg)
19495 +                       goto out_unlock;
19496 +
19497 +               bindex = wbi + 1;
19498 +               wbi = -1;
19499 +               for (; bindex <= bbot; bindex++) {
19500 +                       br = au_sbr(sb, bindex);
19501 +                       if (au_br_writable(br->br_perm)) {
19502 +                               wbi = bindex;
19503 +                               br = au_sbr(sb, wbi);
19504 +                               break;
19505 +                       }
19506 +               }
19507 +       }
19508 +       AuDbg("wbi %d\n", wbi);
19509 +       if (wbi >= 0)
19510 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19511 +                                  /*force_wr*/0);
19512 +
19513 +out_unlock:
19514 +       aufs_read_unlock(root, AuLock_IR);
19515 +       err = PTR_ERR(h_file);
19516 +       if (IS_ERR(h_file))
19517 +               goto out_fd;
19518 +
19519 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19520 +       fd_install(fd, h_file);
19521 +       err = fd;
19522 +       goto out; /* success */
19523 +
19524 +out_fd:
19525 +       put_unused_fd(fd);
19526 +out:
19527 +       AuTraceErr(err);
19528 +       return err;
19529 +}
19530 +
19531 +/* ---------------------------------------------------------------------- */
19532 +
19533 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19534 +{
19535 +       long err;
19536 +       struct dentry *dentry;
19537 +
19538 +       switch (cmd) {
19539 +       case AUFS_CTL_RDU:
19540 +       case AUFS_CTL_RDU_INO:
19541 +               err = au_rdu_ioctl(file, cmd, arg);
19542 +               break;
19543 +
19544 +       case AUFS_CTL_WBR_FD:
19545 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19546 +               break;
19547 +
19548 +       case AUFS_CTL_IBUSY:
19549 +               err = au_ibusy_ioctl(file, arg);
19550 +               break;
19551 +
19552 +       case AUFS_CTL_BRINFO:
19553 +               err = au_brinfo_ioctl(file, arg);
19554 +               break;
19555 +
19556 +       case AUFS_CTL_FHSM_FD:
19557 +               dentry = file->f_path.dentry;
19558 +               if (IS_ROOT(dentry))
19559 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19560 +               else
19561 +                       err = -ENOTTY;
19562 +               break;
19563 +
19564 +       default:
19565 +               /* do not call the lower */
19566 +               AuDbg("0x%x\n", cmd);
19567 +               err = -ENOTTY;
19568 +       }
19569 +
19570 +       AuTraceErr(err);
19571 +       return err;
19572 +}
19573 +
19574 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19575 +{
19576 +       long err;
19577 +
19578 +       switch (cmd) {
19579 +       case AUFS_CTL_MVDOWN:
19580 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19581 +               break;
19582 +
19583 +       case AUFS_CTL_WBR_FD:
19584 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19585 +               break;
19586 +
19587 +       default:
19588 +               /* do not call the lower */
19589 +               AuDbg("0x%x\n", cmd);
19590 +               err = -ENOTTY;
19591 +       }
19592 +
19593 +       AuTraceErr(err);
19594 +       return err;
19595 +}
19596 +
19597 +#ifdef CONFIG_COMPAT
19598 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19599 +                          unsigned long arg)
19600 +{
19601 +       long err;
19602 +
19603 +       switch (cmd) {
19604 +       case AUFS_CTL_RDU:
19605 +       case AUFS_CTL_RDU_INO:
19606 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19607 +               break;
19608 +
19609 +       case AUFS_CTL_IBUSY:
19610 +               err = au_ibusy_compat_ioctl(file, arg);
19611 +               break;
19612 +
19613 +       case AUFS_CTL_BRINFO:
19614 +               err = au_brinfo_compat_ioctl(file, arg);
19615 +               break;
19616 +
19617 +       default:
19618 +               err = aufs_ioctl_dir(file, cmd, arg);
19619 +       }
19620 +
19621 +       AuTraceErr(err);
19622 +       return err;
19623 +}
19624 +
19625 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19626 +                             unsigned long arg)
19627 +{
19628 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19629 +}
19630 +#endif
19631 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19632 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19633 +++ linux/fs/aufs/i_op_add.c    2019-07-11 15:42:14.468904634 +0200
19634 @@ -0,0 +1,936 @@
19635 +// SPDX-License-Identifier: GPL-2.0
19636 +/*
19637 + * Copyright (C) 2005-2019 Junjiro R. Okajima
19638 + *
19639 + * This program, aufs is free software; you can redistribute it and/or modify
19640 + * it under the terms of the GNU General Public License as published by
19641 + * the Free Software Foundation; either version 2 of the License, or
19642 + * (at your option) any later version.
19643 + *
19644 + * This program is distributed in the hope that it will be useful,
19645 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19646 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19647 + * GNU General Public License for more details.
19648 + *
19649 + * You should have received a copy of the GNU General Public License
19650 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19651 + */
19652 +
19653 +/*
19654 + * inode operations (add entry)
19655 + */
19656 +
19657 +#include <linux/iversion.h>
19658 +#include "aufs.h"
19659 +
19660 +/*
19661 + * final procedure of adding a new entry, except link(2).
19662 + * remove whiteout, instantiate, copyup the parent dir's times and size
19663 + * and update version.
19664 + * if it failed, re-create the removed whiteout.
19665 + */
19666 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19667 +                 struct dentry *wh_dentry, struct dentry *dentry)
19668 +{
19669 +       int err, rerr;
19670 +       aufs_bindex_t bwh;
19671 +       struct path h_path;
19672 +       struct super_block *sb;
19673 +       struct inode *inode, *h_dir;
19674 +       struct dentry *wh;
19675 +
19676 +       bwh = -1;
19677 +       sb = dir->i_sb;
19678 +       if (wh_dentry) {
19679 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19680 +               IMustLock(h_dir);
19681 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19682 +               bwh = au_dbwh(dentry);
19683 +               h_path.dentry = wh_dentry;
19684 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19685 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19686 +                                         dentry);
19687 +               if (unlikely(err))
19688 +                       goto out;
19689 +       }
19690 +
19691 +       inode = au_new_inode(dentry, /*must_new*/1);
19692 +       if (!IS_ERR(inode)) {
19693 +               d_instantiate(dentry, inode);
19694 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19695 +               IMustLock(dir);
19696 +               au_dir_ts(dir, bindex);
19697 +               inode_inc_iversion(dir);
19698 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19699 +               return 0; /* success */
19700 +       }
19701 +
19702 +       err = PTR_ERR(inode);
19703 +       if (!wh_dentry)
19704 +               goto out;
19705 +
19706 +       /* revert */
19707 +       /* dir inode is locked */
19708 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19709 +       rerr = PTR_ERR(wh);
19710 +       if (IS_ERR(wh)) {
19711 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19712 +                       dentry, err, rerr);
19713 +               err = -EIO;
19714 +       } else
19715 +               dput(wh);
19716 +
19717 +out:
19718 +       return err;
19719 +}
19720 +
19721 +static int au_d_may_add(struct dentry *dentry)
19722 +{
19723 +       int err;
19724 +
19725 +       err = 0;
19726 +       if (unlikely(d_unhashed(dentry)))
19727 +               err = -ENOENT;
19728 +       if (unlikely(d_really_is_positive(dentry)))
19729 +               err = -EEXIST;
19730 +       return err;
19731 +}
19732 +
19733 +/*
19734 + * simple tests for the adding inode operations.
19735 + * following the checks in vfs, plus the parent-child relationship.
19736 + */
19737 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19738 +              struct dentry *h_parent, int isdir)
19739 +{
19740 +       int err;
19741 +       umode_t h_mode;
19742 +       struct dentry *h_dentry;
19743 +       struct inode *h_inode;
19744 +
19745 +       err = -ENAMETOOLONG;
19746 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19747 +               goto out;
19748 +
19749 +       h_dentry = au_h_dptr(dentry, bindex);
19750 +       if (d_really_is_negative(dentry)) {
19751 +               err = -EEXIST;
19752 +               if (unlikely(d_is_positive(h_dentry)))
19753 +                       goto out;
19754 +       } else {
19755 +               /* rename(2) case */
19756 +               err = -EIO;
19757 +               if (unlikely(d_is_negative(h_dentry)))
19758 +                       goto out;
19759 +               h_inode = d_inode(h_dentry);
19760 +               if (unlikely(!h_inode->i_nlink))
19761 +                       goto out;
19762 +
19763 +               h_mode = h_inode->i_mode;
19764 +               if (!isdir) {
19765 +                       err = -EISDIR;
19766 +                       if (unlikely(S_ISDIR(h_mode)))
19767 +                               goto out;
19768 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19769 +                       err = -ENOTDIR;
19770 +                       goto out;
19771 +               }
19772 +       }
19773 +
19774 +       err = 0;
19775 +       /* expected parent dir is locked */
19776 +       if (unlikely(h_parent != h_dentry->d_parent))
19777 +               err = -EIO;
19778 +
19779 +out:
19780 +       AuTraceErr(err);
19781 +       return err;
19782 +}
19783 +
19784 +/*
19785 + * initial procedure of adding a new entry.
19786 + * prepare writable branch and the parent dir, lock it,
19787 + * and lookup whiteout for the new entry.
19788 + */
19789 +static struct dentry*
19790 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19791 +                 struct dentry *src_dentry, struct au_pin *pin,
19792 +                 struct au_wr_dir_args *wr_dir_args)
19793 +{
19794 +       struct dentry *wh_dentry, *h_parent;
19795 +       struct super_block *sb;
19796 +       struct au_branch *br;
19797 +       int err;
19798 +       unsigned int udba;
19799 +       aufs_bindex_t bcpup;
19800 +
19801 +       AuDbg("%pd\n", dentry);
19802 +
19803 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19804 +       bcpup = err;
19805 +       wh_dentry = ERR_PTR(err);
19806 +       if (unlikely(err < 0))
19807 +               goto out;
19808 +
19809 +       sb = dentry->d_sb;
19810 +       udba = au_opt_udba(sb);
19811 +       err = au_pin(pin, dentry, bcpup, udba,
19812 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19813 +       wh_dentry = ERR_PTR(err);
19814 +       if (unlikely(err))
19815 +               goto out;
19816 +
19817 +       h_parent = au_pinned_h_parent(pin);
19818 +       if (udba != AuOpt_UDBA_NONE
19819 +           && au_dbtop(dentry) == bcpup)
19820 +               err = au_may_add(dentry, bcpup, h_parent,
19821 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19822 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19823 +               err = -ENAMETOOLONG;
19824 +       wh_dentry = ERR_PTR(err);
19825 +       if (unlikely(err))
19826 +               goto out_unpin;
19827 +
19828 +       br = au_sbr(sb, bcpup);
19829 +       if (dt) {
19830 +               struct path tmp = {
19831 +                       .dentry = h_parent,
19832 +                       .mnt    = au_br_mnt(br)
19833 +               };
19834 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19835 +       }
19836 +
19837 +       wh_dentry = NULL;
19838 +       if (bcpup != au_dbwh(dentry))
19839 +               goto out; /* success */
19840 +
19841 +       /*
19842 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19843 +        * would not be able to removed in the future. So we don't allow such
19844 +        * name here and we don't handle ENAMETOOLONG differently here.
19845 +        */
19846 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19847 +
19848 +out_unpin:
19849 +       if (IS_ERR(wh_dentry))
19850 +               au_unpin(pin);
19851 +out:
19852 +       return wh_dentry;
19853 +}
19854 +
19855 +/* ---------------------------------------------------------------------- */
19856 +
19857 +enum { Mknod, Symlink, Creat };
19858 +struct simple_arg {
19859 +       int type;
19860 +       union {
19861 +               struct {
19862 +                       umode_t                 mode;
19863 +                       bool                    want_excl;
19864 +                       bool                    try_aopen;
19865 +                       struct vfsub_aopen_args *aopen;
19866 +               } c;
19867 +               struct {
19868 +                       const char *symname;
19869 +               } s;
19870 +               struct {
19871 +                       umode_t mode;
19872 +                       dev_t dev;
19873 +               } m;
19874 +       } u;
19875 +};
19876 +
19877 +static int add_simple(struct inode *dir, struct dentry *dentry,
19878 +                     struct simple_arg *arg)
19879 +{
19880 +       int err, rerr;
19881 +       aufs_bindex_t btop;
19882 +       unsigned char created;
19883 +       const unsigned char try_aopen
19884 +               = (arg->type == Creat && arg->u.c.try_aopen);
19885 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19886 +       struct dentry *wh_dentry, *parent;
19887 +       struct inode *h_dir;
19888 +       struct super_block *sb;
19889 +       struct au_branch *br;
19890 +       /* to reduce stack size */
19891 +       struct {
19892 +               struct au_dtime dt;
19893 +               struct au_pin pin;
19894 +               struct path h_path;
19895 +               struct au_wr_dir_args wr_dir_args;
19896 +       } *a;
19897 +
19898 +       AuDbg("%pd\n", dentry);
19899 +       IMustLock(dir);
19900 +
19901 +       err = -ENOMEM;
19902 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19903 +       if (unlikely(!a))
19904 +               goto out;
19905 +       a->wr_dir_args.force_btgt = -1;
19906 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19907 +
19908 +       parent = dentry->d_parent; /* dir inode is locked */
19909 +       if (!try_aopen) {
19910 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19911 +               if (unlikely(err))
19912 +                       goto out_free;
19913 +       }
19914 +       err = au_d_may_add(dentry);
19915 +       if (unlikely(err))
19916 +               goto out_unlock;
19917 +       if (!try_aopen)
19918 +               di_write_lock_parent(parent);
19919 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19920 +                                     &a->pin, &a->wr_dir_args);
19921 +       err = PTR_ERR(wh_dentry);
19922 +       if (IS_ERR(wh_dentry))
19923 +               goto out_parent;
19924 +
19925 +       btop = au_dbtop(dentry);
19926 +       sb = dentry->d_sb;
19927 +       br = au_sbr(sb, btop);
19928 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19929 +       a->h_path.mnt = au_br_mnt(br);
19930 +       h_dir = au_pinned_h_dir(&a->pin);
19931 +       switch (arg->type) {
19932 +       case Creat:
19933 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19934 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19935 +                                          arg->u.c.want_excl);
19936 +                       created = !err;
19937 +                       if (!err && try_aopen)
19938 +                               aopen->file->f_mode |= FMODE_CREATED;
19939 +               } else {
19940 +                       aopen->br = br;
19941 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19942 +                       AuDbg("err %d\n", err);
19943 +                       AuDbgFile(aopen->file);
19944 +                       created = err >= 0
19945 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19946 +               }
19947 +               break;
19948 +       case Symlink:
19949 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19950 +               created = !err;
19951 +               break;
19952 +       case Mknod:
19953 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19954 +                                 arg->u.m.dev);
19955 +               created = !err;
19956 +               break;
19957 +       default:
19958 +               BUG();
19959 +       }
19960 +       if (unlikely(err < 0))
19961 +               goto out_unpin;
19962 +
19963 +       err = epilog(dir, btop, wh_dentry, dentry);
19964 +       if (!err)
19965 +               goto out_unpin; /* success */
19966 +
19967 +       /* revert */
19968 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19969 +               /* no delegation since it is just created */
19970 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19971 +                                   /*force*/0);
19972 +               if (rerr) {
19973 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19974 +                               dentry, err, rerr);
19975 +                       err = -EIO;
19976 +               }
19977 +               au_dtime_revert(&a->dt);
19978 +       }
19979 +       if (try_aopen && h_dir->i_op->atomic_open
19980 +           && (aopen->file->f_mode & FMODE_OPENED))
19981 +               /* aopen->file is still opened */
19982 +               au_lcnt_dec(&aopen->br->br_nfiles);
19983 +
19984 +out_unpin:
19985 +       au_unpin(&a->pin);
19986 +       dput(wh_dentry);
19987 +out_parent:
19988 +       if (!try_aopen)
19989 +               di_write_unlock(parent);
19990 +out_unlock:
19991 +       if (unlikely(err)) {
19992 +               au_update_dbtop(dentry);
19993 +               d_drop(dentry);
19994 +       }
19995 +       if (!try_aopen)
19996 +               aufs_read_unlock(dentry, AuLock_DW);
19997 +out_free:
19998 +       au_kfree_rcu(a);
19999 +out:
20000 +       return err;
20001 +}
20002 +
20003 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20004 +              dev_t dev)
20005 +{
20006 +       struct simple_arg arg = {
20007 +               .type = Mknod,
20008 +               .u.m = {
20009 +                       .mode   = mode,
20010 +                       .dev    = dev
20011 +               }
20012 +       };
20013 +       return add_simple(dir, dentry, &arg);
20014 +}
20015 +
20016 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20017 +{
20018 +       struct simple_arg arg = {
20019 +               .type = Symlink,
20020 +               .u.s.symname = symname
20021 +       };
20022 +       return add_simple(dir, dentry, &arg);
20023 +}
20024 +
20025 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20026 +               bool want_excl)
20027 +{
20028 +       struct simple_arg arg = {
20029 +               .type = Creat,
20030 +               .u.c = {
20031 +                       .mode           = mode,
20032 +                       .want_excl      = want_excl
20033 +               }
20034 +       };
20035 +       return add_simple(dir, dentry, &arg);
20036 +}
20037 +
20038 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20039 +                      struct vfsub_aopen_args *aopen_args)
20040 +{
20041 +       struct simple_arg arg = {
20042 +               .type = Creat,
20043 +               .u.c = {
20044 +                       .mode           = aopen_args->create_mode,
20045 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
20046 +                       .try_aopen      = true,
20047 +                       .aopen          = aopen_args
20048 +               }
20049 +       };
20050 +       return add_simple(dir, dentry, &arg);
20051 +}
20052 +
20053 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20054 +{
20055 +       int err;
20056 +       aufs_bindex_t bindex;
20057 +       struct super_block *sb;
20058 +       struct dentry *parent, *h_parent, *h_dentry;
20059 +       struct inode *h_dir, *inode;
20060 +       struct vfsmount *h_mnt;
20061 +       struct au_wr_dir_args wr_dir_args = {
20062 +               .force_btgt     = -1,
20063 +               .flags          = AuWrDir_TMPFILE
20064 +       };
20065 +
20066 +       /* copy-up may happen */
20067 +       inode_lock(dir);
20068 +
20069 +       sb = dir->i_sb;
20070 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20071 +       if (unlikely(err))
20072 +               goto out;
20073 +
20074 +       err = au_di_init(dentry);
20075 +       if (unlikely(err))
20076 +               goto out_si;
20077 +
20078 +       err = -EBUSY;
20079 +       parent = d_find_any_alias(dir);
20080 +       AuDebugOn(!parent);
20081 +       di_write_lock_parent(parent);
20082 +       if (unlikely(d_inode(parent) != dir))
20083 +               goto out_parent;
20084 +
20085 +       err = au_digen_test(parent, au_sigen(sb));
20086 +       if (unlikely(err))
20087 +               goto out_parent;
20088 +
20089 +       bindex = au_dbtop(parent);
20090 +       au_set_dbtop(dentry, bindex);
20091 +       au_set_dbbot(dentry, bindex);
20092 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20093 +       bindex = err;
20094 +       if (unlikely(err < 0))
20095 +               goto out_parent;
20096 +
20097 +       err = -EOPNOTSUPP;
20098 +       h_dir = au_h_iptr(dir, bindex);
20099 +       if (unlikely(!h_dir->i_op->tmpfile))
20100 +               goto out_parent;
20101 +
20102 +       h_mnt = au_sbr_mnt(sb, bindex);
20103 +       err = vfsub_mnt_want_write(h_mnt);
20104 +       if (unlikely(err))
20105 +               goto out_parent;
20106 +
20107 +       h_parent = au_h_dptr(parent, bindex);
20108 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20109 +       if (IS_ERR(h_dentry)) {
20110 +               err = PTR_ERR(h_dentry);
20111 +               goto out_mnt;
20112 +       }
20113 +
20114 +       au_set_dbtop(dentry, bindex);
20115 +       au_set_dbbot(dentry, bindex);
20116 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20117 +       inode = au_new_inode(dentry, /*must_new*/1);
20118 +       if (IS_ERR(inode)) {
20119 +               err = PTR_ERR(inode);
20120 +               au_set_h_dptr(dentry, bindex, NULL);
20121 +               au_set_dbtop(dentry, -1);
20122 +               au_set_dbbot(dentry, -1);
20123 +       } else {
20124 +               if (!inode->i_nlink)
20125 +                       set_nlink(inode, 1);
20126 +               d_tmpfile(dentry, inode);
20127 +               au_di(dentry)->di_tmpfile = 1;
20128 +
20129 +               /* update without i_mutex */
20130 +               if (au_ibtop(dir) == au_dbtop(dentry))
20131 +                       au_cpup_attr_timesizes(dir);
20132 +       }
20133 +       dput(h_dentry);
20134 +
20135 +out_mnt:
20136 +       vfsub_mnt_drop_write(h_mnt);
20137 +out_parent:
20138 +       di_write_unlock(parent);
20139 +       dput(parent);
20140 +       di_write_unlock(dentry);
20141 +       if (unlikely(err)) {
20142 +               au_di_fin(dentry);
20143 +               dentry->d_fsdata = NULL;
20144 +       }
20145 +out_si:
20146 +       si_read_unlock(sb);
20147 +out:
20148 +       inode_unlock(dir);
20149 +       return err;
20150 +}
20151 +
20152 +/* ---------------------------------------------------------------------- */
20153 +
20154 +struct au_link_args {
20155 +       aufs_bindex_t bdst, bsrc;
20156 +       struct au_pin pin;
20157 +       struct path h_path;
20158 +       struct dentry *src_parent, *parent;
20159 +};
20160 +
20161 +static int au_cpup_before_link(struct dentry *src_dentry,
20162 +                              struct au_link_args *a)
20163 +{
20164 +       int err;
20165 +       struct dentry *h_src_dentry;
20166 +       struct au_cp_generic cpg = {
20167 +               .dentry = src_dentry,
20168 +               .bdst   = a->bdst,
20169 +               .bsrc   = a->bsrc,
20170 +               .len    = -1,
20171 +               .pin    = &a->pin,
20172 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20173 +       };
20174 +
20175 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20176 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20177 +       if (unlikely(err))
20178 +               goto out;
20179 +
20180 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20181 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20182 +                    au_opt_udba(src_dentry->d_sb),
20183 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20184 +       if (unlikely(err))
20185 +               goto out;
20186 +
20187 +       err = au_sio_cpup_simple(&cpg);
20188 +       au_unpin(&a->pin);
20189 +
20190 +out:
20191 +       di_read_unlock(a->src_parent, AuLock_IR);
20192 +       return err;
20193 +}
20194 +
20195 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20196 +                          struct au_link_args *a)
20197 +{
20198 +       int err;
20199 +       unsigned char plink;
20200 +       aufs_bindex_t bbot;
20201 +       struct dentry *h_src_dentry;
20202 +       struct inode *h_inode, *inode, *delegated;
20203 +       struct super_block *sb;
20204 +       struct file *h_file;
20205 +
20206 +       plink = 0;
20207 +       h_inode = NULL;
20208 +       sb = src_dentry->d_sb;
20209 +       inode = d_inode(src_dentry);
20210 +       if (au_ibtop(inode) <= a->bdst)
20211 +               h_inode = au_h_iptr(inode, a->bdst);
20212 +       if (!h_inode || !h_inode->i_nlink) {
20213 +               /* copyup src_dentry as the name of dentry. */
20214 +               bbot = au_dbbot(dentry);
20215 +               if (bbot < a->bsrc)
20216 +                       au_set_dbbot(dentry, a->bsrc);
20217 +               au_set_h_dptr(dentry, a->bsrc,
20218 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20219 +               dget(a->h_path.dentry);
20220 +               au_set_h_dptr(dentry, a->bdst, NULL);
20221 +               AuDbg("temporary d_inode...\n");
20222 +               spin_lock(&dentry->d_lock);
20223 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20224 +               spin_unlock(&dentry->d_lock);
20225 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20226 +               if (IS_ERR(h_file))
20227 +                       err = PTR_ERR(h_file);
20228 +               else {
20229 +                       struct au_cp_generic cpg = {
20230 +                               .dentry = dentry,
20231 +                               .bdst   = a->bdst,
20232 +                               .bsrc   = -1,
20233 +                               .len    = -1,
20234 +                               .pin    = &a->pin,
20235 +                               .flags  = AuCpup_KEEPLINO
20236 +                       };
20237 +                       err = au_sio_cpup_simple(&cpg);
20238 +                       au_h_open_post(dentry, a->bsrc, h_file);
20239 +                       if (!err) {
20240 +                               dput(a->h_path.dentry);
20241 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20242 +                       } else
20243 +                               au_set_h_dptr(dentry, a->bdst,
20244 +                                             a->h_path.dentry);
20245 +               }
20246 +               spin_lock(&dentry->d_lock);
20247 +               dentry->d_inode = NULL; /* restore */
20248 +               spin_unlock(&dentry->d_lock);
20249 +               AuDbg("temporary d_inode...done\n");
20250 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20251 +               au_set_dbbot(dentry, bbot);
20252 +       } else {
20253 +               /* the inode of src_dentry already exists on a.bdst branch */
20254 +               h_src_dentry = d_find_alias(h_inode);
20255 +               if (!h_src_dentry && au_plink_test(inode)) {
20256 +                       plink = 1;
20257 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20258 +                       err = PTR_ERR(h_src_dentry);
20259 +                       if (IS_ERR(h_src_dentry))
20260 +                               goto out;
20261 +
20262 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20263 +                               dput(h_src_dentry);
20264 +                               h_src_dentry = NULL;
20265 +                       }
20266 +
20267 +               }
20268 +               if (h_src_dentry) {
20269 +                       delegated = NULL;
20270 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20271 +                                        &a->h_path, &delegated);
20272 +                       if (unlikely(err == -EWOULDBLOCK)) {
20273 +                               pr_warn("cannot retry for NFSv4 delegation"
20274 +                                       " for an internal link\n");
20275 +                               iput(delegated);
20276 +                       }
20277 +                       dput(h_src_dentry);
20278 +               } else {
20279 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20280 +                               h_inode->i_ino, a->bdst);
20281 +                       err = -EIO;
20282 +               }
20283 +       }
20284 +
20285 +       if (!err && !plink)
20286 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20287 +
20288 +out:
20289 +       AuTraceErr(err);
20290 +       return err;
20291 +}
20292 +
20293 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20294 +             struct dentry *dentry)
20295 +{
20296 +       int err, rerr;
20297 +       struct au_dtime dt;
20298 +       struct au_link_args *a;
20299 +       struct dentry *wh_dentry, *h_src_dentry;
20300 +       struct inode *inode, *delegated;
20301 +       struct super_block *sb;
20302 +       struct au_wr_dir_args wr_dir_args = {
20303 +               /* .force_btgt  = -1, */
20304 +               .flags          = AuWrDir_ADD_ENTRY
20305 +       };
20306 +
20307 +       IMustLock(dir);
20308 +       inode = d_inode(src_dentry);
20309 +       IMustLock(inode);
20310 +
20311 +       err = -ENOMEM;
20312 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20313 +       if (unlikely(!a))
20314 +               goto out;
20315 +
20316 +       a->parent = dentry->d_parent; /* dir inode is locked */
20317 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20318 +                                       AuLock_NOPLM | AuLock_GEN);
20319 +       if (unlikely(err))
20320 +               goto out_kfree;
20321 +       err = au_d_linkable(src_dentry);
20322 +       if (unlikely(err))
20323 +               goto out_unlock;
20324 +       err = au_d_may_add(dentry);
20325 +       if (unlikely(err))
20326 +               goto out_unlock;
20327 +
20328 +       a->src_parent = dget_parent(src_dentry);
20329 +       wr_dir_args.force_btgt = au_ibtop(inode);
20330 +
20331 +       di_write_lock_parent(a->parent);
20332 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20333 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20334 +                                     &wr_dir_args);
20335 +       err = PTR_ERR(wh_dentry);
20336 +       if (IS_ERR(wh_dentry))
20337 +               goto out_parent;
20338 +
20339 +       err = 0;
20340 +       sb = dentry->d_sb;
20341 +       a->bdst = au_dbtop(dentry);
20342 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20343 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20344 +       a->bsrc = au_ibtop(inode);
20345 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20346 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20347 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20348 +       if (!h_src_dentry) {
20349 +               a->bsrc = au_dbtop(src_dentry);
20350 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20351 +               AuDebugOn(!h_src_dentry);
20352 +       } else if (IS_ERR(h_src_dentry)) {
20353 +               err = PTR_ERR(h_src_dentry);
20354 +               goto out_parent;
20355 +       }
20356 +
20357 +       /*
20358 +        * aufs doesn't touch the credential so
20359 +        * security_dentry_create_files_as() is unnecessary.
20360 +        */
20361 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20362 +               if (a->bdst < a->bsrc
20363 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20364 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20365 +               else {
20366 +                       delegated = NULL;
20367 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20368 +                                        &a->h_path, &delegated);
20369 +                       if (unlikely(err == -EWOULDBLOCK)) {
20370 +                               pr_warn("cannot retry for NFSv4 delegation"
20371 +                                       " for an internal link\n");
20372 +                               iput(delegated);
20373 +                       }
20374 +               }
20375 +               dput(h_src_dentry);
20376 +       } else {
20377 +               /*
20378 +                * copyup src_dentry to the branch we process,
20379 +                * and then link(2) to it.
20380 +                */
20381 +               dput(h_src_dentry);
20382 +               if (a->bdst < a->bsrc
20383 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20384 +                       au_unpin(&a->pin);
20385 +                       di_write_unlock(a->parent);
20386 +                       err = au_cpup_before_link(src_dentry, a);
20387 +                       di_write_lock_parent(a->parent);
20388 +                       if (!err)
20389 +                               err = au_pin(&a->pin, dentry, a->bdst,
20390 +                                            au_opt_udba(sb),
20391 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20392 +                       if (unlikely(err))
20393 +                               goto out_wh;
20394 +               }
20395 +               if (!err) {
20396 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20397 +                       err = -ENOENT;
20398 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20399 +                               delegated = NULL;
20400 +                               err = vfsub_link(h_src_dentry,
20401 +                                                au_pinned_h_dir(&a->pin),
20402 +                                                &a->h_path, &delegated);
20403 +                               if (unlikely(err == -EWOULDBLOCK)) {
20404 +                                       pr_warn("cannot retry"
20405 +                                               " for NFSv4 delegation"
20406 +                                               " for an internal link\n");
20407 +                                       iput(delegated);
20408 +                               }
20409 +                       }
20410 +               }
20411 +       }
20412 +       if (unlikely(err))
20413 +               goto out_unpin;
20414 +
20415 +       if (wh_dentry) {
20416 +               a->h_path.dentry = wh_dentry;
20417 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20418 +                                         dentry);
20419 +               if (unlikely(err))
20420 +                       goto out_revert;
20421 +       }
20422 +
20423 +       au_dir_ts(dir, a->bdst);
20424 +       inode_inc_iversion(dir);
20425 +       inc_nlink(inode);
20426 +       inode->i_ctime = dir->i_ctime;
20427 +       d_instantiate(dentry, au_igrab(inode));
20428 +       if (d_unhashed(a->h_path.dentry))
20429 +               /* some filesystem calls d_drop() */
20430 +               d_drop(dentry);
20431 +       /* some filesystems consume an inode even hardlink */
20432 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20433 +       goto out_unpin; /* success */
20434 +
20435 +out_revert:
20436 +       /* no delegation since it is just created */
20437 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20438 +                           /*delegated*/NULL, /*force*/0);
20439 +       if (unlikely(rerr)) {
20440 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20441 +               err = -EIO;
20442 +       }
20443 +       au_dtime_revert(&dt);
20444 +out_unpin:
20445 +       au_unpin(&a->pin);
20446 +out_wh:
20447 +       dput(wh_dentry);
20448 +out_parent:
20449 +       di_write_unlock(a->parent);
20450 +       dput(a->src_parent);
20451 +out_unlock:
20452 +       if (unlikely(err)) {
20453 +               au_update_dbtop(dentry);
20454 +               d_drop(dentry);
20455 +       }
20456 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20457 +out_kfree:
20458 +       au_kfree_rcu(a);
20459 +out:
20460 +       AuTraceErr(err);
20461 +       return err;
20462 +}
20463 +
20464 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20465 +{
20466 +       int err, rerr;
20467 +       aufs_bindex_t bindex;
20468 +       unsigned char diropq;
20469 +       struct path h_path;
20470 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20471 +       struct inode *h_inode;
20472 +       struct super_block *sb;
20473 +       struct {
20474 +               struct au_pin pin;
20475 +               struct au_dtime dt;
20476 +       } *a; /* reduce the stack usage */
20477 +       struct au_wr_dir_args wr_dir_args = {
20478 +               .force_btgt     = -1,
20479 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20480 +       };
20481 +
20482 +       IMustLock(dir);
20483 +
20484 +       err = -ENOMEM;
20485 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20486 +       if (unlikely(!a))
20487 +               goto out;
20488 +
20489 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20490 +       if (unlikely(err))
20491 +               goto out_free;
20492 +       err = au_d_may_add(dentry);
20493 +       if (unlikely(err))
20494 +               goto out_unlock;
20495 +
20496 +       parent = dentry->d_parent; /* dir inode is locked */
20497 +       di_write_lock_parent(parent);
20498 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20499 +                                     &a->pin, &wr_dir_args);
20500 +       err = PTR_ERR(wh_dentry);
20501 +       if (IS_ERR(wh_dentry))
20502 +               goto out_parent;
20503 +
20504 +       sb = dentry->d_sb;
20505 +       bindex = au_dbtop(dentry);
20506 +       h_path.dentry = au_h_dptr(dentry, bindex);
20507 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20508 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20509 +       if (unlikely(err))
20510 +               goto out_unpin;
20511 +
20512 +       /* make the dir opaque */
20513 +       diropq = 0;
20514 +       h_inode = d_inode(h_path.dentry);
20515 +       if (wh_dentry
20516 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20517 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20518 +               opq_dentry = au_diropq_create(dentry, bindex);
20519 +               inode_unlock(h_inode);
20520 +               err = PTR_ERR(opq_dentry);
20521 +               if (IS_ERR(opq_dentry))
20522 +                       goto out_dir;
20523 +               dput(opq_dentry);
20524 +               diropq = 1;
20525 +       }
20526 +
20527 +       err = epilog(dir, bindex, wh_dentry, dentry);
20528 +       if (!err) {
20529 +               inc_nlink(dir);
20530 +               goto out_unpin; /* success */
20531 +       }
20532 +
20533 +       /* revert */
20534 +       if (diropq) {
20535 +               AuLabel(revert opq);
20536 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20537 +               rerr = au_diropq_remove(dentry, bindex);
20538 +               inode_unlock(h_inode);
20539 +               if (rerr) {
20540 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20541 +                               dentry, err, rerr);
20542 +                       err = -EIO;
20543 +               }
20544 +       }
20545 +
20546 +out_dir:
20547 +       AuLabel(revert dir);
20548 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20549 +       if (rerr) {
20550 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20551 +                       dentry, err, rerr);
20552 +               err = -EIO;
20553 +       }
20554 +       au_dtime_revert(&a->dt);
20555 +out_unpin:
20556 +       au_unpin(&a->pin);
20557 +       dput(wh_dentry);
20558 +out_parent:
20559 +       di_write_unlock(parent);
20560 +out_unlock:
20561 +       if (unlikely(err)) {
20562 +               au_update_dbtop(dentry);
20563 +               d_drop(dentry);
20564 +       }
20565 +       aufs_read_unlock(dentry, AuLock_DW);
20566 +out_free:
20567 +       au_kfree_rcu(a);
20568 +out:
20569 +       return err;
20570 +}
20571 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20572 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20573 +++ linux/fs/aufs/i_op.c        2019-09-16 09:38:43.216175640 +0200
20574 @@ -0,0 +1,1505 @@
20575 +// SPDX-License-Identifier: GPL-2.0
20576 +/*
20577 + * Copyright (C) 2005-2019 Junjiro R. Okajima
20578 + *
20579 + * This program, aufs is free software; you can redistribute it and/or modify
20580 + * it under the terms of the GNU General Public License as published by
20581 + * the Free Software Foundation; either version 2 of the License, or
20582 + * (at your option) any later version.
20583 + *
20584 + * This program is distributed in the hope that it will be useful,
20585 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20586 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20587 + * GNU General Public License for more details.
20588 + *
20589 + * You should have received a copy of the GNU General Public License
20590 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20591 + */
20592 +
20593 +/*
20594 + * inode operations (except add/del/rename)
20595 + */
20596 +
20597 +#include <linux/device_cgroup.h>
20598 +#include <linux/fs_stack.h>
20599 +#include <linux/iversion.h>
20600 +#include <linux/namei.h>
20601 +#include <linux/security.h>
20602 +#include "aufs.h"
20603 +
20604 +static int h_permission(struct inode *h_inode, int mask,
20605 +                       struct path *h_path, int brperm)
20606 +{
20607 +       int err;
20608 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20609 +
20610 +       err = -EPERM;
20611 +       if (write_mask && IS_IMMUTABLE(h_inode))
20612 +               goto out;
20613 +
20614 +       err = -EACCES;
20615 +       if (((mask & MAY_EXEC)
20616 +            && S_ISREG(h_inode->i_mode)
20617 +            && (path_noexec(h_path)
20618 +                || !(h_inode->i_mode & 0111))))
20619 +               goto out;
20620 +
20621 +       /*
20622 +        * - skip the lower fs test in the case of write to ro branch.
20623 +        * - nfs dir permission write check is optimized, but a policy for
20624 +        *   link/rename requires a real check.
20625 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20626 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20627 +        */
20628 +       if ((write_mask && !au_br_writable(brperm))
20629 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20630 +               && write_mask && !(mask & MAY_READ))
20631 +           || !h_inode->i_op->permission) {
20632 +               /* AuLabel(generic_permission); */
20633 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20634 +               err = generic_permission(h_inode, mask);
20635 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20636 +                       err = h_inode->i_op->permission(h_inode, mask);
20637 +               AuTraceErr(err);
20638 +       } else {
20639 +               /* AuLabel(h_inode->permission); */
20640 +               err = h_inode->i_op->permission(h_inode, mask);
20641 +               AuTraceErr(err);
20642 +       }
20643 +
20644 +       if (!err)
20645 +               err = devcgroup_inode_permission(h_inode, mask);
20646 +       if (!err)
20647 +               err = security_inode_permission(h_inode, mask);
20648 +
20649 +#if 0
20650 +       if (!err) {
20651 +               /* todo: do we need to call ima_path_check()? */
20652 +               struct path h_path = {
20653 +                       .dentry =
20654 +                       .mnt    = h_mnt
20655 +               };
20656 +               err = ima_path_check(&h_path,
20657 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20658 +                                    IMA_COUNT_LEAVE);
20659 +       }
20660 +#endif
20661 +
20662 +out:
20663 +       return err;
20664 +}
20665 +
20666 +static int aufs_permission(struct inode *inode, int mask)
20667 +{
20668 +       int err;
20669 +       aufs_bindex_t bindex, bbot;
20670 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20671 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20672 +       struct inode *h_inode;
20673 +       struct super_block *sb;
20674 +       struct au_branch *br;
20675 +
20676 +       /* todo: support rcu-walk? */
20677 +       if (mask & MAY_NOT_BLOCK)
20678 +               return -ECHILD;
20679 +
20680 +       sb = inode->i_sb;
20681 +       si_read_lock(sb, AuLock_FLUSH);
20682 +       ii_read_lock_child(inode);
20683 +#if 0
20684 +       err = au_iigen_test(inode, au_sigen(sb));
20685 +       if (unlikely(err))
20686 +               goto out;
20687 +#endif
20688 +
20689 +       if (!isdir
20690 +           || write_mask
20691 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20692 +               err = au_busy_or_stale();
20693 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20694 +               if (unlikely(!h_inode
20695 +                            || (h_inode->i_mode & S_IFMT)
20696 +                            != (inode->i_mode & S_IFMT)))
20697 +                       goto out;
20698 +
20699 +               err = 0;
20700 +               bindex = au_ibtop(inode);
20701 +               br = au_sbr(sb, bindex);
20702 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20703 +               if (write_mask
20704 +                   && !err
20705 +                   && !special_file(h_inode->i_mode)) {
20706 +                       /* test whether the upper writable branch exists */
20707 +                       err = -EROFS;
20708 +                       for (; bindex >= 0; bindex--)
20709 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20710 +                                       err = 0;
20711 +                                       break;
20712 +                               }
20713 +               }
20714 +               goto out;
20715 +       }
20716 +
20717 +       /* non-write to dir */
20718 +       err = 0;
20719 +       bbot = au_ibbot(inode);
20720 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20721 +               h_inode = au_h_iptr(inode, bindex);
20722 +               if (h_inode) {
20723 +                       err = au_busy_or_stale();
20724 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20725 +                               break;
20726 +
20727 +                       br = au_sbr(sb, bindex);
20728 +                       err = h_permission(h_inode, mask, &br->br_path,
20729 +                                          br->br_perm);
20730 +               }
20731 +       }
20732 +
20733 +out:
20734 +       ii_read_unlock(inode);
20735 +       si_read_unlock(sb);
20736 +       return err;
20737 +}
20738 +
20739 +/* ---------------------------------------------------------------------- */
20740 +
20741 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20742 +                                 unsigned int flags)
20743 +{
20744 +       struct dentry *ret, *parent;
20745 +       struct inode *inode;
20746 +       struct super_block *sb;
20747 +       int err, npositive;
20748 +
20749 +       IMustLock(dir);
20750 +
20751 +       /* todo: support rcu-walk? */
20752 +       ret = ERR_PTR(-ECHILD);
20753 +       if (flags & LOOKUP_RCU)
20754 +               goto out;
20755 +
20756 +       ret = ERR_PTR(-ENAMETOOLONG);
20757 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20758 +               goto out;
20759 +
20760 +       sb = dir->i_sb;
20761 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20762 +       ret = ERR_PTR(err);
20763 +       if (unlikely(err))
20764 +               goto out;
20765 +
20766 +       err = au_di_init(dentry);
20767 +       ret = ERR_PTR(err);
20768 +       if (unlikely(err))
20769 +               goto out_si;
20770 +
20771 +       inode = NULL;
20772 +       npositive = 0; /* suppress a warning */
20773 +       parent = dentry->d_parent; /* dir inode is locked */
20774 +       di_read_lock_parent(parent, AuLock_IR);
20775 +       err = au_alive_dir(parent);
20776 +       if (!err)
20777 +               err = au_digen_test(parent, au_sigen(sb));
20778 +       if (!err) {
20779 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20780 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20781 +                                          AuLkup_ALLOW_NEG);
20782 +               err = npositive;
20783 +       }
20784 +       di_read_unlock(parent, AuLock_IR);
20785 +       ret = ERR_PTR(err);
20786 +       if (unlikely(err < 0))
20787 +               goto out_unlock;
20788 +
20789 +       if (npositive) {
20790 +               inode = au_new_inode(dentry, /*must_new*/0);
20791 +               if (IS_ERR(inode)) {
20792 +                       ret = (void *)inode;
20793 +                       inode = NULL;
20794 +                       goto out_unlock;
20795 +               }
20796 +       }
20797 +
20798 +       if (inode)
20799 +               atomic_inc(&inode->i_count);
20800 +       ret = d_splice_alias(inode, dentry);
20801 +#if 0
20802 +       if (unlikely(d_need_lookup(dentry))) {
20803 +               spin_lock(&dentry->d_lock);
20804 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20805 +               spin_unlock(&dentry->d_lock);
20806 +       } else
20807 +#endif
20808 +       if (inode) {
20809 +               if (!IS_ERR(ret)) {
20810 +                       iput(inode);
20811 +                       if (ret && ret != dentry)
20812 +                               ii_write_unlock(inode);
20813 +               } else {
20814 +                       ii_write_unlock(inode);
20815 +                       iput(inode);
20816 +                       inode = NULL;
20817 +               }
20818 +       }
20819 +
20820 +out_unlock:
20821 +       di_write_unlock(dentry);
20822 +out_si:
20823 +       si_read_unlock(sb);
20824 +out:
20825 +       return ret;
20826 +}
20827 +
20828 +/* ---------------------------------------------------------------------- */
20829 +
20830 +/*
20831 + * very dirty and complicated aufs ->atomic_open().
20832 + * aufs_atomic_open()
20833 + * + au_aopen_or_create()
20834 + *   + add_simple()
20835 + *     + vfsub_atomic_open()
20836 + *       + branch fs ->atomic_open()
20837 + *        may call the actual 'open' for h_file
20838 + *       + inc br_nfiles only if opened
20839 + * + au_aopen_no_open() or au_aopen_do_open()
20840 + *
20841 + * au_aopen_do_open()
20842 + * + finish_open()
20843 + *   + au_do_aopen()
20844 + *     + au_do_open() the body of all 'open'
20845 + *       + au_do_open_nondir()
20846 + *        set the passed h_file
20847 + *
20848 + * au_aopen_no_open()
20849 + * + finish_no_open()
20850 + */
20851 +
20852 +struct aopen_node {
20853 +       struct hlist_bl_node hblist;
20854 +       struct file *file, *h_file;
20855 +};
20856 +
20857 +static int au_do_aopen(struct inode *inode, struct file *file)
20858 +{
20859 +       struct hlist_bl_head *aopen;
20860 +       struct hlist_bl_node *pos;
20861 +       struct aopen_node *node;
20862 +       struct au_do_open_args args = {
20863 +               .aopen  = 1,
20864 +               .open   = au_do_open_nondir
20865 +       };
20866 +
20867 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20868 +       hlist_bl_lock(aopen);
20869 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20870 +               if (node->file == file) {
20871 +                       args.h_file = node->h_file;
20872 +                       break;
20873 +               }
20874 +       hlist_bl_unlock(aopen);
20875 +       /* AuDebugOn(!args.h_file); */
20876 +
20877 +       return au_do_open(file, &args);
20878 +}
20879 +
20880 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20881 +                           struct aopen_node *aopen_node)
20882 +{
20883 +       int err;
20884 +       struct hlist_bl_head *aopen;
20885 +
20886 +       AuLabel(here);
20887 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20888 +       au_hbl_add(&aopen_node->hblist, aopen);
20889 +       err = finish_open(file, dentry, au_do_aopen);
20890 +       au_hbl_del(&aopen_node->hblist, aopen);
20891 +       /* AuDbgFile(file); */
20892 +       AuDbg("%pd%s%s\n", dentry,
20893 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20894 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20895 +
20896 +       AuTraceErr(err);
20897 +       return err;
20898 +}
20899 +
20900 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20901 +{
20902 +       int err;
20903 +
20904 +       AuLabel(here);
20905 +       dget(dentry);
20906 +       err = finish_no_open(file, dentry);
20907 +
20908 +       AuTraceErr(err);
20909 +       return err;
20910 +}
20911 +
20912 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20913 +                           struct file *file, unsigned int open_flag,
20914 +                           umode_t create_mode)
20915 +{
20916 +       int err, did_open;
20917 +       unsigned int lkup_flags;
20918 +       aufs_bindex_t bindex;
20919 +       struct super_block *sb;
20920 +       struct dentry *parent, *d;
20921 +       struct vfsub_aopen_args args = {
20922 +               .open_flag      = open_flag,
20923 +               .create_mode    = create_mode
20924 +       };
20925 +       struct aopen_node aopen_node = {
20926 +               .file   = file
20927 +       };
20928 +
20929 +       IMustLock(dir);
20930 +       AuDbg("open_flag 0%o\n", open_flag);
20931 +       AuDbgDentry(dentry);
20932 +
20933 +       err = 0;
20934 +       if (!au_di(dentry)) {
20935 +               lkup_flags = LOOKUP_OPEN;
20936 +               if (open_flag & O_CREAT)
20937 +                       lkup_flags |= LOOKUP_CREATE;
20938 +               d = aufs_lookup(dir, dentry, lkup_flags);
20939 +               if (IS_ERR(d)) {
20940 +                       err = PTR_ERR(d);
20941 +                       AuTraceErr(err);
20942 +                       goto out;
20943 +               } else if (d) {
20944 +                       /*
20945 +                        * obsoleted dentry found.
20946 +                        * another error will be returned later.
20947 +                        */
20948 +                       d_drop(d);
20949 +                       AuDbgDentry(d);
20950 +                       dput(d);
20951 +               }
20952 +               AuDbgDentry(dentry);
20953 +       }
20954 +
20955 +       if (d_is_positive(dentry)
20956 +           || d_unhashed(dentry)
20957 +           || d_unlinked(dentry)
20958 +           || !(open_flag & O_CREAT)) {
20959 +               err = au_aopen_no_open(file, dentry);
20960 +               goto out; /* success */
20961 +       }
20962 +
20963 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20964 +       if (unlikely(err))
20965 +               goto out;
20966 +
20967 +       sb = dentry->d_sb;
20968 +       parent = dentry->d_parent;      /* dir is locked */
20969 +       di_write_lock_parent(parent);
20970 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20971 +       if (unlikely(err < 0))
20972 +               goto out_parent;
20973 +
20974 +       AuDbgDentry(dentry);
20975 +       if (d_is_positive(dentry)) {
20976 +               err = au_aopen_no_open(file, dentry);
20977 +               goto out_parent; /* success */
20978 +       }
20979 +
20980 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20981 +       err = PTR_ERR(args.file);
20982 +       if (IS_ERR(args.file))
20983 +               goto out_parent;
20984 +
20985 +       bindex = au_dbtop(dentry);
20986 +       err = au_aopen_or_create(dir, dentry, &args);
20987 +       AuTraceErr(err);
20988 +       AuDbgFile(args.file);
20989 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20990 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20991 +       if (!did_open) {
20992 +               fput(args.file);
20993 +               args.file = NULL;
20994 +       }
20995 +       di_write_unlock(parent);
20996 +       di_write_unlock(dentry);
20997 +       if (unlikely(err < 0)) {
20998 +               if (args.file)
20999 +                       fput(args.file);
21000 +               goto out_sb;
21001 +       }
21002 +
21003 +       if (!did_open)
21004 +               err = au_aopen_no_open(file, dentry);
21005 +       else {
21006 +               aopen_node.h_file = args.file;
21007 +               err = au_aopen_do_open(file, dentry, &aopen_node);
21008 +       }
21009 +       if (unlikely(err < 0)) {
21010 +               if (args.file)
21011 +                       fput(args.file);
21012 +               if (did_open)
21013 +                       au_lcnt_dec(&args.br->br_nfiles);
21014 +       }
21015 +       goto out_sb; /* success */
21016 +
21017 +out_parent:
21018 +       di_write_unlock(parent);
21019 +       di_write_unlock(dentry);
21020 +out_sb:
21021 +       si_read_unlock(sb);
21022 +out:
21023 +       AuTraceErr(err);
21024 +       AuDbgFile(file);
21025 +       return err;
21026 +}
21027 +
21028 +
21029 +/* ---------------------------------------------------------------------- */
21030 +
21031 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21032 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
21033 +                         aufs_bindex_t btop)
21034 +{
21035 +       int err;
21036 +       struct dentry *h_parent;
21037 +       struct inode *h_dir;
21038 +
21039 +       if (add_entry)
21040 +               IMustLock(d_inode(parent));
21041 +       else
21042 +               di_write_lock_parent(parent);
21043 +
21044 +       err = 0;
21045 +       if (!au_h_dptr(parent, bcpup)) {
21046 +               if (btop > bcpup)
21047 +                       err = au_cpup_dirs(dentry, bcpup);
21048 +               else if (btop < bcpup)
21049 +                       err = au_cpdown_dirs(dentry, bcpup);
21050 +               else
21051 +                       BUG();
21052 +       }
21053 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
21054 +               h_parent = au_h_dptr(parent, bcpup);
21055 +               h_dir = d_inode(h_parent);
21056 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
21057 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
21058 +               /* todo: no unlock here */
21059 +               inode_unlock_shared(h_dir);
21060 +
21061 +               AuDbg("bcpup %d\n", bcpup);
21062 +               if (!err) {
21063 +                       if (d_really_is_negative(dentry))
21064 +                               au_set_h_dptr(dentry, btop, NULL);
21065 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
21066 +               }
21067 +       }
21068 +
21069 +       if (!add_entry)
21070 +               di_write_unlock(parent);
21071 +       if (!err)
21072 +               err = bcpup; /* success */
21073 +
21074 +       AuTraceErr(err);
21075 +       return err;
21076 +}
21077 +
21078 +/*
21079 + * decide the branch and the parent dir where we will create a new entry.
21080 + * returns new bindex or an error.
21081 + * copyup the parent dir if needed.
21082 + */
21083 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21084 +             struct au_wr_dir_args *args)
21085 +{
21086 +       int err;
21087 +       unsigned int flags;
21088 +       aufs_bindex_t bcpup, btop, src_btop;
21089 +       const unsigned char add_entry
21090 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
21091 +               | au_ftest_wrdir(args->flags, TMPFILE);
21092 +       struct super_block *sb;
21093 +       struct dentry *parent;
21094 +       struct au_sbinfo *sbinfo;
21095 +
21096 +       sb = dentry->d_sb;
21097 +       sbinfo = au_sbi(sb);
21098 +       parent = dget_parent(dentry);
21099 +       btop = au_dbtop(dentry);
21100 +       bcpup = btop;
21101 +       if (args->force_btgt < 0) {
21102 +               if (src_dentry) {
21103 +                       src_btop = au_dbtop(src_dentry);
21104 +                       if (src_btop < btop)
21105 +                               bcpup = src_btop;
21106 +               } else if (add_entry) {
21107 +                       flags = 0;
21108 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21109 +                               au_fset_wbr(flags, DIR);
21110 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21111 +                       bcpup = err;
21112 +               }
21113 +
21114 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21115 +                       if (add_entry)
21116 +                               err = AuWbrCopyup(sbinfo, dentry);
21117 +                       else {
21118 +                               if (!IS_ROOT(dentry)) {
21119 +                                       di_read_lock_parent(parent, !AuLock_IR);
21120 +                                       err = AuWbrCopyup(sbinfo, dentry);
21121 +                                       di_read_unlock(parent, !AuLock_IR);
21122 +                               } else
21123 +                                       err = AuWbrCopyup(sbinfo, dentry);
21124 +                       }
21125 +                       bcpup = err;
21126 +                       if (unlikely(err < 0))
21127 +                               goto out;
21128 +               }
21129 +       } else {
21130 +               bcpup = args->force_btgt;
21131 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21132 +       }
21133 +
21134 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21135 +       err = bcpup;
21136 +       if (bcpup == btop)
21137 +               goto out; /* success */
21138 +
21139 +       /* copyup the new parent into the branch we process */
21140 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21141 +       if (err >= 0) {
21142 +               if (d_really_is_negative(dentry)) {
21143 +                       au_set_h_dptr(dentry, btop, NULL);
21144 +                       au_set_dbtop(dentry, bcpup);
21145 +                       au_set_dbbot(dentry, bcpup);
21146 +               }
21147 +               AuDebugOn(add_entry
21148 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21149 +                         && !au_h_dptr(dentry, bcpup));
21150 +       }
21151 +
21152 +out:
21153 +       dput(parent);
21154 +       return err;
21155 +}
21156 +
21157 +/* ---------------------------------------------------------------------- */
21158 +
21159 +void au_pin_hdir_unlock(struct au_pin *p)
21160 +{
21161 +       if (p->hdir)
21162 +               au_hn_inode_unlock(p->hdir);
21163 +}
21164 +
21165 +int au_pin_hdir_lock(struct au_pin *p)
21166 +{
21167 +       int err;
21168 +
21169 +       err = 0;
21170 +       if (!p->hdir)
21171 +               goto out;
21172 +
21173 +       /* even if an error happens later, keep this lock */
21174 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21175 +
21176 +       err = -EBUSY;
21177 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21178 +               goto out;
21179 +
21180 +       err = 0;
21181 +       if (p->h_dentry)
21182 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21183 +                                 p->h_parent, p->br);
21184 +
21185 +out:
21186 +       return err;
21187 +}
21188 +
21189 +int au_pin_hdir_relock(struct au_pin *p)
21190 +{
21191 +       int err, i;
21192 +       struct inode *h_i;
21193 +       struct dentry *h_d[] = {
21194 +               p->h_dentry,
21195 +               p->h_parent
21196 +       };
21197 +
21198 +       err = au_pin_hdir_lock(p);
21199 +       if (unlikely(err))
21200 +               goto out;
21201 +
21202 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21203 +               if (!h_d[i])
21204 +                       continue;
21205 +               if (d_is_positive(h_d[i])) {
21206 +                       h_i = d_inode(h_d[i]);
21207 +                       err = !h_i->i_nlink;
21208 +               }
21209 +       }
21210 +
21211 +out:
21212 +       return err;
21213 +}
21214 +
21215 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21216 +{
21217 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21218 +}
21219 +
21220 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21221 +{
21222 +       if (p->hdir) {
21223 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21224 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21225 +               au_pin_hdir_set_owner(p, current);
21226 +       }
21227 +}
21228 +
21229 +void au_pin_hdir_release(struct au_pin *p)
21230 +{
21231 +       if (p->hdir) {
21232 +               au_pin_hdir_set_owner(p, p->task);
21233 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
21234 +       }
21235 +}
21236 +
21237 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21238 +{
21239 +       if (pin && pin->parent)
21240 +               return au_h_dptr(pin->parent, pin->bindex);
21241 +       return NULL;
21242 +}
21243 +
21244 +void au_unpin(struct au_pin *p)
21245 +{
21246 +       if (p->hdir)
21247 +               au_pin_hdir_unlock(p);
21248 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21249 +               vfsub_mnt_drop_write(p->h_mnt);
21250 +       if (!p->hdir)
21251 +               return;
21252 +
21253 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21254 +               di_read_unlock(p->parent, AuLock_IR);
21255 +       iput(p->hdir->hi_inode);
21256 +       dput(p->parent);
21257 +       p->parent = NULL;
21258 +       p->hdir = NULL;
21259 +       p->h_mnt = NULL;
21260 +       /* do not clear p->task */
21261 +}
21262 +
21263 +int au_do_pin(struct au_pin *p)
21264 +{
21265 +       int err;
21266 +       struct super_block *sb;
21267 +       struct inode *h_dir;
21268 +
21269 +       err = 0;
21270 +       sb = p->dentry->d_sb;
21271 +       p->br = au_sbr(sb, p->bindex);
21272 +       if (IS_ROOT(p->dentry)) {
21273 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21274 +                       p->h_mnt = au_br_mnt(p->br);
21275 +                       err = vfsub_mnt_want_write(p->h_mnt);
21276 +                       if (unlikely(err)) {
21277 +                               au_fclr_pin(p->flags, MNT_WRITE);
21278 +                               goto out_err;
21279 +                       }
21280 +               }
21281 +               goto out;
21282 +       }
21283 +
21284 +       p->h_dentry = NULL;
21285 +       if (p->bindex <= au_dbbot(p->dentry))
21286 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21287 +
21288 +       p->parent = dget_parent(p->dentry);
21289 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21290 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21291 +
21292 +       h_dir = NULL;
21293 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21294 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21295 +       if (p->hdir)
21296 +               h_dir = p->hdir->hi_inode;
21297 +
21298 +       /*
21299 +        * udba case, or
21300 +        * if DI_LOCKED is not set, then p->parent may be different
21301 +        * and h_parent can be NULL.
21302 +        */
21303 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21304 +               err = -EBUSY;
21305 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21306 +                       di_read_unlock(p->parent, AuLock_IR);
21307 +               dput(p->parent);
21308 +               p->parent = NULL;
21309 +               goto out_err;
21310 +       }
21311 +
21312 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21313 +               p->h_mnt = au_br_mnt(p->br);
21314 +               err = vfsub_mnt_want_write(p->h_mnt);
21315 +               if (unlikely(err)) {
21316 +                       au_fclr_pin(p->flags, MNT_WRITE);
21317 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21318 +                               di_read_unlock(p->parent, AuLock_IR);
21319 +                       dput(p->parent);
21320 +                       p->parent = NULL;
21321 +                       goto out_err;
21322 +               }
21323 +       }
21324 +
21325 +       au_igrab(h_dir);
21326 +       err = au_pin_hdir_lock(p);
21327 +       if (!err)
21328 +               goto out; /* success */
21329 +
21330 +       au_unpin(p);
21331 +
21332 +out_err:
21333 +       pr_err("err %d\n", err);
21334 +       err = au_busy_or_stale();
21335 +out:
21336 +       return err;
21337 +}
21338 +
21339 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21340 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21341 +                unsigned int udba, unsigned char flags)
21342 +{
21343 +       p->dentry = dentry;
21344 +       p->udba = udba;
21345 +       p->lsc_di = lsc_di;
21346 +       p->lsc_hi = lsc_hi;
21347 +       p->flags = flags;
21348 +       p->bindex = bindex;
21349 +
21350 +       p->parent = NULL;
21351 +       p->hdir = NULL;
21352 +       p->h_mnt = NULL;
21353 +
21354 +       p->h_dentry = NULL;
21355 +       p->h_parent = NULL;
21356 +       p->br = NULL;
21357 +       p->task = current;
21358 +}
21359 +
21360 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21361 +          unsigned int udba, unsigned char flags)
21362 +{
21363 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21364 +                   udba, flags);
21365 +       return au_do_pin(pin);
21366 +}
21367 +
21368 +/* ---------------------------------------------------------------------- */
21369 +
21370 +/*
21371 + * ->setattr() and ->getattr() are called in various cases.
21372 + * chmod, stat: dentry is revalidated.
21373 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21374 + *               unhashed.
21375 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21376 + */
21377 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21378 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21379 +{
21380 +       int err;
21381 +       struct dentry *parent;
21382 +
21383 +       err = 0;
21384 +       if (au_digen_test(dentry, sigen)) {
21385 +               parent = dget_parent(dentry);
21386 +               di_read_lock_parent(parent, AuLock_IR);
21387 +               err = au_refresh_dentry(dentry, parent);
21388 +               di_read_unlock(parent, AuLock_IR);
21389 +               dput(parent);
21390 +       }
21391 +
21392 +       AuTraceErr(err);
21393 +       return err;
21394 +}
21395 +
21396 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21397 +                    struct au_icpup_args *a)
21398 +{
21399 +       int err;
21400 +       loff_t sz;
21401 +       aufs_bindex_t btop, ibtop;
21402 +       struct dentry *hi_wh, *parent;
21403 +       struct inode *inode;
21404 +       struct au_wr_dir_args wr_dir_args = {
21405 +               .force_btgt     = -1,
21406 +               .flags          = 0
21407 +       };
21408 +
21409 +       if (d_is_dir(dentry))
21410 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21411 +       /* plink or hi_wh() case */
21412 +       btop = au_dbtop(dentry);
21413 +       inode = d_inode(dentry);
21414 +       ibtop = au_ibtop(inode);
21415 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21416 +               wr_dir_args.force_btgt = ibtop;
21417 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21418 +       if (unlikely(err < 0))
21419 +               goto out;
21420 +       a->btgt = err;
21421 +       if (err != btop)
21422 +               au_fset_icpup(a->flags, DID_CPUP);
21423 +
21424 +       err = 0;
21425 +       a->pin_flags = AuPin_MNT_WRITE;
21426 +       parent = NULL;
21427 +       if (!IS_ROOT(dentry)) {
21428 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21429 +               parent = dget_parent(dentry);
21430 +               di_write_lock_parent(parent);
21431 +       }
21432 +
21433 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21434 +       if (unlikely(err))
21435 +               goto out_parent;
21436 +
21437 +       sz = -1;
21438 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21439 +       a->h_inode = d_inode(a->h_path.dentry);
21440 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21441 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21442 +               if (ia->ia_size < i_size_read(a->h_inode))
21443 +                       sz = ia->ia_size;
21444 +               inode_unlock_shared(a->h_inode);
21445 +       }
21446 +
21447 +       hi_wh = NULL;
21448 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21449 +               hi_wh = au_hi_wh(inode, a->btgt);
21450 +               if (!hi_wh) {
21451 +                       struct au_cp_generic cpg = {
21452 +                               .dentry = dentry,
21453 +                               .bdst   = a->btgt,
21454 +                               .bsrc   = -1,
21455 +                               .len    = sz,
21456 +                               .pin    = &a->pin
21457 +                       };
21458 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21459 +                       if (unlikely(err))
21460 +                               goto out_unlock;
21461 +                       hi_wh = au_hi_wh(inode, a->btgt);
21462 +                       /* todo: revalidate hi_wh? */
21463 +               }
21464 +       }
21465 +
21466 +       if (parent) {
21467 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21468 +               di_downgrade_lock(parent, AuLock_IR);
21469 +               dput(parent);
21470 +               parent = NULL;
21471 +       }
21472 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21473 +               goto out; /* success */
21474 +
21475 +       if (!d_unhashed(dentry)) {
21476 +               struct au_cp_generic cpg = {
21477 +                       .dentry = dentry,
21478 +                       .bdst   = a->btgt,
21479 +                       .bsrc   = btop,
21480 +                       .len    = sz,
21481 +                       .pin    = &a->pin,
21482 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21483 +               };
21484 +               err = au_sio_cpup_simple(&cpg);
21485 +               if (!err)
21486 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21487 +       } else if (!hi_wh)
21488 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21489 +       else
21490 +               a->h_path.dentry = hi_wh; /* do not dget here */
21491 +
21492 +out_unlock:
21493 +       a->h_inode = d_inode(a->h_path.dentry);
21494 +       if (!err)
21495 +               goto out; /* success */
21496 +       au_unpin(&a->pin);
21497 +out_parent:
21498 +       if (parent) {
21499 +               di_write_unlock(parent);
21500 +               dput(parent);
21501 +       }
21502 +out:
21503 +       if (!err)
21504 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21505 +       return err;
21506 +}
21507 +
21508 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21509 +{
21510 +       int err;
21511 +       struct inode *inode, *delegated;
21512 +       struct super_block *sb;
21513 +       struct file *file;
21514 +       struct au_icpup_args *a;
21515 +
21516 +       inode = d_inode(dentry);
21517 +       IMustLock(inode);
21518 +
21519 +       err = setattr_prepare(dentry, ia);
21520 +       if (unlikely(err))
21521 +               goto out;
21522 +
21523 +       err = -ENOMEM;
21524 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21525 +       if (unlikely(!a))
21526 +               goto out;
21527 +
21528 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21529 +               ia->ia_valid &= ~ATTR_MODE;
21530 +
21531 +       file = NULL;
21532 +       sb = dentry->d_sb;
21533 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21534 +       if (unlikely(err))
21535 +               goto out_kfree;
21536 +
21537 +       if (ia->ia_valid & ATTR_FILE) {
21538 +               /* currently ftruncate(2) only */
21539 +               AuDebugOn(!d_is_reg(dentry));
21540 +               file = ia->ia_file;
21541 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21542 +                                           /*fi_lsc*/0);
21543 +               if (unlikely(err))
21544 +                       goto out_si;
21545 +               ia->ia_file = au_hf_top(file);
21546 +               a->udba = AuOpt_UDBA_NONE;
21547 +       } else {
21548 +               /* fchmod() doesn't pass ia_file */
21549 +               a->udba = au_opt_udba(sb);
21550 +               di_write_lock_child(dentry);
21551 +               /* no d_unlinked(), to set UDBA_NONE for root */
21552 +               if (d_unhashed(dentry))
21553 +                       a->udba = AuOpt_UDBA_NONE;
21554 +               if (a->udba != AuOpt_UDBA_NONE) {
21555 +                       AuDebugOn(IS_ROOT(dentry));
21556 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21557 +                       if (unlikely(err))
21558 +                               goto out_dentry;
21559 +               }
21560 +       }
21561 +
21562 +       err = au_pin_and_icpup(dentry, ia, a);
21563 +       if (unlikely(err < 0))
21564 +               goto out_dentry;
21565 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21566 +               ia->ia_file = NULL;
21567 +               ia->ia_valid &= ~ATTR_FILE;
21568 +       }
21569 +
21570 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21571 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21572 +           == (ATTR_MODE | ATTR_CTIME)) {
21573 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21574 +               if (unlikely(err))
21575 +                       goto out_unlock;
21576 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21577 +                  && (ia->ia_valid & ATTR_CTIME)) {
21578 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21579 +               if (unlikely(err))
21580 +                       goto out_unlock;
21581 +       }
21582 +
21583 +       if (ia->ia_valid & ATTR_SIZE) {
21584 +               struct file *f;
21585 +
21586 +               if (ia->ia_size < i_size_read(inode))
21587 +                       /* unmap only */
21588 +                       truncate_setsize(inode, ia->ia_size);
21589 +
21590 +               f = NULL;
21591 +               if (ia->ia_valid & ATTR_FILE)
21592 +                       f = ia->ia_file;
21593 +               inode_unlock(a->h_inode);
21594 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21595 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21596 +       } else {
21597 +               delegated = NULL;
21598 +               while (1) {
21599 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21600 +                       if (delegated) {
21601 +                               err = break_deleg_wait(&delegated);
21602 +                               if (!err)
21603 +                                       continue;
21604 +                       }
21605 +                       break;
21606 +               }
21607 +       }
21608 +       /*
21609 +        * regardless aufs 'acl' option setting.
21610 +        * why don't all acl-aware fs call this func from their ->setattr()?
21611 +        */
21612 +       if (!err && (ia->ia_valid & ATTR_MODE))
21613 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21614 +       if (!err)
21615 +               au_cpup_attr_changeable(inode);
21616 +
21617 +out_unlock:
21618 +       inode_unlock(a->h_inode);
21619 +       au_unpin(&a->pin);
21620 +       if (unlikely(err))
21621 +               au_update_dbtop(dentry);
21622 +out_dentry:
21623 +       di_write_unlock(dentry);
21624 +       if (file) {
21625 +               fi_write_unlock(file);
21626 +               ia->ia_file = file;
21627 +               ia->ia_valid |= ATTR_FILE;
21628 +       }
21629 +out_si:
21630 +       si_read_unlock(sb);
21631 +out_kfree:
21632 +       au_kfree_rcu(a);
21633 +out:
21634 +       AuTraceErr(err);
21635 +       return err;
21636 +}
21637 +
21638 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21639 +static int au_h_path_to_set_attr(struct dentry *dentry,
21640 +                                struct au_icpup_args *a, struct path *h_path)
21641 +{
21642 +       int err;
21643 +       struct super_block *sb;
21644 +
21645 +       sb = dentry->d_sb;
21646 +       a->udba = au_opt_udba(sb);
21647 +       /* no d_unlinked(), to set UDBA_NONE for root */
21648 +       if (d_unhashed(dentry))
21649 +               a->udba = AuOpt_UDBA_NONE;
21650 +       if (a->udba != AuOpt_UDBA_NONE) {
21651 +               AuDebugOn(IS_ROOT(dentry));
21652 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21653 +               if (unlikely(err))
21654 +                       goto out;
21655 +       }
21656 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21657 +       if (unlikely(err < 0))
21658 +               goto out;
21659 +
21660 +       h_path->dentry = a->h_path.dentry;
21661 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21662 +
21663 +out:
21664 +       return err;
21665 +}
21666 +
21667 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21668 +                 struct au_sxattr *arg)
21669 +{
21670 +       int err;
21671 +       struct path h_path;
21672 +       struct super_block *sb;
21673 +       struct au_icpup_args *a;
21674 +       struct inode *h_inode;
21675 +
21676 +       IMustLock(inode);
21677 +
21678 +       err = -ENOMEM;
21679 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21680 +       if (unlikely(!a))
21681 +               goto out;
21682 +
21683 +       sb = dentry->d_sb;
21684 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21685 +       if (unlikely(err))
21686 +               goto out_kfree;
21687 +
21688 +       h_path.dentry = NULL;   /* silence gcc */
21689 +       di_write_lock_child(dentry);
21690 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21691 +       if (unlikely(err))
21692 +               goto out_di;
21693 +
21694 +       inode_unlock(a->h_inode);
21695 +       switch (arg->type) {
21696 +       case AU_XATTR_SET:
21697 +               AuDebugOn(d_is_negative(h_path.dentry));
21698 +               err = vfsub_setxattr(h_path.dentry,
21699 +                                    arg->u.set.name, arg->u.set.value,
21700 +                                    arg->u.set.size, arg->u.set.flags);
21701 +               break;
21702 +       case AU_ACL_SET:
21703 +               err = -EOPNOTSUPP;
21704 +               h_inode = d_inode(h_path.dentry);
21705 +               if (h_inode->i_op->set_acl)
21706 +                       /* this will call posix_acl_update_mode */
21707 +                       err = h_inode->i_op->set_acl(h_inode,
21708 +                                                    arg->u.acl_set.acl,
21709 +                                                    arg->u.acl_set.type);
21710 +               break;
21711 +       }
21712 +       if (!err)
21713 +               au_cpup_attr_timesizes(inode);
21714 +
21715 +       au_unpin(&a->pin);
21716 +       if (unlikely(err))
21717 +               au_update_dbtop(dentry);
21718 +
21719 +out_di:
21720 +       di_write_unlock(dentry);
21721 +       si_read_unlock(sb);
21722 +out_kfree:
21723 +       au_kfree_rcu(a);
21724 +out:
21725 +       AuTraceErr(err);
21726 +       return err;
21727 +}
21728 +#endif
21729 +
21730 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21731 +                            unsigned int nlink)
21732 +{
21733 +       unsigned int n;
21734 +
21735 +       inode->i_mode = st->mode;
21736 +       /* don't i_[ug]id_write() here */
21737 +       inode->i_uid = st->uid;
21738 +       inode->i_gid = st->gid;
21739 +       inode->i_atime = st->atime;
21740 +       inode->i_mtime = st->mtime;
21741 +       inode->i_ctime = st->ctime;
21742 +
21743 +       au_cpup_attr_nlink(inode, /*force*/0);
21744 +       if (S_ISDIR(inode->i_mode)) {
21745 +               n = inode->i_nlink;
21746 +               n -= nlink;
21747 +               n += st->nlink;
21748 +               smp_mb(); /* for i_nlink */
21749 +               /* 0 can happen */
21750 +               set_nlink(inode, n);
21751 +       }
21752 +
21753 +       spin_lock(&inode->i_lock);
21754 +       inode->i_blocks = st->blocks;
21755 +       i_size_write(inode, st->size);
21756 +       spin_unlock(&inode->i_lock);
21757 +}
21758 +
21759 +/*
21760 + * common routine for aufs_getattr() and au_getxattr().
21761 + * returns zero or negative (an error).
21762 + * @dentry will be read-locked in success.
21763 + */
21764 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21765 +                     int locked)
21766 +{
21767 +       int err;
21768 +       unsigned int mnt_flags, sigen;
21769 +       unsigned char udba_none;
21770 +       aufs_bindex_t bindex;
21771 +       struct super_block *sb, *h_sb;
21772 +       struct inode *inode;
21773 +
21774 +       h_path->mnt = NULL;
21775 +       h_path->dentry = NULL;
21776 +
21777 +       err = 0;
21778 +       sb = dentry->d_sb;
21779 +       mnt_flags = au_mntflags(sb);
21780 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21781 +
21782 +       if (unlikely(locked))
21783 +               goto body; /* skip locking dinfo */
21784 +
21785 +       /* support fstat(2) */
21786 +       if (!d_unlinked(dentry) && !udba_none) {
21787 +               sigen = au_sigen(sb);
21788 +               err = au_digen_test(dentry, sigen);
21789 +               if (!err) {
21790 +                       di_read_lock_child(dentry, AuLock_IR);
21791 +                       err = au_dbrange_test(dentry);
21792 +                       if (unlikely(err)) {
21793 +                               di_read_unlock(dentry, AuLock_IR);
21794 +                               goto out;
21795 +                       }
21796 +               } else {
21797 +                       AuDebugOn(IS_ROOT(dentry));
21798 +                       di_write_lock_child(dentry);
21799 +                       err = au_dbrange_test(dentry);
21800 +                       if (!err)
21801 +                               err = au_reval_for_attr(dentry, sigen);
21802 +                       if (!err)
21803 +                               di_downgrade_lock(dentry, AuLock_IR);
21804 +                       else {
21805 +                               di_write_unlock(dentry);
21806 +                               goto out;
21807 +                       }
21808 +               }
21809 +       } else
21810 +               di_read_lock_child(dentry, AuLock_IR);
21811 +
21812 +body:
21813 +       inode = d_inode(dentry);
21814 +       bindex = au_ibtop(inode);
21815 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21816 +       h_sb = h_path->mnt->mnt_sb;
21817 +       if (!force
21818 +           && !au_test_fs_bad_iattr(h_sb)
21819 +           && udba_none)
21820 +               goto out; /* success */
21821 +
21822 +       if (au_dbtop(dentry) == bindex)
21823 +               h_path->dentry = au_h_dptr(dentry, bindex);
21824 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21825 +               h_path->dentry = au_plink_lkup(inode, bindex);
21826 +               if (IS_ERR(h_path->dentry))
21827 +                       /* pretending success */
21828 +                       h_path->dentry = NULL;
21829 +               else
21830 +                       dput(h_path->dentry);
21831 +       }
21832 +
21833 +out:
21834 +       return err;
21835 +}
21836 +
21837 +static int aufs_getattr(const struct path *path, struct kstat *st,
21838 +                       u32 request, unsigned int query)
21839 +{
21840 +       int err;
21841 +       unsigned char positive;
21842 +       struct path h_path;
21843 +       struct dentry *dentry;
21844 +       struct inode *inode;
21845 +       struct super_block *sb;
21846 +
21847 +       dentry = path->dentry;
21848 +       inode = d_inode(dentry);
21849 +       sb = dentry->d_sb;
21850 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21851 +       if (unlikely(err))
21852 +               goto out;
21853 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
21854 +       if (unlikely(err))
21855 +               goto out_si;
21856 +       if (unlikely(!h_path.dentry))
21857 +               /* illegally overlapped or something */
21858 +               goto out_fill; /* pretending success */
21859 +
21860 +       positive = d_is_positive(h_path.dentry);
21861 +       if (positive)
21862 +               /* no vfsub version */
21863 +               err = vfs_getattr(&h_path, st, request, query);
21864 +       if (!err) {
21865 +               if (positive)
21866 +                       au_refresh_iattr(inode, st,
21867 +                                        d_inode(h_path.dentry)->i_nlink);
21868 +               goto out_fill; /* success */
21869 +       }
21870 +       AuTraceErr(err);
21871 +       goto out_di;
21872 +
21873 +out_fill:
21874 +       generic_fillattr(inode, st);
21875 +out_di:
21876 +       di_read_unlock(dentry, AuLock_IR);
21877 +out_si:
21878 +       si_read_unlock(sb);
21879 +out:
21880 +       AuTraceErr(err);
21881 +       return err;
21882 +}
21883 +
21884 +/* ---------------------------------------------------------------------- */
21885 +
21886 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21887 +                                struct delayed_call *done)
21888 +{
21889 +       const char *ret;
21890 +       struct dentry *h_dentry;
21891 +       struct inode *h_inode;
21892 +       int err;
21893 +       aufs_bindex_t bindex;
21894 +
21895 +       ret = NULL; /* suppress a warning */
21896 +       err = -ECHILD;
21897 +       if (!dentry)
21898 +               goto out;
21899 +
21900 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21901 +       if (unlikely(err))
21902 +               goto out;
21903 +
21904 +       err = au_d_hashed_positive(dentry);
21905 +       if (unlikely(err))
21906 +               goto out_unlock;
21907 +
21908 +       err = -EINVAL;
21909 +       inode = d_inode(dentry);
21910 +       bindex = au_ibtop(inode);
21911 +       h_inode = au_h_iptr(inode, bindex);
21912 +       if (unlikely(!h_inode->i_op->get_link))
21913 +               goto out_unlock;
21914 +
21915 +       err = -EBUSY;
21916 +       h_dentry = NULL;
21917 +       if (au_dbtop(dentry) <= bindex) {
21918 +               h_dentry = au_h_dptr(dentry, bindex);
21919 +               if (h_dentry)
21920 +                       dget(h_dentry);
21921 +       }
21922 +       if (!h_dentry) {
21923 +               h_dentry = d_find_any_alias(h_inode);
21924 +               if (IS_ERR(h_dentry)) {
21925 +                       err = PTR_ERR(h_dentry);
21926 +                       goto out_unlock;
21927 +               }
21928 +       }
21929 +       if (unlikely(!h_dentry))
21930 +               goto out_unlock;
21931 +
21932 +       err = 0;
21933 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21934 +       AuDbgDentry(h_dentry);
21935 +       ret = vfs_get_link(h_dentry, done);
21936 +       dput(h_dentry);
21937 +       if (IS_ERR(ret))
21938 +               err = PTR_ERR(ret);
21939 +
21940 +out_unlock:
21941 +       aufs_read_unlock(dentry, AuLock_IR);
21942 +out:
21943 +       if (unlikely(err))
21944 +               ret = ERR_PTR(err);
21945 +       AuTraceErrPtr(ret);
21946 +       return ret;
21947 +}
21948 +
21949 +/* ---------------------------------------------------------------------- */
21950 +
21951 +static int au_is_special(struct inode *inode)
21952 +{
21953 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21954 +}
21955 +
21956 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21957 +                           int flags)
21958 +{
21959 +       int err;
21960 +       aufs_bindex_t bindex;
21961 +       struct super_block *sb;
21962 +       struct inode *h_inode;
21963 +       struct vfsmount *h_mnt;
21964 +
21965 +       sb = inode->i_sb;
21966 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21967 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21968 +
21969 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21970 +       lockdep_off();
21971 +       si_read_lock(sb, AuLock_FLUSH);
21972 +       ii_write_lock_child(inode);
21973 +
21974 +       err = 0;
21975 +       bindex = au_ibtop(inode);
21976 +       h_inode = au_h_iptr(inode, bindex);
21977 +       if (!au_test_ro(sb, bindex, inode)) {
21978 +               h_mnt = au_sbr_mnt(sb, bindex);
21979 +               err = vfsub_mnt_want_write(h_mnt);
21980 +               if (!err) {
21981 +                       err = vfsub_update_time(h_inode, ts, flags);
21982 +                       vfsub_mnt_drop_write(h_mnt);
21983 +               }
21984 +       } else if (au_is_special(h_inode)) {
21985 +               /*
21986 +                * Never copy-up here.
21987 +                * These special files may already be opened and used for
21988 +                * communicating. If we copied it up, then the communication
21989 +                * would be corrupted.
21990 +                */
21991 +               AuWarn1("timestamps for i%lu are ignored "
21992 +                       "since it is on readonly branch (hi%lu).\n",
21993 +                       inode->i_ino, h_inode->i_ino);
21994 +       } else if (flags & ~S_ATIME) {
21995 +               err = -EIO;
21996 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21997 +               AuDebugOn(1);
21998 +       }
21999 +
22000 +       if (!err)
22001 +               au_cpup_attr_timesizes(inode);
22002 +       ii_write_unlock(inode);
22003 +       si_read_unlock(sb);
22004 +       lockdep_on();
22005 +
22006 +       if (!err && (flags & S_VERSION))
22007 +               inode_inc_iversion(inode);
22008 +
22009 +       return err;
22010 +}
22011 +
22012 +/* ---------------------------------------------------------------------- */
22013 +
22014 +/* no getattr version will be set by module.c:aufs_init() */
22015 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22016 +       aufs_iop[] = {
22017 +       [AuIop_SYMLINK] = {
22018 +               .permission     = aufs_permission,
22019 +#ifdef CONFIG_FS_POSIX_ACL
22020 +               .get_acl        = aufs_get_acl,
22021 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
22022 +#endif
22023 +
22024 +               .setattr        = aufs_setattr,
22025 +               .getattr        = aufs_getattr,
22026 +
22027 +#ifdef CONFIG_AUFS_XATTR
22028 +               .listxattr      = aufs_listxattr,
22029 +#endif
22030 +
22031 +               .get_link       = aufs_get_link,
22032 +
22033 +               /* .update_time = aufs_update_time */
22034 +       },
22035 +       [AuIop_DIR] = {
22036 +               .create         = aufs_create,
22037 +               .lookup         = aufs_lookup,
22038 +               .link           = aufs_link,
22039 +               .unlink         = aufs_unlink,
22040 +               .symlink        = aufs_symlink,
22041 +               .mkdir          = aufs_mkdir,
22042 +               .rmdir          = aufs_rmdir,
22043 +               .mknod          = aufs_mknod,
22044 +               .rename         = aufs_rename,
22045 +
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 +               .atomic_open    = aufs_atomic_open,
22061 +               .tmpfile        = aufs_tmpfile
22062 +       },
22063 +       [AuIop_OTHER] = {
22064 +               .permission     = aufs_permission,
22065 +#ifdef CONFIG_FS_POSIX_ACL
22066 +               .get_acl        = aufs_get_acl,
22067 +               .set_acl        = aufs_set_acl,
22068 +#endif
22069 +
22070 +               .setattr        = aufs_setattr,
22071 +               .getattr        = aufs_getattr,
22072 +
22073 +#ifdef CONFIG_AUFS_XATTR
22074 +               .listxattr      = aufs_listxattr,
22075 +#endif
22076 +
22077 +               .update_time    = aufs_update_time
22078 +       }
22079 +};
22080 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22081 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22082 +++ linux/fs/aufs/i_op_del.c    2019-07-11 15:42:14.468904634 +0200
22083 @@ -0,0 +1,513 @@
22084 +// SPDX-License-Identifier: GPL-2.0
22085 +/*
22086 + * Copyright (C) 2005-2019 Junjiro R. Okajima
22087 + *
22088 + * This program, aufs is free software; you can redistribute it and/or modify
22089 + * it under the terms of the GNU General Public License as published by
22090 + * the Free Software Foundation; either version 2 of the License, or
22091 + * (at your option) any later version.
22092 + *
22093 + * This program is distributed in the hope that it will be useful,
22094 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22095 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22096 + * GNU General Public License for more details.
22097 + *
22098 + * You should have received a copy of the GNU General Public License
22099 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22100 + */
22101 +
22102 +/*
22103 + * inode operations (del entry)
22104 + */
22105 +
22106 +#include <linux/iversion.h>
22107 +#include "aufs.h"
22108 +
22109 +/*
22110 + * decide if a new whiteout for @dentry is necessary or not.
22111 + * when it is necessary, prepare the parent dir for the upper branch whose
22112 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22113 + * be done by caller.
22114 + * return value:
22115 + * 0: wh is unnecessary
22116 + * plus: wh is necessary
22117 + * minus: error
22118 + */
22119 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22120 +{
22121 +       int need_wh, err;
22122 +       aufs_bindex_t btop;
22123 +       struct super_block *sb;
22124 +
22125 +       sb = dentry->d_sb;
22126 +       btop = au_dbtop(dentry);
22127 +       if (*bcpup < 0) {
22128 +               *bcpup = btop;
22129 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22130 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22131 +                       *bcpup = err;
22132 +                       if (unlikely(err < 0))
22133 +                               goto out;
22134 +               }
22135 +       } else
22136 +               AuDebugOn(btop < *bcpup
22137 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22138 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22139 +
22140 +       if (*bcpup != btop) {
22141 +               err = au_cpup_dirs(dentry, *bcpup);
22142 +               if (unlikely(err))
22143 +                       goto out;
22144 +               need_wh = 1;
22145 +       } else {
22146 +               struct au_dinfo *dinfo, *tmp;
22147 +
22148 +               need_wh = -ENOMEM;
22149 +               dinfo = au_di(dentry);
22150 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22151 +               if (tmp) {
22152 +                       au_di_cp(tmp, dinfo);
22153 +                       au_di_swap(tmp, dinfo);
22154 +                       /* returns the number of positive dentries */
22155 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22156 +                                                /* AuLkup_IGNORE_PERM */ 0);
22157 +                       au_di_swap(tmp, dinfo);
22158 +                       au_rw_write_unlock(&tmp->di_rwsem);
22159 +                       au_di_free(tmp);
22160 +               }
22161 +       }
22162 +       AuDbg("need_wh %d\n", need_wh);
22163 +       err = need_wh;
22164 +
22165 +out:
22166 +       return err;
22167 +}
22168 +
22169 +/*
22170 + * simple tests for the del-entry operations.
22171 + * following the checks in vfs, plus the parent-child relationship.
22172 + */
22173 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22174 +              struct dentry *h_parent, int isdir)
22175 +{
22176 +       int err;
22177 +       umode_t h_mode;
22178 +       struct dentry *h_dentry, *h_latest;
22179 +       struct inode *h_inode;
22180 +
22181 +       h_dentry = au_h_dptr(dentry, bindex);
22182 +       if (d_really_is_positive(dentry)) {
22183 +               err = -ENOENT;
22184 +               if (unlikely(d_is_negative(h_dentry)))
22185 +                       goto out;
22186 +               h_inode = d_inode(h_dentry);
22187 +               if (unlikely(!h_inode->i_nlink))
22188 +                       goto out;
22189 +
22190 +               h_mode = h_inode->i_mode;
22191 +               if (!isdir) {
22192 +                       err = -EISDIR;
22193 +                       if (unlikely(S_ISDIR(h_mode)))
22194 +                               goto out;
22195 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22196 +                       err = -ENOTDIR;
22197 +                       goto out;
22198 +               }
22199 +       } else {
22200 +               /* rename(2) case */
22201 +               err = -EIO;
22202 +               if (unlikely(d_is_positive(h_dentry)))
22203 +                       goto out;
22204 +       }
22205 +
22206 +       err = -ENOENT;
22207 +       /* expected parent dir is locked */
22208 +       if (unlikely(h_parent != h_dentry->d_parent))
22209 +               goto out;
22210 +       err = 0;
22211 +
22212 +       /*
22213 +        * rmdir a dir may break the consistency on some filesystem.
22214 +        * let's try heavy test.
22215 +        */
22216 +       err = -EACCES;
22217 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22218 +                    && au_test_h_perm(d_inode(h_parent),
22219 +                                      MAY_EXEC | MAY_WRITE)))
22220 +               goto out;
22221 +
22222 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22223 +       err = -EIO;
22224 +       if (IS_ERR(h_latest))
22225 +               goto out;
22226 +       if (h_latest == h_dentry)
22227 +               err = 0;
22228 +       dput(h_latest);
22229 +
22230 +out:
22231 +       return err;
22232 +}
22233 +
22234 +/*
22235 + * decide the branch where we operate for @dentry. the branch index will be set
22236 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22237 + * dir for reverting.
22238 + * when a new whiteout is necessary, create it.
22239 + */
22240 +static struct dentry*
22241 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22242 +                   struct au_dtime *dt, struct au_pin *pin)
22243 +{
22244 +       struct dentry *wh_dentry;
22245 +       struct super_block *sb;
22246 +       struct path h_path;
22247 +       int err, need_wh;
22248 +       unsigned int udba;
22249 +       aufs_bindex_t bcpup;
22250 +
22251 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22252 +       wh_dentry = ERR_PTR(need_wh);
22253 +       if (unlikely(need_wh < 0))
22254 +               goto out;
22255 +
22256 +       sb = dentry->d_sb;
22257 +       udba = au_opt_udba(sb);
22258 +       bcpup = *rbcpup;
22259 +       err = au_pin(pin, dentry, bcpup, udba,
22260 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22261 +       wh_dentry = ERR_PTR(err);
22262 +       if (unlikely(err))
22263 +               goto out;
22264 +
22265 +       h_path.dentry = au_pinned_h_parent(pin);
22266 +       if (udba != AuOpt_UDBA_NONE
22267 +           && au_dbtop(dentry) == bcpup) {
22268 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22269 +               wh_dentry = ERR_PTR(err);
22270 +               if (unlikely(err))
22271 +                       goto out_unpin;
22272 +       }
22273 +
22274 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22275 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22276 +       wh_dentry = NULL;
22277 +       if (!need_wh)
22278 +               goto out; /* success, no need to create whiteout */
22279 +
22280 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22281 +       if (IS_ERR(wh_dentry))
22282 +               goto out_unpin;
22283 +
22284 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22285 +       goto out; /* success */
22286 +
22287 +out_unpin:
22288 +       au_unpin(pin);
22289 +out:
22290 +       return wh_dentry;
22291 +}
22292 +
22293 +/*
22294 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22295 + * in order to be revertible and save time for removing many child whiteouts
22296 + * under the dir.
22297 + * returns 1 when there are too many child whiteout and caller should remove
22298 + * them asynchronously. returns 0 when the number of children is enough small to
22299 + * remove now or the branch fs is a remote fs.
22300 + * otherwise return an error.
22301 + */
22302 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22303 +                          struct au_nhash *whlist, struct inode *dir)
22304 +{
22305 +       int rmdir_later, err, dirwh;
22306 +       struct dentry *h_dentry;
22307 +       struct super_block *sb;
22308 +       struct inode *inode;
22309 +
22310 +       sb = dentry->d_sb;
22311 +       SiMustAnyLock(sb);
22312 +       h_dentry = au_h_dptr(dentry, bindex);
22313 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22314 +       if (unlikely(err))
22315 +               goto out;
22316 +
22317 +       /* stop monitoring */
22318 +       inode = d_inode(dentry);
22319 +       au_hn_free(au_hi(inode, bindex));
22320 +
22321 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22322 +               dirwh = au_sbi(sb)->si_dirwh;
22323 +               rmdir_later = (dirwh <= 1);
22324 +               if (!rmdir_later)
22325 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22326 +                                                             dirwh);
22327 +               if (rmdir_later)
22328 +                       return rmdir_later;
22329 +       }
22330 +
22331 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22332 +       if (unlikely(err)) {
22333 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22334 +                       h_dentry, bindex, err);
22335 +               err = 0;
22336 +       }
22337 +
22338 +out:
22339 +       AuTraceErr(err);
22340 +       return err;
22341 +}
22342 +
22343 +/*
22344 + * final procedure for deleting a entry.
22345 + * maintain dentry and iattr.
22346 + */
22347 +static void epilog(struct inode *dir, struct dentry *dentry,
22348 +                  aufs_bindex_t bindex)
22349 +{
22350 +       struct inode *inode;
22351 +
22352 +       inode = d_inode(dentry);
22353 +       d_drop(dentry);
22354 +       inode->i_ctime = dir->i_ctime;
22355 +
22356 +       au_dir_ts(dir, bindex);
22357 +       inode_inc_iversion(dir);
22358 +}
22359 +
22360 +/*
22361 + * when an error happened, remove the created whiteout and revert everything.
22362 + */
22363 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22364 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22365 +                    struct dentry *dentry, struct au_dtime *dt)
22366 +{
22367 +       int rerr;
22368 +       struct path h_path = {
22369 +               .dentry = wh_dentry,
22370 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22371 +       };
22372 +
22373 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22374 +       if (!rerr) {
22375 +               au_set_dbwh(dentry, bwh);
22376 +               au_dtime_revert(dt);
22377 +               return 0;
22378 +       }
22379 +
22380 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22381 +       return -EIO;
22382 +}
22383 +
22384 +/* ---------------------------------------------------------------------- */
22385 +
22386 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22387 +{
22388 +       int err;
22389 +       aufs_bindex_t bwh, bindex, btop;
22390 +       struct inode *inode, *h_dir, *delegated;
22391 +       struct dentry *parent, *wh_dentry;
22392 +       /* to reduce stack size */
22393 +       struct {
22394 +               struct au_dtime dt;
22395 +               struct au_pin pin;
22396 +               struct path h_path;
22397 +       } *a;
22398 +
22399 +       IMustLock(dir);
22400 +
22401 +       err = -ENOMEM;
22402 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22403 +       if (unlikely(!a))
22404 +               goto out;
22405 +
22406 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22407 +       if (unlikely(err))
22408 +               goto out_free;
22409 +       err = au_d_hashed_positive(dentry);
22410 +       if (unlikely(err))
22411 +               goto out_unlock;
22412 +       inode = d_inode(dentry);
22413 +       IMustLock(inode);
22414 +       err = -EISDIR;
22415 +       if (unlikely(d_is_dir(dentry)))
22416 +               goto out_unlock; /* possible? */
22417 +
22418 +       btop = au_dbtop(dentry);
22419 +       bwh = au_dbwh(dentry);
22420 +       bindex = -1;
22421 +       parent = dentry->d_parent; /* dir inode is locked */
22422 +       di_write_lock_parent(parent);
22423 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22424 +                                       &a->pin);
22425 +       err = PTR_ERR(wh_dentry);
22426 +       if (IS_ERR(wh_dentry))
22427 +               goto out_parent;
22428 +
22429 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22430 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22431 +       dget(a->h_path.dentry);
22432 +       if (bindex == btop) {
22433 +               h_dir = au_pinned_h_dir(&a->pin);
22434 +               delegated = NULL;
22435 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22436 +               if (unlikely(err == -EWOULDBLOCK)) {
22437 +                       pr_warn("cannot retry for NFSv4 delegation"
22438 +                               " for an internal unlink\n");
22439 +                       iput(delegated);
22440 +               }
22441 +       } else {
22442 +               /* dir inode is locked */
22443 +               h_dir = d_inode(wh_dentry->d_parent);
22444 +               IMustLock(h_dir);
22445 +               err = 0;
22446 +       }
22447 +
22448 +       if (!err) {
22449 +               vfsub_drop_nlink(inode);
22450 +               epilog(dir, dentry, bindex);
22451 +
22452 +               /* update target timestamps */
22453 +               if (bindex == btop) {
22454 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22455 +                       /*ignore*/
22456 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22457 +               } else
22458 +                       /* todo: this timestamp may be reverted later */
22459 +                       inode->i_ctime = h_dir->i_ctime;
22460 +               goto out_unpin; /* success */
22461 +       }
22462 +
22463 +       /* revert */
22464 +       if (wh_dentry) {
22465 +               int rerr;
22466 +
22467 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22468 +                                &a->dt);
22469 +               if (rerr)
22470 +                       err = rerr;
22471 +       }
22472 +
22473 +out_unpin:
22474 +       au_unpin(&a->pin);
22475 +       dput(wh_dentry);
22476 +       dput(a->h_path.dentry);
22477 +out_parent:
22478 +       di_write_unlock(parent);
22479 +out_unlock:
22480 +       aufs_read_unlock(dentry, AuLock_DW);
22481 +out_free:
22482 +       au_kfree_rcu(a);
22483 +out:
22484 +       return err;
22485 +}
22486 +
22487 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22488 +{
22489 +       int err, rmdir_later;
22490 +       aufs_bindex_t bwh, bindex, btop;
22491 +       struct inode *inode;
22492 +       struct dentry *parent, *wh_dentry, *h_dentry;
22493 +       struct au_whtmp_rmdir *args;
22494 +       /* to reduce stack size */
22495 +       struct {
22496 +               struct au_dtime dt;
22497 +               struct au_pin pin;
22498 +       } *a;
22499 +
22500 +       IMustLock(dir);
22501 +
22502 +       err = -ENOMEM;
22503 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22504 +       if (unlikely(!a))
22505 +               goto out;
22506 +
22507 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22508 +       if (unlikely(err))
22509 +               goto out_free;
22510 +       err = au_alive_dir(dentry);
22511 +       if (unlikely(err))
22512 +               goto out_unlock;
22513 +       inode = d_inode(dentry);
22514 +       IMustLock(inode);
22515 +       err = -ENOTDIR;
22516 +       if (unlikely(!d_is_dir(dentry)))
22517 +               goto out_unlock; /* possible? */
22518 +
22519 +       err = -ENOMEM;
22520 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22521 +       if (unlikely(!args))
22522 +               goto out_unlock;
22523 +
22524 +       parent = dentry->d_parent; /* dir inode is locked */
22525 +       di_write_lock_parent(parent);
22526 +       err = au_test_empty(dentry, &args->whlist);
22527 +       if (unlikely(err))
22528 +               goto out_parent;
22529 +
22530 +       btop = au_dbtop(dentry);
22531 +       bwh = au_dbwh(dentry);
22532 +       bindex = -1;
22533 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22534 +                                       &a->pin);
22535 +       err = PTR_ERR(wh_dentry);
22536 +       if (IS_ERR(wh_dentry))
22537 +               goto out_parent;
22538 +
22539 +       h_dentry = au_h_dptr(dentry, btop);
22540 +       dget(h_dentry);
22541 +       rmdir_later = 0;
22542 +       if (bindex == btop) {
22543 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22544 +               if (err > 0) {
22545 +                       rmdir_later = err;
22546 +                       err = 0;
22547 +               }
22548 +       } else {
22549 +               /* stop monitoring */
22550 +               au_hn_free(au_hi(inode, btop));
22551 +
22552 +               /* dir inode is locked */
22553 +               IMustLock(d_inode(wh_dentry->d_parent));
22554 +               err = 0;
22555 +       }
22556 +
22557 +       if (!err) {
22558 +               vfsub_dead_dir(inode);
22559 +               au_set_dbdiropq(dentry, -1);
22560 +               epilog(dir, dentry, bindex);
22561 +
22562 +               if (rmdir_later) {
22563 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22564 +                       args = NULL;
22565 +               }
22566 +
22567 +               goto out_unpin; /* success */
22568 +       }
22569 +
22570 +       /* revert */
22571 +       AuLabel(revert);
22572 +       if (wh_dentry) {
22573 +               int rerr;
22574 +
22575 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22576 +                                &a->dt);
22577 +               if (rerr)
22578 +                       err = rerr;
22579 +       }
22580 +
22581 +out_unpin:
22582 +       au_unpin(&a->pin);
22583 +       dput(wh_dentry);
22584 +       dput(h_dentry);
22585 +out_parent:
22586 +       di_write_unlock(parent);
22587 +       if (args)
22588 +               au_whtmp_rmdir_free(args);
22589 +out_unlock:
22590 +       aufs_read_unlock(dentry, AuLock_DW);
22591 +out_free:
22592 +       au_kfree_rcu(a);
22593 +out:
22594 +       AuTraceErr(err);
22595 +       return err;
22596 +}
22597 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22598 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22599 +++ linux/fs/aufs/i_op_ren.c    2019-07-11 15:42:14.468904634 +0200
22600 @@ -0,0 +1,1250 @@
22601 +// SPDX-License-Identifier: GPL-2.0
22602 +/*
22603 + * Copyright (C) 2005-2019 Junjiro R. Okajima
22604 + *
22605 + * This program, aufs is free software; you can redistribute it and/or modify
22606 + * it under the terms of the GNU General Public License as published by
22607 + * the Free Software Foundation; either version 2 of the License, or
22608 + * (at your option) any later version.
22609 + *
22610 + * This program is distributed in the hope that it will be useful,
22611 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22612 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22613 + * GNU General Public License for more details.
22614 + *
22615 + * You should have received a copy of the GNU General Public License
22616 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22617 + */
22618 +
22619 +/*
22620 + * inode operation (rename entry)
22621 + * todo: this is crazy monster
22622 + */
22623 +
22624 +#include <linux/iversion.h>
22625 +#include "aufs.h"
22626 +
22627 +enum { AuSRC, AuDST, AuSrcDst };
22628 +enum { AuPARENT, AuCHILD, AuParentChild };
22629 +
22630 +#define AuRen_ISDIR_SRC                1
22631 +#define AuRen_ISDIR_DST                (1 << 1)
22632 +#define AuRen_ISSAMEDIR                (1 << 2)
22633 +#define AuRen_WHSRC            (1 << 3)
22634 +#define AuRen_WHDST            (1 << 4)
22635 +#define AuRen_MNT_WRITE                (1 << 5)
22636 +#define AuRen_DT_DSTDIR                (1 << 6)
22637 +#define AuRen_DIROPQ_SRC       (1 << 7)
22638 +#define AuRen_DIROPQ_DST       (1 << 8)
22639 +#define AuRen_DIRREN           (1 << 9)
22640 +#define AuRen_DROPPED_SRC      (1 << 10)
22641 +#define AuRen_DROPPED_DST      (1 << 11)
22642 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22643 +#define au_fset_ren(flags, name) \
22644 +       do { (flags) |= AuRen_##name; } while (0)
22645 +#define au_fclr_ren(flags, name) \
22646 +       do { (flags) &= ~AuRen_##name; } while (0)
22647 +
22648 +#ifndef CONFIG_AUFS_DIRREN
22649 +#undef AuRen_DIRREN
22650 +#define AuRen_DIRREN           0
22651 +#endif
22652 +
22653 +struct au_ren_args {
22654 +       struct {
22655 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22656 +                       *wh_dentry;
22657 +               struct inode *dir, *inode;
22658 +               struct au_hinode *hdir, *hinode;
22659 +               struct au_dtime dt[AuParentChild];
22660 +               aufs_bindex_t btop, bdiropq;
22661 +       } sd[AuSrcDst];
22662 +
22663 +#define src_dentry     sd[AuSRC].dentry
22664 +#define src_dir                sd[AuSRC].dir
22665 +#define src_inode      sd[AuSRC].inode
22666 +#define src_h_dentry   sd[AuSRC].h_dentry
22667 +#define src_parent     sd[AuSRC].parent
22668 +#define src_h_parent   sd[AuSRC].h_parent
22669 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22670 +#define src_hdir       sd[AuSRC].hdir
22671 +#define src_hinode     sd[AuSRC].hinode
22672 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22673 +#define src_dt         sd[AuSRC].dt
22674 +#define src_btop       sd[AuSRC].btop
22675 +#define src_bdiropq    sd[AuSRC].bdiropq
22676 +
22677 +#define dst_dentry     sd[AuDST].dentry
22678 +#define dst_dir                sd[AuDST].dir
22679 +#define dst_inode      sd[AuDST].inode
22680 +#define dst_h_dentry   sd[AuDST].h_dentry
22681 +#define dst_parent     sd[AuDST].parent
22682 +#define dst_h_parent   sd[AuDST].h_parent
22683 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22684 +#define dst_hdir       sd[AuDST].hdir
22685 +#define dst_hinode     sd[AuDST].hinode
22686 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22687 +#define dst_dt         sd[AuDST].dt
22688 +#define dst_btop       sd[AuDST].btop
22689 +#define dst_bdiropq    sd[AuDST].bdiropq
22690 +
22691 +       struct dentry *h_trap;
22692 +       struct au_branch *br;
22693 +       struct path h_path;
22694 +       struct au_nhash whlist;
22695 +       aufs_bindex_t btgt, src_bwh;
22696 +
22697 +       struct {
22698 +               unsigned short auren_flags;
22699 +               unsigned char flags;    /* syscall parameter */
22700 +               unsigned char exchange;
22701 +       } __packed;
22702 +
22703 +       struct au_whtmp_rmdir *thargs;
22704 +       struct dentry *h_dst;
22705 +       struct au_hinode *h_root;
22706 +};
22707 +
22708 +/* ---------------------------------------------------------------------- */
22709 +
22710 +/*
22711 + * functions for reverting.
22712 + * when an error happened in a single rename systemcall, we should revert
22713 + * everything as if nothing happened.
22714 + * we don't need to revert the copied-up/down the parent dir since they are
22715 + * harmless.
22716 + */
22717 +
22718 +#define RevertFailure(fmt, ...) do { \
22719 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22720 +               ##__VA_ARGS__, err, rerr); \
22721 +       err = -EIO; \
22722 +} while (0)
22723 +
22724 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22725 +{
22726 +       int rerr;
22727 +       struct dentry *d;
22728 +#define src_or_dst(member) a->sd[idx].member
22729 +
22730 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22731 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22732 +       rerr = au_diropq_remove(d, a->btgt);
22733 +       au_hn_inode_unlock(src_or_dst(hinode));
22734 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22735 +       if (rerr)
22736 +               RevertFailure("remove diropq %pd", d);
22737 +
22738 +#undef src_or_dst_
22739 +}
22740 +
22741 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22742 +{
22743 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22744 +               au_ren_do_rev_diropq(err, a, AuSRC);
22745 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22746 +               au_ren_do_rev_diropq(err, a, AuDST);
22747 +}
22748 +
22749 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22750 +{
22751 +       int rerr;
22752 +       struct inode *delegated;
22753 +
22754 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22755 +                                         a->src_h_parent);
22756 +       rerr = PTR_ERR(a->h_path.dentry);
22757 +       if (IS_ERR(a->h_path.dentry)) {
22758 +               RevertFailure("lkup one %pd", a->src_dentry);
22759 +               return;
22760 +       }
22761 +
22762 +       delegated = NULL;
22763 +       rerr = vfsub_rename(a->dst_h_dir,
22764 +                           au_h_dptr(a->src_dentry, a->btgt),
22765 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22766 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22767 +               pr_warn("cannot retry for NFSv4 delegation"
22768 +                       " for an internal rename\n");
22769 +               iput(delegated);
22770 +       }
22771 +       d_drop(a->h_path.dentry);
22772 +       dput(a->h_path.dentry);
22773 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22774 +       if (rerr)
22775 +               RevertFailure("rename %pd", a->src_dentry);
22776 +}
22777 +
22778 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22779 +{
22780 +       int rerr;
22781 +       struct inode *delegated;
22782 +
22783 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22784 +                                         a->dst_h_parent);
22785 +       rerr = PTR_ERR(a->h_path.dentry);
22786 +       if (IS_ERR(a->h_path.dentry)) {
22787 +               RevertFailure("lkup one %pd", a->dst_dentry);
22788 +               return;
22789 +       }
22790 +       if (d_is_positive(a->h_path.dentry)) {
22791 +               d_drop(a->h_path.dentry);
22792 +               dput(a->h_path.dentry);
22793 +               return;
22794 +       }
22795 +
22796 +       delegated = NULL;
22797 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22798 +                           &delegated, a->flags);
22799 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22800 +               pr_warn("cannot retry for NFSv4 delegation"
22801 +                       " for an internal rename\n");
22802 +               iput(delegated);
22803 +       }
22804 +       d_drop(a->h_path.dentry);
22805 +       dput(a->h_path.dentry);
22806 +       if (!rerr)
22807 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22808 +       else
22809 +               RevertFailure("rename %pd", a->h_dst);
22810 +}
22811 +
22812 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22813 +{
22814 +       int rerr;
22815 +
22816 +       a->h_path.dentry = a->src_wh_dentry;
22817 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22818 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22819 +       if (rerr)
22820 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22821 +}
22822 +#undef RevertFailure
22823 +
22824 +/* ---------------------------------------------------------------------- */
22825 +
22826 +/*
22827 + * when we have to copyup the renaming entry, do it with the rename-target name
22828 + * in order to minimize the cost (the later actual rename is unnecessary).
22829 + * otherwise rename it on the target branch.
22830 + */
22831 +static int au_ren_or_cpup(struct au_ren_args *a)
22832 +{
22833 +       int err;
22834 +       struct dentry *d;
22835 +       struct inode *delegated;
22836 +
22837 +       d = a->src_dentry;
22838 +       if (au_dbtop(d) == a->btgt) {
22839 +               a->h_path.dentry = a->dst_h_dentry;
22840 +               AuDebugOn(au_dbtop(d) != a->btgt);
22841 +               delegated = NULL;
22842 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22843 +                                  a->dst_h_dir, &a->h_path, &delegated,
22844 +                                  a->flags);
22845 +               if (unlikely(err == -EWOULDBLOCK)) {
22846 +                       pr_warn("cannot retry for NFSv4 delegation"
22847 +                               " for an internal rename\n");
22848 +                       iput(delegated);
22849 +               }
22850 +       } else
22851 +               BUG();
22852 +
22853 +       if (!err && a->h_dst)
22854 +               /* it will be set to dinfo later */
22855 +               dget(a->h_dst);
22856 +
22857 +       return err;
22858 +}
22859 +
22860 +/* cf. aufs_rmdir() */
22861 +static int au_ren_del_whtmp(struct au_ren_args *a)
22862 +{
22863 +       int err;
22864 +       struct inode *dir;
22865 +
22866 +       dir = a->dst_dir;
22867 +       SiMustAnyLock(dir->i_sb);
22868 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22869 +                                    au_sbi(dir->i_sb)->si_dirwh)
22870 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22871 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22872 +               if (unlikely(err))
22873 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22874 +                               "ignored.\n", a->h_dst, err);
22875 +       } else {
22876 +               au_nhash_wh_free(&a->thargs->whlist);
22877 +               a->thargs->whlist = a->whlist;
22878 +               a->whlist.nh_num = 0;
22879 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22880 +               dput(a->h_dst);
22881 +               a->thargs = NULL;
22882 +       }
22883 +
22884 +       return 0;
22885 +}
22886 +
22887 +/* make it 'opaque' dir. */
22888 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22889 +{
22890 +       int err;
22891 +       struct dentry *d, *diropq;
22892 +#define src_or_dst(member) a->sd[idx].member
22893 +
22894 +       err = 0;
22895 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22896 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22897 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22898 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22899 +       diropq = au_diropq_create(d, a->btgt);
22900 +       au_hn_inode_unlock(src_or_dst(hinode));
22901 +       if (IS_ERR(diropq))
22902 +               err = PTR_ERR(diropq);
22903 +       else
22904 +               dput(diropq);
22905 +
22906 +#undef src_or_dst_
22907 +       return err;
22908 +}
22909 +
22910 +static int au_ren_diropq(struct au_ren_args *a)
22911 +{
22912 +       int err;
22913 +       unsigned char always;
22914 +       struct dentry *d;
22915 +
22916 +       err = 0;
22917 +       d = a->dst_dentry; /* already renamed on the branch */
22918 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22919 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22920 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22921 +           && a->btgt != au_dbdiropq(a->src_dentry)
22922 +           && (a->dst_wh_dentry
22923 +               || a->btgt <= au_dbdiropq(d)
22924 +               /* hide the lower to keep xino */
22925 +               /* the lowers may not be a dir, but we hide them anyway */
22926 +               || a->btgt < au_dbbot(d)
22927 +               || always)) {
22928 +               AuDbg("here\n");
22929 +               err = au_ren_do_diropq(a, AuSRC);
22930 +               if (unlikely(err))
22931 +                       goto out;
22932 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22933 +       }
22934 +       if (!a->exchange)
22935 +               goto out; /* success */
22936 +
22937 +       d = a->src_dentry; /* already renamed on the branch */
22938 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22939 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22940 +           && (a->btgt < au_dbdiropq(d)
22941 +               || a->btgt < au_dbbot(d)
22942 +               || always)) {
22943 +               AuDbgDentry(a->src_dentry);
22944 +               AuDbgDentry(a->dst_dentry);
22945 +               err = au_ren_do_diropq(a, AuDST);
22946 +               if (unlikely(err))
22947 +                       goto out_rev_src;
22948 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22949 +       }
22950 +       goto out; /* success */
22951 +
22952 +out_rev_src:
22953 +       AuDbg("err %d, reverting src\n", err);
22954 +       au_ren_rev_diropq(err, a);
22955 +out:
22956 +       return err;
22957 +}
22958 +
22959 +static int do_rename(struct au_ren_args *a)
22960 +{
22961 +       int err;
22962 +       struct dentry *d, *h_d;
22963 +
22964 +       if (!a->exchange) {
22965 +               /* prepare workqueue args for asynchronous rmdir */
22966 +               h_d = a->dst_h_dentry;
22967 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22968 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22969 +                   && d_is_positive(h_d)) {
22970 +                       err = -ENOMEM;
22971 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22972 +                                                        GFP_NOFS);
22973 +                       if (unlikely(!a->thargs))
22974 +                               goto out;
22975 +                       a->h_dst = dget(h_d);
22976 +               }
22977 +
22978 +               /* create whiteout for src_dentry */
22979 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22980 +                       a->src_bwh = au_dbwh(a->src_dentry);
22981 +                       AuDebugOn(a->src_bwh >= 0);
22982 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22983 +                                                       a->src_h_parent);
22984 +                       err = PTR_ERR(a->src_wh_dentry);
22985 +                       if (IS_ERR(a->src_wh_dentry))
22986 +                               goto out_thargs;
22987 +               }
22988 +
22989 +               /* lookup whiteout for dentry */
22990 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22991 +                       h_d = au_wh_lkup(a->dst_h_parent,
22992 +                                        &a->dst_dentry->d_name, a->br);
22993 +                       err = PTR_ERR(h_d);
22994 +                       if (IS_ERR(h_d))
22995 +                               goto out_whsrc;
22996 +                       if (d_is_negative(h_d))
22997 +                               dput(h_d);
22998 +                       else
22999 +                               a->dst_wh_dentry = h_d;
23000 +               }
23001 +
23002 +               /* rename dentry to tmpwh */
23003 +               if (a->thargs) {
23004 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
23005 +                       if (unlikely(err))
23006 +                               goto out_whdst;
23007 +
23008 +                       d = a->dst_dentry;
23009 +                       au_set_h_dptr(d, a->btgt, NULL);
23010 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
23011 +                       if (unlikely(err))
23012 +                               goto out_whtmp;
23013 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
23014 +               }
23015 +       }
23016 +
23017 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
23018 +#if 0
23019 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23020 +              && d_is_positive(a->dst_h_dentry)
23021 +              && a->src_btop != a->btgt);
23022 +#endif
23023 +
23024 +       /* rename by vfs_rename or cpup */
23025 +       err = au_ren_or_cpup(a);
23026 +       if (unlikely(err))
23027 +               /* leave the copied-up one */
23028 +               goto out_whtmp;
23029 +
23030 +       /* make dir opaque */
23031 +       err = au_ren_diropq(a);
23032 +       if (unlikely(err))
23033 +               goto out_rename;
23034 +
23035 +       /* update target timestamps */
23036 +       if (a->exchange) {
23037 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23038 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23039 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23040 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23041 +       }
23042 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
23043 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23044 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23045 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23046 +
23047 +       if (!a->exchange) {
23048 +               /* remove whiteout for dentry */
23049 +               if (a->dst_wh_dentry) {
23050 +                       a->h_path.dentry = a->dst_wh_dentry;
23051 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23052 +                                                 a->dst_dentry);
23053 +                       if (unlikely(err))
23054 +                               goto out_diropq;
23055 +               }
23056 +
23057 +               /* remove whtmp */
23058 +               if (a->thargs)
23059 +                       au_ren_del_whtmp(a); /* ignore this error */
23060 +
23061 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23062 +       }
23063 +       err = 0;
23064 +       goto out_success;
23065 +
23066 +out_diropq:
23067 +       au_ren_rev_diropq(err, a);
23068 +out_rename:
23069 +       au_ren_rev_rename(err, a);
23070 +       dput(a->h_dst);
23071 +out_whtmp:
23072 +       if (a->thargs)
23073 +               au_ren_rev_whtmp(err, a);
23074 +out_whdst:
23075 +       dput(a->dst_wh_dentry);
23076 +       a->dst_wh_dentry = NULL;
23077 +out_whsrc:
23078 +       if (a->src_wh_dentry)
23079 +               au_ren_rev_whsrc(err, a);
23080 +out_success:
23081 +       dput(a->src_wh_dentry);
23082 +       dput(a->dst_wh_dentry);
23083 +out_thargs:
23084 +       if (a->thargs) {
23085 +               dput(a->h_dst);
23086 +               au_whtmp_rmdir_free(a->thargs);
23087 +               a->thargs = NULL;
23088 +       }
23089 +out:
23090 +       return err;
23091 +}
23092 +
23093 +/* ---------------------------------------------------------------------- */
23094 +
23095 +/*
23096 + * test if @dentry dir can be rename destination or not.
23097 + * success means, it is a logically empty dir.
23098 + */
23099 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23100 +{
23101 +       return au_test_empty(dentry, whlist);
23102 +}
23103 +
23104 +/*
23105 + * test if @a->src_dentry dir can be rename source or not.
23106 + * if it can, return 0.
23107 + * success means,
23108 + * - it is a logically empty dir.
23109 + * - or, it exists on writable branch and has no children including whiteouts
23110 + *   on the lower branch unless DIRREN is on.
23111 + */
23112 +static int may_rename_srcdir(struct au_ren_args *a)
23113 +{
23114 +       int err;
23115 +       unsigned int rdhash;
23116 +       aufs_bindex_t btop, btgt;
23117 +       struct dentry *dentry;
23118 +       struct super_block *sb;
23119 +       struct au_sbinfo *sbinfo;
23120 +
23121 +       dentry = a->src_dentry;
23122 +       sb = dentry->d_sb;
23123 +       sbinfo = au_sbi(sb);
23124 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23125 +               au_fset_ren(a->auren_flags, DIRREN);
23126 +
23127 +       btgt = a->btgt;
23128 +       btop = au_dbtop(dentry);
23129 +       if (btop != btgt) {
23130 +               struct au_nhash whlist;
23131 +
23132 +               SiMustAnyLock(sb);
23133 +               rdhash = sbinfo->si_rdhash;
23134 +               if (!rdhash)
23135 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23136 +                                                          dentry));
23137 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23138 +               if (unlikely(err))
23139 +                       goto out;
23140 +               err = au_test_empty(dentry, &whlist);
23141 +               au_nhash_wh_free(&whlist);
23142 +               goto out;
23143 +       }
23144 +
23145 +       if (btop == au_dbtaildir(dentry))
23146 +               return 0; /* success */
23147 +
23148 +       err = au_test_empty_lower(dentry);
23149 +
23150 +out:
23151 +       if (err == -ENOTEMPTY) {
23152 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23153 +                       err = 0;
23154 +               } else {
23155 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23156 +                               "branches, is not supported\n");
23157 +                       err = -EXDEV;
23158 +               }
23159 +       }
23160 +       return err;
23161 +}
23162 +
23163 +/* side effect: sets whlist and h_dentry */
23164 +static int au_ren_may_dir(struct au_ren_args *a)
23165 +{
23166 +       int err;
23167 +       unsigned int rdhash;
23168 +       struct dentry *d;
23169 +
23170 +       d = a->dst_dentry;
23171 +       SiMustAnyLock(d->d_sb);
23172 +
23173 +       err = 0;
23174 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23175 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23176 +               if (!rdhash)
23177 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23178 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23179 +               if (unlikely(err))
23180 +                       goto out;
23181 +
23182 +               if (!a->exchange) {
23183 +                       au_set_dbtop(d, a->dst_btop);
23184 +                       err = may_rename_dstdir(d, &a->whlist);
23185 +                       au_set_dbtop(d, a->btgt);
23186 +               } else
23187 +                       err = may_rename_srcdir(a);
23188 +       }
23189 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23190 +       if (unlikely(err))
23191 +               goto out;
23192 +
23193 +       d = a->src_dentry;
23194 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23195 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23196 +               err = may_rename_srcdir(a);
23197 +               if (unlikely(err)) {
23198 +                       au_nhash_wh_free(&a->whlist);
23199 +                       a->whlist.nh_num = 0;
23200 +               }
23201 +       }
23202 +out:
23203 +       return err;
23204 +}
23205 +
23206 +/* ---------------------------------------------------------------------- */
23207 +
23208 +/*
23209 + * simple tests for rename.
23210 + * following the checks in vfs, plus the parent-child relationship.
23211 + */
23212 +static int au_may_ren(struct au_ren_args *a)
23213 +{
23214 +       int err, isdir;
23215 +       struct inode *h_inode;
23216 +
23217 +       if (a->src_btop == a->btgt) {
23218 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23219 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23220 +               if (unlikely(err))
23221 +                       goto out;
23222 +               err = -EINVAL;
23223 +               if (unlikely(a->src_h_dentry == a->h_trap))
23224 +                       goto out;
23225 +       }
23226 +
23227 +       err = 0;
23228 +       if (a->dst_btop != a->btgt)
23229 +               goto out;
23230 +
23231 +       err = -ENOTEMPTY;
23232 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23233 +               goto out;
23234 +
23235 +       err = -EIO;
23236 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23237 +       if (d_really_is_negative(a->dst_dentry)) {
23238 +               if (d_is_negative(a->dst_h_dentry))
23239 +                       err = au_may_add(a->dst_dentry, a->btgt,
23240 +                                        a->dst_h_parent, isdir);
23241 +       } else {
23242 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23243 +                       goto out;
23244 +               h_inode = d_inode(a->dst_h_dentry);
23245 +               if (h_inode->i_nlink)
23246 +                       err = au_may_del(a->dst_dentry, a->btgt,
23247 +                                        a->dst_h_parent, isdir);
23248 +       }
23249 +
23250 +out:
23251 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23252 +               err = -EIO;
23253 +       AuTraceErr(err);
23254 +       return err;
23255 +}
23256 +
23257 +/* ---------------------------------------------------------------------- */
23258 +
23259 +/*
23260 + * locking order
23261 + * (VFS)
23262 + * - src_dir and dir by lock_rename()
23263 + * - inode if exists
23264 + * (aufs)
23265 + * - lock all
23266 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23267 + *     + si_read_lock
23268 + *     + di_write_lock2_child()
23269 + *       + di_write_lock_child()
23270 + *        + ii_write_lock_child()
23271 + *       + di_write_lock_child2()
23272 + *        + ii_write_lock_child2()
23273 + *     + src_parent and parent
23274 + *       + di_write_lock_parent()
23275 + *        + ii_write_lock_parent()
23276 + *       + di_write_lock_parent2()
23277 + *        + ii_write_lock_parent2()
23278 + *   + lower src_dir and dir by vfsub_lock_rename()
23279 + *   + verify the every relationships between child and parent. if any
23280 + *     of them failed, unlock all and return -EBUSY.
23281 + */
23282 +static void au_ren_unlock(struct au_ren_args *a)
23283 +{
23284 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23285 +                           a->dst_h_parent, a->dst_hdir);
23286 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23287 +           && a->h_root)
23288 +               au_hn_inode_unlock(a->h_root);
23289 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23290 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23291 +}
23292 +
23293 +static int au_ren_lock(struct au_ren_args *a)
23294 +{
23295 +       int err;
23296 +       unsigned int udba;
23297 +
23298 +       err = 0;
23299 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23300 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23301 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23302 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23303 +
23304 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23305 +       if (unlikely(err))
23306 +               goto out;
23307 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23308 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23309 +               struct dentry *root;
23310 +               struct inode *dir;
23311 +
23312 +               /*
23313 +                * sbinfo is already locked, so this ii_read_lock is
23314 +                * unnecessary. but our debugging feature checks it.
23315 +                */
23316 +               root = a->src_inode->i_sb->s_root;
23317 +               if (root != a->src_parent && root != a->dst_parent) {
23318 +                       dir = d_inode(root);
23319 +                       ii_read_lock_parent3(dir);
23320 +                       a->h_root = au_hi(dir, a->btgt);
23321 +                       ii_read_unlock(dir);
23322 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23323 +               }
23324 +       }
23325 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23326 +                                     a->dst_h_parent, a->dst_hdir);
23327 +       udba = au_opt_udba(a->src_dentry->d_sb);
23328 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23329 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23330 +               err = au_busy_or_stale();
23331 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23332 +               err = au_h_verify(a->src_h_dentry, udba,
23333 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23334 +                                 a->br);
23335 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23336 +               err = au_h_verify(a->dst_h_dentry, udba,
23337 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23338 +                                 a->br);
23339 +       if (!err)
23340 +               goto out; /* success */
23341 +
23342 +       err = au_busy_or_stale();
23343 +       au_ren_unlock(a);
23344 +
23345 +out:
23346 +       return err;
23347 +}
23348 +
23349 +/* ---------------------------------------------------------------------- */
23350 +
23351 +static void au_ren_refresh_dir(struct au_ren_args *a)
23352 +{
23353 +       struct inode *dir;
23354 +
23355 +       dir = a->dst_dir;
23356 +       inode_inc_iversion(dir);
23357 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23358 +               /* is this updating defined in POSIX? */
23359 +               au_cpup_attr_timesizes(a->src_inode);
23360 +               au_cpup_attr_nlink(dir, /*force*/1);
23361 +       }
23362 +       au_dir_ts(dir, a->btgt);
23363 +
23364 +       if (a->exchange) {
23365 +               dir = a->src_dir;
23366 +               inode_inc_iversion(dir);
23367 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23368 +                       /* is this updating defined in POSIX? */
23369 +                       au_cpup_attr_timesizes(a->dst_inode);
23370 +                       au_cpup_attr_nlink(dir, /*force*/1);
23371 +               }
23372 +               au_dir_ts(dir, a->btgt);
23373 +       }
23374 +
23375 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23376 +               return;
23377 +
23378 +       dir = a->src_dir;
23379 +       inode_inc_iversion(dir);
23380 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23381 +               au_cpup_attr_nlink(dir, /*force*/1);
23382 +       au_dir_ts(dir, a->btgt);
23383 +}
23384 +
23385 +static void au_ren_refresh(struct au_ren_args *a)
23386 +{
23387 +       aufs_bindex_t bbot, bindex;
23388 +       struct dentry *d, *h_d;
23389 +       struct inode *i, *h_i;
23390 +       struct super_block *sb;
23391 +
23392 +       d = a->dst_dentry;
23393 +       d_drop(d);
23394 +       if (a->h_dst)
23395 +               /* already dget-ed by au_ren_or_cpup() */
23396 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23397 +
23398 +       i = a->dst_inode;
23399 +       if (i) {
23400 +               if (!a->exchange) {
23401 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23402 +                               vfsub_drop_nlink(i);
23403 +                       else {
23404 +                               vfsub_dead_dir(i);
23405 +                               au_cpup_attr_timesizes(i);
23406 +                       }
23407 +                       au_update_dbrange(d, /*do_put_zero*/1);
23408 +               } else
23409 +                       au_cpup_attr_nlink(i, /*force*/1);
23410 +       } else {
23411 +               bbot = a->btgt;
23412 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23413 +                       au_set_h_dptr(d, bindex, NULL);
23414 +               bbot = au_dbbot(d);
23415 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23416 +                       au_set_h_dptr(d, bindex, NULL);
23417 +               au_update_dbrange(d, /*do_put_zero*/0);
23418 +       }
23419 +
23420 +       if (a->exchange
23421 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23422 +               d_drop(a->src_dentry);
23423 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23424 +                       au_set_dbwh(a->src_dentry, -1);
23425 +               return;
23426 +       }
23427 +
23428 +       d = a->src_dentry;
23429 +       au_set_dbwh(d, -1);
23430 +       bbot = au_dbbot(d);
23431 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23432 +               h_d = au_h_dptr(d, bindex);
23433 +               if (h_d)
23434 +                       au_set_h_dptr(d, bindex, NULL);
23435 +       }
23436 +       au_set_dbbot(d, a->btgt);
23437 +
23438 +       sb = d->d_sb;
23439 +       i = a->src_inode;
23440 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23441 +               return; /* success */
23442 +
23443 +       bbot = au_ibbot(i);
23444 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23445 +               h_i = au_h_iptr(i, bindex);
23446 +               if (h_i) {
23447 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23448 +                       /* ignore this error */
23449 +                       au_set_h_iptr(i, bindex, NULL, 0);
23450 +               }
23451 +       }
23452 +       au_set_ibbot(i, a->btgt);
23453 +}
23454 +
23455 +/* ---------------------------------------------------------------------- */
23456 +
23457 +/* mainly for link(2) and rename(2) */
23458 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23459 +{
23460 +       aufs_bindex_t bdiropq, bwh;
23461 +       struct dentry *parent;
23462 +       struct au_branch *br;
23463 +
23464 +       parent = dentry->d_parent;
23465 +       IMustLock(d_inode(parent)); /* dir is locked */
23466 +
23467 +       bdiropq = au_dbdiropq(parent);
23468 +       bwh = au_dbwh(dentry);
23469 +       br = au_sbr(dentry->d_sb, btgt);
23470 +       if (au_br_rdonly(br)
23471 +           || (0 <= bdiropq && bdiropq < btgt)
23472 +           || (0 <= bwh && bwh < btgt))
23473 +               btgt = -1;
23474 +
23475 +       AuDbg("btgt %d\n", btgt);
23476 +       return btgt;
23477 +}
23478 +
23479 +/* sets src_btop, dst_btop and btgt */
23480 +static int au_ren_wbr(struct au_ren_args *a)
23481 +{
23482 +       int err;
23483 +       struct au_wr_dir_args wr_dir_args = {
23484 +               /* .force_btgt  = -1, */
23485 +               .flags          = AuWrDir_ADD_ENTRY
23486 +       };
23487 +
23488 +       a->src_btop = au_dbtop(a->src_dentry);
23489 +       a->dst_btop = au_dbtop(a->dst_dentry);
23490 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23491 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23492 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23493 +       wr_dir_args.force_btgt = a->src_btop;
23494 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23495 +               wr_dir_args.force_btgt = a->dst_btop;
23496 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23497 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23498 +       a->btgt = err;
23499 +       if (a->exchange)
23500 +               au_update_dbtop(a->dst_dentry);
23501 +
23502 +       return err;
23503 +}
23504 +
23505 +static void au_ren_dt(struct au_ren_args *a)
23506 +{
23507 +       a->h_path.dentry = a->src_h_parent;
23508 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23509 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23510 +               a->h_path.dentry = a->dst_h_parent;
23511 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23512 +       }
23513 +
23514 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23515 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23516 +           && !a->exchange)
23517 +               return;
23518 +
23519 +       a->h_path.dentry = a->src_h_dentry;
23520 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23521 +       if (d_is_positive(a->dst_h_dentry)) {
23522 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23523 +               a->h_path.dentry = a->dst_h_dentry;
23524 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23525 +       }
23526 +}
23527 +
23528 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23529 +{
23530 +       struct dentry *h_d;
23531 +       struct inode *h_inode;
23532 +
23533 +       au_dtime_revert(a->src_dt + AuPARENT);
23534 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23535 +               au_dtime_revert(a->dst_dt + AuPARENT);
23536 +
23537 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23538 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23539 +               h_inode = d_inode(h_d);
23540 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23541 +               au_dtime_revert(a->src_dt + AuCHILD);
23542 +               inode_unlock(h_inode);
23543 +
23544 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23545 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23546 +                       h_inode = d_inode(h_d);
23547 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23548 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23549 +                       inode_unlock(h_inode);
23550 +               }
23551 +       }
23552 +}
23553 +
23554 +/* ---------------------------------------------------------------------- */
23555 +
23556 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23557 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23558 +               unsigned int _flags)
23559 +{
23560 +       int err, lock_flags;
23561 +       void *rev;
23562 +       /* reduce stack space */
23563 +       struct au_ren_args *a;
23564 +       struct au_pin pin;
23565 +
23566 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23567 +       IMustLock(_src_dir);
23568 +       IMustLock(_dst_dir);
23569 +
23570 +       err = -EINVAL;
23571 +       if (unlikely(_flags & RENAME_WHITEOUT))
23572 +               goto out;
23573 +
23574 +       err = -ENOMEM;
23575 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23576 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23577 +       if (unlikely(!a))
23578 +               goto out;
23579 +
23580 +       a->flags = _flags;
23581 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23582 +                    && RENAME_EXCHANGE > U8_MAX);
23583 +       a->exchange = _flags & RENAME_EXCHANGE;
23584 +       a->src_dir = _src_dir;
23585 +       a->src_dentry = _src_dentry;
23586 +       a->src_inode = NULL;
23587 +       if (d_really_is_positive(a->src_dentry))
23588 +               a->src_inode = d_inode(a->src_dentry);
23589 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23590 +       a->dst_dir = _dst_dir;
23591 +       a->dst_dentry = _dst_dentry;
23592 +       a->dst_inode = NULL;
23593 +       if (d_really_is_positive(a->dst_dentry))
23594 +               a->dst_inode = d_inode(a->dst_dentry);
23595 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23596 +       if (a->dst_inode) {
23597 +               /*
23598 +                * if EXCHANGE && src is non-dir && dst is dir,
23599 +                * dst is not locked.
23600 +                */
23601 +               /* IMustLock(a->dst_inode); */
23602 +               au_igrab(a->dst_inode);
23603 +       }
23604 +
23605 +       err = -ENOTDIR;
23606 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23607 +       if (d_is_dir(a->src_dentry)) {
23608 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23609 +               if (unlikely(!a->exchange
23610 +                            && d_really_is_positive(a->dst_dentry)
23611 +                            && !d_is_dir(a->dst_dentry)))
23612 +                       goto out_free;
23613 +               lock_flags |= AuLock_DIRS;
23614 +       }
23615 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23616 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23617 +               if (unlikely(!a->exchange
23618 +                            && d_really_is_positive(a->src_dentry)
23619 +                            && !d_is_dir(a->src_dentry)))
23620 +                       goto out_free;
23621 +               lock_flags |= AuLock_DIRS;
23622 +       }
23623 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23624 +                                       lock_flags);
23625 +       if (unlikely(err))
23626 +               goto out_free;
23627 +
23628 +       err = au_d_hashed_positive(a->src_dentry);
23629 +       if (unlikely(err))
23630 +               goto out_unlock;
23631 +       err = -ENOENT;
23632 +       if (a->dst_inode) {
23633 +               /*
23634 +                * If it is a dir, VFS unhash it before this
23635 +                * function. It means we cannot rely upon d_unhashed().
23636 +                */
23637 +               if (unlikely(!a->dst_inode->i_nlink))
23638 +                       goto out_unlock;
23639 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23640 +                       err = au_d_hashed_positive(a->dst_dentry);
23641 +                       if (unlikely(err && !a->exchange))
23642 +                               goto out_unlock;
23643 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23644 +                       goto out_unlock;
23645 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23646 +               goto out_unlock;
23647 +
23648 +       /*
23649 +        * is it possible?
23650 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23651 +        * there may exist a problem somewhere else.
23652 +        */
23653 +       err = -EINVAL;
23654 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23655 +               goto out_unlock;
23656 +
23657 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23658 +       di_write_lock_parent(a->dst_parent);
23659 +
23660 +       /* which branch we process */
23661 +       err = au_ren_wbr(a);
23662 +       if (unlikely(err < 0))
23663 +               goto out_parent;
23664 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23665 +       a->h_path.mnt = au_br_mnt(a->br);
23666 +
23667 +       /* are they available to be renamed */
23668 +       err = au_ren_may_dir(a);
23669 +       if (unlikely(err))
23670 +               goto out_children;
23671 +
23672 +       /* prepare the writable parent dir on the same branch */
23673 +       if (a->dst_btop == a->btgt) {
23674 +               au_fset_ren(a->auren_flags, WHDST);
23675 +       } else {
23676 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23677 +               if (unlikely(err))
23678 +                       goto out_children;
23679 +       }
23680 +
23681 +       err = 0;
23682 +       if (!a->exchange) {
23683 +               if (a->src_dir != a->dst_dir) {
23684 +                       /*
23685 +                        * this temporary unlock is safe,
23686 +                        * because both dir->i_mutex are locked.
23687 +                        */
23688 +                       di_write_unlock(a->dst_parent);
23689 +                       di_write_lock_parent(a->src_parent);
23690 +                       err = au_wr_dir_need_wh(a->src_dentry,
23691 +                                               au_ftest_ren(a->auren_flags,
23692 +                                                            ISDIR_SRC),
23693 +                                               &a->btgt);
23694 +                       di_write_unlock(a->src_parent);
23695 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23696 +                                             /*isdir*/1);
23697 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23698 +               } else
23699 +                       err = au_wr_dir_need_wh(a->src_dentry,
23700 +                                               au_ftest_ren(a->auren_flags,
23701 +                                                            ISDIR_SRC),
23702 +                                               &a->btgt);
23703 +       }
23704 +       if (unlikely(err < 0))
23705 +               goto out_children;
23706 +       if (err)
23707 +               au_fset_ren(a->auren_flags, WHSRC);
23708 +
23709 +       /* cpup src */
23710 +       if (a->src_btop != a->btgt) {
23711 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23712 +                            au_opt_udba(a->src_dentry->d_sb),
23713 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23714 +               if (!err) {
23715 +                       struct au_cp_generic cpg = {
23716 +                               .dentry = a->src_dentry,
23717 +                               .bdst   = a->btgt,
23718 +                               .bsrc   = a->src_btop,
23719 +                               .len    = -1,
23720 +                               .pin    = &pin,
23721 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23722 +                       };
23723 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23724 +                       err = au_sio_cpup_simple(&cpg);
23725 +                       au_unpin(&pin);
23726 +               }
23727 +               if (unlikely(err))
23728 +                       goto out_children;
23729 +               a->src_btop = a->btgt;
23730 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23731 +               if (!a->exchange)
23732 +                       au_fset_ren(a->auren_flags, WHSRC);
23733 +       }
23734 +
23735 +       /* cpup dst */
23736 +       if (a->exchange && a->dst_inode
23737 +           && a->dst_btop != a->btgt) {
23738 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23739 +                            au_opt_udba(a->dst_dentry->d_sb),
23740 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23741 +               if (!err) {
23742 +                       struct au_cp_generic cpg = {
23743 +                               .dentry = a->dst_dentry,
23744 +                               .bdst   = a->btgt,
23745 +                               .bsrc   = a->dst_btop,
23746 +                               .len    = -1,
23747 +                               .pin    = &pin,
23748 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23749 +                       };
23750 +                       err = au_sio_cpup_simple(&cpg);
23751 +                       au_unpin(&pin);
23752 +               }
23753 +               if (unlikely(err))
23754 +                       goto out_children;
23755 +               a->dst_btop = a->btgt;
23756 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23757 +       }
23758 +
23759 +       /* lock them all */
23760 +       err = au_ren_lock(a);
23761 +       if (unlikely(err))
23762 +               /* leave the copied-up one */
23763 +               goto out_children;
23764 +
23765 +       if (!a->exchange) {
23766 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23767 +                       err = au_may_ren(a);
23768 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23769 +                       err = -ENAMETOOLONG;
23770 +               if (unlikely(err))
23771 +                       goto out_hdir;
23772 +       }
23773 +
23774 +       /* store timestamps to be revertible */
23775 +       au_ren_dt(a);
23776 +
23777 +       /* store dirren info */
23778 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23779 +               err = au_dr_rename(a->src_dentry, a->btgt,
23780 +                                  &a->dst_dentry->d_name, &rev);
23781 +               AuTraceErr(err);
23782 +               if (unlikely(err))
23783 +                       goto out_dt;
23784 +       }
23785 +
23786 +       /* here we go */
23787 +       err = do_rename(a);
23788 +       if (unlikely(err))
23789 +               goto out_dirren;
23790 +
23791 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23792 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23793 +
23794 +       /* update dir attributes */
23795 +       au_ren_refresh_dir(a);
23796 +
23797 +       /* dput/iput all lower dentries */
23798 +       au_ren_refresh(a);
23799 +
23800 +       goto out_hdir; /* success */
23801 +
23802 +out_dirren:
23803 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23804 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23805 +out_dt:
23806 +       au_ren_rev_dt(err, a);
23807 +out_hdir:
23808 +       au_ren_unlock(a);
23809 +out_children:
23810 +       au_nhash_wh_free(&a->whlist);
23811 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23812 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23813 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23814 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23815 +       }
23816 +out_parent:
23817 +       if (!err) {
23818 +               if (d_unhashed(a->src_dentry))
23819 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23820 +               if (d_unhashed(a->dst_dentry))
23821 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23822 +               if (!a->exchange)
23823 +                       d_move(a->src_dentry, a->dst_dentry);
23824 +               else {
23825 +                       d_exchange(a->src_dentry, a->dst_dentry);
23826 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23827 +                               d_drop(a->dst_dentry);
23828 +               }
23829 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23830 +                       d_drop(a->src_dentry);
23831 +       } else {
23832 +               au_update_dbtop(a->dst_dentry);
23833 +               if (!a->dst_inode)
23834 +                       d_drop(a->dst_dentry);
23835 +       }
23836 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23837 +               di_write_unlock(a->dst_parent);
23838 +       else
23839 +               di_write_unlock2(a->src_parent, a->dst_parent);
23840 +out_unlock:
23841 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23842 +out_free:
23843 +       iput(a->dst_inode);
23844 +       if (a->thargs)
23845 +               au_whtmp_rmdir_free(a->thargs);
23846 +       au_kfree_rcu(a);
23847 +out:
23848 +       AuTraceErr(err);
23849 +       return err;
23850 +}
23851 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23852 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23853 +++ linux/fs/aufs/Kconfig       2019-07-11 15:42:14.458904362 +0200
23854 @@ -0,0 +1,199 @@
23855 +# SPDX-License-Identifier: GPL-2.0
23856 +config AUFS_FS
23857 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23858 +       help
23859 +       Aufs is a stackable unification filesystem such as Unionfs,
23860 +       which unifies several directories and provides a merged single
23861 +       directory.
23862 +       In the early days, aufs was entirely re-designed and
23863 +       re-implemented Unionfs Version 1.x series. Introducing many
23864 +       original ideas, approaches and improvements, it becomes totally
23865 +       different from Unionfs while keeping the basic features.
23866 +
23867 +if AUFS_FS
23868 +choice
23869 +       prompt "Maximum number of branches"
23870 +       default AUFS_BRANCH_MAX_127
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_127
23876 +       bool "127"
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 +config AUFS_BRANCH_MAX_511
23882 +       bool "511"
23883 +       help
23884 +       Specifies the maximum number of branches (or member directories)
23885 +       in a single aufs. The larger value consumes more system
23886 +       resources and has a minor impact to performance.
23887 +config AUFS_BRANCH_MAX_1023
23888 +       bool "1023"
23889 +       help
23890 +       Specifies the maximum number of branches (or member directories)
23891 +       in a single aufs. The larger value consumes more system
23892 +       resources and has a minor impact to performance.
23893 +config AUFS_BRANCH_MAX_32767
23894 +       bool "32767"
23895 +       help
23896 +       Specifies the maximum number of branches (or member directories)
23897 +       in a single aufs. The larger value consumes more system
23898 +       resources and has a minor impact to performance.
23899 +endchoice
23900 +
23901 +config AUFS_SBILIST
23902 +       bool
23903 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23904 +       default y
23905 +       help
23906 +       Automatic configuration for internal use.
23907 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23908 +
23909 +config AUFS_HNOTIFY
23910 +       bool "Detect direct branch access (bypassing aufs)"
23911 +       help
23912 +       If you want to modify files on branches directly, eg. bypassing aufs,
23913 +       and want aufs to detect the changes of them fully, then enable this
23914 +       option and use 'udba=notify' mount option.
23915 +       Currently there is only one available configuration, "fsnotify".
23916 +       It will have a negative impact to the performance.
23917 +       See detail in aufs.5.
23918 +
23919 +choice
23920 +       prompt "method" if AUFS_HNOTIFY
23921 +       default AUFS_HFSNOTIFY
23922 +config AUFS_HFSNOTIFY
23923 +       bool "fsnotify"
23924 +       select FSNOTIFY
23925 +endchoice
23926 +
23927 +config AUFS_EXPORT
23928 +       bool "NFS-exportable aufs"
23929 +       depends on EXPORTFS
23930 +       help
23931 +       If you want to export your mounted aufs via NFS, then enable this
23932 +       option. There are several requirements for this configuration.
23933 +       See detail in aufs.5.
23934 +
23935 +config AUFS_INO_T_64
23936 +       bool
23937 +       depends on AUFS_EXPORT
23938 +       depends on 64BIT && !(ALPHA || S390)
23939 +       default y
23940 +       help
23941 +       Automatic configuration for internal use.
23942 +       /* typedef unsigned long/int __kernel_ino_t */
23943 +       /* alpha and s390x are int */
23944 +
23945 +config AUFS_XATTR
23946 +       bool "support for XATTR/EA (including Security Labels)"
23947 +       help
23948 +       If your branch fs supports XATTR/EA and you want to make them
23949 +       available in aufs too, then enable this opsion and specify the
23950 +       branch attributes for EA.
23951 +       See detail in aufs.5.
23952 +
23953 +config AUFS_FHSM
23954 +       bool "File-based Hierarchical Storage Management"
23955 +       help
23956 +       Hierarchical Storage Management (or HSM) is a well-known feature
23957 +       in the storage world. Aufs provides this feature as file-based.
23958 +       with multiple branches.
23959 +       These multiple branches are prioritized, ie. the topmost one
23960 +       should be the fastest drive and be used heavily.
23961 +
23962 +config AUFS_RDU
23963 +       bool "Readdir in userspace"
23964 +       help
23965 +       Aufs has two methods to provide a merged view for a directory,
23966 +       by a user-space library and by kernel-space natively. The latter
23967 +       is always enabled but sometimes large and slow.
23968 +       If you enable this option, install the library in aufs2-util
23969 +       package, and set some environment variables for your readdir(3),
23970 +       then the work will be handled in user-space which generally
23971 +       shows better performance in most cases.
23972 +       See detail in aufs.5.
23973 +
23974 +config AUFS_DIRREN
23975 +       bool "Workaround for rename(2)-ing a directory"
23976 +       help
23977 +       By default, aufs returns EXDEV error in renameing a dir who has
23978 +       his child on the lower branch, since it is a bad idea to issue
23979 +       rename(2) internally for every lower branch. But user may not
23980 +       accept this behaviour. So here is a workaround to allow such
23981 +       rename(2) and store some extra infromation on the writable
23982 +       branch. Obviously this costs high (and I don't like it).
23983 +       To use this feature, you need to enable this configuration AND
23984 +       to specify the mount option `dirren.'
23985 +       See details in aufs.5 and the design documents.
23986 +
23987 +config AUFS_SHWH
23988 +       bool "Show whiteouts"
23989 +       help
23990 +       If you want to make the whiteouts in aufs visible, then enable
23991 +       this option and specify 'shwh' mount option. Although it may
23992 +       sounds like philosophy or something, but in technically it
23993 +       simply shows the name of whiteout with keeping its behaviour.
23994 +
23995 +config AUFS_BR_RAMFS
23996 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23997 +       help
23998 +       If you want to use ramfs as an aufs branch fs, then enable this
23999 +       option. Generally tmpfs is recommended.
24000 +       Aufs prohibited them to be a branch fs by default, because
24001 +       initramfs becomes unusable after switch_root or something
24002 +       generally. If you sets initramfs as an aufs branch and boot your
24003 +       system by switch_root, you will meet a problem easily since the
24004 +       files in initramfs may be inaccessible.
24005 +       Unless you are going to use ramfs as an aufs branch fs without
24006 +       switch_root or something, leave it N.
24007 +
24008 +config AUFS_BR_FUSE
24009 +       bool "Fuse fs as an aufs branch"
24010 +       depends on FUSE_FS
24011 +       select AUFS_POLL
24012 +       help
24013 +       If you want to use fuse-based userspace filesystem as an aufs
24014 +       branch fs, then enable this option.
24015 +       It implements the internal poll(2) operation which is
24016 +       implemented by fuse only (curretnly).
24017 +
24018 +config AUFS_POLL
24019 +       bool
24020 +       help
24021 +       Automatic configuration for internal use.
24022 +
24023 +config AUFS_BR_HFSPLUS
24024 +       bool "Hfsplus as an aufs branch"
24025 +       depends on HFSPLUS_FS
24026 +       default y
24027 +       help
24028 +       If you want to use hfsplus fs as an aufs branch fs, then enable
24029 +       this option. This option introduces a small overhead at
24030 +       copying-up a file on hfsplus.
24031 +
24032 +config AUFS_BDEV_LOOP
24033 +       bool
24034 +       depends on BLK_DEV_LOOP
24035 +       default y
24036 +       help
24037 +       Automatic configuration for internal use.
24038 +       Convert =[ym] into =y.
24039 +
24040 +config AUFS_DEBUG
24041 +       bool "Debug aufs"
24042 +       help
24043 +       Enable this to compile aufs internal debug code.
24044 +       It will have a negative impact to the performance.
24045 +
24046 +config AUFS_MAGIC_SYSRQ
24047 +       bool
24048 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
24049 +       default y
24050 +       help
24051 +       Automatic configuration for internal use.
24052 +       When aufs supports Magic SysRq, enabled automatically.
24053 +endif
24054 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24055 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
24056 +++ linux/fs/aufs/lcnt.h        2019-07-11 15:42:14.468904634 +0200
24057 @@ -0,0 +1,186 @@
24058 +/* SPDX-License-Identifier: GPL-2.0 */
24059 +/*
24060 + * Copyright (C) 2018-2019 Junjiro R. Okajima
24061 + *
24062 + * This program, aufs is free software; you can redistribute it and/or modify
24063 + * it under the terms of the GNU General Public License as published by
24064 + * the Free Software Foundation; either version 2 of the License, or
24065 + * (at your option) any later version.
24066 + *
24067 + * This program is distributed in the hope that it will be useful,
24068 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24069 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24070 + * GNU General Public License for more details.
24071 + *
24072 + * You should have received a copy of the GNU General Public License
24073 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24074 + */
24075 +
24076 +/*
24077 + * simple long counter wrapper
24078 + */
24079 +
24080 +#ifndef __AUFS_LCNT_H__
24081 +#define __AUFS_LCNT_H__
24082 +
24083 +#ifdef __KERNEL__
24084 +
24085 +#include "debug.h"
24086 +
24087 +#define AuLCntATOMIC   1
24088 +#define AuLCntPCPUCNT  2
24089 +/*
24090 + * why does percpu_refcount require extra synchronize_rcu()s in
24091 + * au_br_do_free()
24092 + */
24093 +#define AuLCntPCPUREF  3
24094 +
24095 +/* #define AuLCntChosen        AuLCntATOMIC */
24096 +#define AuLCntChosen   AuLCntPCPUCNT
24097 +/* #define AuLCntChosen        AuLCntPCPUREF */
24098 +
24099 +#if AuLCntChosen == AuLCntATOMIC
24100 +#include <linux/atomic.h>
24101 +
24102 +typedef atomic_long_t au_lcnt_t;
24103 +
24104 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24105 +{
24106 +       atomic_long_set(cnt, 0);
24107 +       return 0;
24108 +}
24109 +
24110 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24111 +{
24112 +       /* empty */
24113 +}
24114 +
24115 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24116 +                              int do_sync __maybe_unused)
24117 +{
24118 +       /* empty */
24119 +}
24120 +
24121 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24122 +{
24123 +       atomic_long_inc(cnt);
24124 +}
24125 +
24126 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24127 +{
24128 +       atomic_long_dec(cnt);
24129 +}
24130 +
24131 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24132 +{
24133 +       return atomic_long_read(cnt);
24134 +}
24135 +#endif
24136 +
24137 +#if AuLCntChosen == AuLCntPCPUCNT
24138 +#include <linux/percpu_counter.h>
24139 +
24140 +typedef struct percpu_counter au_lcnt_t;
24141 +
24142 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24143 +{
24144 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24145 +}
24146 +
24147 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24148 +{
24149 +       /* empty */
24150 +}
24151 +
24152 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24153 +{
24154 +       percpu_counter_destroy(cnt);
24155 +}
24156 +
24157 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24158 +{
24159 +       percpu_counter_inc(cnt);
24160 +}
24161 +
24162 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24163 +{
24164 +       percpu_counter_dec(cnt);
24165 +}
24166 +
24167 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24168 +{
24169 +       s64 n;
24170 +
24171 +       n = percpu_counter_sum(cnt);
24172 +       BUG_ON(n < 0);
24173 +       if (LONG_MAX != LLONG_MAX
24174 +           && n > LONG_MAX)
24175 +               AuWarn1("%s\n", "wrap-around");
24176 +
24177 +       return n;
24178 +}
24179 +#endif
24180 +
24181 +#if AuLCntChosen == AuLCntPCPUREF
24182 +#include <linux/percpu-refcount.h>
24183 +
24184 +typedef struct percpu_ref au_lcnt_t;
24185 +
24186 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24187 +{
24188 +       if (!release)
24189 +               release = percpu_ref_exit;
24190 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24191 +}
24192 +
24193 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24194 +{
24195 +       synchronize_rcu();
24196 +}
24197 +
24198 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24199 +{
24200 +       percpu_ref_kill(cnt);
24201 +       if (do_sync)
24202 +               au_lcnt_wait_for_fin(cnt);
24203 +}
24204 +
24205 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24206 +{
24207 +       percpu_ref_get(cnt);
24208 +}
24209 +
24210 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24211 +{
24212 +       percpu_ref_put(cnt);
24213 +}
24214 +
24215 +/*
24216 + * avoid calling this func as possible.
24217 + */
24218 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24219 +{
24220 +       long l;
24221 +
24222 +       percpu_ref_switch_to_atomic_sync(cnt);
24223 +       l = atomic_long_read(&cnt->count);
24224 +       if (do_rev)
24225 +               percpu_ref_switch_to_percpu(cnt);
24226 +
24227 +       /* percpu_ref is initialized by 1 instead of 0 */
24228 +       return l - 1;
24229 +}
24230 +#endif
24231 +
24232 +#ifdef CONFIG_AUFS_DEBUG
24233 +#define AuLCntZero(val) do {                   \
24234 +       long l = val;                           \
24235 +       if (l)                                  \
24236 +               AuDbg("%s = %ld\n", #val, l);   \
24237 +} while (0)
24238 +#else
24239 +#define AuLCntZero(val)                do {} while (0)
24240 +#endif
24241 +
24242 +#endif /* __KERNEL__ */
24243 +#endif /* __AUFS_LCNT_H__ */
24244 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24245 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24246 +++ linux/fs/aufs/loop.c        2019-07-11 15:42:14.468904634 +0200
24247 @@ -0,0 +1,148 @@
24248 +// SPDX-License-Identifier: GPL-2.0
24249 +/*
24250 + * Copyright (C) 2005-2019 Junjiro R. Okajima
24251 + *
24252 + * This program, aufs is free software; you can redistribute it and/or modify
24253 + * it under the terms of the GNU General Public License as published by
24254 + * the Free Software Foundation; either version 2 of the License, or
24255 + * (at your option) any later version.
24256 + *
24257 + * This program is distributed in the hope that it will be useful,
24258 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24259 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24260 + * GNU General Public License for more details.
24261 + *
24262 + * You should have received a copy of the GNU General Public License
24263 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24264 + */
24265 +
24266 +/*
24267 + * support for loopback block device as a branch
24268 + */
24269 +
24270 +#include "aufs.h"
24271 +
24272 +/* added into drivers/block/loop.c */
24273 +static struct file *(*backing_file_func)(struct super_block *sb);
24274 +
24275 +/*
24276 + * test if two lower dentries have overlapping branches.
24277 + */
24278 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24279 +{
24280 +       struct super_block *h_sb;
24281 +       struct file *backing_file;
24282 +
24283 +       if (unlikely(!backing_file_func)) {
24284 +               /* don't load "loop" module here */
24285 +               backing_file_func = symbol_get(loop_backing_file);
24286 +               if (unlikely(!backing_file_func))
24287 +                       /* "loop" module is not loaded */
24288 +                       return 0;
24289 +       }
24290 +
24291 +       h_sb = h_adding->d_sb;
24292 +       backing_file = backing_file_func(h_sb);
24293 +       if (!backing_file)
24294 +               return 0;
24295 +
24296 +       h_adding = backing_file->f_path.dentry;
24297 +       /*
24298 +        * h_adding can be local NFS.
24299 +        * in this case aufs cannot detect the loop.
24300 +        */
24301 +       if (unlikely(h_adding->d_sb == sb))
24302 +               return 1;
24303 +       return !!au_test_subdir(h_adding, sb->s_root);
24304 +}
24305 +
24306 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24307 +int au_test_loopback_kthread(void)
24308 +{
24309 +       int ret;
24310 +       struct task_struct *tsk = current;
24311 +       char c, comm[sizeof(tsk->comm)];
24312 +
24313 +       ret = 0;
24314 +       if (tsk->flags & PF_KTHREAD) {
24315 +               get_task_comm(comm, tsk);
24316 +               c = comm[4];
24317 +               ret = ('0' <= c && c <= '9'
24318 +                      && !strncmp(comm, "loop", 4));
24319 +       }
24320 +
24321 +       return ret;
24322 +}
24323 +
24324 +/* ---------------------------------------------------------------------- */
24325 +
24326 +#define au_warn_loopback_step  16
24327 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24328 +static unsigned long *au_warn_loopback_array;
24329 +
24330 +void au_warn_loopback(struct super_block *h_sb)
24331 +{
24332 +       int i, new_nelem;
24333 +       unsigned long *a, magic;
24334 +       static DEFINE_SPINLOCK(spin);
24335 +
24336 +       magic = h_sb->s_magic;
24337 +       spin_lock(&spin);
24338 +       a = au_warn_loopback_array;
24339 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24340 +               if (a[i] == magic) {
24341 +                       spin_unlock(&spin);
24342 +                       return;
24343 +               }
24344 +
24345 +       /* h_sb is new to us, print it */
24346 +       if (i < au_warn_loopback_nelem) {
24347 +               a[i] = magic;
24348 +               goto pr;
24349 +       }
24350 +
24351 +       /* expand the array */
24352 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24353 +       a = au_kzrealloc(au_warn_loopback_array,
24354 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24355 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24356 +                        /*may_shrink*/0);
24357 +       if (a) {
24358 +               au_warn_loopback_nelem = new_nelem;
24359 +               au_warn_loopback_array = a;
24360 +               a[i] = magic;
24361 +               goto pr;
24362 +       }
24363 +
24364 +       spin_unlock(&spin);
24365 +       AuWarn1("realloc failed, ignored\n");
24366 +       return;
24367 +
24368 +pr:
24369 +       spin_unlock(&spin);
24370 +       pr_warn("you may want to try another patch for loopback file "
24371 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24372 +}
24373 +
24374 +int au_loopback_init(void)
24375 +{
24376 +       int err;
24377 +       struct super_block *sb __maybe_unused;
24378 +
24379 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24380 +
24381 +       err = 0;
24382 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24383 +                                        sizeof(unsigned long), GFP_NOFS);
24384 +       if (unlikely(!au_warn_loopback_array))
24385 +               err = -ENOMEM;
24386 +
24387 +       return err;
24388 +}
24389 +
24390 +void au_loopback_fin(void)
24391 +{
24392 +       if (backing_file_func)
24393 +               symbol_put(loop_backing_file);
24394 +       au_kfree_try_rcu(au_warn_loopback_array);
24395 +}
24396 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24397 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24398 +++ linux/fs/aufs/loop.h        2019-07-11 15:42:14.468904634 +0200
24399 @@ -0,0 +1,55 @@
24400 +/* SPDX-License-Identifier: GPL-2.0 */
24401 +/*
24402 + * Copyright (C) 2005-2019 Junjiro R. Okajima
24403 + *
24404 + * This program, aufs is free software; you can redistribute it and/or modify
24405 + * it under the terms of the GNU General Public License as published by
24406 + * the Free Software Foundation; either version 2 of the License, or
24407 + * (at your option) any later version.
24408 + *
24409 + * This program is distributed in the hope that it will be useful,
24410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24412 + * GNU General Public License for more details.
24413 + *
24414 + * You should have received a copy of the GNU General Public License
24415 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24416 + */
24417 +
24418 +/*
24419 + * support for loopback mount as a branch
24420 + */
24421 +
24422 +#ifndef __AUFS_LOOP_H__
24423 +#define __AUFS_LOOP_H__
24424 +
24425 +#ifdef __KERNEL__
24426 +
24427 +struct dentry;
24428 +struct super_block;
24429 +
24430 +#ifdef CONFIG_AUFS_BDEV_LOOP
24431 +/* drivers/block/loop.c */
24432 +struct file *loop_backing_file(struct super_block *sb);
24433 +
24434 +/* loop.c */
24435 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24436 +int au_test_loopback_kthread(void);
24437 +void au_warn_loopback(struct super_block *h_sb);
24438 +
24439 +int au_loopback_init(void);
24440 +void au_loopback_fin(void);
24441 +#else
24442 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24443 +
24444 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24445 +          struct dentry *h_adding)
24446 +AuStubInt0(au_test_loopback_kthread, void)
24447 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24448 +
24449 +AuStubInt0(au_loopback_init, void)
24450 +AuStubVoid(au_loopback_fin, void)
24451 +#endif /* BLK_DEV_LOOP */
24452 +
24453 +#endif /* __KERNEL__ */
24454 +#endif /* __AUFS_LOOP_H__ */
24455 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24456 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24457 +++ linux/fs/aufs/magic.mk      2019-07-11 15:42:14.468904634 +0200
24458 @@ -0,0 +1,31 @@
24459 +# SPDX-License-Identifier: GPL-2.0
24460 +
24461 +# defined in ${srctree}/fs/fuse/inode.c
24462 +# tristate
24463 +ifdef CONFIG_FUSE_FS
24464 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24465 +endif
24466 +
24467 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24468 +# tristate
24469 +ifdef CONFIG_XFS_FS
24470 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24471 +endif
24472 +
24473 +# defined in ${srctree}/fs/configfs/mount.c
24474 +# tristate
24475 +ifdef CONFIG_CONFIGFS_FS
24476 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24477 +endif
24478 +
24479 +# defined in ${srctree}/fs/ubifs/ubifs.h
24480 +# tristate
24481 +ifdef CONFIG_UBIFS_FS
24482 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24483 +endif
24484 +
24485 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24486 +# tristate
24487 +ifdef CONFIG_HFSPLUS_FS
24488 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24489 +endif
24490 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24491 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24492 +++ linux/fs/aufs/Makefile      2019-07-11 15:42:14.462237786 +0200
24493 @@ -0,0 +1,46 @@
24494 +# SPDX-License-Identifier: GPL-2.0
24495 +
24496 +include ${src}/magic.mk
24497 +ifeq (${CONFIG_AUFS_FS},m)
24498 +include ${src}/conf.mk
24499 +endif
24500 +-include ${src}/priv_def.mk
24501 +
24502 +# cf. include/linux/kernel.h
24503 +# enable pr_debug
24504 +ccflags-y += -DDEBUG
24505 +# sparse requires the full pathname
24506 +ifdef M
24507 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24508 +else
24509 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24510 +endif
24511 +
24512 +obj-$(CONFIG_AUFS_FS) += aufs.o
24513 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24514 +       wkq.o vfsub.o dcsub.o \
24515 +       cpup.o whout.o wbr_policy.o \
24516 +       dinfo.o dentry.o \
24517 +       dynop.o \
24518 +       finfo.o file.o f_op.o \
24519 +       dir.o vdir.o \
24520 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24521 +       mvdown.o ioctl.o
24522 +
24523 +# all are boolean
24524 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24525 +aufs-$(CONFIG_SYSFS) += sysfs.o
24526 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24527 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24528 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24529 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24530 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24531 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24532 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24533 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24534 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24535 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24536 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24537 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24538 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24539 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24540 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24541 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24542 +++ linux/fs/aufs/module.c      2019-07-11 15:42:14.468904634 +0200
24543 @@ -0,0 +1,273 @@
24544 +// SPDX-License-Identifier: GPL-2.0
24545 +/*
24546 + * Copyright (C) 2005-2019 Junjiro R. Okajima
24547 + *
24548 + * This program, aufs is free software; you can redistribute it and/or modify
24549 + * it under the terms of the GNU General Public License as published by
24550 + * the Free Software Foundation; either version 2 of the License, or
24551 + * (at your option) any later version.
24552 + *
24553 + * This program is distributed in the hope that it will be useful,
24554 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24555 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24556 + * GNU General Public License for more details.
24557 + *
24558 + * You should have received a copy of the GNU General Public License
24559 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24560 + */
24561 +
24562 +/*
24563 + * module global variables and operations
24564 + */
24565 +
24566 +#include <linux/module.h>
24567 +#include <linux/seq_file.h>
24568 +#include "aufs.h"
24569 +
24570 +/* shrinkable realloc */
24571 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24572 +{
24573 +       size_t sz;
24574 +       int diff;
24575 +
24576 +       sz = 0;
24577 +       diff = -1;
24578 +       if (p) {
24579 +#if 0 /* unused */
24580 +               if (!new_sz) {
24581 +                       au_kfree_rcu(p);
24582 +                       p = NULL;
24583 +                       goto out;
24584 +               }
24585 +#else
24586 +               AuDebugOn(!new_sz);
24587 +#endif
24588 +               sz = ksize(p);
24589 +               diff = au_kmidx_sub(sz, new_sz);
24590 +       }
24591 +       if (sz && !diff)
24592 +               goto out;
24593 +
24594 +       if (sz < new_sz)
24595 +               /* expand or SLOB */
24596 +               p = krealloc(p, new_sz, gfp);
24597 +       else if (new_sz < sz && may_shrink) {
24598 +               /* shrink */
24599 +               void *q;
24600 +
24601 +               q = kmalloc(new_sz, gfp);
24602 +               if (q) {
24603 +                       if (p) {
24604 +                               memcpy(q, p, new_sz);
24605 +                               au_kfree_try_rcu(p);
24606 +                       }
24607 +                       p = q;
24608 +               } else
24609 +                       p = NULL;
24610 +       }
24611 +
24612 +out:
24613 +       return p;
24614 +}
24615 +
24616 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24617 +                  int may_shrink)
24618 +{
24619 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24620 +       if (p && new_sz > nused)
24621 +               memset(p + nused, 0, new_sz - nused);
24622 +       return p;
24623 +}
24624 +
24625 +/* ---------------------------------------------------------------------- */
24626 +/*
24627 + * aufs caches
24628 + */
24629 +struct kmem_cache *au_cache[AuCache_Last];
24630 +
24631 +static void au_cache_fin(void)
24632 +{
24633 +       int i;
24634 +
24635 +       /*
24636 +        * Make sure all delayed rcu free inodes are flushed before we
24637 +        * destroy cache.
24638 +        */
24639 +       rcu_barrier();
24640 +
24641 +       /* excluding AuCache_HNOTIFY */
24642 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24643 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24644 +               kmem_cache_destroy(au_cache[i]);
24645 +               au_cache[i] = NULL;
24646 +       }
24647 +}
24648 +
24649 +static int __init au_cache_init(void)
24650 +{
24651 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24652 +       if (au_cache[AuCache_DINFO])
24653 +               /* SLAB_DESTROY_BY_RCU */
24654 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24655 +                                                      au_icntnr_init_once);
24656 +       if (au_cache[AuCache_ICNTNR])
24657 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24658 +                                                     au_fi_init_once);
24659 +       if (au_cache[AuCache_FINFO])
24660 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24661 +       if (au_cache[AuCache_VDIR])
24662 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24663 +       if (au_cache[AuCache_DEHSTR])
24664 +               return 0;
24665 +
24666 +       au_cache_fin();
24667 +       return -ENOMEM;
24668 +}
24669 +
24670 +/* ---------------------------------------------------------------------- */
24671 +
24672 +int au_dir_roflags;
24673 +
24674 +#ifdef CONFIG_AUFS_SBILIST
24675 +/*
24676 + * iterate_supers_type() doesn't protect us from
24677 + * remounting (branch management)
24678 + */
24679 +struct hlist_bl_head au_sbilist;
24680 +#endif
24681 +
24682 +/*
24683 + * functions for module interface.
24684 + */
24685 +MODULE_LICENSE("GPL");
24686 +/* MODULE_LICENSE("GPL v2"); */
24687 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24688 +MODULE_DESCRIPTION(AUFS_NAME
24689 +       " -- Advanced multi layered unification filesystem");
24690 +MODULE_VERSION(AUFS_VERSION);
24691 +MODULE_ALIAS_FS(AUFS_NAME);
24692 +
24693 +/* this module parameter has no meaning when SYSFS is disabled */
24694 +int sysaufs_brs = 1;
24695 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24696 +module_param_named(brs, sysaufs_brs, int, 0444);
24697 +
24698 +/* this module parameter has no meaning when USER_NS is disabled */
24699 +bool au_userns;
24700 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24701 +module_param_named(allow_userns, au_userns, bool, 0444);
24702 +
24703 +/* ---------------------------------------------------------------------- */
24704 +
24705 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24706 +
24707 +int au_seq_path(struct seq_file *seq, struct path *path)
24708 +{
24709 +       int err;
24710 +
24711 +       err = seq_path(seq, path, au_esc_chars);
24712 +       if (err >= 0)
24713 +               err = 0;
24714 +       else
24715 +               err = -ENOMEM;
24716 +
24717 +       return err;
24718 +}
24719 +
24720 +/* ---------------------------------------------------------------------- */
24721 +
24722 +static int __init aufs_init(void)
24723 +{
24724 +       int err, i;
24725 +       char *p;
24726 +
24727 +       p = au_esc_chars;
24728 +       for (i = 1; i <= ' '; i++)
24729 +               *p++ = i;
24730 +       *p++ = '\\';
24731 +       *p++ = '\x7f';
24732 +       *p = 0;
24733 +
24734 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24735 +
24736 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24737 +       for (i = 0; i < AuIop_Last; i++)
24738 +               aufs_iop_nogetattr[i].getattr = NULL;
24739 +
24740 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24741 +
24742 +       au_sbilist_init();
24743 +       sysaufs_brs_init();
24744 +       au_debug_init();
24745 +       au_dy_init();
24746 +       err = sysaufs_init();
24747 +       if (unlikely(err))
24748 +               goto out;
24749 +       err = dbgaufs_init();
24750 +       if (unlikely(err))
24751 +               goto out_sysaufs;
24752 +       err = au_procfs_init();
24753 +       if (unlikely(err))
24754 +               goto out_dbgaufs;
24755 +       err = au_wkq_init();
24756 +       if (unlikely(err))
24757 +               goto out_procfs;
24758 +       err = au_loopback_init();
24759 +       if (unlikely(err))
24760 +               goto out_wkq;
24761 +       err = au_hnotify_init();
24762 +       if (unlikely(err))
24763 +               goto out_loopback;
24764 +       err = au_sysrq_init();
24765 +       if (unlikely(err))
24766 +               goto out_hin;
24767 +       err = au_cache_init();
24768 +       if (unlikely(err))
24769 +               goto out_sysrq;
24770 +
24771 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24772 +       err = register_filesystem(&aufs_fs_type);
24773 +       if (unlikely(err))
24774 +               goto out_cache;
24775 +
24776 +       /* since we define pr_fmt, call printk directly */
24777 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24778 +       goto out; /* success */
24779 +
24780 +out_cache:
24781 +       au_cache_fin();
24782 +out_sysrq:
24783 +       au_sysrq_fin();
24784 +out_hin:
24785 +       au_hnotify_fin();
24786 +out_loopback:
24787 +       au_loopback_fin();
24788 +out_wkq:
24789 +       au_wkq_fin();
24790 +out_procfs:
24791 +       au_procfs_fin();
24792 +out_dbgaufs:
24793 +       dbgaufs_fin();
24794 +out_sysaufs:
24795 +       sysaufs_fin();
24796 +       au_dy_fin();
24797 +out:
24798 +       return err;
24799 +}
24800 +
24801 +static void __exit aufs_exit(void)
24802 +{
24803 +       unregister_filesystem(&aufs_fs_type);
24804 +       au_cache_fin();
24805 +       au_sysrq_fin();
24806 +       au_hnotify_fin();
24807 +       au_loopback_fin();
24808 +       au_wkq_fin();
24809 +       au_procfs_fin();
24810 +       dbgaufs_fin();
24811 +       sysaufs_fin();
24812 +       au_dy_fin();
24813 +}
24814 +
24815 +module_init(aufs_init);
24816 +module_exit(aufs_exit);
24817 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24818 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24819 +++ linux/fs/aufs/module.h      2019-07-11 15:42:14.468904634 +0200
24820 @@ -0,0 +1,166 @@
24821 +/* SPDX-License-Identifier: GPL-2.0 */
24822 +/*
24823 + * Copyright (C) 2005-2019 Junjiro R. Okajima
24824 + *
24825 + * This program, aufs is free software; you can redistribute it and/or modify
24826 + * it under the terms of the GNU General Public License as published by
24827 + * the Free Software Foundation; either version 2 of the License, or
24828 + * (at your option) any later version.
24829 + *
24830 + * This program is distributed in the hope that it will be useful,
24831 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24832 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24833 + * GNU General Public License for more details.
24834 + *
24835 + * You should have received a copy of the GNU General Public License
24836 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24837 + */
24838 +
24839 +/*
24840 + * module initialization and module-global
24841 + */
24842 +
24843 +#ifndef __AUFS_MODULE_H__
24844 +#define __AUFS_MODULE_H__
24845 +
24846 +#ifdef __KERNEL__
24847 +
24848 +#include <linux/slab.h>
24849 +#include "debug.h"
24850 +#include "dentry.h"
24851 +#include "dir.h"
24852 +#include "file.h"
24853 +#include "inode.h"
24854 +
24855 +struct path;
24856 +struct seq_file;
24857 +
24858 +/* module parameters */
24859 +extern int sysaufs_brs;
24860 +extern bool au_userns;
24861 +
24862 +/* ---------------------------------------------------------------------- */
24863 +
24864 +extern int au_dir_roflags;
24865 +
24866 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24867 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24868 +                  int may_shrink);
24869 +
24870 +/*
24871 + * Comparing the size of the object with sizeof(struct rcu_head)
24872 + * case 1: object is always larger
24873 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24874 + * case 2: object is always smaller
24875 + *     --> au_kfree_small()
24876 + * case 3: object can be any size
24877 + *     --> au_kfree_try_rcu()
24878 + */
24879 +
24880 +static inline void au_kfree_do_rcu(const void *p)
24881 +{
24882 +       struct {
24883 +               struct rcu_head rcu;
24884 +       } *a = (void *)p;
24885 +
24886 +       kfree_rcu(a, rcu);
24887 +}
24888 +
24889 +#define au_kfree_rcu(_p) do {                                          \
24890 +               typeof(_p) p = (_p);                                    \
24891 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24892 +               if (p)                                                  \
24893 +                       au_kfree_do_rcu(p);                             \
24894 +       } while (0)
24895 +
24896 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24897 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24898 +
24899 +static inline void au_kfree_try_rcu(const void *p)
24900 +{
24901 +       if (!p)
24902 +               return;
24903 +       if (au_kfree_sz_test(p))
24904 +               au_kfree_do_rcu(p);
24905 +       else
24906 +               kfree(p);
24907 +}
24908 +
24909 +static inline void au_kfree_small(const void *p)
24910 +{
24911 +       if (!p)
24912 +               return;
24913 +       AuDebugOn(au_kfree_sz_test(p));
24914 +       kfree(p);
24915 +}
24916 +
24917 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24918 +{
24919 +#ifndef CONFIG_SLOB
24920 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24921 +#else
24922 +       return -1; /* SLOB is untested */
24923 +#endif
24924 +}
24925 +
24926 +int au_seq_path(struct seq_file *seq, struct path *path);
24927 +
24928 +#ifdef CONFIG_PROC_FS
24929 +/* procfs.c */
24930 +int __init au_procfs_init(void);
24931 +void au_procfs_fin(void);
24932 +#else
24933 +AuStubInt0(au_procfs_init, void);
24934 +AuStubVoid(au_procfs_fin, void);
24935 +#endif
24936 +
24937 +/* ---------------------------------------------------------------------- */
24938 +
24939 +/* kmem cache */
24940 +enum {
24941 +       AuCache_DINFO,
24942 +       AuCache_ICNTNR,
24943 +       AuCache_FINFO,
24944 +       AuCache_VDIR,
24945 +       AuCache_DEHSTR,
24946 +       AuCache_HNOTIFY, /* must be last */
24947 +       AuCache_Last
24948 +};
24949 +
24950 +extern struct kmem_cache *au_cache[AuCache_Last];
24951 +
24952 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24953 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24954 +#define AuCacheCtor(type, ctor)        \
24955 +       kmem_cache_create(#type, sizeof(struct type), \
24956 +                         __alignof__(struct type), AuCacheFlags, ctor)
24957 +
24958 +#define AuCacheFuncs(name, index)                                      \
24959 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24960 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24961 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24962 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24963 +                                                                       \
24964 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24965 +       { void *p = rcu;                                                \
24966 +               p -= offsetof(struct au_##name, rcu);                   \
24967 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24968 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24969 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24970 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24971 +                                                                       \
24972 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24973 +       { /* au_cache_free_##name##_norcu(p); */                        \
24974 +               au_cache_free_##name##_rcu(p); }
24975 +
24976 +AuCacheFuncs(dinfo, DINFO);
24977 +AuCacheFuncs(icntnr, ICNTNR);
24978 +AuCacheFuncs(finfo, FINFO);
24979 +AuCacheFuncs(vdir, VDIR);
24980 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24981 +#ifdef CONFIG_AUFS_HNOTIFY
24982 +AuCacheFuncs(hnotify, HNOTIFY);
24983 +#endif
24984 +
24985 +#endif /* __KERNEL__ */
24986 +#endif /* __AUFS_MODULE_H__ */
24987 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24988 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24989 +++ linux/fs/aufs/mvdown.c      2019-07-11 15:42:14.472238057 +0200
24990 @@ -0,0 +1,706 @@
24991 +// SPDX-License-Identifier: GPL-2.0
24992 +/*
24993 + * Copyright (C) 2011-2019 Junjiro R. Okajima
24994 + *
24995 + * This program, aufs is free software; you can redistribute it and/or modify
24996 + * it under the terms of the GNU General Public License as published by
24997 + * the Free Software Foundation; either version 2 of the License, or
24998 + * (at your option) any later version.
24999 + *
25000 + * This program is distributed in the hope that it will be useful,
25001 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25002 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25003 + * GNU General Public License for more details.
25004 + *
25005 + * You should have received a copy of the GNU General Public License
25006 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25007 + */
25008 +
25009 +/*
25010 + * move-down, opposite of copy-up
25011 + */
25012 +
25013 +#include "aufs.h"
25014 +
25015 +struct au_mvd_args {
25016 +       struct {
25017 +               struct super_block *h_sb;
25018 +               struct dentry *h_parent;
25019 +               struct au_hinode *hdir;
25020 +               struct inode *h_dir, *h_inode;
25021 +               struct au_pin pin;
25022 +       } info[AUFS_MVDOWN_NARRAY];
25023 +
25024 +       struct aufs_mvdown mvdown;
25025 +       struct dentry *dentry, *parent;
25026 +       struct inode *inode, *dir;
25027 +       struct super_block *sb;
25028 +       aufs_bindex_t bopq, bwh, bfound;
25029 +       unsigned char rename_lock;
25030 +};
25031 +
25032 +#define mvd_errno              mvdown.au_errno
25033 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25034 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25035 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25036 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
25037 +
25038 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
25039 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
25040 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
25041 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
25042 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
25043 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
25044 +
25045 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
25046 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
25047 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
25048 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
25049 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
25050 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
25051 +
25052 +#define AU_MVD_PR(flag, ...) do {                      \
25053 +               if (flag)                               \
25054 +                       pr_err(__VA_ARGS__);            \
25055 +       } while (0)
25056 +
25057 +static int find_lower_writable(struct au_mvd_args *a)
25058 +{
25059 +       struct super_block *sb;
25060 +       aufs_bindex_t bindex, bbot;
25061 +       struct au_branch *br;
25062 +
25063 +       sb = a->sb;
25064 +       bindex = a->mvd_bsrc;
25065 +       bbot = au_sbbot(sb);
25066 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
25067 +               for (bindex++; bindex <= bbot; bindex++) {
25068 +                       br = au_sbr(sb, bindex);
25069 +                       if (au_br_fhsm(br->br_perm)
25070 +                           && !sb_rdonly(au_br_sb(br)))
25071 +                               return bindex;
25072 +               }
25073 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
25074 +               for (bindex++; bindex <= bbot; bindex++) {
25075 +                       br = au_sbr(sb, bindex);
25076 +                       if (!au_br_rdonly(br))
25077 +                               return bindex;
25078 +               }
25079 +       else
25080 +               for (bindex++; bindex <= bbot; bindex++) {
25081 +                       br = au_sbr(sb, bindex);
25082 +                       if (!sb_rdonly(au_br_sb(br))) {
25083 +                               if (au_br_rdonly(br))
25084 +                                       a->mvdown.flags
25085 +                                               |= AUFS_MVDOWN_ROLOWER_R;
25086 +                               return bindex;
25087 +                       }
25088 +               }
25089 +
25090 +       return -1;
25091 +}
25092 +
25093 +/* make the parent dir on bdst */
25094 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25095 +{
25096 +       int err;
25097 +
25098 +       err = 0;
25099 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25100 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25101 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25102 +       a->mvd_h_dst_parent = NULL;
25103 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
25104 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25105 +       if (!a->mvd_h_dst_parent) {
25106 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25107 +               if (unlikely(err)) {
25108 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25109 +                       goto out;
25110 +               }
25111 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25112 +       }
25113 +
25114 +out:
25115 +       AuTraceErr(err);
25116 +       return err;
25117 +}
25118 +
25119 +/* lock them all */
25120 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25121 +{
25122 +       int err;
25123 +       struct dentry *h_trap;
25124 +
25125 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25126 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25127 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25128 +                    au_opt_udba(a->sb),
25129 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25130 +       AuTraceErr(err);
25131 +       if (unlikely(err)) {
25132 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25133 +               goto out;
25134 +       }
25135 +
25136 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25137 +               a->rename_lock = 0;
25138 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25139 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25140 +                           au_opt_udba(a->sb),
25141 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25142 +               err = au_do_pin(&a->mvd_pin_src);
25143 +               AuTraceErr(err);
25144 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25145 +               if (unlikely(err)) {
25146 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25147 +                       goto out_dst;
25148 +               }
25149 +               goto out; /* success */
25150 +       }
25151 +
25152 +       a->rename_lock = 1;
25153 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25154 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25155 +                    au_opt_udba(a->sb),
25156 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25157 +       AuTraceErr(err);
25158 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25159 +       if (unlikely(err)) {
25160 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25161 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25162 +               goto out_dst;
25163 +       }
25164 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25165 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25166 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25167 +       if (h_trap) {
25168 +               err = (h_trap != a->mvd_h_src_parent);
25169 +               if (err)
25170 +                       err = (h_trap != a->mvd_h_dst_parent);
25171 +       }
25172 +       BUG_ON(err); /* it should never happen */
25173 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25174 +               err = -EBUSY;
25175 +               AuTraceErr(err);
25176 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25177 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25178 +               au_pin_hdir_lock(&a->mvd_pin_src);
25179 +               au_unpin(&a->mvd_pin_src);
25180 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25181 +               goto out_dst;
25182 +       }
25183 +       goto out; /* success */
25184 +
25185 +out_dst:
25186 +       au_unpin(&a->mvd_pin_dst);
25187 +out:
25188 +       AuTraceErr(err);
25189 +       return err;
25190 +}
25191 +
25192 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25193 +{
25194 +       if (!a->rename_lock)
25195 +               au_unpin(&a->mvd_pin_src);
25196 +       else {
25197 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25198 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25199 +               au_pin_hdir_lock(&a->mvd_pin_src);
25200 +               au_unpin(&a->mvd_pin_src);
25201 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25202 +       }
25203 +       au_unpin(&a->mvd_pin_dst);
25204 +}
25205 +
25206 +/* copy-down the file */
25207 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25208 +{
25209 +       int err;
25210 +       struct au_cp_generic cpg = {
25211 +               .dentry = a->dentry,
25212 +               .bdst   = a->mvd_bdst,
25213 +               .bsrc   = a->mvd_bsrc,
25214 +               .len    = -1,
25215 +               .pin    = &a->mvd_pin_dst,
25216 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25217 +       };
25218 +
25219 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25220 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25221 +               au_fset_cpup(cpg.flags, OVERWRITE);
25222 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25223 +               au_fset_cpup(cpg.flags, RWDST);
25224 +       err = au_sio_cpdown_simple(&cpg);
25225 +       if (unlikely(err))
25226 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25227 +
25228 +       AuTraceErr(err);
25229 +       return err;
25230 +}
25231 +
25232 +/*
25233 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25234 + * were sleeping
25235 + */
25236 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25237 +{
25238 +       int err;
25239 +       struct path h_path;
25240 +       struct au_branch *br;
25241 +       struct inode *delegated;
25242 +
25243 +       br = au_sbr(a->sb, a->mvd_bdst);
25244 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25245 +       err = PTR_ERR(h_path.dentry);
25246 +       if (IS_ERR(h_path.dentry)) {
25247 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25248 +               goto out;
25249 +       }
25250 +
25251 +       err = 0;
25252 +       if (d_is_positive(h_path.dentry)) {
25253 +               h_path.mnt = au_br_mnt(br);
25254 +               delegated = NULL;
25255 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25256 +                                  &delegated, /*force*/0);
25257 +               if (unlikely(err == -EWOULDBLOCK)) {
25258 +                       pr_warn("cannot retry for NFSv4 delegation"
25259 +                               " for an internal unlink\n");
25260 +                       iput(delegated);
25261 +               }
25262 +               if (unlikely(err))
25263 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25264 +       }
25265 +       dput(h_path.dentry);
25266 +
25267 +out:
25268 +       AuTraceErr(err);
25269 +       return err;
25270 +}
25271 +
25272 +/*
25273 + * unlink the topmost h_dentry
25274 + */
25275 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25276 +{
25277 +       int err;
25278 +       struct path h_path;
25279 +       struct inode *delegated;
25280 +
25281 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25282 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25283 +       delegated = NULL;
25284 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25285 +       if (unlikely(err == -EWOULDBLOCK)) {
25286 +               pr_warn("cannot retry for NFSv4 delegation"
25287 +                       " for an internal unlink\n");
25288 +               iput(delegated);
25289 +       }
25290 +       if (unlikely(err))
25291 +               AU_MVD_PR(dmsg, "unlink failed\n");
25292 +
25293 +       AuTraceErr(err);
25294 +       return err;
25295 +}
25296 +
25297 +/* Since mvdown succeeded, we ignore an error of this function */
25298 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25299 +{
25300 +       int err;
25301 +       struct au_branch *br;
25302 +
25303 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25304 +       br = au_sbr(a->sb, a->mvd_bsrc);
25305 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25306 +       if (!err) {
25307 +               br = au_sbr(a->sb, a->mvd_bdst);
25308 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25309 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25310 +       }
25311 +       if (!err)
25312 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25313 +       else
25314 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25315 +}
25316 +
25317 +/*
25318 + * copy-down the file and unlink the bsrc file.
25319 + * - unlink the bdst whout if exist
25320 + * - copy-down the file (with whtmp name and rename)
25321 + * - unlink the bsrc file
25322 + */
25323 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25324 +{
25325 +       int err;
25326 +
25327 +       err = au_do_mkdir(dmsg, a);
25328 +       if (!err)
25329 +               err = au_do_lock(dmsg, a);
25330 +       if (unlikely(err))
25331 +               goto out;
25332 +
25333 +       /*
25334 +        * do not revert the activities we made on bdst since they should be
25335 +        * harmless in aufs.
25336 +        */
25337 +
25338 +       err = au_do_cpdown(dmsg, a);
25339 +       if (!err)
25340 +               err = au_do_unlink_wh(dmsg, a);
25341 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25342 +               err = au_do_unlink(dmsg, a);
25343 +       if (unlikely(err))
25344 +               goto out_unlock;
25345 +
25346 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25347 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25348 +       if (find_lower_writable(a) < 0)
25349 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25350 +
25351 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25352 +               au_do_stfs(dmsg, a);
25353 +
25354 +       /* maintain internal array */
25355 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25356 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25357 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25358 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25359 +               au_set_ibtop(a->inode, a->mvd_bdst);
25360 +       } else {
25361 +               /* hide the lower */
25362 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25363 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25364 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25365 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25366 +       }
25367 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25368 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25369 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25370 +               au_set_ibbot(a->inode, a->mvd_bdst);
25371 +
25372 +out_unlock:
25373 +       au_do_unlock(dmsg, a);
25374 +out:
25375 +       AuTraceErr(err);
25376 +       return err;
25377 +}
25378 +
25379 +/* ---------------------------------------------------------------------- */
25380 +
25381 +/* make sure the file is idle */
25382 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25383 +{
25384 +       int err, plinked;
25385 +
25386 +       err = 0;
25387 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25388 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25389 +           && au_dcount(a->dentry) == 1
25390 +           && atomic_read(&a->inode->i_count) == 1
25391 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25392 +           && (!plinked || !au_plink_test(a->inode))
25393 +           && a->inode->i_nlink == 1)
25394 +               goto out;
25395 +
25396 +       err = -EBUSY;
25397 +       AU_MVD_PR(dmsg,
25398 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25399 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25400 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25401 +                 a->mvd_h_src_inode->i_nlink,
25402 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25403 +
25404 +out:
25405 +       AuTraceErr(err);
25406 +       return err;
25407 +}
25408 +
25409 +/* make sure the parent dir is fine */
25410 +static int au_mvd_args_parent(const unsigned char dmsg,
25411 +                             struct au_mvd_args *a)
25412 +{
25413 +       int err;
25414 +       aufs_bindex_t bindex;
25415 +
25416 +       err = 0;
25417 +       if (unlikely(au_alive_dir(a->parent))) {
25418 +               err = -ENOENT;
25419 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25420 +               goto out;
25421 +       }
25422 +
25423 +       a->bopq = au_dbdiropq(a->parent);
25424 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25425 +       AuDbg("b%d\n", bindex);
25426 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25427 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25428 +               err = -EINVAL;
25429 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25430 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25431 +                         a->bopq, a->mvd_bdst);
25432 +       }
25433 +
25434 +out:
25435 +       AuTraceErr(err);
25436 +       return err;
25437 +}
25438 +
25439 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25440 +                                   struct au_mvd_args *a)
25441 +{
25442 +       int err;
25443 +       struct au_dinfo *dinfo, *tmp;
25444 +
25445 +       /* lookup the next lower positive entry */
25446 +       err = -ENOMEM;
25447 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25448 +       if (unlikely(!tmp))
25449 +               goto out;
25450 +
25451 +       a->bfound = -1;
25452 +       a->bwh = -1;
25453 +       dinfo = au_di(a->dentry);
25454 +       au_di_cp(tmp, dinfo);
25455 +       au_di_swap(tmp, dinfo);
25456 +
25457 +       /* returns the number of positive dentries */
25458 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25459 +                            /* AuLkup_IGNORE_PERM */ 0);
25460 +       if (!err)
25461 +               a->bwh = au_dbwh(a->dentry);
25462 +       else if (err > 0)
25463 +               a->bfound = au_dbtop(a->dentry);
25464 +
25465 +       au_di_swap(tmp, dinfo);
25466 +       au_rw_write_unlock(&tmp->di_rwsem);
25467 +       au_di_free(tmp);
25468 +       if (unlikely(err < 0))
25469 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25470 +
25471 +       /*
25472 +        * here, we have these cases.
25473 +        * bfound == -1
25474 +        *      no positive dentry under bsrc. there are more sub-cases.
25475 +        *      bwh < 0
25476 +        *              there no whiteout, we can safely move-down.
25477 +        *      bwh <= bsrc
25478 +        *              impossible
25479 +        *      bsrc < bwh && bwh < bdst
25480 +        *              there is a whiteout on RO branch. cannot proceed.
25481 +        *      bwh == bdst
25482 +        *              there is a whiteout on the RW target branch. it should
25483 +        *              be removed.
25484 +        *      bdst < bwh
25485 +        *              there is a whiteout somewhere unrelated branch.
25486 +        * -1 < bfound && bfound <= bsrc
25487 +        *      impossible.
25488 +        * bfound < bdst
25489 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25490 +        *      proceed.
25491 +        * bfound == bdst
25492 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25493 +        *      error.
25494 +        * bdst < bfound
25495 +        *      found, after we create the file on bdst, it will be hidden.
25496 +        */
25497 +
25498 +       AuDebugOn(a->bfound == -1
25499 +                 && a->bwh != -1
25500 +                 && a->bwh <= a->mvd_bsrc);
25501 +       AuDebugOn(-1 < a->bfound
25502 +                 && a->bfound <= a->mvd_bsrc);
25503 +
25504 +       err = -EINVAL;
25505 +       if (a->bfound == -1
25506 +           && a->mvd_bsrc < a->bwh
25507 +           && a->bwh != -1
25508 +           && a->bwh < a->mvd_bdst) {
25509 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25510 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25511 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25512 +               goto out;
25513 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25514 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25515 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25516 +                         a->mvd_bdst, a->bfound);
25517 +               goto out;
25518 +       }
25519 +
25520 +       err = 0; /* success */
25521 +
25522 +out:
25523 +       AuTraceErr(err);
25524 +       return err;
25525 +}
25526 +
25527 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25528 +{
25529 +       int err;
25530 +
25531 +       err = 0;
25532 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25533 +           && a->bfound == a->mvd_bdst)
25534 +               err = -EEXIST;
25535 +       AuTraceErr(err);
25536 +       return err;
25537 +}
25538 +
25539 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25540 +{
25541 +       int err;
25542 +       struct au_branch *br;
25543 +
25544 +       err = -EISDIR;
25545 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25546 +               goto out;
25547 +
25548 +       err = -EINVAL;
25549 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25550 +               a->mvd_bsrc = au_ibtop(a->inode);
25551 +       else {
25552 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25553 +               if (unlikely(a->mvd_bsrc < 0
25554 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25555 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25556 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25557 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25558 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25559 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25560 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25561 +                       AU_MVD_PR(dmsg, "no upper\n");
25562 +                       goto out;
25563 +               }
25564 +       }
25565 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25566 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25567 +               AU_MVD_PR(dmsg, "on the bottom\n");
25568 +               goto out;
25569 +       }
25570 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25571 +       br = au_sbr(a->sb, a->mvd_bsrc);
25572 +       err = au_br_rdonly(br);
25573 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25574 +               if (unlikely(err))
25575 +                       goto out;
25576 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25577 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25578 +               if (err)
25579 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25580 +               /* go on */
25581 +       } else
25582 +               goto out;
25583 +
25584 +       err = -EINVAL;
25585 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25586 +               a->mvd_bdst = find_lower_writable(a);
25587 +               if (unlikely(a->mvd_bdst < 0)) {
25588 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25589 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25590 +                       goto out;
25591 +               }
25592 +       } else {
25593 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25594 +               if (unlikely(a->mvd_bdst < 0
25595 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25596 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25597 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25598 +                       goto out;
25599 +               }
25600 +       }
25601 +
25602 +       err = au_mvd_args_busy(dmsg, a);
25603 +       if (!err)
25604 +               err = au_mvd_args_parent(dmsg, a);
25605 +       if (!err)
25606 +               err = au_mvd_args_intermediate(dmsg, a);
25607 +       if (!err)
25608 +               err = au_mvd_args_exist(dmsg, a);
25609 +       if (!err)
25610 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25611 +
25612 +out:
25613 +       AuTraceErr(err);
25614 +       return err;
25615 +}
25616 +
25617 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25618 +{
25619 +       int err, e;
25620 +       unsigned char dmsg;
25621 +       struct au_mvd_args *args;
25622 +       struct inode *inode;
25623 +
25624 +       inode = d_inode(dentry);
25625 +       err = -EPERM;
25626 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25627 +               goto out;
25628 +
25629 +       err = -ENOMEM;
25630 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25631 +       if (unlikely(!args))
25632 +               goto out;
25633 +
25634 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25635 +       if (!err)
25636 +               /* VERIFY_WRITE */
25637 +               err = !access_ok(uarg, sizeof(*uarg));
25638 +       if (unlikely(err)) {
25639 +               err = -EFAULT;
25640 +               AuTraceErr(err);
25641 +               goto out_free;
25642 +       }
25643 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25644 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25645 +       args->mvdown.au_errno = 0;
25646 +       args->dentry = dentry;
25647 +       args->inode = inode;
25648 +       args->sb = dentry->d_sb;
25649 +
25650 +       err = -ENOENT;
25651 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25652 +       args->parent = dget_parent(dentry);
25653 +       args->dir = d_inode(args->parent);
25654 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25655 +       dput(args->parent);
25656 +       if (unlikely(args->parent != dentry->d_parent)) {
25657 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25658 +               goto out_dir;
25659 +       }
25660 +
25661 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25662 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25663 +       if (unlikely(err))
25664 +               goto out_inode;
25665 +
25666 +       di_write_lock_parent(args->parent);
25667 +       err = au_mvd_args(dmsg, args);
25668 +       if (unlikely(err))
25669 +               goto out_parent;
25670 +
25671 +       err = au_do_mvdown(dmsg, args);
25672 +       if (unlikely(err))
25673 +               goto out_parent;
25674 +
25675 +       au_cpup_attr_timesizes(args->dir);
25676 +       au_cpup_attr_timesizes(inode);
25677 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25678 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25679 +       /* au_digen_dec(dentry); */
25680 +
25681 +out_parent:
25682 +       di_write_unlock(args->parent);
25683 +       aufs_read_unlock(dentry, AuLock_DW);
25684 +out_inode:
25685 +       inode_unlock(inode);
25686 +out_dir:
25687 +       inode_unlock(args->dir);
25688 +out_free:
25689 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25690 +       if (unlikely(e))
25691 +               err = -EFAULT;
25692 +       au_kfree_rcu(args);
25693 +out:
25694 +       AuTraceErr(err);
25695 +       return err;
25696 +}
25697 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25698 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25699 +++ linux/fs/aufs/opts.c        2019-09-16 09:38:43.216175640 +0200
25700 @@ -0,0 +1,1880 @@
25701 +// SPDX-License-Identifier: GPL-2.0
25702 +/*
25703 + * Copyright (C) 2005-2019 Junjiro R. Okajima
25704 + *
25705 + * This program, aufs is free software; you can redistribute it and/or modify
25706 + * it under the terms of the GNU General Public License as published by
25707 + * the Free Software Foundation; either version 2 of the License, or
25708 + * (at your option) any later version.
25709 + *
25710 + * This program is distributed in the hope that it will be useful,
25711 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25712 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25713 + * GNU General Public License for more details.
25714 + *
25715 + * You should have received a copy of the GNU General Public License
25716 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25717 + */
25718 +
25719 +/*
25720 + * mount options/flags
25721 + */
25722 +
25723 +#include <linux/namei.h>
25724 +#include <linux/types.h> /* a distribution requires */
25725 +#include <linux/parser.h>
25726 +#include "aufs.h"
25727 +
25728 +/* ---------------------------------------------------------------------- */
25729 +
25730 +enum {
25731 +       Opt_br,
25732 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25733 +       Opt_idel, Opt_imod,
25734 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25735 +       Opt_rdblk_def, Opt_rdhash_def,
25736 +       Opt_xino, Opt_noxino,
25737 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25738 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25739 +       Opt_trunc_xib, Opt_notrunc_xib,
25740 +       Opt_shwh, Opt_noshwh,
25741 +       Opt_plink, Opt_noplink, Opt_list_plink,
25742 +       Opt_udba,
25743 +       Opt_dio, Opt_nodio,
25744 +       Opt_diropq_a, Opt_diropq_w,
25745 +       Opt_warn_perm, Opt_nowarn_perm,
25746 +       Opt_wbr_copyup, Opt_wbr_create,
25747 +       Opt_fhsm_sec,
25748 +       Opt_verbose, Opt_noverbose,
25749 +       Opt_sum, Opt_nosum, Opt_wsum,
25750 +       Opt_dirperm1, Opt_nodirperm1,
25751 +       Opt_dirren, Opt_nodirren,
25752 +       Opt_acl, Opt_noacl,
25753 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25754 +};
25755 +
25756 +static match_table_t options = {
25757 +       {Opt_br, "br=%s"},
25758 +       {Opt_br, "br:%s"},
25759 +
25760 +       {Opt_add, "add=%d:%s"},
25761 +       {Opt_add, "add:%d:%s"},
25762 +       {Opt_add, "ins=%d:%s"},
25763 +       {Opt_add, "ins:%d:%s"},
25764 +       {Opt_append, "append=%s"},
25765 +       {Opt_append, "append:%s"},
25766 +       {Opt_prepend, "prepend=%s"},
25767 +       {Opt_prepend, "prepend:%s"},
25768 +
25769 +       {Opt_del, "del=%s"},
25770 +       {Opt_del, "del:%s"},
25771 +       /* {Opt_idel, "idel:%d"}, */
25772 +       {Opt_mod, "mod=%s"},
25773 +       {Opt_mod, "mod:%s"},
25774 +       /* {Opt_imod, "imod:%d:%s"}, */
25775 +
25776 +       {Opt_dirwh, "dirwh=%d"},
25777 +
25778 +       {Opt_xino, "xino=%s"},
25779 +       {Opt_noxino, "noxino"},
25780 +       {Opt_trunc_xino, "trunc_xino"},
25781 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25782 +       {Opt_notrunc_xino, "notrunc_xino"},
25783 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25784 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25785 +       /* {Opt_zxino, "zxino=%s"}, */
25786 +       {Opt_trunc_xib, "trunc_xib"},
25787 +       {Opt_notrunc_xib, "notrunc_xib"},
25788 +
25789 +#ifdef CONFIG_PROC_FS
25790 +       {Opt_plink, "plink"},
25791 +#else
25792 +       {Opt_ignore_silent, "plink"},
25793 +#endif
25794 +
25795 +       {Opt_noplink, "noplink"},
25796 +
25797 +#ifdef CONFIG_AUFS_DEBUG
25798 +       {Opt_list_plink, "list_plink"},
25799 +#endif
25800 +
25801 +       {Opt_udba, "udba=%s"},
25802 +
25803 +       {Opt_dio, "dio"},
25804 +       {Opt_nodio, "nodio"},
25805 +
25806 +#ifdef CONFIG_AUFS_DIRREN
25807 +       {Opt_dirren, "dirren"},
25808 +       {Opt_nodirren, "nodirren"},
25809 +#else
25810 +       {Opt_ignore, "dirren"},
25811 +       {Opt_ignore_silent, "nodirren"},
25812 +#endif
25813 +
25814 +#ifdef CONFIG_AUFS_FHSM
25815 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25816 +#else
25817 +       {Opt_ignore, "fhsm_sec=%d"},
25818 +#endif
25819 +
25820 +       {Opt_diropq_a, "diropq=always"},
25821 +       {Opt_diropq_a, "diropq=a"},
25822 +       {Opt_diropq_w, "diropq=whiteouted"},
25823 +       {Opt_diropq_w, "diropq=w"},
25824 +
25825 +       {Opt_warn_perm, "warn_perm"},
25826 +       {Opt_nowarn_perm, "nowarn_perm"},
25827 +
25828 +       /* keep them temporary */
25829 +       {Opt_ignore_silent, "nodlgt"},
25830 +       {Opt_ignore, "clean_plink"},
25831 +
25832 +#ifdef CONFIG_AUFS_SHWH
25833 +       {Opt_shwh, "shwh"},
25834 +#endif
25835 +       {Opt_noshwh, "noshwh"},
25836 +
25837 +       {Opt_dirperm1, "dirperm1"},
25838 +       {Opt_nodirperm1, "nodirperm1"},
25839 +
25840 +       {Opt_verbose, "verbose"},
25841 +       {Opt_verbose, "v"},
25842 +       {Opt_noverbose, "noverbose"},
25843 +       {Opt_noverbose, "quiet"},
25844 +       {Opt_noverbose, "q"},
25845 +       {Opt_noverbose, "silent"},
25846 +
25847 +       {Opt_sum, "sum"},
25848 +       {Opt_nosum, "nosum"},
25849 +       {Opt_wsum, "wsum"},
25850 +
25851 +       {Opt_rdcache, "rdcache=%d"},
25852 +       {Opt_rdblk, "rdblk=%d"},
25853 +       {Opt_rdblk_def, "rdblk=def"},
25854 +       {Opt_rdhash, "rdhash=%d"},
25855 +       {Opt_rdhash_def, "rdhash=def"},
25856 +
25857 +       {Opt_wbr_create, "create=%s"},
25858 +       {Opt_wbr_create, "create_policy=%s"},
25859 +       {Opt_wbr_copyup, "cpup=%s"},
25860 +       {Opt_wbr_copyup, "copyup=%s"},
25861 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25862 +
25863 +       /* generic VFS flag */
25864 +#ifdef CONFIG_FS_POSIX_ACL
25865 +       {Opt_acl, "acl"},
25866 +       {Opt_noacl, "noacl"},
25867 +#else
25868 +       {Opt_ignore, "acl"},
25869 +       {Opt_ignore_silent, "noacl"},
25870 +#endif
25871 +
25872 +       /* internal use for the scripts */
25873 +       {Opt_ignore_silent, "si=%s"},
25874 +
25875 +       {Opt_br, "dirs=%s"},
25876 +       {Opt_ignore, "debug=%d"},
25877 +       {Opt_ignore, "delete=whiteout"},
25878 +       {Opt_ignore, "delete=all"},
25879 +       {Opt_ignore, "imap=%s"},
25880 +
25881 +       /* temporary workaround, due to old mount(8)? */
25882 +       {Opt_ignore_silent, "relatime"},
25883 +
25884 +       {Opt_err, NULL}
25885 +};
25886 +
25887 +/* ---------------------------------------------------------------------- */
25888 +
25889 +static const char *au_parser_pattern(int val, match_table_t tbl)
25890 +{
25891 +       struct match_token *p;
25892 +
25893 +       p = tbl;
25894 +       while (p->pattern) {
25895 +               if (p->token == val)
25896 +                       return p->pattern;
25897 +               p++;
25898 +       }
25899 +       BUG();
25900 +       return "??";
25901 +}
25902 +
25903 +static const char *au_optstr(int *val, match_table_t tbl)
25904 +{
25905 +       struct match_token *p;
25906 +       int v;
25907 +
25908 +       v = *val;
25909 +       if (!v)
25910 +               goto out;
25911 +       p = tbl;
25912 +       while (p->pattern) {
25913 +               if (p->token
25914 +                   && (v & p->token) == p->token) {
25915 +                       *val &= ~p->token;
25916 +                       return p->pattern;
25917 +               }
25918 +               p++;
25919 +       }
25920 +
25921 +out:
25922 +       return NULL;
25923 +}
25924 +
25925 +/* ---------------------------------------------------------------------- */
25926 +
25927 +static match_table_t brperm = {
25928 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25929 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25930 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25931 +       {0, NULL}
25932 +};
25933 +
25934 +static match_table_t brattr = {
25935 +       /* general */
25936 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25937 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25938 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25939 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25940 +#ifdef CONFIG_AUFS_FHSM
25941 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25942 +#endif
25943 +#ifdef CONFIG_AUFS_XATTR
25944 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25945 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25946 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25947 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25948 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25949 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25950 +#endif
25951 +
25952 +       /* ro/rr branch */
25953 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25954 +
25955 +       /* rw branch */
25956 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25957 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25958 +
25959 +       {0, NULL}
25960 +};
25961 +
25962 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25963 +{
25964 +       int attr, v;
25965 +       char *p;
25966 +
25967 +       attr = 0;
25968 +       do {
25969 +               p = strchr(str, '+');
25970 +               if (p)
25971 +                       *p = 0;
25972 +               v = match_token(str, table, args);
25973 +               if (v) {
25974 +                       if (v & AuBrAttr_CMOO_Mask)
25975 +                               attr &= ~AuBrAttr_CMOO_Mask;
25976 +                       attr |= v;
25977 +               } else {
25978 +                       if (p)
25979 +                               *p = '+';
25980 +                       pr_warn("ignored branch attribute %s\n", str);
25981 +                       break;
25982 +               }
25983 +               if (p)
25984 +                       str = p + 1;
25985 +       } while (p);
25986 +
25987 +       return attr;
25988 +}
25989 +
25990 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25991 +{
25992 +       int sz;
25993 +       const char *p;
25994 +       char *q;
25995 +
25996 +       q = str->a;
25997 +       *q = 0;
25998 +       p = au_optstr(&perm, brattr);
25999 +       if (p) {
26000 +               sz = strlen(p);
26001 +               memcpy(q, p, sz + 1);
26002 +               q += sz;
26003 +       } else
26004 +               goto out;
26005 +
26006 +       do {
26007 +               p = au_optstr(&perm, brattr);
26008 +               if (p) {
26009 +                       *q++ = '+';
26010 +                       sz = strlen(p);
26011 +                       memcpy(q, p, sz + 1);
26012 +                       q += sz;
26013 +               }
26014 +       } while (p);
26015 +
26016 +out:
26017 +       return q - str->a;
26018 +}
26019 +
26020 +static int noinline_for_stack br_perm_val(char *perm)
26021 +{
26022 +       int val, bad, sz;
26023 +       char *p;
26024 +       substring_t args[MAX_OPT_ARGS];
26025 +       au_br_perm_str_t attr;
26026 +
26027 +       p = strchr(perm, '+');
26028 +       if (p)
26029 +               *p = 0;
26030 +       val = match_token(perm, brperm, args);
26031 +       if (!val) {
26032 +               if (p)
26033 +                       *p = '+';
26034 +               pr_warn("ignored branch permission %s\n", perm);
26035 +               val = AuBrPerm_RO;
26036 +               goto out;
26037 +       }
26038 +       if (!p)
26039 +               goto out;
26040 +
26041 +       val |= br_attr_val(p + 1, brattr, args);
26042 +
26043 +       bad = 0;
26044 +       switch (val & AuBrPerm_Mask) {
26045 +       case AuBrPerm_RO:
26046 +       case AuBrPerm_RR:
26047 +               bad = val & AuBrWAttr_Mask;
26048 +               val &= ~AuBrWAttr_Mask;
26049 +               break;
26050 +       case AuBrPerm_RW:
26051 +               bad = val & AuBrRAttr_Mask;
26052 +               val &= ~AuBrRAttr_Mask;
26053 +               break;
26054 +       }
26055 +
26056 +       /*
26057 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26058 +        * does not treat it as an error, just warning.
26059 +        * this is a tiny guard for the user operation.
26060 +        */
26061 +       if (val & AuBrAttr_UNPIN) {
26062 +               bad |= AuBrAttr_UNPIN;
26063 +               val &= ~AuBrAttr_UNPIN;
26064 +       }
26065 +
26066 +       if (unlikely(bad)) {
26067 +               sz = au_do_optstr_br_attr(&attr, bad);
26068 +               AuDebugOn(!sz);
26069 +               pr_warn("ignored branch attribute %s\n", attr.a);
26070 +       }
26071 +
26072 +out:
26073 +       return val;
26074 +}
26075 +
26076 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
26077 +{
26078 +       au_br_perm_str_t attr;
26079 +       const char *p;
26080 +       char *q;
26081 +       int sz;
26082 +
26083 +       q = str->a;
26084 +       p = au_optstr(&perm, brperm);
26085 +       AuDebugOn(!p || !*p);
26086 +       sz = strlen(p);
26087 +       memcpy(q, p, sz + 1);
26088 +       q += sz;
26089 +
26090 +       sz = au_do_optstr_br_attr(&attr, perm);
26091 +       if (sz) {
26092 +               *q++ = '+';
26093 +               memcpy(q, attr.a, sz + 1);
26094 +       }
26095 +
26096 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
26097 +}
26098 +
26099 +/* ---------------------------------------------------------------------- */
26100 +
26101 +static match_table_t udbalevel = {
26102 +       {AuOpt_UDBA_REVAL, "reval"},
26103 +       {AuOpt_UDBA_NONE, "none"},
26104 +#ifdef CONFIG_AUFS_HNOTIFY
26105 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26106 +#ifdef CONFIG_AUFS_HFSNOTIFY
26107 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26108 +#endif
26109 +#endif
26110 +       {-1, NULL}
26111 +};
26112 +
26113 +static int noinline_for_stack udba_val(char *str)
26114 +{
26115 +       substring_t args[MAX_OPT_ARGS];
26116 +
26117 +       return match_token(str, udbalevel, args);
26118 +}
26119 +
26120 +const char *au_optstr_udba(int udba)
26121 +{
26122 +       return au_parser_pattern(udba, udbalevel);
26123 +}
26124 +
26125 +/* ---------------------------------------------------------------------- */
26126 +
26127 +static match_table_t au_wbr_create_policy = {
26128 +       {AuWbrCreate_TDP, "tdp"},
26129 +       {AuWbrCreate_TDP, "top-down-parent"},
26130 +       {AuWbrCreate_RR, "rr"},
26131 +       {AuWbrCreate_RR, "round-robin"},
26132 +       {AuWbrCreate_MFS, "mfs"},
26133 +       {AuWbrCreate_MFS, "most-free-space"},
26134 +       {AuWbrCreate_MFSV, "mfs:%d"},
26135 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26136 +
26137 +       /* top-down regardless the parent, and then mfs */
26138 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26139 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26140 +
26141 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26142 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26143 +       {AuWbrCreate_PMFS, "pmfs"},
26144 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26145 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26146 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26147 +
26148 +       {-1, NULL}
26149 +};
26150 +
26151 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26152 +                           struct au_opt_wbr_create *create)
26153 +{
26154 +       int err;
26155 +       unsigned long long ull;
26156 +
26157 +       err = 0;
26158 +       if (!match_u64(arg, &ull))
26159 +               create->mfsrr_watermark = ull;
26160 +       else {
26161 +               pr_err("bad integer in %s\n", str);
26162 +               err = -EINVAL;
26163 +       }
26164 +
26165 +       return err;
26166 +}
26167 +
26168 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26169 +                         struct au_opt_wbr_create *create)
26170 +{
26171 +       int n, err;
26172 +
26173 +       err = 0;
26174 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26175 +               create->mfs_second = n;
26176 +       else {
26177 +               pr_err("bad integer in %s\n", str);
26178 +               err = -EINVAL;
26179 +       }
26180 +
26181 +       return err;
26182 +}
26183 +
26184 +static int noinline_for_stack
26185 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26186 +{
26187 +       int err, e;
26188 +       substring_t args[MAX_OPT_ARGS];
26189 +
26190 +       err = match_token(str, au_wbr_create_policy, args);
26191 +       create->wbr_create = err;
26192 +       switch (err) {
26193 +       case AuWbrCreate_MFSRRV:
26194 +       case AuWbrCreate_TDMFSV:
26195 +       case AuWbrCreate_PMFSRRV:
26196 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26197 +               if (!e)
26198 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26199 +               if (unlikely(e))
26200 +                       err = e;
26201 +               break;
26202 +       case AuWbrCreate_MFSRR:
26203 +       case AuWbrCreate_TDMFS:
26204 +       case AuWbrCreate_PMFSRR:
26205 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26206 +               if (unlikely(e)) {
26207 +                       err = e;
26208 +                       break;
26209 +               }
26210 +               /*FALLTHROUGH*/
26211 +       case AuWbrCreate_MFS:
26212 +       case AuWbrCreate_PMFS:
26213 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26214 +               break;
26215 +       case AuWbrCreate_MFSV:
26216 +       case AuWbrCreate_PMFSV:
26217 +               e = au_wbr_mfs_sec(&args[0], str, create);
26218 +               if (unlikely(e))
26219 +                       err = e;
26220 +               break;
26221 +       }
26222 +
26223 +       return err;
26224 +}
26225 +
26226 +const char *au_optstr_wbr_create(int wbr_create)
26227 +{
26228 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26229 +}
26230 +
26231 +static match_table_t au_wbr_copyup_policy = {
26232 +       {AuWbrCopyup_TDP, "tdp"},
26233 +       {AuWbrCopyup_TDP, "top-down-parent"},
26234 +       {AuWbrCopyup_BUP, "bup"},
26235 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26236 +       {AuWbrCopyup_BU, "bu"},
26237 +       {AuWbrCopyup_BU, "bottom-up"},
26238 +       {-1, NULL}
26239 +};
26240 +
26241 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26242 +{
26243 +       substring_t args[MAX_OPT_ARGS];
26244 +
26245 +       return match_token(str, au_wbr_copyup_policy, args);
26246 +}
26247 +
26248 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26249 +{
26250 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26251 +}
26252 +
26253 +/* ---------------------------------------------------------------------- */
26254 +
26255 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26256 +
26257 +static void dump_opts(struct au_opts *opts)
26258 +{
26259 +#ifdef CONFIG_AUFS_DEBUG
26260 +       /* reduce stack space */
26261 +       union {
26262 +               struct au_opt_add *add;
26263 +               struct au_opt_del *del;
26264 +               struct au_opt_mod *mod;
26265 +               struct au_opt_xino *xino;
26266 +               struct au_opt_xino_itrunc *xino_itrunc;
26267 +               struct au_opt_wbr_create *create;
26268 +       } u;
26269 +       struct au_opt *opt;
26270 +
26271 +       opt = opts->opt;
26272 +       while (opt->type != Opt_tail) {
26273 +               switch (opt->type) {
26274 +               case Opt_add:
26275 +                       u.add = &opt->add;
26276 +                       AuDbg("add {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_del:
26281 +               case Opt_idel:
26282 +                       u.del = &opt->del;
26283 +                       AuDbg("del {%s, %p}\n",
26284 +                             u.del->pathname, u.del->h_path.dentry);
26285 +                       break;
26286 +               case Opt_mod:
26287 +               case Opt_imod:
26288 +                       u.mod = &opt->mod;
26289 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26290 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26291 +                       break;
26292 +               case Opt_append:
26293 +                       u.add = &opt->add;
26294 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26295 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26296 +                                 u.add->path.dentry);
26297 +                       break;
26298 +               case Opt_prepend:
26299 +                       u.add = &opt->add;
26300 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26301 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26302 +                                 u.add->path.dentry);
26303 +                       break;
26304 +               case Opt_dirwh:
26305 +                       AuDbg("dirwh %d\n", opt->dirwh);
26306 +                       break;
26307 +               case Opt_rdcache:
26308 +                       AuDbg("rdcache %d\n", opt->rdcache);
26309 +                       break;
26310 +               case Opt_rdblk:
26311 +                       AuDbg("rdblk %u\n", opt->rdblk);
26312 +                       break;
26313 +               case Opt_rdblk_def:
26314 +                       AuDbg("rdblk_def\n");
26315 +                       break;
26316 +               case Opt_rdhash:
26317 +                       AuDbg("rdhash %u\n", opt->rdhash);
26318 +                       break;
26319 +               case Opt_rdhash_def:
26320 +                       AuDbg("rdhash_def\n");
26321 +                       break;
26322 +               case Opt_xino:
26323 +                       u.xino = &opt->xino;
26324 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26325 +                       break;
26326 +               case Opt_trunc_xino:
26327 +                       AuLabel(trunc_xino);
26328 +                       break;
26329 +               case Opt_notrunc_xino:
26330 +                       AuLabel(notrunc_xino);
26331 +                       break;
26332 +               case Opt_trunc_xino_path:
26333 +               case Opt_itrunc_xino:
26334 +                       u.xino_itrunc = &opt->xino_itrunc;
26335 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26336 +                       break;
26337 +               case Opt_noxino:
26338 +                       AuLabel(noxino);
26339 +                       break;
26340 +               case Opt_trunc_xib:
26341 +                       AuLabel(trunc_xib);
26342 +                       break;
26343 +               case Opt_notrunc_xib:
26344 +                       AuLabel(notrunc_xib);
26345 +                       break;
26346 +               case Opt_shwh:
26347 +                       AuLabel(shwh);
26348 +                       break;
26349 +               case Opt_noshwh:
26350 +                       AuLabel(noshwh);
26351 +                       break;
26352 +               case Opt_dirperm1:
26353 +                       AuLabel(dirperm1);
26354 +                       break;
26355 +               case Opt_nodirperm1:
26356 +                       AuLabel(nodirperm1);
26357 +                       break;
26358 +               case Opt_plink:
26359 +                       AuLabel(plink);
26360 +                       break;
26361 +               case Opt_noplink:
26362 +                       AuLabel(noplink);
26363 +                       break;
26364 +               case Opt_list_plink:
26365 +                       AuLabel(list_plink);
26366 +                       break;
26367 +               case Opt_udba:
26368 +                       AuDbg("udba %d, %s\n",
26369 +                                 opt->udba, au_optstr_udba(opt->udba));
26370 +                       break;
26371 +               case Opt_dio:
26372 +                       AuLabel(dio);
26373 +                       break;
26374 +               case Opt_nodio:
26375 +                       AuLabel(nodio);
26376 +                       break;
26377 +               case Opt_diropq_a:
26378 +                       AuLabel(diropq_a);
26379 +                       break;
26380 +               case Opt_diropq_w:
26381 +                       AuLabel(diropq_w);
26382 +                       break;
26383 +               case Opt_warn_perm:
26384 +                       AuLabel(warn_perm);
26385 +                       break;
26386 +               case Opt_nowarn_perm:
26387 +                       AuLabel(nowarn_perm);
26388 +                       break;
26389 +               case Opt_verbose:
26390 +                       AuLabel(verbose);
26391 +                       break;
26392 +               case Opt_noverbose:
26393 +                       AuLabel(noverbose);
26394 +                       break;
26395 +               case Opt_sum:
26396 +                       AuLabel(sum);
26397 +                       break;
26398 +               case Opt_nosum:
26399 +                       AuLabel(nosum);
26400 +                       break;
26401 +               case Opt_wsum:
26402 +                       AuLabel(wsum);
26403 +                       break;
26404 +               case Opt_wbr_create:
26405 +                       u.create = &opt->wbr_create;
26406 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26407 +                                 au_optstr_wbr_create(u.create->wbr_create));
26408 +                       switch (u.create->wbr_create) {
26409 +                       case AuWbrCreate_MFSV:
26410 +                       case AuWbrCreate_PMFSV:
26411 +                               AuDbg("%d sec\n", u.create->mfs_second);
26412 +                               break;
26413 +                       case AuWbrCreate_MFSRR:
26414 +                       case AuWbrCreate_TDMFS:
26415 +                               AuDbg("%llu watermark\n",
26416 +                                         u.create->mfsrr_watermark);
26417 +                               break;
26418 +                       case AuWbrCreate_MFSRRV:
26419 +                       case AuWbrCreate_TDMFSV:
26420 +                       case AuWbrCreate_PMFSRRV:
26421 +                               AuDbg("%llu watermark, %d sec\n",
26422 +                                         u.create->mfsrr_watermark,
26423 +                                         u.create->mfs_second);
26424 +                               break;
26425 +                       }
26426 +                       break;
26427 +               case Opt_wbr_copyup:
26428 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26429 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26430 +                       break;
26431 +               case Opt_fhsm_sec:
26432 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26433 +                       break;
26434 +               case Opt_dirren:
26435 +                       AuLabel(dirren);
26436 +                       break;
26437 +               case Opt_nodirren:
26438 +                       AuLabel(nodirren);
26439 +                       break;
26440 +               case Opt_acl:
26441 +                       AuLabel(acl);
26442 +                       break;
26443 +               case Opt_noacl:
26444 +                       AuLabel(noacl);
26445 +                       break;
26446 +               default:
26447 +                       BUG();
26448 +               }
26449 +               opt++;
26450 +       }
26451 +#endif
26452 +}
26453 +
26454 +void au_opts_free(struct au_opts *opts)
26455 +{
26456 +       struct au_opt *opt;
26457 +
26458 +       opt = opts->opt;
26459 +       while (opt->type != Opt_tail) {
26460 +               switch (opt->type) {
26461 +               case Opt_add:
26462 +               case Opt_append:
26463 +               case Opt_prepend:
26464 +                       path_put(&opt->add.path);
26465 +                       break;
26466 +               case Opt_del:
26467 +               case Opt_idel:
26468 +                       path_put(&opt->del.h_path);
26469 +                       break;
26470 +               case Opt_mod:
26471 +               case Opt_imod:
26472 +                       dput(opt->mod.h_root);
26473 +                       break;
26474 +               case Opt_xino:
26475 +                       fput(opt->xino.file);
26476 +                       break;
26477 +               }
26478 +               opt++;
26479 +       }
26480 +}
26481 +
26482 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26483 +                  aufs_bindex_t bindex)
26484 +{
26485 +       int err;
26486 +       struct au_opt_add *add = &opt->add;
26487 +       char *p;
26488 +
26489 +       add->bindex = bindex;
26490 +       add->perm = AuBrPerm_RO;
26491 +       add->pathname = opt_str;
26492 +       p = strchr(opt_str, '=');
26493 +       if (p) {
26494 +               *p++ = 0;
26495 +               if (*p)
26496 +                       add->perm = br_perm_val(p);
26497 +       }
26498 +
26499 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26500 +       if (!err) {
26501 +               if (!p) {
26502 +                       add->perm = AuBrPerm_RO;
26503 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26504 +                               add->perm = AuBrPerm_RR;
26505 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26506 +                               add->perm = AuBrPerm_RW;
26507 +               }
26508 +               opt->type = Opt_add;
26509 +               goto out;
26510 +       }
26511 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26512 +       err = -EINVAL;
26513 +
26514 +out:
26515 +       return err;
26516 +}
26517 +
26518 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26519 +{
26520 +       int err;
26521 +
26522 +       del->pathname = args[0].from;
26523 +       AuDbg("del path %s\n", del->pathname);
26524 +
26525 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26526 +       if (unlikely(err))
26527 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26528 +
26529 +       return err;
26530 +}
26531 +
26532 +#if 0 /* reserved for future use */
26533 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26534 +                             struct au_opt_del *del, substring_t args[])
26535 +{
26536 +       int err;
26537 +       struct dentry *root;
26538 +
26539 +       err = -EINVAL;
26540 +       root = sb->s_root;
26541 +       aufs_read_lock(root, AuLock_FLUSH);
26542 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26543 +               pr_err("out of bounds, %d\n", bindex);
26544 +               goto out;
26545 +       }
26546 +
26547 +       err = 0;
26548 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26549 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26550 +
26551 +out:
26552 +       aufs_read_unlock(root, !AuLock_IR);
26553 +       return err;
26554 +}
26555 +#endif
26556 +
26557 +static int noinline_for_stack
26558 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26559 +{
26560 +       int err;
26561 +       struct path path;
26562 +       char *p;
26563 +
26564 +       err = -EINVAL;
26565 +       mod->path = args[0].from;
26566 +       p = strchr(mod->path, '=');
26567 +       if (unlikely(!p)) {
26568 +               pr_err("no permission %s\n", args[0].from);
26569 +               goto out;
26570 +       }
26571 +
26572 +       *p++ = 0;
26573 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26574 +       if (unlikely(err)) {
26575 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26576 +               goto out;
26577 +       }
26578 +
26579 +       mod->perm = br_perm_val(p);
26580 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26581 +       mod->h_root = dget(path.dentry);
26582 +       path_put(&path);
26583 +
26584 +out:
26585 +       return err;
26586 +}
26587 +
26588 +#if 0 /* reserved for future use */
26589 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26590 +                             struct au_opt_mod *mod, substring_t args[])
26591 +{
26592 +       int err;
26593 +       struct dentry *root;
26594 +
26595 +       err = -EINVAL;
26596 +       root = sb->s_root;
26597 +       aufs_read_lock(root, AuLock_FLUSH);
26598 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26599 +               pr_err("out of bounds, %d\n", bindex);
26600 +               goto out;
26601 +       }
26602 +
26603 +       err = 0;
26604 +       mod->perm = br_perm_val(args[1].from);
26605 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26606 +             mod->path, mod->perm, args[1].from);
26607 +       mod->h_root = dget(au_h_dptr(root, bindex));
26608 +
26609 +out:
26610 +       aufs_read_unlock(root, !AuLock_IR);
26611 +       return err;
26612 +}
26613 +#endif
26614 +
26615 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26616 +                             substring_t args[])
26617 +{
26618 +       int err;
26619 +       struct file *file;
26620 +
26621 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26622 +       err = PTR_ERR(file);
26623 +       if (IS_ERR(file))
26624 +               goto out;
26625 +
26626 +       err = -EINVAL;
26627 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26628 +               fput(file);
26629 +               pr_err("%s must be outside\n", args[0].from);
26630 +               goto out;
26631 +       }
26632 +
26633 +       err = 0;
26634 +       xino->file = file;
26635 +       xino->path = args[0].from;
26636 +
26637 +out:
26638 +       return err;
26639 +}
26640 +
26641 +static int noinline_for_stack
26642 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26643 +                              struct au_opt_xino_itrunc *xino_itrunc,
26644 +                              substring_t args[])
26645 +{
26646 +       int err;
26647 +       aufs_bindex_t bbot, bindex;
26648 +       struct path path;
26649 +       struct dentry *root;
26650 +
26651 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26652 +       if (unlikely(err)) {
26653 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26654 +               goto out;
26655 +       }
26656 +
26657 +       xino_itrunc->bindex = -1;
26658 +       root = sb->s_root;
26659 +       aufs_read_lock(root, AuLock_FLUSH);
26660 +       bbot = au_sbbot(sb);
26661 +       for (bindex = 0; bindex <= bbot; bindex++) {
26662 +               if (au_h_dptr(root, bindex) == path.dentry) {
26663 +                       xino_itrunc->bindex = bindex;
26664 +                       break;
26665 +               }
26666 +       }
26667 +       aufs_read_unlock(root, !AuLock_IR);
26668 +       path_put(&path);
26669 +
26670 +       if (unlikely(xino_itrunc->bindex < 0)) {
26671 +               pr_err("no such branch %s\n", args[0].from);
26672 +               err = -EINVAL;
26673 +       }
26674 +
26675 +out:
26676 +       return err;
26677 +}
26678 +
26679 +/* called without aufs lock */
26680 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26681 +{
26682 +       int err, n, token;
26683 +       aufs_bindex_t bindex;
26684 +       unsigned char skipped;
26685 +       struct dentry *root;
26686 +       struct au_opt *opt, *opt_tail;
26687 +       char *opt_str;
26688 +       /* reduce the stack space */
26689 +       union {
26690 +               struct au_opt_xino_itrunc *xino_itrunc;
26691 +               struct au_opt_wbr_create *create;
26692 +       } u;
26693 +       struct {
26694 +               substring_t args[MAX_OPT_ARGS];
26695 +       } *a;
26696 +
26697 +       err = -ENOMEM;
26698 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26699 +       if (unlikely(!a))
26700 +               goto out;
26701 +
26702 +       root = sb->s_root;
26703 +       err = 0;
26704 +       bindex = 0;
26705 +       opt = opts->opt;
26706 +       opt_tail = opt + opts->max_opt - 1;
26707 +       opt->type = Opt_tail;
26708 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26709 +               err = -EINVAL;
26710 +               skipped = 0;
26711 +               token = match_token(opt_str, options, a->args);
26712 +               switch (token) {
26713 +               case Opt_br:
26714 +                       err = 0;
26715 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26716 +                              && *opt_str) {
26717 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26718 +                                             bindex++);
26719 +                               if (unlikely(!err && ++opt > opt_tail)) {
26720 +                                       err = -E2BIG;
26721 +                                       break;
26722 +                               }
26723 +                               opt->type = Opt_tail;
26724 +                               skipped = 1;
26725 +                       }
26726 +                       break;
26727 +               case Opt_add:
26728 +                       if (unlikely(match_int(&a->args[0], &n))) {
26729 +                               pr_err("bad integer in %s\n", opt_str);
26730 +                               break;
26731 +                       }
26732 +                       bindex = n;
26733 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26734 +                                     bindex);
26735 +                       if (!err)
26736 +                               opt->type = token;
26737 +                       break;
26738 +               case Opt_append:
26739 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26740 +                                     /*dummy bindex*/1);
26741 +                       if (!err)
26742 +                               opt->type = token;
26743 +                       break;
26744 +               case Opt_prepend:
26745 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26746 +                                     /*bindex*/0);
26747 +                       if (!err)
26748 +                               opt->type = token;
26749 +                       break;
26750 +               case Opt_del:
26751 +                       err = au_opts_parse_del(&opt->del, a->args);
26752 +                       if (!err)
26753 +                               opt->type = token;
26754 +                       break;
26755 +#if 0 /* reserved for future use */
26756 +               case Opt_idel:
26757 +                       del->pathname = "(indexed)";
26758 +                       if (unlikely(match_int(&args[0], &n))) {
26759 +                               pr_err("bad integer in %s\n", opt_str);
26760 +                               break;
26761 +                       }
26762 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26763 +                       if (!err)
26764 +                               opt->type = token;
26765 +                       break;
26766 +#endif
26767 +               case Opt_mod:
26768 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26769 +                       if (!err)
26770 +                               opt->type = token;
26771 +                       break;
26772 +#ifdef IMOD /* reserved for future use */
26773 +               case Opt_imod:
26774 +                       u.mod->path = "(indexed)";
26775 +                       if (unlikely(match_int(&a->args[0], &n))) {
26776 +                               pr_err("bad integer in %s\n", opt_str);
26777 +                               break;
26778 +                       }
26779 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26780 +                       if (!err)
26781 +                               opt->type = token;
26782 +                       break;
26783 +#endif
26784 +               case Opt_xino:
26785 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26786 +                       if (!err)
26787 +                               opt->type = token;
26788 +                       break;
26789 +
26790 +               case Opt_trunc_xino_path:
26791 +                       err = au_opts_parse_xino_itrunc_path
26792 +                               (sb, &opt->xino_itrunc, a->args);
26793 +                       if (!err)
26794 +                               opt->type = token;
26795 +                       break;
26796 +
26797 +               case Opt_itrunc_xino:
26798 +                       u.xino_itrunc = &opt->xino_itrunc;
26799 +                       if (unlikely(match_int(&a->args[0], &n))) {
26800 +                               pr_err("bad integer in %s\n", opt_str);
26801 +                               break;
26802 +                       }
26803 +                       u.xino_itrunc->bindex = n;
26804 +                       aufs_read_lock(root, AuLock_FLUSH);
26805 +                       if (n < 0 || au_sbbot(sb) < n) {
26806 +                               pr_err("out of bounds, %d\n", n);
26807 +                               aufs_read_unlock(root, !AuLock_IR);
26808 +                               break;
26809 +                       }
26810 +                       aufs_read_unlock(root, !AuLock_IR);
26811 +                       err = 0;
26812 +                       opt->type = token;
26813 +                       break;
26814 +
26815 +               case Opt_dirwh:
26816 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26817 +                               break;
26818 +                       err = 0;
26819 +                       opt->type = token;
26820 +                       break;
26821 +
26822 +               case Opt_rdcache:
26823 +                       if (unlikely(match_int(&a->args[0], &n))) {
26824 +                               pr_err("bad integer in %s\n", opt_str);
26825 +                               break;
26826 +                       }
26827 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26828 +                               pr_err("rdcache must be smaller than %d\n",
26829 +                                      AUFS_RDCACHE_MAX);
26830 +                               break;
26831 +                       }
26832 +                       opt->rdcache = n;
26833 +                       err = 0;
26834 +                       opt->type = token;
26835 +                       break;
26836 +               case Opt_rdblk:
26837 +                       if (unlikely(match_int(&a->args[0], &n)
26838 +                                    || n < 0
26839 +                                    || n > KMALLOC_MAX_SIZE)) {
26840 +                               pr_err("bad integer in %s\n", opt_str);
26841 +                               break;
26842 +                       }
26843 +                       if (unlikely(n && n < NAME_MAX)) {
26844 +                               pr_err("rdblk must be larger than %d\n",
26845 +                                      NAME_MAX);
26846 +                               break;
26847 +                       }
26848 +                       opt->rdblk = n;
26849 +                       err = 0;
26850 +                       opt->type = token;
26851 +                       break;
26852 +               case Opt_rdhash:
26853 +                       if (unlikely(match_int(&a->args[0], &n)
26854 +                                    || n < 0
26855 +                                    || n * sizeof(struct hlist_head)
26856 +                                    > KMALLOC_MAX_SIZE)) {
26857 +                               pr_err("bad integer in %s\n", opt_str);
26858 +                               break;
26859 +                       }
26860 +                       opt->rdhash = n;
26861 +                       err = 0;
26862 +                       opt->type = token;
26863 +                       break;
26864 +
26865 +               case Opt_trunc_xino:
26866 +               case Opt_notrunc_xino:
26867 +               case Opt_noxino:
26868 +               case Opt_trunc_xib:
26869 +               case Opt_notrunc_xib:
26870 +               case Opt_shwh:
26871 +               case Opt_noshwh:
26872 +               case Opt_dirperm1:
26873 +               case Opt_nodirperm1:
26874 +               case Opt_plink:
26875 +               case Opt_noplink:
26876 +               case Opt_list_plink:
26877 +               case Opt_dio:
26878 +               case Opt_nodio:
26879 +               case Opt_diropq_a:
26880 +               case Opt_diropq_w:
26881 +               case Opt_warn_perm:
26882 +               case Opt_nowarn_perm:
26883 +               case Opt_verbose:
26884 +               case Opt_noverbose:
26885 +               case Opt_sum:
26886 +               case Opt_nosum:
26887 +               case Opt_wsum:
26888 +               case Opt_rdblk_def:
26889 +               case Opt_rdhash_def:
26890 +               case Opt_dirren:
26891 +               case Opt_nodirren:
26892 +               case Opt_acl:
26893 +               case Opt_noacl:
26894 +                       err = 0;
26895 +                       opt->type = token;
26896 +                       break;
26897 +
26898 +               case Opt_udba:
26899 +                       opt->udba = udba_val(a->args[0].from);
26900 +                       if (opt->udba >= 0) {
26901 +                               err = 0;
26902 +                               opt->type = token;
26903 +                       } else
26904 +                               pr_err("wrong value, %s\n", opt_str);
26905 +                       break;
26906 +
26907 +               case Opt_wbr_create:
26908 +                       u.create = &opt->wbr_create;
26909 +                       u.create->wbr_create
26910 +                               = au_wbr_create_val(a->args[0].from, u.create);
26911 +                       if (u.create->wbr_create >= 0) {
26912 +                               err = 0;
26913 +                               opt->type = token;
26914 +                       } else
26915 +                               pr_err("wrong value, %s\n", opt_str);
26916 +                       break;
26917 +               case Opt_wbr_copyup:
26918 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26919 +                       if (opt->wbr_copyup >= 0) {
26920 +                               err = 0;
26921 +                               opt->type = token;
26922 +                       } else
26923 +                               pr_err("wrong value, %s\n", opt_str);
26924 +                       break;
26925 +
26926 +               case Opt_fhsm_sec:
26927 +                       if (unlikely(match_int(&a->args[0], &n)
26928 +                                    || n < 0)) {
26929 +                               pr_err("bad integer in %s\n", opt_str);
26930 +                               break;
26931 +                       }
26932 +                       if (sysaufs_brs) {
26933 +                               opt->fhsm_second = n;
26934 +                               opt->type = token;
26935 +                       } else
26936 +                               pr_warn("ignored %s\n", opt_str);
26937 +                       err = 0;
26938 +                       break;
26939 +
26940 +               case Opt_ignore:
26941 +                       pr_warn("ignored %s\n", opt_str);
26942 +                       /*FALLTHROUGH*/
26943 +               case Opt_ignore_silent:
26944 +                       skipped = 1;
26945 +                       err = 0;
26946 +                       break;
26947 +               case Opt_err:
26948 +                       pr_err("unknown option %s\n", opt_str);
26949 +                       break;
26950 +               }
26951 +
26952 +               if (!err && !skipped) {
26953 +                       if (unlikely(++opt > opt_tail)) {
26954 +                               err = -E2BIG;
26955 +                               opt--;
26956 +                               opt->type = Opt_tail;
26957 +                               break;
26958 +                       }
26959 +                       opt->type = Opt_tail;
26960 +               }
26961 +       }
26962 +
26963 +       au_kfree_rcu(a);
26964 +       dump_opts(opts);
26965 +       if (unlikely(err))
26966 +               au_opts_free(opts);
26967 +
26968 +out:
26969 +       return err;
26970 +}
26971 +
26972 +static int au_opt_wbr_create(struct super_block *sb,
26973 +                            struct au_opt_wbr_create *create)
26974 +{
26975 +       int err;
26976 +       struct au_sbinfo *sbinfo;
26977 +
26978 +       SiMustWriteLock(sb);
26979 +
26980 +       err = 1; /* handled */
26981 +       sbinfo = au_sbi(sb);
26982 +       if (sbinfo->si_wbr_create_ops->fin) {
26983 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26984 +               if (!err)
26985 +                       err = 1;
26986 +       }
26987 +
26988 +       sbinfo->si_wbr_create = create->wbr_create;
26989 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26990 +       switch (create->wbr_create) {
26991 +       case AuWbrCreate_MFSRRV:
26992 +       case AuWbrCreate_MFSRR:
26993 +       case AuWbrCreate_TDMFS:
26994 +       case AuWbrCreate_TDMFSV:
26995 +       case AuWbrCreate_PMFSRR:
26996 +       case AuWbrCreate_PMFSRRV:
26997 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26998 +               /*FALLTHROUGH*/
26999 +       case AuWbrCreate_MFS:
27000 +       case AuWbrCreate_MFSV:
27001 +       case AuWbrCreate_PMFS:
27002 +       case AuWbrCreate_PMFSV:
27003 +               sbinfo->si_wbr_mfs.mfs_expire
27004 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27005 +               break;
27006 +       }
27007 +
27008 +       if (sbinfo->si_wbr_create_ops->init)
27009 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27010 +
27011 +       return err;
27012 +}
27013 +
27014 +/*
27015 + * returns,
27016 + * plus: processed without an error
27017 + * zero: unprocessed
27018 + */
27019 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27020 +                        struct au_opts *opts)
27021 +{
27022 +       int err;
27023 +       struct au_sbinfo *sbinfo;
27024 +
27025 +       SiMustWriteLock(sb);
27026 +
27027 +       err = 1; /* handled */
27028 +       sbinfo = au_sbi(sb);
27029 +       switch (opt->type) {
27030 +       case Opt_udba:
27031 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27032 +               sbinfo->si_mntflags |= opt->udba;
27033 +               opts->given_udba |= opt->udba;
27034 +               break;
27035 +
27036 +       case Opt_plink:
27037 +               au_opt_set(sbinfo->si_mntflags, PLINK);
27038 +               break;
27039 +       case Opt_noplink:
27040 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27041 +                       au_plink_put(sb, /*verbose*/1);
27042 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
27043 +               break;
27044 +       case Opt_list_plink:
27045 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27046 +                       au_plink_list(sb);
27047 +               break;
27048 +
27049 +       case Opt_dio:
27050 +               au_opt_set(sbinfo->si_mntflags, DIO);
27051 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27052 +               break;
27053 +       case Opt_nodio:
27054 +               au_opt_clr(sbinfo->si_mntflags, DIO);
27055 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27056 +               break;
27057 +
27058 +       case Opt_fhsm_sec:
27059 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27060 +               break;
27061 +
27062 +       case Opt_diropq_a:
27063 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27064 +               break;
27065 +       case Opt_diropq_w:
27066 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27067 +               break;
27068 +
27069 +       case Opt_warn_perm:
27070 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27071 +               break;
27072 +       case Opt_nowarn_perm:
27073 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27074 +               break;
27075 +
27076 +       case Opt_verbose:
27077 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
27078 +               break;
27079 +       case Opt_noverbose:
27080 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27081 +               break;
27082 +
27083 +       case Opt_sum:
27084 +               au_opt_set(sbinfo->si_mntflags, SUM);
27085 +               break;
27086 +       case Opt_wsum:
27087 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27088 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27089 +               break;
27090 +       case Opt_nosum:
27091 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27092 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
27093 +               break;
27094 +
27095 +       case Opt_wbr_create:
27096 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27097 +               break;
27098 +       case Opt_wbr_copyup:
27099 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27100 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27101 +               break;
27102 +
27103 +       case Opt_dirwh:
27104 +               sbinfo->si_dirwh = opt->dirwh;
27105 +               break;
27106 +
27107 +       case Opt_rdcache:
27108 +               sbinfo->si_rdcache
27109 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27110 +               break;
27111 +       case Opt_rdblk:
27112 +               sbinfo->si_rdblk = opt->rdblk;
27113 +               break;
27114 +       case Opt_rdblk_def:
27115 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27116 +               break;
27117 +       case Opt_rdhash:
27118 +               sbinfo->si_rdhash = opt->rdhash;
27119 +               break;
27120 +       case Opt_rdhash_def:
27121 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27122 +               break;
27123 +
27124 +       case Opt_shwh:
27125 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27126 +               break;
27127 +       case Opt_noshwh:
27128 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27129 +               break;
27130 +
27131 +       case Opt_dirperm1:
27132 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27133 +               break;
27134 +       case Opt_nodirperm1:
27135 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27136 +               break;
27137 +
27138 +       case Opt_trunc_xino:
27139 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27140 +               break;
27141 +       case Opt_notrunc_xino:
27142 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27143 +               break;
27144 +
27145 +       case Opt_trunc_xino_path:
27146 +       case Opt_itrunc_xino:
27147 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27148 +                                   /*idx_begin*/0);
27149 +               if (!err)
27150 +                       err = 1;
27151 +               break;
27152 +
27153 +       case Opt_trunc_xib:
27154 +               au_fset_opts(opts->flags, TRUNC_XIB);
27155 +               break;
27156 +       case Opt_notrunc_xib:
27157 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27158 +               break;
27159 +
27160 +       case Opt_dirren:
27161 +               err = 1;
27162 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27163 +                       err = au_dr_opt_set(sb);
27164 +                       if (!err)
27165 +                               err = 1;
27166 +               }
27167 +               if (err == 1)
27168 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27169 +               break;
27170 +       case Opt_nodirren:
27171 +               err = 1;
27172 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27173 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27174 +                                                             DR_FLUSHED));
27175 +                       if (!err)
27176 +                               err = 1;
27177 +               }
27178 +               if (err == 1)
27179 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27180 +               break;
27181 +
27182 +       case Opt_acl:
27183 +               sb->s_flags |= SB_POSIXACL;
27184 +               break;
27185 +       case Opt_noacl:
27186 +               sb->s_flags &= ~SB_POSIXACL;
27187 +               break;
27188 +
27189 +       default:
27190 +               err = 0;
27191 +               break;
27192 +       }
27193 +
27194 +       return err;
27195 +}
27196 +
27197 +/*
27198 + * returns tri-state.
27199 + * plus: processed without an error
27200 + * zero: unprocessed
27201 + * minus: error
27202 + */
27203 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27204 +                    struct au_opts *opts)
27205 +{
27206 +       int err, do_refresh;
27207 +
27208 +       err = 0;
27209 +       switch (opt->type) {
27210 +       case Opt_append:
27211 +               opt->add.bindex = au_sbbot(sb) + 1;
27212 +               if (opt->add.bindex < 0)
27213 +                       opt->add.bindex = 0;
27214 +               goto add;
27215 +               /* Always goto add, not fallthrough */
27216 +       case Opt_prepend:
27217 +               opt->add.bindex = 0;
27218 +               /* fallthrough */
27219 +       add: /* indented label */
27220 +       case Opt_add:
27221 +               err = au_br_add(sb, &opt->add,
27222 +                               au_ftest_opts(opts->flags, REMOUNT));
27223 +               if (!err) {
27224 +                       err = 1;
27225 +                       au_fset_opts(opts->flags, REFRESH);
27226 +               }
27227 +               break;
27228 +
27229 +       case Opt_del:
27230 +       case Opt_idel:
27231 +               err = au_br_del(sb, &opt->del,
27232 +                               au_ftest_opts(opts->flags, REMOUNT));
27233 +               if (!err) {
27234 +                       err = 1;
27235 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27236 +                       au_fset_opts(opts->flags, REFRESH);
27237 +               }
27238 +               break;
27239 +
27240 +       case Opt_mod:
27241 +       case Opt_imod:
27242 +               err = au_br_mod(sb, &opt->mod,
27243 +                               au_ftest_opts(opts->flags, REMOUNT),
27244 +                               &do_refresh);
27245 +               if (!err) {
27246 +                       err = 1;
27247 +                       if (do_refresh)
27248 +                               au_fset_opts(opts->flags, REFRESH);
27249 +               }
27250 +               break;
27251 +       }
27252 +       return err;
27253 +}
27254 +
27255 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27256 +                      struct au_opt_xino **opt_xino,
27257 +                      struct au_opts *opts)
27258 +{
27259 +       int err;
27260 +
27261 +       err = 0;
27262 +       switch (opt->type) {
27263 +       case Opt_xino:
27264 +               err = au_xino_set(sb, &opt->xino,
27265 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27266 +               if (unlikely(err))
27267 +                       break;
27268 +
27269 +               *opt_xino = &opt->xino;
27270 +               break;
27271 +
27272 +       case Opt_noxino:
27273 +               au_xino_clr(sb);
27274 +               *opt_xino = (void *)-1;
27275 +               break;
27276 +       }
27277 +
27278 +       return err;
27279 +}
27280 +
27281 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27282 +                  unsigned int pending)
27283 +{
27284 +       int err, fhsm;
27285 +       aufs_bindex_t bindex, bbot;
27286 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27287 +       struct au_branch *br;
27288 +       struct au_wbr *wbr;
27289 +       struct dentry *root, *dentry;
27290 +       struct inode *dir, *h_dir;
27291 +       struct au_sbinfo *sbinfo;
27292 +       struct au_hinode *hdir;
27293 +
27294 +       SiMustAnyLock(sb);
27295 +
27296 +       sbinfo = au_sbi(sb);
27297 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27298 +
27299 +       if (!(sb_flags & SB_RDONLY)) {
27300 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27301 +                       pr_warn("first branch should be rw\n");
27302 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27303 +                       pr_warn_once("shwh should be used with ro\n");
27304 +       }
27305 +
27306 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27307 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27308 +               pr_warn_once("udba=*notify requires xino\n");
27309 +
27310 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27311 +               pr_warn_once("dirperm1 breaks the protection"
27312 +                            " by the permission bits on the lower branch\n");
27313 +
27314 +       err = 0;
27315 +       fhsm = 0;
27316 +       root = sb->s_root;
27317 +       dir = d_inode(root);
27318 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27319 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27320 +                                     UDBA_NONE);
27321 +       bbot = au_sbbot(sb);
27322 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27323 +               skip = 0;
27324 +               h_dir = au_h_iptr(dir, bindex);
27325 +               br = au_sbr(sb, bindex);
27326 +
27327 +               if ((br->br_perm & AuBrAttr_ICEX)
27328 +                   && !h_dir->i_op->listxattr)
27329 +                       br->br_perm &= ~AuBrAttr_ICEX;
27330 +#if 0
27331 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27332 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27333 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27334 +#endif
27335 +
27336 +               do_free = 0;
27337 +               wbr = br->br_wbr;
27338 +               if (wbr)
27339 +                       wbr_wh_read_lock(wbr);
27340 +
27341 +               if (!au_br_writable(br->br_perm)) {
27342 +                       do_free = !!wbr;
27343 +                       skip = (!wbr
27344 +                               || (!wbr->wbr_whbase
27345 +                                   && !wbr->wbr_plink
27346 +                                   && !wbr->wbr_orph));
27347 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27348 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27349 +                       skip = (!wbr || !wbr->wbr_whbase);
27350 +                       if (skip && wbr) {
27351 +                               if (do_plink)
27352 +                                       skip = !!wbr->wbr_plink;
27353 +                               else
27354 +                                       skip = !wbr->wbr_plink;
27355 +                       }
27356 +               } else {
27357 +                       /* skip = (br->br_whbase && br->br_ohph); */
27358 +                       skip = (wbr && wbr->wbr_whbase);
27359 +                       if (skip) {
27360 +                               if (do_plink)
27361 +                                       skip = !!wbr->wbr_plink;
27362 +                               else
27363 +                                       skip = !wbr->wbr_plink;
27364 +                       }
27365 +               }
27366 +               if (wbr)
27367 +                       wbr_wh_read_unlock(wbr);
27368 +
27369 +               if (can_no_dreval) {
27370 +                       dentry = br->br_path.dentry;
27371 +                       spin_lock(&dentry->d_lock);
27372 +                       if (dentry->d_flags &
27373 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27374 +                               can_no_dreval = 0;
27375 +                       spin_unlock(&dentry->d_lock);
27376 +               }
27377 +
27378 +               if (au_br_fhsm(br->br_perm)) {
27379 +                       fhsm++;
27380 +                       AuDebugOn(!br->br_fhsm);
27381 +               }
27382 +
27383 +               if (skip)
27384 +                       continue;
27385 +
27386 +               hdir = au_hi(dir, bindex);
27387 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27388 +               if (wbr)
27389 +                       wbr_wh_write_lock(wbr);
27390 +               err = au_wh_init(br, sb);
27391 +               if (wbr)
27392 +                       wbr_wh_write_unlock(wbr);
27393 +               au_hn_inode_unlock(hdir);
27394 +
27395 +               if (!err && do_free) {
27396 +                       au_kfree_rcu(wbr);
27397 +                       br->br_wbr = NULL;
27398 +               }
27399 +       }
27400 +
27401 +       if (can_no_dreval)
27402 +               au_fset_si(sbinfo, NO_DREVAL);
27403 +       else
27404 +               au_fclr_si(sbinfo, NO_DREVAL);
27405 +
27406 +       if (fhsm >= 2) {
27407 +               au_fset_si(sbinfo, FHSM);
27408 +               for (bindex = bbot; bindex >= 0; bindex--) {
27409 +                       br = au_sbr(sb, bindex);
27410 +                       if (au_br_fhsm(br->br_perm)) {
27411 +                               au_fhsm_set_bottom(sb, bindex);
27412 +                               break;
27413 +                       }
27414 +               }
27415 +       } else {
27416 +               au_fclr_si(sbinfo, FHSM);
27417 +               au_fhsm_set_bottom(sb, -1);
27418 +       }
27419 +
27420 +       return err;
27421 +}
27422 +
27423 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27424 +{
27425 +       int err;
27426 +       unsigned int tmp;
27427 +       aufs_bindex_t bindex, bbot;
27428 +       struct au_opt *opt;
27429 +       struct au_opt_xino *opt_xino, xino;
27430 +       struct au_sbinfo *sbinfo;
27431 +       struct au_branch *br;
27432 +       struct inode *dir;
27433 +
27434 +       SiMustWriteLock(sb);
27435 +
27436 +       err = 0;
27437 +       opt_xino = NULL;
27438 +       opt = opts->opt;
27439 +       while (err >= 0 && opt->type != Opt_tail)
27440 +               err = au_opt_simple(sb, opt++, opts);
27441 +       if (err > 0)
27442 +               err = 0;
27443 +       else if (unlikely(err < 0))
27444 +               goto out;
27445 +
27446 +       /* disable xino and udba temporary */
27447 +       sbinfo = au_sbi(sb);
27448 +       tmp = sbinfo->si_mntflags;
27449 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27450 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27451 +
27452 +       opt = opts->opt;
27453 +       while (err >= 0 && opt->type != Opt_tail)
27454 +               err = au_opt_br(sb, opt++, opts);
27455 +       if (err > 0)
27456 +               err = 0;
27457 +       else if (unlikely(err < 0))
27458 +               goto out;
27459 +
27460 +       bbot = au_sbbot(sb);
27461 +       if (unlikely(bbot < 0)) {
27462 +               err = -EINVAL;
27463 +               pr_err("no branches\n");
27464 +               goto out;
27465 +       }
27466 +
27467 +       if (au_opt_test(tmp, XINO))
27468 +               au_opt_set(sbinfo->si_mntflags, XINO);
27469 +       opt = opts->opt;
27470 +       while (!err && opt->type != Opt_tail)
27471 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27472 +       if (unlikely(err))
27473 +               goto out;
27474 +
27475 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27476 +       if (unlikely(err))
27477 +               goto out;
27478 +
27479 +       /* restore xino */
27480 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27481 +               xino.file = au_xino_def(sb);
27482 +               err = PTR_ERR(xino.file);
27483 +               if (IS_ERR(xino.file))
27484 +                       goto out;
27485 +
27486 +               err = au_xino_set(sb, &xino, /*remount*/0);
27487 +               fput(xino.file);
27488 +               if (unlikely(err))
27489 +                       goto out;
27490 +       }
27491 +
27492 +       /* restore udba */
27493 +       tmp &= AuOptMask_UDBA;
27494 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27495 +       sbinfo->si_mntflags |= tmp;
27496 +       bbot = au_sbbot(sb);
27497 +       for (bindex = 0; bindex <= bbot; bindex++) {
27498 +               br = au_sbr(sb, bindex);
27499 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27500 +               if (unlikely(err))
27501 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27502 +                               bindex, err);
27503 +               /* go on even if err */
27504 +       }
27505 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27506 +               dir = d_inode(sb->s_root);
27507 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27508 +       }
27509 +
27510 +out:
27511 +       return err;
27512 +}
27513 +
27514 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27515 +{
27516 +       int err, rerr;
27517 +       unsigned char no_dreval;
27518 +       struct inode *dir;
27519 +       struct au_opt_xino *opt_xino;
27520 +       struct au_opt *opt;
27521 +       struct au_sbinfo *sbinfo;
27522 +
27523 +       SiMustWriteLock(sb);
27524 +
27525 +       err = au_dr_opt_flush(sb);
27526 +       if (unlikely(err))
27527 +               goto out;
27528 +       au_fset_opts(opts->flags, DR_FLUSHED);
27529 +
27530 +       dir = d_inode(sb->s_root);
27531 +       sbinfo = au_sbi(sb);
27532 +       opt_xino = NULL;
27533 +       opt = opts->opt;
27534 +       while (err >= 0 && opt->type != Opt_tail) {
27535 +               err = au_opt_simple(sb, opt, opts);
27536 +               if (!err)
27537 +                       err = au_opt_br(sb, opt, opts);
27538 +               if (!err)
27539 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27540 +               opt++;
27541 +       }
27542 +       if (err > 0)
27543 +               err = 0;
27544 +       AuTraceErr(err);
27545 +       /* go on even err */
27546 +
27547 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27548 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27549 +       if (unlikely(rerr && !err))
27550 +               err = rerr;
27551 +
27552 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27553 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27554 +
27555 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27556 +               rerr = au_xib_trunc(sb);
27557 +               if (unlikely(rerr && !err))
27558 +                       err = rerr;
27559 +       }
27560 +
27561 +       /* will be handled by the caller */
27562 +       if (!au_ftest_opts(opts->flags, REFRESH)
27563 +           && (opts->given_udba
27564 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27565 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27566 +                   ))
27567 +               au_fset_opts(opts->flags, REFRESH);
27568 +
27569 +       AuDbg("status 0x%x\n", opts->flags);
27570 +
27571 +out:
27572 +       return err;
27573 +}
27574 +
27575 +/* ---------------------------------------------------------------------- */
27576 +
27577 +unsigned int au_opt_udba(struct super_block *sb)
27578 +{
27579 +       return au_mntflags(sb) & AuOptMask_UDBA;
27580 +}
27581 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27582 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27583 +++ linux/fs/aufs/opts.h        2019-07-11 15:42:14.472238057 +0200
27584 @@ -0,0 +1,225 @@
27585 +/* SPDX-License-Identifier: GPL-2.0 */
27586 +/*
27587 + * Copyright (C) 2005-2019 Junjiro R. Okajima
27588 + *
27589 + * This program, aufs is free software; you can redistribute it and/or modify
27590 + * it under the terms of the GNU General Public License as published by
27591 + * the Free Software Foundation; either version 2 of the License, or
27592 + * (at your option) any later version.
27593 + *
27594 + * This program is distributed in the hope that it will be useful,
27595 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27596 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27597 + * GNU General Public License for more details.
27598 + *
27599 + * You should have received a copy of the GNU General Public License
27600 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27601 + */
27602 +
27603 +/*
27604 + * mount options/flags
27605 + */
27606 +
27607 +#ifndef __AUFS_OPTS_H__
27608 +#define __AUFS_OPTS_H__
27609 +
27610 +#ifdef __KERNEL__
27611 +
27612 +#include <linux/path.h>
27613 +
27614 +struct file;
27615 +
27616 +/* ---------------------------------------------------------------------- */
27617 +
27618 +/* mount flags */
27619 +#define AuOpt_XINO             1               /* external inode number bitmap
27620 +                                                  and translation table */
27621 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27622 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27623 +#define AuOpt_UDBA_REVAL       (1 << 3)
27624 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27625 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27626 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27627 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27628 +                                                  bits */
27629 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27630 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27631 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27632 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27633 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27634 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27635 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27636 +
27637 +#ifndef CONFIG_AUFS_HNOTIFY
27638 +#undef AuOpt_UDBA_HNOTIFY
27639 +#define AuOpt_UDBA_HNOTIFY     0
27640 +#endif
27641 +#ifndef CONFIG_AUFS_DIRREN
27642 +#undef AuOpt_DIRREN
27643 +#define AuOpt_DIRREN           0
27644 +#endif
27645 +#ifndef CONFIG_AUFS_SHWH
27646 +#undef AuOpt_SHWH
27647 +#define AuOpt_SHWH             0
27648 +#endif
27649 +
27650 +#define AuOpt_Def      (AuOpt_XINO \
27651 +                        | AuOpt_UDBA_REVAL \
27652 +                        | AuOpt_PLINK \
27653 +                        /* | AuOpt_DIRPERM1 */ \
27654 +                        | AuOpt_WARN_PERM)
27655 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27656 +                        | AuOpt_UDBA_REVAL \
27657 +                        | AuOpt_UDBA_HNOTIFY)
27658 +
27659 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27660 +#define au_opt_set(flags, name) do { \
27661 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27662 +       ((flags) |= AuOpt_##name); \
27663 +} while (0)
27664 +#define au_opt_set_udba(flags, name) do { \
27665 +       (flags) &= ~AuOptMask_UDBA; \
27666 +       ((flags) |= AuOpt_##name); \
27667 +} while (0)
27668 +#define au_opt_clr(flags, name) do { \
27669 +       ((flags) &= ~AuOpt_##name); \
27670 +} while (0)
27671 +
27672 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27673 +{
27674 +#ifdef CONFIG_PROC_FS
27675 +       return mntflags;
27676 +#else
27677 +       return mntflags & ~AuOpt_PLINK;
27678 +#endif
27679 +}
27680 +
27681 +/* ---------------------------------------------------------------------- */
27682 +
27683 +/* policies to select one among multiple writable branches */
27684 +enum {
27685 +       AuWbrCreate_TDP,        /* top down parent */
27686 +       AuWbrCreate_RR,         /* round robin */
27687 +       AuWbrCreate_MFS,        /* most free space */
27688 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27689 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27690 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27691 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27692 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27693 +       AuWbrCreate_PMFS,       /* parent and mfs */
27694 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27695 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27696 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27697 +
27698 +       AuWbrCreate_Def = AuWbrCreate_TDP
27699 +};
27700 +
27701 +enum {
27702 +       AuWbrCopyup_TDP,        /* top down parent */
27703 +       AuWbrCopyup_BUP,        /* bottom up parent */
27704 +       AuWbrCopyup_BU,         /* bottom up */
27705 +
27706 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27707 +};
27708 +
27709 +/* ---------------------------------------------------------------------- */
27710 +
27711 +struct au_opt_add {
27712 +       aufs_bindex_t   bindex;
27713 +       char            *pathname;
27714 +       int             perm;
27715 +       struct path     path;
27716 +};
27717 +
27718 +struct au_opt_del {
27719 +       char            *pathname;
27720 +       struct path     h_path;
27721 +};
27722 +
27723 +struct au_opt_mod {
27724 +       char            *path;
27725 +       int             perm;
27726 +       struct dentry   *h_root;
27727 +};
27728 +
27729 +struct au_opt_xino {
27730 +       char            *path;
27731 +       struct file     *file;
27732 +};
27733 +
27734 +struct au_opt_xino_itrunc {
27735 +       aufs_bindex_t   bindex;
27736 +};
27737 +
27738 +struct au_opt_wbr_create {
27739 +       int                     wbr_create;
27740 +       int                     mfs_second;
27741 +       unsigned long long      mfsrr_watermark;
27742 +};
27743 +
27744 +struct au_opt {
27745 +       int type;
27746 +       union {
27747 +               struct au_opt_xino      xino;
27748 +               struct au_opt_xino_itrunc xino_itrunc;
27749 +               struct au_opt_add       add;
27750 +               struct au_opt_del       del;
27751 +               struct au_opt_mod       mod;
27752 +               int                     dirwh;
27753 +               int                     rdcache;
27754 +               unsigned int            rdblk;
27755 +               unsigned int            rdhash;
27756 +               int                     udba;
27757 +               struct au_opt_wbr_create wbr_create;
27758 +               int                     wbr_copyup;
27759 +               unsigned int            fhsm_second;
27760 +       };
27761 +};
27762 +
27763 +/* opts flags */
27764 +#define AuOpts_REMOUNT         1
27765 +#define AuOpts_REFRESH         (1 << 1)
27766 +#define AuOpts_TRUNC_XIB       (1 << 2)
27767 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27768 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27769 +#define AuOpts_DR_FLUSHED      (1 << 5)
27770 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27771 +#define au_fset_opts(flags, name) \
27772 +       do { (flags) |= AuOpts_##name; } while (0)
27773 +#define au_fclr_opts(flags, name) \
27774 +       do { (flags) &= ~AuOpts_##name; } while (0)
27775 +
27776 +#ifndef CONFIG_AUFS_DIRREN
27777 +#undef AuOpts_DR_FLUSHED
27778 +#define AuOpts_DR_FLUSHED      0
27779 +#endif
27780 +
27781 +struct au_opts {
27782 +       struct au_opt   *opt;
27783 +       int             max_opt;
27784 +
27785 +       unsigned int    given_udba;
27786 +       unsigned int    flags;
27787 +       unsigned long   sb_flags;
27788 +};
27789 +
27790 +/* ---------------------------------------------------------------------- */
27791 +
27792 +/* opts.c */
27793 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27794 +const char *au_optstr_udba(int udba);
27795 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27796 +const char *au_optstr_wbr_create(int wbr_create);
27797 +
27798 +void au_opts_free(struct au_opts *opts);
27799 +struct super_block;
27800 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27801 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27802 +                  unsigned int pending);
27803 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27804 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27805 +
27806 +unsigned int au_opt_udba(struct super_block *sb);
27807 +
27808 +#endif /* __KERNEL__ */
27809 +#endif /* __AUFS_OPTS_H__ */
27810 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27811 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27812 +++ linux/fs/aufs/plink.c       2019-07-11 15:42:14.472238057 +0200
27813 @@ -0,0 +1,516 @@
27814 +// SPDX-License-Identifier: GPL-2.0
27815 +/*
27816 + * Copyright (C) 2005-2019 Junjiro R. Okajima
27817 + *
27818 + * This program, aufs is free software; you can redistribute it and/or modify
27819 + * it under the terms of the GNU General Public License as published by
27820 + * the Free Software Foundation; either version 2 of the License, or
27821 + * (at your option) any later version.
27822 + *
27823 + * This program is distributed in the hope that it will be useful,
27824 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27825 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27826 + * GNU General Public License for more details.
27827 + *
27828 + * You should have received a copy of the GNU General Public License
27829 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27830 + */
27831 +
27832 +/*
27833 + * pseudo-link
27834 + */
27835 +
27836 +#include "aufs.h"
27837 +
27838 +/*
27839 + * the pseudo-link maintenance mode.
27840 + * during a user process maintains the pseudo-links,
27841 + * prohibit adding a new plink and branch manipulation.
27842 + *
27843 + * Flags
27844 + * NOPLM:
27845 + *     For entry functions which will handle plink, and i_mutex is already held
27846 + *     in VFS.
27847 + *     They cannot wait and should return an error at once.
27848 + *     Callers has to check the error.
27849 + * NOPLMW:
27850 + *     For entry functions which will handle plink, but i_mutex is not held
27851 + *     in VFS.
27852 + *     They can wait the plink maintenance mode to finish.
27853 + *
27854 + * They behave like F_SETLK and F_SETLKW.
27855 + * If the caller never handle plink, then both flags are unnecessary.
27856 + */
27857 +
27858 +int au_plink_maint(struct super_block *sb, int flags)
27859 +{
27860 +       int err;
27861 +       pid_t pid, ppid;
27862 +       struct task_struct *parent, *prev;
27863 +       struct au_sbinfo *sbi;
27864 +
27865 +       SiMustAnyLock(sb);
27866 +
27867 +       err = 0;
27868 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27869 +               goto out;
27870 +
27871 +       sbi = au_sbi(sb);
27872 +       pid = sbi->si_plink_maint_pid;
27873 +       if (!pid || pid == current->pid)
27874 +               goto out;
27875 +
27876 +       /* todo: it highly depends upon /sbin/mount.aufs */
27877 +       prev = NULL;
27878 +       parent = current;
27879 +       ppid = 0;
27880 +       rcu_read_lock();
27881 +       while (1) {
27882 +               parent = rcu_dereference(parent->real_parent);
27883 +               if (parent == prev)
27884 +                       break;
27885 +               ppid = task_pid_vnr(parent);
27886 +               if (pid == ppid) {
27887 +                       rcu_read_unlock();
27888 +                       goto out;
27889 +               }
27890 +               prev = parent;
27891 +       }
27892 +       rcu_read_unlock();
27893 +
27894 +       if (au_ftest_lock(flags, NOPLMW)) {
27895 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27896 +               /* AuDebugOn(!lockdep_depth(current)); */
27897 +               while (sbi->si_plink_maint_pid) {
27898 +                       si_read_unlock(sb);
27899 +                       /* gave up wake_up_bit() */
27900 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27901 +
27902 +                       if (au_ftest_lock(flags, FLUSH))
27903 +                               au_nwt_flush(&sbi->si_nowait);
27904 +                       si_noflush_read_lock(sb);
27905 +               }
27906 +       } else if (au_ftest_lock(flags, NOPLM)) {
27907 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27908 +               err = -EAGAIN;
27909 +       }
27910 +
27911 +out:
27912 +       return err;
27913 +}
27914 +
27915 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27916 +{
27917 +       spin_lock(&sbinfo->si_plink_maint_lock);
27918 +       sbinfo->si_plink_maint_pid = 0;
27919 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27920 +       wake_up_all(&sbinfo->si_plink_wq);
27921 +}
27922 +
27923 +int au_plink_maint_enter(struct super_block *sb)
27924 +{
27925 +       int err;
27926 +       struct au_sbinfo *sbinfo;
27927 +
27928 +       err = 0;
27929 +       sbinfo = au_sbi(sb);
27930 +       /* make sure i am the only one in this fs */
27931 +       si_write_lock(sb, AuLock_FLUSH);
27932 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27933 +               spin_lock(&sbinfo->si_plink_maint_lock);
27934 +               if (!sbinfo->si_plink_maint_pid)
27935 +                       sbinfo->si_plink_maint_pid = current->pid;
27936 +               else
27937 +                       err = -EBUSY;
27938 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27939 +       }
27940 +       si_write_unlock(sb);
27941 +
27942 +       return err;
27943 +}
27944 +
27945 +/* ---------------------------------------------------------------------- */
27946 +
27947 +#ifdef CONFIG_AUFS_DEBUG
27948 +void au_plink_list(struct super_block *sb)
27949 +{
27950 +       int i;
27951 +       struct au_sbinfo *sbinfo;
27952 +       struct hlist_bl_head *hbl;
27953 +       struct hlist_bl_node *pos;
27954 +       struct au_icntnr *icntnr;
27955 +
27956 +       SiMustAnyLock(sb);
27957 +
27958 +       sbinfo = au_sbi(sb);
27959 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27960 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27961 +
27962 +       for (i = 0; i < AuPlink_NHASH; i++) {
27963 +               hbl = sbinfo->si_plink + i;
27964 +               hlist_bl_lock(hbl);
27965 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27966 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27967 +               hlist_bl_unlock(hbl);
27968 +       }
27969 +}
27970 +#endif
27971 +
27972 +/* is the inode pseudo-linked? */
27973 +int au_plink_test(struct inode *inode)
27974 +{
27975 +       int found, i;
27976 +       struct au_sbinfo *sbinfo;
27977 +       struct hlist_bl_head *hbl;
27978 +       struct hlist_bl_node *pos;
27979 +       struct au_icntnr *icntnr;
27980 +
27981 +       sbinfo = au_sbi(inode->i_sb);
27982 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27983 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27984 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27985 +
27986 +       found = 0;
27987 +       i = au_plink_hash(inode->i_ino);
27988 +       hbl =  sbinfo->si_plink + i;
27989 +       hlist_bl_lock(hbl);
27990 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27991 +               if (&icntnr->vfs_inode == inode) {
27992 +                       found = 1;
27993 +                       break;
27994 +               }
27995 +       hlist_bl_unlock(hbl);
27996 +       return found;
27997 +}
27998 +
27999 +/* ---------------------------------------------------------------------- */
28000 +
28001 +/*
28002 + * generate a name for plink.
28003 + * the file will be stored under AUFS_WH_PLINKDIR.
28004 + */
28005 +/* 20 is max digits length of ulong 64 */
28006 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28007 +
28008 +static int plink_name(char *name, int len, struct inode *inode,
28009 +                     aufs_bindex_t bindex)
28010 +{
28011 +       int rlen;
28012 +       struct inode *h_inode;
28013 +
28014 +       h_inode = au_h_iptr(inode, bindex);
28015 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28016 +       return rlen;
28017 +}
28018 +
28019 +struct au_do_plink_lkup_args {
28020 +       struct dentry **errp;
28021 +       struct qstr *tgtname;
28022 +       struct dentry *h_parent;
28023 +       struct au_branch *br;
28024 +};
28025 +
28026 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28027 +                                      struct dentry *h_parent,
28028 +                                      struct au_branch *br)
28029 +{
28030 +       struct dentry *h_dentry;
28031 +       struct inode *h_inode;
28032 +
28033 +       h_inode = d_inode(h_parent);
28034 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28035 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
28036 +       inode_unlock_shared(h_inode);
28037 +       return h_dentry;
28038 +}
28039 +
28040 +static void au_call_do_plink_lkup(void *args)
28041 +{
28042 +       struct au_do_plink_lkup_args *a = args;
28043 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28044 +}
28045 +
28046 +/* lookup the plink-ed @inode under the branch at @bindex */
28047 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28048 +{
28049 +       struct dentry *h_dentry, *h_parent;
28050 +       struct au_branch *br;
28051 +       int wkq_err;
28052 +       char a[PLINK_NAME_LEN];
28053 +       struct qstr tgtname = QSTR_INIT(a, 0);
28054 +
28055 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28056 +
28057 +       br = au_sbr(inode->i_sb, bindex);
28058 +       h_parent = br->br_wbr->wbr_plink;
28059 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28060 +
28061 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28062 +               struct au_do_plink_lkup_args args = {
28063 +                       .errp           = &h_dentry,
28064 +                       .tgtname        = &tgtname,
28065 +                       .h_parent       = h_parent,
28066 +                       .br             = br
28067 +               };
28068 +
28069 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28070 +               if (unlikely(wkq_err))
28071 +                       h_dentry = ERR_PTR(wkq_err);
28072 +       } else
28073 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28074 +
28075 +       return h_dentry;
28076 +}
28077 +
28078 +/* create a pseudo-link */
28079 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28080 +                     struct dentry *h_dentry, struct au_branch *br)
28081 +{
28082 +       int err;
28083 +       struct path h_path = {
28084 +               .mnt = au_br_mnt(br)
28085 +       };
28086 +       struct inode *h_dir, *delegated;
28087 +
28088 +       h_dir = d_inode(h_parent);
28089 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28090 +again:
28091 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
28092 +       err = PTR_ERR(h_path.dentry);
28093 +       if (IS_ERR(h_path.dentry))
28094 +               goto out;
28095 +
28096 +       err = 0;
28097 +       /* wh.plink dir is not monitored */
28098 +       /* todo: is it really safe? */
28099 +       if (d_is_positive(h_path.dentry)
28100 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28101 +               delegated = NULL;
28102 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28103 +               if (unlikely(err == -EWOULDBLOCK)) {
28104 +                       pr_warn("cannot retry for NFSv4 delegation"
28105 +                               " for an internal unlink\n");
28106 +                       iput(delegated);
28107 +               }
28108 +               dput(h_path.dentry);
28109 +               h_path.dentry = NULL;
28110 +               if (!err)
28111 +                       goto again;
28112 +       }
28113 +       if (!err && d_is_negative(h_path.dentry)) {
28114 +               delegated = NULL;
28115 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28116 +               if (unlikely(err == -EWOULDBLOCK)) {
28117 +                       pr_warn("cannot retry for NFSv4 delegation"
28118 +                               " for an internal link\n");
28119 +                       iput(delegated);
28120 +               }
28121 +       }
28122 +       dput(h_path.dentry);
28123 +
28124 +out:
28125 +       inode_unlock(h_dir);
28126 +       return err;
28127 +}
28128 +
28129 +struct do_whplink_args {
28130 +       int *errp;
28131 +       struct qstr *tgt;
28132 +       struct dentry *h_parent;
28133 +       struct dentry *h_dentry;
28134 +       struct au_branch *br;
28135 +};
28136 +
28137 +static void call_do_whplink(void *args)
28138 +{
28139 +       struct do_whplink_args *a = args;
28140 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28141 +}
28142 +
28143 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28144 +                  aufs_bindex_t bindex, struct au_branch *br)
28145 +{
28146 +       int err, wkq_err;
28147 +       struct au_wbr *wbr;
28148 +       struct dentry *h_parent;
28149 +       char a[PLINK_NAME_LEN];
28150 +       struct qstr tgtname = QSTR_INIT(a, 0);
28151 +
28152 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28153 +       h_parent = wbr->wbr_plink;
28154 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28155 +
28156 +       /* always superio. */
28157 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28158 +               struct do_whplink_args args = {
28159 +                       .errp           = &err,
28160 +                       .tgt            = &tgtname,
28161 +                       .h_parent       = h_parent,
28162 +                       .h_dentry       = h_dentry,
28163 +                       .br             = br
28164 +               };
28165 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28166 +               if (unlikely(wkq_err))
28167 +                       err = wkq_err;
28168 +       } else
28169 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
28170 +
28171 +       return err;
28172 +}
28173 +
28174 +/*
28175 + * create a new pseudo-link for @h_dentry on @bindex.
28176 + * the linked inode is held in aufs @inode.
28177 + */
28178 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28179 +                    struct dentry *h_dentry)
28180 +{
28181 +       struct super_block *sb;
28182 +       struct au_sbinfo *sbinfo;
28183 +       struct hlist_bl_head *hbl;
28184 +       struct hlist_bl_node *pos;
28185 +       struct au_icntnr *icntnr;
28186 +       int found, err, cnt, i;
28187 +
28188 +       sb = inode->i_sb;
28189 +       sbinfo = au_sbi(sb);
28190 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28191 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28192 +
28193 +       found = au_plink_test(inode);
28194 +       if (found)
28195 +               return;
28196 +
28197 +       i = au_plink_hash(inode->i_ino);
28198 +       hbl = sbinfo->si_plink + i;
28199 +       au_igrab(inode);
28200 +
28201 +       hlist_bl_lock(hbl);
28202 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28203 +               if (&icntnr->vfs_inode == inode) {
28204 +                       found = 1;
28205 +                       break;
28206 +               }
28207 +       }
28208 +       if (!found) {
28209 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28210 +               hlist_bl_add_head(&icntnr->plink, hbl);
28211 +       }
28212 +       hlist_bl_unlock(hbl);
28213 +       if (!found) {
28214 +               cnt = au_hbl_count(hbl);
28215 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28216 +               if (cnt > AUFS_PLINK_WARN)
28217 +                       AuWarn1(msg ", %d\n", cnt);
28218 +#undef msg
28219 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28220 +               if (unlikely(err)) {
28221 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28222 +                       au_hbl_del(&icntnr->plink, hbl);
28223 +                       iput(&icntnr->vfs_inode);
28224 +               }
28225 +       } else
28226 +               iput(&icntnr->vfs_inode);
28227 +}
28228 +
28229 +/* free all plinks */
28230 +void au_plink_put(struct super_block *sb, int verbose)
28231 +{
28232 +       int i, warned;
28233 +       struct au_sbinfo *sbinfo;
28234 +       struct hlist_bl_head *hbl;
28235 +       struct hlist_bl_node *pos, *tmp;
28236 +       struct au_icntnr *icntnr;
28237 +
28238 +       SiMustWriteLock(sb);
28239 +
28240 +       sbinfo = au_sbi(sb);
28241 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28242 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28243 +
28244 +       /* no spin_lock since sbinfo is write-locked */
28245 +       warned = 0;
28246 +       for (i = 0; i < AuPlink_NHASH; i++) {
28247 +               hbl = sbinfo->si_plink + i;
28248 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28249 +                       pr_warn("pseudo-link is not flushed");
28250 +                       warned = 1;
28251 +               }
28252 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28253 +                       iput(&icntnr->vfs_inode);
28254 +               INIT_HLIST_BL_HEAD(hbl);
28255 +       }
28256 +}
28257 +
28258 +void au_plink_clean(struct super_block *sb, int verbose)
28259 +{
28260 +       struct dentry *root;
28261 +
28262 +       root = sb->s_root;
28263 +       aufs_write_lock(root);
28264 +       if (au_opt_test(au_mntflags(sb), PLINK))
28265 +               au_plink_put(sb, verbose);
28266 +       aufs_write_unlock(root);
28267 +}
28268 +
28269 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28270 +{
28271 +       int do_put;
28272 +       aufs_bindex_t btop, bbot, bindex;
28273 +
28274 +       do_put = 0;
28275 +       btop = au_ibtop(inode);
28276 +       bbot = au_ibbot(inode);
28277 +       if (btop >= 0) {
28278 +               for (bindex = btop; bindex <= bbot; bindex++) {
28279 +                       if (!au_h_iptr(inode, bindex)
28280 +                           || au_ii_br_id(inode, bindex) != br_id)
28281 +                               continue;
28282 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28283 +                       do_put = 1;
28284 +                       break;
28285 +               }
28286 +               if (do_put)
28287 +                       for (bindex = btop; bindex <= bbot; bindex++)
28288 +                               if (au_h_iptr(inode, bindex)) {
28289 +                                       do_put = 0;
28290 +                                       break;
28291 +                               }
28292 +       } else
28293 +               do_put = 1;
28294 +
28295 +       return do_put;
28296 +}
28297 +
28298 +/* free the plinks on a branch specified by @br_id */
28299 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28300 +{
28301 +       struct au_sbinfo *sbinfo;
28302 +       struct hlist_bl_head *hbl;
28303 +       struct hlist_bl_node *pos, *tmp;
28304 +       struct au_icntnr *icntnr;
28305 +       struct inode *inode;
28306 +       int i, do_put;
28307 +
28308 +       SiMustWriteLock(sb);
28309 +
28310 +       sbinfo = au_sbi(sb);
28311 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28312 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28313 +
28314 +       /* no bit_lock since sbinfo is write-locked */
28315 +       for (i = 0; i < AuPlink_NHASH; i++) {
28316 +               hbl = sbinfo->si_plink + i;
28317 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28318 +                       inode = au_igrab(&icntnr->vfs_inode);
28319 +                       ii_write_lock_child(inode);
28320 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28321 +                       if (do_put) {
28322 +                               hlist_bl_del(&icntnr->plink);
28323 +                               iput(inode);
28324 +                       }
28325 +                       ii_write_unlock(inode);
28326 +                       iput(inode);
28327 +               }
28328 +       }
28329 +}
28330 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28331 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28332 +++ linux/fs/aufs/poll.c        2019-07-11 15:42:14.472238057 +0200
28333 @@ -0,0 +1,51 @@
28334 +// SPDX-License-Identifier: GPL-2.0
28335 +/*
28336 + * Copyright (C) 2005-2019 Junjiro R. Okajima
28337 + *
28338 + * This program, aufs is free software; you can redistribute it and/or modify
28339 + * it under the terms of the GNU General Public License as published by
28340 + * the Free Software Foundation; either version 2 of the License, or
28341 + * (at your option) any later version.
28342 + *
28343 + * This program is distributed in the hope that it will be useful,
28344 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28345 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28346 + * GNU General Public License for more details.
28347 + *
28348 + * You should have received a copy of the GNU General Public License
28349 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28350 + */
28351 +
28352 +/*
28353 + * poll operation
28354 + * There is only one filesystem which implements ->poll operation, currently.
28355 + */
28356 +
28357 +#include "aufs.h"
28358 +
28359 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28360 +{
28361 +       __poll_t mask;
28362 +       struct file *h_file;
28363 +       struct super_block *sb;
28364 +
28365 +       /* We should pretend an error happened. */
28366 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28367 +       sb = file->f_path.dentry->d_sb;
28368 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28369 +
28370 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28371 +       if (IS_ERR(h_file)) {
28372 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28373 +               goto out;
28374 +       }
28375 +
28376 +       mask = vfs_poll(h_file, pt);
28377 +       fput(h_file); /* instead of au_read_post() */
28378 +
28379 +out:
28380 +       si_read_unlock(sb);
28381 +       if (mask & EPOLLERR)
28382 +               AuDbg("mask 0x%x\n", mask);
28383 +       return mask;
28384 +}
28385 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28386 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28387 +++ linux/fs/aufs/posix_acl.c   2019-11-25 12:42:00.275843147 +0100
28388 @@ -0,0 +1,105 @@
28389 +// SPDX-License-Identifier: GPL-2.0
28390 +/*
28391 + * Copyright (C) 2014-2019 Junjiro R. Okajima
28392 + *
28393 + * This program, aufs is free software; you can redistribute it and/or modify
28394 + * it under the terms of the GNU General Public License as published by
28395 + * the Free Software Foundation; either version 2 of the License, or
28396 + * (at your option) any later version.
28397 + *
28398 + * This program is distributed in the hope that it will be useful,
28399 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28400 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28401 + * GNU General Public License for more details.
28402 + *
28403 + * You should have received a copy of the GNU General Public License
28404 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28405 + */
28406 +
28407 +/*
28408 + * posix acl operations
28409 + */
28410 +
28411 +#include <linux/fs.h>
28412 +#include "aufs.h"
28413 +
28414 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28415 +{
28416 +       struct posix_acl *acl;
28417 +       int err;
28418 +       aufs_bindex_t bindex;
28419 +       struct inode *h_inode;
28420 +       struct super_block *sb;
28421 +
28422 +       acl = NULL;
28423 +       sb = inode->i_sb;
28424 +       si_read_lock(sb, AuLock_FLUSH);
28425 +       ii_read_lock_child(inode);
28426 +       if (!(sb->s_flags & SB_POSIXACL))
28427 +               goto out;
28428 +
28429 +       bindex = au_ibtop(inode);
28430 +       h_inode = au_h_iptr(inode, bindex);
28431 +       if (unlikely(!h_inode
28432 +                    || ((h_inode->i_mode & S_IFMT)
28433 +                        != (inode->i_mode & S_IFMT)))) {
28434 +               err = au_busy_or_stale();
28435 +               acl = ERR_PTR(err);
28436 +               goto out;
28437 +       }
28438 +
28439 +       /* always topmost only */
28440 +       acl = get_acl(h_inode, type);
28441 +       if (IS_ERR(acl))
28442 +               forget_cached_acl(inode, type);
28443 +       else
28444 +               set_cached_acl(inode, type, acl);
28445 +
28446 +out:
28447 +       ii_read_unlock(inode);
28448 +       si_read_unlock(sb);
28449 +
28450 +       AuTraceErrPtr(acl);
28451 +       return acl;
28452 +}
28453 +
28454 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28455 +{
28456 +       int err;
28457 +       ssize_t ssz;
28458 +       struct dentry *dentry;
28459 +       struct au_sxattr arg = {
28460 +               .type = AU_ACL_SET,
28461 +               .u.acl_set = {
28462 +                       .acl    = acl,
28463 +                       .type   = type
28464 +               },
28465 +       };
28466 +
28467 +       IMustLock(inode);
28468 +
28469 +       if (inode->i_ino == AUFS_ROOT_INO)
28470 +               dentry = dget(inode->i_sb->s_root);
28471 +       else {
28472 +               dentry = d_find_alias(inode);
28473 +               if (!dentry)
28474 +                       dentry = d_find_any_alias(inode);
28475 +               if (!dentry) {
28476 +                       pr_warn("cannot handle this inode, "
28477 +                               "please report to aufs-users ML\n");
28478 +                       err = -ENOENT;
28479 +                       goto out;
28480 +               }
28481 +       }
28482 +
28483 +       ssz = au_sxattr(dentry, inode, &arg);
28484 +       /* forget even it if succeeds since the branch might set differently */
28485 +       forget_cached_acl(inode, type);
28486 +       dput(dentry);
28487 +       err = ssz;
28488 +       if (ssz >= 0)
28489 +               err = 0;
28490 +
28491 +out:
28492 +       return err;
28493 +}
28494 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28495 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28496 +++ linux/fs/aufs/procfs.c      2019-07-11 15:42:14.472238057 +0200
28497 @@ -0,0 +1,171 @@
28498 +// SPDX-License-Identifier: GPL-2.0
28499 +/*
28500 + * Copyright (C) 2010-2019 Junjiro R. Okajima
28501 + *
28502 + * This program, aufs is free software; you can redistribute it and/or modify
28503 + * it under the terms of the GNU General Public License as published by
28504 + * the Free Software Foundation; either version 2 of the License, or
28505 + * (at your option) any later version.
28506 + *
28507 + * This program is distributed in the hope that it will be useful,
28508 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28509 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28510 + * GNU General Public License for more details.
28511 + *
28512 + * You should have received a copy of the GNU General Public License
28513 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28514 + */
28515 +
28516 +/*
28517 + * procfs interfaces
28518 + */
28519 +
28520 +#include <linux/proc_fs.h>
28521 +#include "aufs.h"
28522 +
28523 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28524 +{
28525 +       struct au_sbinfo *sbinfo;
28526 +
28527 +       sbinfo = file->private_data;
28528 +       if (sbinfo) {
28529 +               au_plink_maint_leave(sbinfo);
28530 +               kobject_put(&sbinfo->si_kobj);
28531 +       }
28532 +
28533 +       return 0;
28534 +}
28535 +
28536 +static void au_procfs_plm_write_clean(struct file *file)
28537 +{
28538 +       struct au_sbinfo *sbinfo;
28539 +
28540 +       sbinfo = file->private_data;
28541 +       if (sbinfo)
28542 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28543 +}
28544 +
28545 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28546 +{
28547 +       int err;
28548 +       struct super_block *sb;
28549 +       struct au_sbinfo *sbinfo;
28550 +       struct hlist_bl_node *pos;
28551 +
28552 +       err = -EBUSY;
28553 +       if (unlikely(file->private_data))
28554 +               goto out;
28555 +
28556 +       sb = NULL;
28557 +       /* don't use au_sbilist_lock() here */
28558 +       hlist_bl_lock(&au_sbilist);
28559 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28560 +               if (id == sysaufs_si_id(sbinfo)) {
28561 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28562 +                               sb = sbinfo->si_sb;
28563 +                       break;
28564 +               }
28565 +       hlist_bl_unlock(&au_sbilist);
28566 +
28567 +       err = -EINVAL;
28568 +       if (unlikely(!sb))
28569 +               goto out;
28570 +
28571 +       err = au_plink_maint_enter(sb);
28572 +       if (!err)
28573 +               /* keep kobject_get() */
28574 +               file->private_data = sbinfo;
28575 +       else
28576 +               kobject_put(&sbinfo->si_kobj);
28577 +out:
28578 +       return err;
28579 +}
28580 +
28581 +/*
28582 + * Accept a valid "si=xxxx" only.
28583 + * Once it is accepted successfully, accept "clean" too.
28584 + */
28585 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28586 +                                  size_t count, loff_t *ppos)
28587 +{
28588 +       ssize_t err;
28589 +       unsigned long id;
28590 +       /* last newline is allowed */
28591 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28592 +
28593 +       err = -EACCES;
28594 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28595 +               goto out;
28596 +
28597 +       err = -EINVAL;
28598 +       if (unlikely(count > sizeof(buf)))
28599 +               goto out;
28600 +
28601 +       err = copy_from_user(buf, ubuf, count);
28602 +       if (unlikely(err)) {
28603 +               err = -EFAULT;
28604 +               goto out;
28605 +       }
28606 +       buf[count] = 0;
28607 +
28608 +       err = -EINVAL;
28609 +       if (!strcmp("clean", buf)) {
28610 +               au_procfs_plm_write_clean(file);
28611 +               goto out_success;
28612 +       } else if (unlikely(strncmp("si=", buf, 3)))
28613 +               goto out;
28614 +
28615 +       err = kstrtoul(buf + 3, 16, &id);
28616 +       if (unlikely(err))
28617 +               goto out;
28618 +
28619 +       err = au_procfs_plm_write_si(file, id);
28620 +       if (unlikely(err))
28621 +               goto out;
28622 +
28623 +out_success:
28624 +       err = count; /* success */
28625 +out:
28626 +       return err;
28627 +}
28628 +
28629 +static const struct file_operations au_procfs_plm_fop = {
28630 +       .write          = au_procfs_plm_write,
28631 +       .release        = au_procfs_plm_release,
28632 +       .owner          = THIS_MODULE
28633 +};
28634 +
28635 +/* ---------------------------------------------------------------------- */
28636 +
28637 +static struct proc_dir_entry *au_procfs_dir;
28638 +
28639 +void au_procfs_fin(void)
28640 +{
28641 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28642 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28643 +}
28644 +
28645 +int __init au_procfs_init(void)
28646 +{
28647 +       int err;
28648 +       struct proc_dir_entry *entry;
28649 +
28650 +       err = -ENOMEM;
28651 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28652 +       if (unlikely(!au_procfs_dir))
28653 +               goto out;
28654 +
28655 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28656 +                           au_procfs_dir, &au_procfs_plm_fop);
28657 +       if (unlikely(!entry))
28658 +               goto out_dir;
28659 +
28660 +       err = 0;
28661 +       goto out; /* success */
28662 +
28663 +
28664 +out_dir:
28665 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28666 +out:
28667 +       return err;
28668 +}
28669 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28670 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28671 +++ linux/fs/aufs/rdu.c 2019-07-11 15:42:14.472238057 +0200
28672 @@ -0,0 +1,384 @@
28673 +// SPDX-License-Identifier: GPL-2.0
28674 +/*
28675 + * Copyright (C) 2005-2019 Junjiro R. Okajima
28676 + *
28677 + * This program, aufs is free software; you can redistribute it and/or modify
28678 + * it under the terms of the GNU General Public License as published by
28679 + * the Free Software Foundation; either version 2 of the License, or
28680 + * (at your option) any later version.
28681 + *
28682 + * This program is distributed in the hope that it will be useful,
28683 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28684 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28685 + * GNU General Public License for more details.
28686 + *
28687 + * You should have received a copy of the GNU General Public License
28688 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28689 + */
28690 +
28691 +/*
28692 + * readdir in userspace.
28693 + */
28694 +
28695 +#include <linux/compat.h>
28696 +#include <linux/fs_stack.h>
28697 +#include <linux/security.h>
28698 +#include "aufs.h"
28699 +
28700 +/* bits for struct aufs_rdu.flags */
28701 +#define        AuRdu_CALLED    1
28702 +#define        AuRdu_CONT      (1 << 1)
28703 +#define        AuRdu_FULL      (1 << 2)
28704 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28705 +#define au_fset_rdu(flags, name) \
28706 +       do { (flags) |= AuRdu_##name; } while (0)
28707 +#define au_fclr_rdu(flags, name) \
28708 +       do { (flags) &= ~AuRdu_##name; } while (0)
28709 +
28710 +struct au_rdu_arg {
28711 +       struct dir_context              ctx;
28712 +       struct aufs_rdu                 *rdu;
28713 +       union au_rdu_ent_ul             ent;
28714 +       unsigned long                   end;
28715 +
28716 +       struct super_block              *sb;
28717 +       int                             err;
28718 +};
28719 +
28720 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28721 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28722 +{
28723 +       int err, len;
28724 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28725 +       struct aufs_rdu *rdu = arg->rdu;
28726 +       struct au_rdu_ent ent;
28727 +
28728 +       err = 0;
28729 +       arg->err = 0;
28730 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28731 +       len = au_rdu_len(nlen);
28732 +       if (arg->ent.ul + len  < arg->end) {
28733 +               ent.ino = h_ino;
28734 +               ent.bindex = rdu->cookie.bindex;
28735 +               ent.type = d_type;
28736 +               ent.nlen = nlen;
28737 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28738 +                       ent.type = DT_UNKNOWN;
28739 +
28740 +               /* unnecessary to support mmap_sem since this is a dir */
28741 +               err = -EFAULT;
28742 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28743 +                       goto out;
28744 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28745 +                       goto out;
28746 +               /* the terminating NULL */
28747 +               if (__put_user(0, arg->ent.e->name + nlen))
28748 +                       goto out;
28749 +               err = 0;
28750 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28751 +               arg->ent.ul += len;
28752 +               rdu->rent++;
28753 +       } else {
28754 +               err = -EFAULT;
28755 +               au_fset_rdu(rdu->cookie.flags, FULL);
28756 +               rdu->full = 1;
28757 +               rdu->tail = arg->ent;
28758 +       }
28759 +
28760 +out:
28761 +       /* AuTraceErr(err); */
28762 +       return err;
28763 +}
28764 +
28765 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28766 +{
28767 +       int err;
28768 +       loff_t offset;
28769 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28770 +
28771 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28772 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28773 +       err = offset;
28774 +       if (unlikely(offset != cookie->h_pos))
28775 +               goto out;
28776 +
28777 +       err = 0;
28778 +       do {
28779 +               arg->err = 0;
28780 +               au_fclr_rdu(cookie->flags, CALLED);
28781 +               /* smp_mb(); */
28782 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28783 +               if (err >= 0)
28784 +                       err = arg->err;
28785 +       } while (!err
28786 +                && au_ftest_rdu(cookie->flags, CALLED)
28787 +                && !au_ftest_rdu(cookie->flags, FULL));
28788 +       cookie->h_pos = h_file->f_pos;
28789 +
28790 +out:
28791 +       AuTraceErr(err);
28792 +       return err;
28793 +}
28794 +
28795 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28796 +{
28797 +       int err;
28798 +       aufs_bindex_t bbot;
28799 +       struct au_rdu_arg arg = {
28800 +               .ctx = {
28801 +                       .actor = au_rdu_fill
28802 +               }
28803 +       };
28804 +       struct dentry *dentry;
28805 +       struct inode *inode;
28806 +       struct file *h_file;
28807 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28808 +
28809 +       /* VERIFY_WRITE */
28810 +       err = !access_ok(rdu->ent.e, rdu->sz);
28811 +       if (unlikely(err)) {
28812 +               err = -EFAULT;
28813 +               AuTraceErr(err);
28814 +               goto out;
28815 +       }
28816 +       rdu->rent = 0;
28817 +       rdu->tail = rdu->ent;
28818 +       rdu->full = 0;
28819 +       arg.rdu = rdu;
28820 +       arg.ent = rdu->ent;
28821 +       arg.end = arg.ent.ul;
28822 +       arg.end += rdu->sz;
28823 +
28824 +       err = -ENOTDIR;
28825 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28826 +               goto out;
28827 +
28828 +       err = security_file_permission(file, MAY_READ);
28829 +       AuTraceErr(err);
28830 +       if (unlikely(err))
28831 +               goto out;
28832 +
28833 +       dentry = file->f_path.dentry;
28834 +       inode = d_inode(dentry);
28835 +       inode_lock_shared(inode);
28836 +
28837 +       arg.sb = inode->i_sb;
28838 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28839 +       if (unlikely(err))
28840 +               goto out_mtx;
28841 +       err = au_alive_dir(dentry);
28842 +       if (unlikely(err))
28843 +               goto out_si;
28844 +       /* todo: reval? */
28845 +       fi_read_lock(file);
28846 +
28847 +       err = -EAGAIN;
28848 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28849 +                    && cookie->generation != au_figen(file)))
28850 +               goto out_unlock;
28851 +
28852 +       err = 0;
28853 +       if (!rdu->blk) {
28854 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28855 +               if (!rdu->blk)
28856 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28857 +       }
28858 +       bbot = au_fbtop(file);
28859 +       if (cookie->bindex < bbot)
28860 +               cookie->bindex = bbot;
28861 +       bbot = au_fbbot_dir(file);
28862 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28863 +       for (; !err && cookie->bindex <= bbot;
28864 +            cookie->bindex++, cookie->h_pos = 0) {
28865 +               h_file = au_hf_dir(file, cookie->bindex);
28866 +               if (!h_file)
28867 +                       continue;
28868 +
28869 +               au_fclr_rdu(cookie->flags, FULL);
28870 +               err = au_rdu_do(h_file, &arg);
28871 +               AuTraceErr(err);
28872 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28873 +                       break;
28874 +       }
28875 +       AuDbg("rent %llu\n", rdu->rent);
28876 +
28877 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28878 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28879 +               au_fset_rdu(cookie->flags, CONT);
28880 +               cookie->generation = au_figen(file);
28881 +       }
28882 +
28883 +       ii_read_lock_child(inode);
28884 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28885 +       ii_read_unlock(inode);
28886 +
28887 +out_unlock:
28888 +       fi_read_unlock(file);
28889 +out_si:
28890 +       si_read_unlock(arg.sb);
28891 +out_mtx:
28892 +       inode_unlock_shared(inode);
28893 +out:
28894 +       AuTraceErr(err);
28895 +       return err;
28896 +}
28897 +
28898 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28899 +{
28900 +       int err;
28901 +       ino_t ino;
28902 +       unsigned long long nent;
28903 +       union au_rdu_ent_ul *u;
28904 +       struct au_rdu_ent ent;
28905 +       struct super_block *sb;
28906 +
28907 +       err = 0;
28908 +       nent = rdu->nent;
28909 +       u = &rdu->ent;
28910 +       sb = file->f_path.dentry->d_sb;
28911 +       si_read_lock(sb, AuLock_FLUSH);
28912 +       while (nent-- > 0) {
28913 +               /* unnecessary to support mmap_sem since this is a dir */
28914 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28915 +               if (!err)
28916 +                       /* VERIFY_WRITE */
28917 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28918 +               if (unlikely(err)) {
28919 +                       err = -EFAULT;
28920 +                       AuTraceErr(err);
28921 +                       break;
28922 +               }
28923 +
28924 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28925 +               if (!ent.wh)
28926 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28927 +               else
28928 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28929 +                                       &ino);
28930 +               if (unlikely(err)) {
28931 +                       AuTraceErr(err);
28932 +                       break;
28933 +               }
28934 +
28935 +               err = __put_user(ino, &u->e->ino);
28936 +               if (unlikely(err)) {
28937 +                       err = -EFAULT;
28938 +                       AuTraceErr(err);
28939 +                       break;
28940 +               }
28941 +               u->ul += au_rdu_len(ent.nlen);
28942 +       }
28943 +       si_read_unlock(sb);
28944 +
28945 +       return err;
28946 +}
28947 +
28948 +/* ---------------------------------------------------------------------- */
28949 +
28950 +static int au_rdu_verify(struct aufs_rdu *rdu)
28951 +{
28952 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28953 +             "%llu, b%d, 0x%x, g%u}\n",
28954 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28955 +             rdu->blk,
28956 +             rdu->rent, rdu->shwh, rdu->full,
28957 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28958 +             rdu->cookie.generation);
28959 +
28960 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28961 +               return 0;
28962 +
28963 +       AuDbg("%u:%u\n",
28964 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28965 +       return -EINVAL;
28966 +}
28967 +
28968 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28969 +{
28970 +       long err, e;
28971 +       struct aufs_rdu rdu;
28972 +       void __user *p = (void __user *)arg;
28973 +
28974 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28975 +       if (unlikely(err)) {
28976 +               err = -EFAULT;
28977 +               AuTraceErr(err);
28978 +               goto out;
28979 +       }
28980 +       err = au_rdu_verify(&rdu);
28981 +       if (unlikely(err))
28982 +               goto out;
28983 +
28984 +       switch (cmd) {
28985 +       case AUFS_CTL_RDU:
28986 +               err = au_rdu(file, &rdu);
28987 +               if (unlikely(err))
28988 +                       break;
28989 +
28990 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28991 +               if (unlikely(e)) {
28992 +                       err = -EFAULT;
28993 +                       AuTraceErr(err);
28994 +               }
28995 +               break;
28996 +       case AUFS_CTL_RDU_INO:
28997 +               err = au_rdu_ino(file, &rdu);
28998 +               break;
28999 +
29000 +       default:
29001 +               /* err = -ENOTTY; */
29002 +               err = -EINVAL;
29003 +       }
29004 +
29005 +out:
29006 +       AuTraceErr(err);
29007 +       return err;
29008 +}
29009 +
29010 +#ifdef CONFIG_COMPAT
29011 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29012 +{
29013 +       long err, e;
29014 +       struct aufs_rdu rdu;
29015 +       void __user *p = compat_ptr(arg);
29016 +
29017 +       /* todo: get_user()? */
29018 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29019 +       if (unlikely(err)) {
29020 +               err = -EFAULT;
29021 +               AuTraceErr(err);
29022 +               goto out;
29023 +       }
29024 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29025 +       err = au_rdu_verify(&rdu);
29026 +       if (unlikely(err))
29027 +               goto out;
29028 +
29029 +       switch (cmd) {
29030 +       case AUFS_CTL_RDU:
29031 +               err = au_rdu(file, &rdu);
29032 +               if (unlikely(err))
29033 +                       break;
29034 +
29035 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29036 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29037 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29038 +               if (unlikely(e)) {
29039 +                       err = -EFAULT;
29040 +                       AuTraceErr(err);
29041 +               }
29042 +               break;
29043 +       case AUFS_CTL_RDU_INO:
29044 +               err = au_rdu_ino(file, &rdu);
29045 +               break;
29046 +
29047 +       default:
29048 +               /* err = -ENOTTY; */
29049 +               err = -EINVAL;
29050 +       }
29051 +
29052 +out:
29053 +       AuTraceErr(err);
29054 +       return err;
29055 +}
29056 +#endif
29057 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29058 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29059 +++ linux/fs/aufs/rwsem.h       2019-07-11 15:42:14.472238057 +0200
29060 @@ -0,0 +1,73 @@
29061 +/* SPDX-License-Identifier: GPL-2.0 */
29062 +/*
29063 + * Copyright (C) 2005-2019 Junjiro R. Okajima
29064 + *
29065 + * This program, aufs is free software; you can redistribute it and/or modify
29066 + * it under the terms of the GNU General Public License as published by
29067 + * the Free Software Foundation; either version 2 of the License, or
29068 + * (at your option) any later version.
29069 + *
29070 + * This program is distributed in the hope that it will be useful,
29071 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29072 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29073 + * GNU General Public License for more details.
29074 + *
29075 + * You should have received a copy of the GNU General Public License
29076 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29077 + */
29078 +
29079 +/*
29080 + * simple read-write semaphore wrappers
29081 + */
29082 +
29083 +#ifndef __AUFS_RWSEM_H__
29084 +#define __AUFS_RWSEM_H__
29085 +
29086 +#ifdef __KERNEL__
29087 +
29088 +#include "debug.h"
29089 +
29090 +/* in the future, the name 'au_rwsem' will be totally gone */
29091 +#define au_rwsem       rw_semaphore
29092 +
29093 +/* to debug easier, do not make them inlined functions */
29094 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29095 +/* rwsem_is_locked() is unusable */
29096 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
29097 +                                         && debug_locks \
29098 +                                         && !lockdep_is_held_type(rw, 1))
29099 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
29100 +                                         && debug_locks \
29101 +                                         && !lockdep_is_held_type(rw, 0))
29102 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
29103 +                                         && debug_locks \
29104 +                                         && !lockdep_is_held(rw))
29105 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
29106 +                                         && debug_locks \
29107 +                                         && lockdep_is_held(rw))
29108 +
29109 +#define au_rw_init(rw) init_rwsem(rw)
29110 +
29111 +#define au_rw_init_wlock(rw) do {              \
29112 +               au_rw_init(rw);                 \
29113 +               down_write(rw);                 \
29114 +       } while (0)
29115 +
29116 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29117 +               au_rw_init(rw);                 \
29118 +               down_write_nested(rw, lsc);     \
29119 +       } while (0)
29120 +
29121 +#define au_rw_read_lock(rw)            down_read(rw)
29122 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29123 +#define au_rw_read_unlock(rw)          up_read(rw)
29124 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29125 +#define au_rw_write_lock(rw)           down_write(rw)
29126 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29127 +#define au_rw_write_unlock(rw)         up_write(rw)
29128 +/* why is not _nested version defined? */
29129 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29130 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29131 +
29132 +#endif /* __KERNEL__ */
29133 +#endif /* __AUFS_RWSEM_H__ */
29134 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29135 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29136 +++ linux/fs/aufs/sbinfo.c      2019-07-11 15:42:14.472238057 +0200
29137 @@ -0,0 +1,314 @@
29138 +// SPDX-License-Identifier: GPL-2.0
29139 +/*
29140 + * Copyright (C) 2005-2019 Junjiro R. Okajima
29141 + *
29142 + * This program, aufs is free software; you can redistribute it and/or modify
29143 + * it under the terms of the GNU General Public License as published by
29144 + * the Free Software Foundation; either version 2 of the License, or
29145 + * (at your option) any later version.
29146 + *
29147 + * This program is distributed in the hope that it will be useful,
29148 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29149 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29150 + * GNU General Public License for more details.
29151 + *
29152 + * You should have received a copy of the GNU General Public License
29153 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29154 + */
29155 +
29156 +/*
29157 + * superblock private data
29158 + */
29159 +
29160 +#include <linux/iversion.h>
29161 +#include "aufs.h"
29162 +
29163 +/*
29164 + * they are necessary regardless sysfs is disabled.
29165 + */
29166 +void au_si_free(struct kobject *kobj)
29167 +{
29168 +       int i;
29169 +       struct au_sbinfo *sbinfo;
29170 +       char *locked __maybe_unused; /* debug only */
29171 +
29172 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29173 +       for (i = 0; i < AuPlink_NHASH; i++)
29174 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29175 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29176 +
29177 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29178 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29179 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29180 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29181 +
29182 +       dbgaufs_si_fin(sbinfo);
29183 +       au_rw_write_lock(&sbinfo->si_rwsem);
29184 +       au_br_free(sbinfo);
29185 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29186 +
29187 +       au_kfree_try_rcu(sbinfo->si_branch);
29188 +       mutex_destroy(&sbinfo->si_xib_mtx);
29189 +       AuRwDestroy(&sbinfo->si_rwsem);
29190 +
29191 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29192 +       /* si_nfiles is waited too */
29193 +       au_kfree_rcu(sbinfo);
29194 +}
29195 +
29196 +int au_si_alloc(struct super_block *sb)
29197 +{
29198 +       int err, i;
29199 +       struct au_sbinfo *sbinfo;
29200 +
29201 +       err = -ENOMEM;
29202 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29203 +       if (unlikely(!sbinfo))
29204 +               goto out;
29205 +
29206 +       /* will be reallocated separately */
29207 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29208 +       if (unlikely(!sbinfo->si_branch))
29209 +               goto out_sbinfo;
29210 +
29211 +       err = sysaufs_si_init(sbinfo);
29212 +       if (!err) {
29213 +               dbgaufs_si_null(sbinfo);
29214 +               err = dbgaufs_si_init(sbinfo);
29215 +               if (unlikely(err))
29216 +                       kobject_put(&sbinfo->si_kobj);
29217 +       }
29218 +       if (unlikely(err))
29219 +               goto out_br;
29220 +
29221 +       au_nwt_init(&sbinfo->si_nowait);
29222 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29223 +
29224 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29225 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29226 +
29227 +       sbinfo->si_bbot = -1;
29228 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29229 +
29230 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29231 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29232 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29233 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29234 +
29235 +       au_fhsm_init(sbinfo);
29236 +
29237 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29238 +
29239 +       sbinfo->si_xino_jiffy = jiffies;
29240 +       sbinfo->si_xino_expire
29241 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29242 +       mutex_init(&sbinfo->si_xib_mtx);
29243 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29244 +
29245 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29246 +
29247 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29248 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29249 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29250 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29251 +
29252 +       for (i = 0; i < AuPlink_NHASH; i++)
29253 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29254 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29255 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29256 +
29257 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29258 +
29259 +       /* with getattr by default */
29260 +       sbinfo->si_iop_array = aufs_iop;
29261 +
29262 +       /* leave other members for sysaufs and si_mnt. */
29263 +       sbinfo->si_sb = sb;
29264 +       sb->s_fs_info = sbinfo;
29265 +       si_pid_set(sb);
29266 +       return 0; /* success */
29267 +
29268 +out_br:
29269 +       au_kfree_try_rcu(sbinfo->si_branch);
29270 +out_sbinfo:
29271 +       au_kfree_rcu(sbinfo);
29272 +out:
29273 +       return err;
29274 +}
29275 +
29276 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29277 +{
29278 +       int err, sz;
29279 +       struct au_branch **brp;
29280 +
29281 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29282 +
29283 +       err = -ENOMEM;
29284 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29285 +       if (unlikely(!sz))
29286 +               sz = sizeof(*brp);
29287 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29288 +                          may_shrink);
29289 +       if (brp) {
29290 +               sbinfo->si_branch = brp;
29291 +               err = 0;
29292 +       }
29293 +
29294 +       return err;
29295 +}
29296 +
29297 +/* ---------------------------------------------------------------------- */
29298 +
29299 +unsigned int au_sigen_inc(struct super_block *sb)
29300 +{
29301 +       unsigned int gen;
29302 +       struct inode *inode;
29303 +
29304 +       SiMustWriteLock(sb);
29305 +
29306 +       gen = ++au_sbi(sb)->si_generation;
29307 +       au_update_digen(sb->s_root);
29308 +       inode = d_inode(sb->s_root);
29309 +       au_update_iigen(inode, /*half*/0);
29310 +       inode_inc_iversion(inode);
29311 +       return gen;
29312 +}
29313 +
29314 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29315 +{
29316 +       aufs_bindex_t br_id;
29317 +       int i;
29318 +       struct au_sbinfo *sbinfo;
29319 +
29320 +       SiMustWriteLock(sb);
29321 +
29322 +       sbinfo = au_sbi(sb);
29323 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29324 +               br_id = ++sbinfo->si_last_br_id;
29325 +               AuDebugOn(br_id < 0);
29326 +               if (br_id && au_br_index(sb, br_id) < 0)
29327 +                       return br_id;
29328 +       }
29329 +
29330 +       return -1;
29331 +}
29332 +
29333 +/* ---------------------------------------------------------------------- */
29334 +
29335 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29336 +int si_read_lock(struct super_block *sb, int flags)
29337 +{
29338 +       int err;
29339 +
29340 +       err = 0;
29341 +       if (au_ftest_lock(flags, FLUSH))
29342 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29343 +
29344 +       si_noflush_read_lock(sb);
29345 +       err = au_plink_maint(sb, flags);
29346 +       if (unlikely(err))
29347 +               si_read_unlock(sb);
29348 +
29349 +       return err;
29350 +}
29351 +
29352 +int si_write_lock(struct super_block *sb, int flags)
29353 +{
29354 +       int err;
29355 +
29356 +       if (au_ftest_lock(flags, FLUSH))
29357 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29358 +
29359 +       si_noflush_write_lock(sb);
29360 +       err = au_plink_maint(sb, flags);
29361 +       if (unlikely(err))
29362 +               si_write_unlock(sb);
29363 +
29364 +       return err;
29365 +}
29366 +
29367 +/* dentry and super_block lock. call at entry point */
29368 +int aufs_read_lock(struct dentry *dentry, int flags)
29369 +{
29370 +       int err;
29371 +       struct super_block *sb;
29372 +
29373 +       sb = dentry->d_sb;
29374 +       err = si_read_lock(sb, flags);
29375 +       if (unlikely(err))
29376 +               goto out;
29377 +
29378 +       if (au_ftest_lock(flags, DW))
29379 +               di_write_lock_child(dentry);
29380 +       else
29381 +               di_read_lock_child(dentry, flags);
29382 +
29383 +       if (au_ftest_lock(flags, GEN)) {
29384 +               err = au_digen_test(dentry, au_sigen(sb));
29385 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29386 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29387 +               else if (!err)
29388 +                       err = au_dbrange_test(dentry);
29389 +               if (unlikely(err))
29390 +                       aufs_read_unlock(dentry, flags);
29391 +       }
29392 +
29393 +out:
29394 +       return err;
29395 +}
29396 +
29397 +void aufs_read_unlock(struct dentry *dentry, int flags)
29398 +{
29399 +       if (au_ftest_lock(flags, DW))
29400 +               di_write_unlock(dentry);
29401 +       else
29402 +               di_read_unlock(dentry, flags);
29403 +       si_read_unlock(dentry->d_sb);
29404 +}
29405 +
29406 +void aufs_write_lock(struct dentry *dentry)
29407 +{
29408 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29409 +       di_write_lock_child(dentry);
29410 +}
29411 +
29412 +void aufs_write_unlock(struct dentry *dentry)
29413 +{
29414 +       di_write_unlock(dentry);
29415 +       si_write_unlock(dentry->d_sb);
29416 +}
29417 +
29418 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29419 +{
29420 +       int err;
29421 +       unsigned int sigen;
29422 +       struct super_block *sb;
29423 +
29424 +       sb = d1->d_sb;
29425 +       err = si_read_lock(sb, flags);
29426 +       if (unlikely(err))
29427 +               goto out;
29428 +
29429 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29430 +
29431 +       if (au_ftest_lock(flags, GEN)) {
29432 +               sigen = au_sigen(sb);
29433 +               err = au_digen_test(d1, sigen);
29434 +               AuDebugOn(!err && au_dbrange_test(d1));
29435 +               if (!err) {
29436 +                       err = au_digen_test(d2, sigen);
29437 +                       AuDebugOn(!err && au_dbrange_test(d2));
29438 +               }
29439 +               if (unlikely(err))
29440 +                       aufs_read_and_write_unlock2(d1, d2);
29441 +       }
29442 +
29443 +out:
29444 +       return err;
29445 +}
29446 +
29447 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29448 +{
29449 +       di_write_unlock2(d1, d2);
29450 +       si_read_unlock(d1->d_sb);
29451 +}
29452 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29453 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29454 +++ linux/fs/aufs/super.c       2019-07-11 15:42:14.472238057 +0200
29455 @@ -0,0 +1,1047 @@
29456 +// SPDX-License-Identifier: GPL-2.0
29457 +/*
29458 + * Copyright (C) 2005-2019 Junjiro R. Okajima
29459 + *
29460 + * This program, aufs is free software; you can redistribute it and/or modify
29461 + * it under the terms of the GNU General Public License as published by
29462 + * the Free Software Foundation; either version 2 of the License, or
29463 + * (at your option) any later version.
29464 + *
29465 + * This program is distributed in the hope that it will be useful,
29466 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29467 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29468 + * GNU General Public License for more details.
29469 + *
29470 + * You should have received a copy of the GNU General Public License
29471 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29472 + */
29473 +
29474 +/*
29475 + * mount and super_block operations
29476 + */
29477 +
29478 +#include <linux/iversion.h>
29479 +#include <linux/mm.h>
29480 +#include <linux/seq_file.h>
29481 +#include <linux/statfs.h>
29482 +#include <linux/vmalloc.h>
29483 +#include "aufs.h"
29484 +
29485 +/*
29486 + * super_operations
29487 + */
29488 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29489 +{
29490 +       struct au_icntnr *c;
29491 +
29492 +       c = au_cache_alloc_icntnr();
29493 +       if (c) {
29494 +               au_icntnr_init(c);
29495 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29496 +               c->iinfo.ii_hinode = NULL;
29497 +               return &c->vfs_inode;
29498 +       }
29499 +       return NULL;
29500 +}
29501 +
29502 +static void aufs_destroy_inode(struct inode *inode)
29503 +{
29504 +       if (!au_is_bad_inode(inode))
29505 +               au_iinfo_fin(inode);
29506 +}
29507 +
29508 +static void aufs_free_inode(struct inode *inode)
29509 +{
29510 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29511 +}
29512 +
29513 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29514 +{
29515 +       struct inode *inode;
29516 +       int err;
29517 +
29518 +       inode = iget_locked(sb, ino);
29519 +       if (unlikely(!inode)) {
29520 +               inode = ERR_PTR(-ENOMEM);
29521 +               goto out;
29522 +       }
29523 +       if (!(inode->i_state & I_NEW))
29524 +               goto out;
29525 +
29526 +       err = au_xigen_new(inode);
29527 +       if (!err)
29528 +               err = au_iinfo_init(inode);
29529 +       if (!err)
29530 +               inode_inc_iversion(inode);
29531 +       else {
29532 +               iget_failed(inode);
29533 +               inode = ERR_PTR(err);
29534 +       }
29535 +
29536 +out:
29537 +       /* never return NULL */
29538 +       AuDebugOn(!inode);
29539 +       AuTraceErrPtr(inode);
29540 +       return inode;
29541 +}
29542 +
29543 +/* lock free root dinfo */
29544 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29545 +{
29546 +       int err;
29547 +       aufs_bindex_t bindex, bbot;
29548 +       struct path path;
29549 +       struct au_hdentry *hdp;
29550 +       struct au_branch *br;
29551 +       au_br_perm_str_t perm;
29552 +
29553 +       err = 0;
29554 +       bbot = au_sbbot(sb);
29555 +       bindex = 0;
29556 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29557 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29558 +               br = au_sbr(sb, bindex);
29559 +               path.mnt = au_br_mnt(br);
29560 +               path.dentry = hdp->hd_dentry;
29561 +               err = au_seq_path(seq, &path);
29562 +               if (!err) {
29563 +                       au_optstr_br_perm(&perm, br->br_perm);
29564 +                       seq_printf(seq, "=%s", perm.a);
29565 +                       if (bindex != bbot)
29566 +                               seq_putc(seq, ':');
29567 +               }
29568 +       }
29569 +       if (unlikely(err || seq_has_overflowed(seq)))
29570 +               err = -E2BIG;
29571 +
29572 +       return err;
29573 +}
29574 +
29575 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29576 +                      const char *append)
29577 +{
29578 +       char *p;
29579 +
29580 +       p = fmt;
29581 +       while (*pat != ':')
29582 +               *p++ = *pat++;
29583 +       *p++ = *pat++;
29584 +       strcpy(p, append);
29585 +       AuDebugOn(strlen(fmt) >= len);
29586 +}
29587 +
29588 +static void au_show_wbr_create(struct seq_file *m, int v,
29589 +                              struct au_sbinfo *sbinfo)
29590 +{
29591 +       const char *pat;
29592 +       char fmt[32];
29593 +       struct au_wbr_mfs *mfs;
29594 +
29595 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29596 +
29597 +       seq_puts(m, ",create=");
29598 +       pat = au_optstr_wbr_create(v);
29599 +       mfs = &sbinfo->si_wbr_mfs;
29600 +       switch (v) {
29601 +       case AuWbrCreate_TDP:
29602 +       case AuWbrCreate_RR:
29603 +       case AuWbrCreate_MFS:
29604 +       case AuWbrCreate_PMFS:
29605 +               seq_puts(m, pat);
29606 +               break;
29607 +       case AuWbrCreate_MFSRR:
29608 +       case AuWbrCreate_TDMFS:
29609 +       case AuWbrCreate_PMFSRR:
29610 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29611 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29612 +               break;
29613 +       case AuWbrCreate_MFSV:
29614 +       case AuWbrCreate_PMFSV:
29615 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29616 +               seq_printf(m, fmt,
29617 +                          jiffies_to_msecs(mfs->mfs_expire)
29618 +                          / MSEC_PER_SEC);
29619 +               break;
29620 +       case AuWbrCreate_MFSRRV:
29621 +       case AuWbrCreate_TDMFSV:
29622 +       case AuWbrCreate_PMFSRRV:
29623 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29624 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29625 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29626 +               break;
29627 +       default:
29628 +               BUG();
29629 +       }
29630 +}
29631 +
29632 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29633 +{
29634 +#ifdef CONFIG_SYSFS
29635 +       return 0;
29636 +#else
29637 +       int err;
29638 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29639 +       aufs_bindex_t bindex, brid;
29640 +       struct qstr *name;
29641 +       struct file *f;
29642 +       struct dentry *d, *h_root;
29643 +       struct au_branch *br;
29644 +
29645 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29646 +
29647 +       err = 0;
29648 +       f = au_sbi(sb)->si_xib;
29649 +       if (!f)
29650 +               goto out;
29651 +
29652 +       /* stop printing the default xino path on the first writable branch */
29653 +       h_root = NULL;
29654 +       bindex = au_xi_root(sb, f->f_path.dentry);
29655 +       if (bindex >= 0) {
29656 +               br = au_sbr_sb(sb, bindex);
29657 +               h_root = au_br_dentry(br);
29658 +       }
29659 +
29660 +       d = f->f_path.dentry;
29661 +       name = &d->d_name;
29662 +       /* safe ->d_parent because the file is unlinked */
29663 +       if (d->d_parent == h_root
29664 +           && name->len == len
29665 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29666 +               goto out;
29667 +
29668 +       seq_puts(seq, ",xino=");
29669 +       err = au_xino_path(seq, f);
29670 +
29671 +out:
29672 +       return err;
29673 +#endif
29674 +}
29675 +
29676 +/* seq_file will re-call me in case of too long string */
29677 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29678 +{
29679 +       int err;
29680 +       unsigned int mnt_flags, v;
29681 +       struct super_block *sb;
29682 +       struct au_sbinfo *sbinfo;
29683 +
29684 +#define AuBool(name, str) do { \
29685 +       v = au_opt_test(mnt_flags, name); \
29686 +       if (v != au_opt_test(AuOpt_Def, name)) \
29687 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29688 +} while (0)
29689 +
29690 +#define AuStr(name, str) do { \
29691 +       v = mnt_flags & AuOptMask_##name; \
29692 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29693 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29694 +} while (0)
29695 +
29696 +#define AuUInt(name, str, val) do { \
29697 +       if (val != AUFS_##name##_DEF) \
29698 +               seq_printf(m, "," #str "=%u", val); \
29699 +} while (0)
29700 +
29701 +       sb = dentry->d_sb;
29702 +       if (sb->s_flags & SB_POSIXACL)
29703 +               seq_puts(m, ",acl");
29704 +#if 0
29705 +       if (sb->s_flags & SB_I_VERSION)
29706 +               seq_puts(m, ",i_version");
29707 +#endif
29708 +
29709 +       /* lock free root dinfo */
29710 +       si_noflush_read_lock(sb);
29711 +       sbinfo = au_sbi(sb);
29712 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29713 +
29714 +       mnt_flags = au_mntflags(sb);
29715 +       if (au_opt_test(mnt_flags, XINO)) {
29716 +               err = au_show_xino(m, sb);
29717 +               if (unlikely(err))
29718 +                       goto out;
29719 +       } else
29720 +               seq_puts(m, ",noxino");
29721 +
29722 +       AuBool(TRUNC_XINO, trunc_xino);
29723 +       AuStr(UDBA, udba);
29724 +       AuBool(SHWH, shwh);
29725 +       AuBool(PLINK, plink);
29726 +       AuBool(DIO, dio);
29727 +       AuBool(DIRPERM1, dirperm1);
29728 +
29729 +       v = sbinfo->si_wbr_create;
29730 +       if (v != AuWbrCreate_Def)
29731 +               au_show_wbr_create(m, v, sbinfo);
29732 +
29733 +       v = sbinfo->si_wbr_copyup;
29734 +       if (v != AuWbrCopyup_Def)
29735 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29736 +
29737 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29738 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29739 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29740 +
29741 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29742 +
29743 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29744 +       AuUInt(RDCACHE, rdcache, v);
29745 +
29746 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29747 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29748 +
29749 +       au_fhsm_show(m, sbinfo);
29750 +
29751 +       AuBool(DIRREN, dirren);
29752 +       AuBool(SUM, sum);
29753 +       /* AuBool(SUM_W, wsum); */
29754 +       AuBool(WARN_PERM, warn_perm);
29755 +       AuBool(VERBOSE, verbose);
29756 +
29757 +out:
29758 +       /* be sure to print "br:" last */
29759 +       if (!sysaufs_brs) {
29760 +               seq_puts(m, ",br:");
29761 +               au_show_brs(m, sb);
29762 +       }
29763 +       si_read_unlock(sb);
29764 +       return 0;
29765 +
29766 +#undef AuBool
29767 +#undef AuStr
29768 +#undef AuUInt
29769 +}
29770 +
29771 +/* ---------------------------------------------------------------------- */
29772 +
29773 +/* sum mode which returns the summation for statfs(2) */
29774 +
29775 +static u64 au_add_till_max(u64 a, u64 b)
29776 +{
29777 +       u64 old;
29778 +
29779 +       old = a;
29780 +       a += b;
29781 +       if (old <= a)
29782 +               return a;
29783 +       return ULLONG_MAX;
29784 +}
29785 +
29786 +static u64 au_mul_till_max(u64 a, long mul)
29787 +{
29788 +       u64 old;
29789 +
29790 +       old = a;
29791 +       a *= mul;
29792 +       if (old <= a)
29793 +               return a;
29794 +       return ULLONG_MAX;
29795 +}
29796 +
29797 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29798 +{
29799 +       int err;
29800 +       long bsize, factor;
29801 +       u64 blocks, bfree, bavail, files, ffree;
29802 +       aufs_bindex_t bbot, bindex, i;
29803 +       unsigned char shared;
29804 +       struct path h_path;
29805 +       struct super_block *h_sb;
29806 +
29807 +       err = 0;
29808 +       bsize = LONG_MAX;
29809 +       files = 0;
29810 +       ffree = 0;
29811 +       blocks = 0;
29812 +       bfree = 0;
29813 +       bavail = 0;
29814 +       bbot = au_sbbot(sb);
29815 +       for (bindex = 0; bindex <= bbot; bindex++) {
29816 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29817 +               h_sb = h_path.mnt->mnt_sb;
29818 +               shared = 0;
29819 +               for (i = 0; !shared && i < bindex; i++)
29820 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29821 +               if (shared)
29822 +                       continue;
29823 +
29824 +               /* sb->s_root for NFS is unreliable */
29825 +               h_path.dentry = h_path.mnt->mnt_root;
29826 +               err = vfs_statfs(&h_path, buf);
29827 +               if (unlikely(err))
29828 +                       goto out;
29829 +
29830 +               if (bsize > buf->f_bsize) {
29831 +                       /*
29832 +                        * we will reduce bsize, so we have to expand blocks
29833 +                        * etc. to match them again
29834 +                        */
29835 +                       factor = (bsize / buf->f_bsize);
29836 +                       blocks = au_mul_till_max(blocks, factor);
29837 +                       bfree = au_mul_till_max(bfree, factor);
29838 +                       bavail = au_mul_till_max(bavail, factor);
29839 +                       bsize = buf->f_bsize;
29840 +               }
29841 +
29842 +               factor = (buf->f_bsize / bsize);
29843 +               blocks = au_add_till_max(blocks,
29844 +                               au_mul_till_max(buf->f_blocks, factor));
29845 +               bfree = au_add_till_max(bfree,
29846 +                               au_mul_till_max(buf->f_bfree, factor));
29847 +               bavail = au_add_till_max(bavail,
29848 +                               au_mul_till_max(buf->f_bavail, factor));
29849 +               files = au_add_till_max(files, buf->f_files);
29850 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29851 +       }
29852 +
29853 +       buf->f_bsize = bsize;
29854 +       buf->f_blocks = blocks;
29855 +       buf->f_bfree = bfree;
29856 +       buf->f_bavail = bavail;
29857 +       buf->f_files = files;
29858 +       buf->f_ffree = ffree;
29859 +       buf->f_frsize = 0;
29860 +
29861 +out:
29862 +       return err;
29863 +}
29864 +
29865 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29866 +{
29867 +       int err;
29868 +       struct path h_path;
29869 +       struct super_block *sb;
29870 +
29871 +       /* lock free root dinfo */
29872 +       sb = dentry->d_sb;
29873 +       si_noflush_read_lock(sb);
29874 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29875 +               /* sb->s_root for NFS is unreliable */
29876 +               h_path.mnt = au_sbr_mnt(sb, 0);
29877 +               h_path.dentry = h_path.mnt->mnt_root;
29878 +               err = vfs_statfs(&h_path, buf);
29879 +       } else
29880 +               err = au_statfs_sum(sb, buf);
29881 +       si_read_unlock(sb);
29882 +
29883 +       if (!err) {
29884 +               buf->f_type = AUFS_SUPER_MAGIC;
29885 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29886 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29887 +       }
29888 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29889 +
29890 +       return err;
29891 +}
29892 +
29893 +/* ---------------------------------------------------------------------- */
29894 +
29895 +static int aufs_sync_fs(struct super_block *sb, int wait)
29896 +{
29897 +       int err, e;
29898 +       aufs_bindex_t bbot, bindex;
29899 +       struct au_branch *br;
29900 +       struct super_block *h_sb;
29901 +
29902 +       err = 0;
29903 +       si_noflush_read_lock(sb);
29904 +       bbot = au_sbbot(sb);
29905 +       for (bindex = 0; bindex <= bbot; bindex++) {
29906 +               br = au_sbr(sb, bindex);
29907 +               if (!au_br_writable(br->br_perm))
29908 +                       continue;
29909 +
29910 +               h_sb = au_sbr_sb(sb, bindex);
29911 +               e = vfsub_sync_filesystem(h_sb, wait);
29912 +               if (unlikely(e && !err))
29913 +                       err = e;
29914 +               /* go on even if an error happens */
29915 +       }
29916 +       si_read_unlock(sb);
29917 +
29918 +       return err;
29919 +}
29920 +
29921 +/* ---------------------------------------------------------------------- */
29922 +
29923 +/* final actions when unmounting a file system */
29924 +static void aufs_put_super(struct super_block *sb)
29925 +{
29926 +       struct au_sbinfo *sbinfo;
29927 +
29928 +       sbinfo = au_sbi(sb);
29929 +       if (sbinfo)
29930 +               kobject_put(&sbinfo->si_kobj);
29931 +}
29932 +
29933 +/* ---------------------------------------------------------------------- */
29934 +
29935 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29936 +                    struct super_block *sb, void *arg)
29937 +{
29938 +       void *array;
29939 +       unsigned long long n, sz;
29940 +
29941 +       array = NULL;
29942 +       n = 0;
29943 +       if (!*hint)
29944 +               goto out;
29945 +
29946 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29947 +               array = ERR_PTR(-EMFILE);
29948 +               pr_err("hint %llu\n", *hint);
29949 +               goto out;
29950 +       }
29951 +
29952 +       sz = sizeof(array) * *hint;
29953 +       array = kzalloc(sz, GFP_NOFS);
29954 +       if (unlikely(!array))
29955 +               array = vzalloc(sz);
29956 +       if (unlikely(!array)) {
29957 +               array = ERR_PTR(-ENOMEM);
29958 +               goto out;
29959 +       }
29960 +
29961 +       n = cb(sb, array, *hint, arg);
29962 +       AuDebugOn(n > *hint);
29963 +
29964 +out:
29965 +       *hint = n;
29966 +       return array;
29967 +}
29968 +
29969 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29970 +                                      unsigned long long max __maybe_unused,
29971 +                                      void *arg)
29972 +{
29973 +       unsigned long long n;
29974 +       struct inode **p, *inode;
29975 +       struct list_head *head;
29976 +
29977 +       n = 0;
29978 +       p = a;
29979 +       head = arg;
29980 +       spin_lock(&sb->s_inode_list_lock);
29981 +       list_for_each_entry(inode, head, i_sb_list) {
29982 +               if (!au_is_bad_inode(inode)
29983 +                   && au_ii(inode)->ii_btop >= 0) {
29984 +                       spin_lock(&inode->i_lock);
29985 +                       if (atomic_read(&inode->i_count)) {
29986 +                               au_igrab(inode);
29987 +                               *p++ = inode;
29988 +                               n++;
29989 +                               AuDebugOn(n > max);
29990 +                       }
29991 +                       spin_unlock(&inode->i_lock);
29992 +               }
29993 +       }
29994 +       spin_unlock(&sb->s_inode_list_lock);
29995 +
29996 +       return n;
29997 +}
29998 +
29999 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30000 +{
30001 +       struct au_sbinfo *sbi;
30002 +
30003 +       sbi = au_sbi(sb);
30004 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30005 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30006 +}
30007 +
30008 +void au_iarray_free(struct inode **a, unsigned long long max)
30009 +{
30010 +       unsigned long long ull;
30011 +
30012 +       for (ull = 0; ull < max; ull++)
30013 +               iput(a[ull]);
30014 +       kvfree(a);
30015 +}
30016 +
30017 +/* ---------------------------------------------------------------------- */
30018 +
30019 +/*
30020 + * refresh dentry and inode at remount time.
30021 + */
30022 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30023 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30024 +                     struct dentry *parent)
30025 +{
30026 +       int err;
30027 +
30028 +       di_write_lock_child(dentry);
30029 +       di_read_lock_parent(parent, AuLock_IR);
30030 +       err = au_refresh_dentry(dentry, parent);
30031 +       if (!err && dir_flags)
30032 +               au_hn_reset(d_inode(dentry), dir_flags);
30033 +       di_read_unlock(parent, AuLock_IR);
30034 +       di_write_unlock(dentry);
30035 +
30036 +       return err;
30037 +}
30038 +
30039 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30040 +                          struct au_sbinfo *sbinfo,
30041 +                          const unsigned int dir_flags, unsigned int do_idop)
30042 +{
30043 +       int err;
30044 +       struct dentry *parent;
30045 +
30046 +       err = 0;
30047 +       parent = dget_parent(dentry);
30048 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30049 +               if (d_really_is_positive(dentry)) {
30050 +                       if (!d_is_dir(dentry))
30051 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30052 +                                                parent);
30053 +                       else {
30054 +                               err = au_do_refresh(dentry, dir_flags, parent);
30055 +                               if (unlikely(err))
30056 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30057 +                       }
30058 +               } else
30059 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30060 +               AuDbgDentry(dentry);
30061 +       }
30062 +       dput(parent);
30063 +
30064 +       if (!err) {
30065 +               if (do_idop)
30066 +                       au_refresh_dop(dentry, /*force_reval*/0);
30067 +       } else
30068 +               au_refresh_dop(dentry, /*force_reval*/1);
30069 +
30070 +       AuTraceErr(err);
30071 +       return err;
30072 +}
30073 +
30074 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30075 +{
30076 +       int err, i, j, ndentry, e;
30077 +       unsigned int sigen;
30078 +       struct au_dcsub_pages dpages;
30079 +       struct au_dpage *dpage;
30080 +       struct dentry **dentries, *d;
30081 +       struct au_sbinfo *sbinfo;
30082 +       struct dentry *root = sb->s_root;
30083 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30084 +
30085 +       if (do_idop)
30086 +               au_refresh_dop(root, /*force_reval*/0);
30087 +
30088 +       err = au_dpages_init(&dpages, GFP_NOFS);
30089 +       if (unlikely(err))
30090 +               goto out;
30091 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30092 +       if (unlikely(err))
30093 +               goto out_dpages;
30094 +
30095 +       sigen = au_sigen(sb);
30096 +       sbinfo = au_sbi(sb);
30097 +       for (i = 0; i < dpages.ndpage; i++) {
30098 +               dpage = dpages.dpages + i;
30099 +               dentries = dpage->dentries;
30100 +               ndentry = dpage->ndentry;
30101 +               for (j = 0; j < ndentry; j++) {
30102 +                       d = dentries[j];
30103 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30104 +                                           do_idop);
30105 +                       if (unlikely(e && !err))
30106 +                               err = e;
30107 +                       /* go on even err */
30108 +               }
30109 +       }
30110 +
30111 +out_dpages:
30112 +       au_dpages_free(&dpages);
30113 +out:
30114 +       return err;
30115 +}
30116 +
30117 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30118 +{
30119 +       int err, e;
30120 +       unsigned int sigen;
30121 +       unsigned long long max, ull;
30122 +       struct inode *inode, **array;
30123 +
30124 +       array = au_iarray_alloc(sb, &max);
30125 +       err = PTR_ERR(array);
30126 +       if (IS_ERR(array))
30127 +               goto out;
30128 +
30129 +       err = 0;
30130 +       sigen = au_sigen(sb);
30131 +       for (ull = 0; ull < max; ull++) {
30132 +               inode = array[ull];
30133 +               if (unlikely(!inode))
30134 +                       break;
30135 +
30136 +               e = 0;
30137 +               ii_write_lock_child(inode);
30138 +               if (au_iigen(inode, NULL) != sigen) {
30139 +                       e = au_refresh_hinode_self(inode);
30140 +                       if (unlikely(e)) {
30141 +                               au_refresh_iop(inode, /*force_getattr*/1);
30142 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30143 +                               if (!err)
30144 +                                       err = e;
30145 +                               /* go on even if err */
30146 +                       }
30147 +               }
30148 +               if (!e && do_idop)
30149 +                       au_refresh_iop(inode, /*force_getattr*/0);
30150 +               ii_write_unlock(inode);
30151 +       }
30152 +
30153 +       au_iarray_free(array, max);
30154 +
30155 +out:
30156 +       return err;
30157 +}
30158 +
30159 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30160 +{
30161 +       int err, e;
30162 +       unsigned int udba;
30163 +       aufs_bindex_t bindex, bbot;
30164 +       struct dentry *root;
30165 +       struct inode *inode;
30166 +       struct au_branch *br;
30167 +       struct au_sbinfo *sbi;
30168 +
30169 +       au_sigen_inc(sb);
30170 +       sbi = au_sbi(sb);
30171 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30172 +
30173 +       root = sb->s_root;
30174 +       DiMustNoWaiters(root);
30175 +       inode = d_inode(root);
30176 +       IiMustNoWaiters(inode);
30177 +
30178 +       udba = au_opt_udba(sb);
30179 +       bbot = au_sbbot(sb);
30180 +       for (bindex = 0; bindex <= bbot; bindex++) {
30181 +               br = au_sbr(sb, bindex);
30182 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30183 +               if (unlikely(err))
30184 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30185 +                               bindex, err);
30186 +               /* go on even if err */
30187 +       }
30188 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30189 +
30190 +       if (do_idop) {
30191 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30192 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30193 +                       sb->s_d_op = &aufs_dop_noreval;
30194 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30195 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30196 +               } else {
30197 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30198 +                       sb->s_d_op = &aufs_dop;
30199 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30200 +                       sbi->si_iop_array = aufs_iop;
30201 +               }
30202 +               pr_info("reset to %ps and %ps\n",
30203 +                       sb->s_d_op, sbi->si_iop_array);
30204 +       }
30205 +
30206 +       di_write_unlock(root);
30207 +       err = au_refresh_d(sb, do_idop);
30208 +       e = au_refresh_i(sb, do_idop);
30209 +       if (unlikely(e && !err))
30210 +               err = e;
30211 +       /* aufs_write_lock() calls ..._child() */
30212 +       di_write_lock_child(root);
30213 +
30214 +       au_cpup_attr_all(inode, /*force*/1);
30215 +
30216 +       if (unlikely(err))
30217 +               AuIOErr("refresh failed, ignored, %d\n", err);
30218 +}
30219 +
30220 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30221 +static int cvt_err(int err)
30222 +{
30223 +       AuTraceErr(err);
30224 +
30225 +       switch (err) {
30226 +       case -ENOENT:
30227 +       case -ENOTDIR:
30228 +       case -EEXIST:
30229 +       case -EIO:
30230 +               err = -EINVAL;
30231 +       }
30232 +       return err;
30233 +}
30234 +
30235 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30236 +{
30237 +       int err, do_dx;
30238 +       unsigned int mntflags;
30239 +       struct au_opts opts = {
30240 +               .opt = NULL
30241 +       };
30242 +       struct dentry *root;
30243 +       struct inode *inode;
30244 +       struct au_sbinfo *sbinfo;
30245 +
30246 +       err = 0;
30247 +       root = sb->s_root;
30248 +       if (!data || !*data) {
30249 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30250 +               if (!err) {
30251 +                       di_write_lock_child(root);
30252 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30253 +                       aufs_write_unlock(root);
30254 +               }
30255 +               goto out;
30256 +       }
30257 +
30258 +       err = -ENOMEM;
30259 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30260 +       if (unlikely(!opts.opt))
30261 +               goto out;
30262 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30263 +       opts.flags = AuOpts_REMOUNT;
30264 +       opts.sb_flags = *flags;
30265 +
30266 +       /* parse it before aufs lock */
30267 +       err = au_opts_parse(sb, data, &opts);
30268 +       if (unlikely(err))
30269 +               goto out_opts;
30270 +
30271 +       sbinfo = au_sbi(sb);
30272 +       inode = d_inode(root);
30273 +       inode_lock(inode);
30274 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30275 +       if (unlikely(err))
30276 +               goto out_mtx;
30277 +       di_write_lock_child(root);
30278 +
30279 +       /* au_opts_remount() may return an error */
30280 +       err = au_opts_remount(sb, &opts);
30281 +       au_opts_free(&opts);
30282 +
30283 +       if (au_ftest_opts(opts.flags, REFRESH))
30284 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30285 +
30286 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30287 +               mntflags = au_mntflags(sb);
30288 +               do_dx = !!au_opt_test(mntflags, DIO);
30289 +               au_dy_arefresh(do_dx);
30290 +       }
30291 +
30292 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30293 +       aufs_write_unlock(root);
30294 +
30295 +out_mtx:
30296 +       inode_unlock(inode);
30297 +out_opts:
30298 +       free_page((unsigned long)opts.opt);
30299 +out:
30300 +       err = cvt_err(err);
30301 +       AuTraceErr(err);
30302 +       return err;
30303 +}
30304 +
30305 +static const struct super_operations aufs_sop = {
30306 +       .alloc_inode    = aufs_alloc_inode,
30307 +       .destroy_inode  = aufs_destroy_inode,
30308 +       .free_inode     = aufs_free_inode,
30309 +       /* always deleting, no clearing */
30310 +       .drop_inode     = generic_delete_inode,
30311 +       .show_options   = aufs_show_options,
30312 +       .statfs         = aufs_statfs,
30313 +       .put_super      = aufs_put_super,
30314 +       .sync_fs        = aufs_sync_fs,
30315 +       .remount_fs     = aufs_remount_fs
30316 +};
30317 +
30318 +/* ---------------------------------------------------------------------- */
30319 +
30320 +static int alloc_root(struct super_block *sb)
30321 +{
30322 +       int err;
30323 +       struct inode *inode;
30324 +       struct dentry *root;
30325 +
30326 +       err = -ENOMEM;
30327 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30328 +       err = PTR_ERR(inode);
30329 +       if (IS_ERR(inode))
30330 +               goto out;
30331 +
30332 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30333 +       inode->i_fop = &aufs_dir_fop;
30334 +       inode->i_mode = S_IFDIR;
30335 +       set_nlink(inode, 2);
30336 +       unlock_new_inode(inode);
30337 +
30338 +       root = d_make_root(inode);
30339 +       if (unlikely(!root))
30340 +               goto out;
30341 +       err = PTR_ERR(root);
30342 +       if (IS_ERR(root))
30343 +               goto out;
30344 +
30345 +       err = au_di_init(root);
30346 +       if (!err) {
30347 +               sb->s_root = root;
30348 +               return 0; /* success */
30349 +       }
30350 +       dput(root);
30351 +
30352 +out:
30353 +       return err;
30354 +}
30355 +
30356 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30357 +                          int silent __maybe_unused)
30358 +{
30359 +       int err;
30360 +       struct au_opts opts = {
30361 +               .opt = NULL
30362 +       };
30363 +       struct au_sbinfo *sbinfo;
30364 +       struct dentry *root;
30365 +       struct inode *inode;
30366 +       char *arg = raw_data;
30367 +
30368 +       if (unlikely(!arg || !*arg)) {
30369 +               err = -EINVAL;
30370 +               pr_err("no arg\n");
30371 +               goto out;
30372 +       }
30373 +
30374 +       err = -ENOMEM;
30375 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30376 +       if (unlikely(!opts.opt))
30377 +               goto out;
30378 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30379 +       opts.sb_flags = sb->s_flags;
30380 +
30381 +       err = au_si_alloc(sb);
30382 +       if (unlikely(err))
30383 +               goto out_opts;
30384 +       sbinfo = au_sbi(sb);
30385 +
30386 +       /* all timestamps always follow the ones on the branch */
30387 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30388 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30389 +       sb->s_op = &aufs_sop;
30390 +       sb->s_d_op = &aufs_dop;
30391 +       sb->s_magic = AUFS_SUPER_MAGIC;
30392 +       sb->s_maxbytes = 0;
30393 +       sb->s_stack_depth = 1;
30394 +       au_export_init(sb);
30395 +       au_xattr_init(sb);
30396 +
30397 +       err = alloc_root(sb);
30398 +       if (unlikely(err)) {
30399 +               si_write_unlock(sb);
30400 +               goto out_info;
30401 +       }
30402 +       root = sb->s_root;
30403 +       inode = d_inode(root);
30404 +
30405 +       /*
30406 +        * actually we can parse options regardless aufs lock here.
30407 +        * but at remount time, parsing must be done before aufs lock.
30408 +        * so we follow the same rule.
30409 +        */
30410 +       ii_write_lock_parent(inode);
30411 +       aufs_write_unlock(root);
30412 +       err = au_opts_parse(sb, arg, &opts);
30413 +       if (unlikely(err))
30414 +               goto out_root;
30415 +
30416 +       /* lock vfs_inode first, then aufs. */
30417 +       inode_lock(inode);
30418 +       aufs_write_lock(root);
30419 +       err = au_opts_mount(sb, &opts);
30420 +       au_opts_free(&opts);
30421 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30422 +               sb->s_d_op = &aufs_dop_noreval;
30423 +               pr_info("%ps\n", sb->s_d_op);
30424 +               au_refresh_dop(root, /*force_reval*/0);
30425 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30426 +               au_refresh_iop(inode, /*force_getattr*/0);
30427 +       }
30428 +       aufs_write_unlock(root);
30429 +       inode_unlock(inode);
30430 +       if (!err)
30431 +               goto out_opts; /* success */
30432 +
30433 +out_root:
30434 +       dput(root);
30435 +       sb->s_root = NULL;
30436 +out_info:
30437 +       kobject_put(&sbinfo->si_kobj);
30438 +       sb->s_fs_info = NULL;
30439 +out_opts:
30440 +       free_page((unsigned long)opts.opt);
30441 +out:
30442 +       AuTraceErr(err);
30443 +       err = cvt_err(err);
30444 +       AuTraceErr(err);
30445 +       return err;
30446 +}
30447 +
30448 +/* ---------------------------------------------------------------------- */
30449 +
30450 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30451 +                                const char *dev_name __maybe_unused,
30452 +                                void *raw_data)
30453 +{
30454 +       struct dentry *root;
30455 +
30456 +       /* all timestamps always follow the ones on the branch */
30457 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30458 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30459 +       if (IS_ERR(root))
30460 +               goto out;
30461 +
30462 +       au_sbilist_add(root->d_sb);
30463 +
30464 +out:
30465 +       return root;
30466 +}
30467 +
30468 +static void aufs_kill_sb(struct super_block *sb)
30469 +{
30470 +       struct au_sbinfo *sbinfo;
30471 +
30472 +       sbinfo = au_sbi(sb);
30473 +       if (sbinfo) {
30474 +               au_sbilist_del(sb);
30475 +               aufs_write_lock(sb->s_root);
30476 +               au_fhsm_fin(sb);
30477 +               if (sbinfo->si_wbr_create_ops->fin)
30478 +                       sbinfo->si_wbr_create_ops->fin(sb);
30479 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30480 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30481 +                       au_remount_refresh(sb, /*do_idop*/0);
30482 +               }
30483 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30484 +                       au_plink_put(sb, /*verbose*/1);
30485 +               au_xino_clr(sb);
30486 +               au_dr_opt_flush(sb);
30487 +               sbinfo->si_sb = NULL;
30488 +               aufs_write_unlock(sb->s_root);
30489 +               au_nwt_flush(&sbinfo->si_nowait);
30490 +       }
30491 +       kill_anon_super(sb);
30492 +}
30493 +
30494 +struct file_system_type aufs_fs_type = {
30495 +       .name           = AUFS_FSTYPE,
30496 +       /* a race between rename and others */
30497 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30498 +       .mount          = aufs_mount,
30499 +       .kill_sb        = aufs_kill_sb,
30500 +       /* no need to __module_get() and module_put(). */
30501 +       .owner          = THIS_MODULE,
30502 +};
30503 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30504 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30505 +++ linux/fs/aufs/super.h       2019-07-11 15:42:14.472238057 +0200
30506 @@ -0,0 +1,589 @@
30507 +/* SPDX-License-Identifier: GPL-2.0 */
30508 +/*
30509 + * Copyright (C) 2005-2019 Junjiro R. Okajima
30510 + *
30511 + * This program, aufs is free software; you can redistribute it and/or modify
30512 + * it under the terms of the GNU General Public License as published by
30513 + * the Free Software Foundation; either version 2 of the License, or
30514 + * (at your option) any later version.
30515 + *
30516 + * This program is distributed in the hope that it will be useful,
30517 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30518 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30519 + * GNU General Public License for more details.
30520 + *
30521 + * You should have received a copy of the GNU General Public License
30522 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30523 + */
30524 +
30525 +/*
30526 + * super_block operations
30527 + */
30528 +
30529 +#ifndef __AUFS_SUPER_H__
30530 +#define __AUFS_SUPER_H__
30531 +
30532 +#ifdef __KERNEL__
30533 +
30534 +#include <linux/fs.h>
30535 +#include <linux/kobject.h>
30536 +#include "hbl.h"
30537 +#include "lcnt.h"
30538 +#include "rwsem.h"
30539 +#include "wkq.h"
30540 +
30541 +/* policies to select one among multiple writable branches */
30542 +struct au_wbr_copyup_operations {
30543 +       int (*copyup)(struct dentry *dentry);
30544 +};
30545 +
30546 +#define AuWbr_DIR      1               /* target is a dir */
30547 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30548 +
30549 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30550 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30551 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30552 +
30553 +struct au_wbr_create_operations {
30554 +       int (*create)(struct dentry *dentry, unsigned int flags);
30555 +       int (*init)(struct super_block *sb);
30556 +       int (*fin)(struct super_block *sb);
30557 +};
30558 +
30559 +struct au_wbr_mfs {
30560 +       struct mutex    mfs_lock; /* protect this structure */
30561 +       unsigned long   mfs_jiffy;
30562 +       unsigned long   mfs_expire;
30563 +       aufs_bindex_t   mfs_bindex;
30564 +
30565 +       unsigned long long      mfsrr_bytes;
30566 +       unsigned long long      mfsrr_watermark;
30567 +};
30568 +
30569 +#define AuPlink_NHASH 100
30570 +static inline int au_plink_hash(ino_t ino)
30571 +{
30572 +       return ino % AuPlink_NHASH;
30573 +}
30574 +
30575 +/* File-based Hierarchical Storage Management */
30576 +struct au_fhsm {
30577 +#ifdef CONFIG_AUFS_FHSM
30578 +       /* allow only one process who can receive the notification */
30579 +       spinlock_t              fhsm_spin;
30580 +       pid_t                   fhsm_pid;
30581 +       wait_queue_head_t       fhsm_wqh;
30582 +       atomic_t                fhsm_readable;
30583 +
30584 +       /* these are protected by si_rwsem */
30585 +       unsigned long           fhsm_expire;
30586 +       aufs_bindex_t           fhsm_bottom;
30587 +#endif
30588 +};
30589 +
30590 +struct au_branch;
30591 +struct au_sbinfo {
30592 +       /* nowait tasks in the system-wide workqueue */
30593 +       struct au_nowait_tasks  si_nowait;
30594 +
30595 +       /*
30596 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30597 +        * rwsem for au_sbinfo is necessary.
30598 +        */
30599 +       struct au_rwsem         si_rwsem;
30600 +
30601 +       /*
30602 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30603 +        * remount.
30604 +        */
30605 +       au_lcnt_t               si_ninodes, si_nfiles;
30606 +
30607 +       /* branch management */
30608 +       unsigned int            si_generation;
30609 +
30610 +       /* see AuSi_ flags */
30611 +       unsigned char           au_si_status;
30612 +
30613 +       aufs_bindex_t           si_bbot;
30614 +
30615 +       /* dirty trick to keep br_id plus */
30616 +       unsigned int            si_last_br_id :
30617 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30618 +       struct au_branch        **si_branch;
30619 +
30620 +       /* policy to select a writable branch */
30621 +       unsigned char           si_wbr_copyup;
30622 +       unsigned char           si_wbr_create;
30623 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30624 +       struct au_wbr_create_operations *si_wbr_create_ops;
30625 +
30626 +       /* round robin */
30627 +       atomic_t                si_wbr_rr_next;
30628 +
30629 +       /* most free space */
30630 +       struct au_wbr_mfs       si_wbr_mfs;
30631 +
30632 +       /* File-based Hierarchical Storage Management */
30633 +       struct au_fhsm          si_fhsm;
30634 +
30635 +       /* mount flags */
30636 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30637 +       unsigned int            si_mntflags;
30638 +
30639 +       /* external inode number (bitmap and translation table) */
30640 +       vfs_readf_t             si_xread;
30641 +       vfs_writef_t            si_xwrite;
30642 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30643 +
30644 +       struct file             *si_xib;
30645 +       struct mutex            si_xib_mtx; /* protect xib members */
30646 +       unsigned long           *si_xib_buf;
30647 +       unsigned long           si_xib_last_pindex;
30648 +       int                     si_xib_next_bit;
30649 +
30650 +       unsigned long           si_xino_jiffy;
30651 +       unsigned long           si_xino_expire;
30652 +       /* reserved for future use */
30653 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30654 +
30655 +#ifdef CONFIG_AUFS_EXPORT
30656 +       /* i_generation */
30657 +       /* todo: make xigen file an array to support many inode numbers */
30658 +       struct file             *si_xigen;
30659 +       atomic_t                si_xigen_next;
30660 +#endif
30661 +
30662 +       /* dirty trick to support atomic_open */
30663 +       struct hlist_bl_head    si_aopen;
30664 +
30665 +       /* vdir parameters */
30666 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30667 +       unsigned int            si_rdblk;       /* deblk size */
30668 +       unsigned int            si_rdhash;      /* hash size */
30669 +
30670 +       /*
30671 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30672 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30673 +        * future fsck.aufs or kernel thread will remove them later.
30674 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30675 +        */
30676 +       unsigned int            si_dirwh;
30677 +
30678 +       /* pseudo_link list */
30679 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30680 +       wait_queue_head_t       si_plink_wq;
30681 +       spinlock_t              si_plink_maint_lock;
30682 +       pid_t                   si_plink_maint_pid;
30683 +
30684 +       /* file list */
30685 +       struct hlist_bl_head    si_files;
30686 +
30687 +       /* with/without getattr, brother of sb->s_d_op */
30688 +       struct inode_operations *si_iop_array;
30689 +
30690 +       /*
30691 +        * sysfs and lifetime management.
30692 +        * this is not a small structure and it may be a waste of memory in case
30693 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30694 +        * but using sysfs is majority.
30695 +        */
30696 +       struct kobject          si_kobj;
30697 +#ifdef CONFIG_DEBUG_FS
30698 +       struct dentry            *si_dbgaufs;
30699 +       struct dentry            *si_dbgaufs_plink;
30700 +       struct dentry            *si_dbgaufs_xib;
30701 +#ifdef CONFIG_AUFS_EXPORT
30702 +       struct dentry            *si_dbgaufs_xigen;
30703 +#endif
30704 +#endif
30705 +
30706 +#ifdef CONFIG_AUFS_SBILIST
30707 +       struct hlist_bl_node    si_list;
30708 +#endif
30709 +
30710 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30711 +       struct super_block      *si_sb;
30712 +};
30713 +
30714 +/* sbinfo status flags */
30715 +/*
30716 + * set true when refresh_dirs() failed at remount time.
30717 + * then try refreshing dirs at access time again.
30718 + * if it is false, refreshing dirs at access time is unnecessary
30719 + */
30720 +#define AuSi_FAILED_REFRESH_DIR        1
30721 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30722 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30723 +
30724 +#ifndef CONFIG_AUFS_FHSM
30725 +#undef AuSi_FHSM
30726 +#define AuSi_FHSM              0
30727 +#endif
30728 +
30729 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30730 +                                          unsigned int flag)
30731 +{
30732 +       AuRwMustAnyLock(&sbi->si_rwsem);
30733 +       return sbi->au_si_status & flag;
30734 +}
30735 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30736 +#define au_fset_si(sbinfo, name) do { \
30737 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30738 +       (sbinfo)->au_si_status |= AuSi_##name; \
30739 +} while (0)
30740 +#define au_fclr_si(sbinfo, name) do { \
30741 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30742 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30743 +} while (0)
30744 +
30745 +/* ---------------------------------------------------------------------- */
30746 +
30747 +/* policy to select one among writable branches */
30748 +#define AuWbrCopyup(sbinfo, ...) \
30749 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30750 +#define AuWbrCreate(sbinfo, ...) \
30751 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30752 +
30753 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30754 +#define AuLock_DW              1               /* write-lock dentry */
30755 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30756 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30757 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30758 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30759 +                                               /* except RENAME_EXCHANGE */
30760 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30761 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30762 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30763 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30764 +#define au_fset_lock(flags, name) \
30765 +       do { (flags) |= AuLock_##name; } while (0)
30766 +#define au_fclr_lock(flags, name) \
30767 +       do { (flags) &= ~AuLock_##name; } while (0)
30768 +
30769 +/* ---------------------------------------------------------------------- */
30770 +
30771 +/* super.c */
30772 +extern struct file_system_type aufs_fs_type;
30773 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30774 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30775 +                                          unsigned long long max, void *arg);
30776 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30777 +                    struct super_block *sb, void *arg);
30778 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30779 +void au_iarray_free(struct inode **a, unsigned long long max);
30780 +
30781 +/* sbinfo.c */
30782 +void au_si_free(struct kobject *kobj);
30783 +int au_si_alloc(struct super_block *sb);
30784 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30785 +
30786 +unsigned int au_sigen_inc(struct super_block *sb);
30787 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30788 +
30789 +int si_read_lock(struct super_block *sb, int flags);
30790 +int si_write_lock(struct super_block *sb, int flags);
30791 +int aufs_read_lock(struct dentry *dentry, int flags);
30792 +void aufs_read_unlock(struct dentry *dentry, int flags);
30793 +void aufs_write_lock(struct dentry *dentry);
30794 +void aufs_write_unlock(struct dentry *dentry);
30795 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30796 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30797 +
30798 +/* wbr_policy.c */
30799 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30800 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30801 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30802 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30803 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30804 +
30805 +/* mvdown.c */
30806 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30807 +
30808 +#ifdef CONFIG_AUFS_FHSM
30809 +/* fhsm.c */
30810 +
30811 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30812 +{
30813 +       pid_t pid;
30814 +
30815 +       spin_lock(&fhsm->fhsm_spin);
30816 +       pid = fhsm->fhsm_pid;
30817 +       spin_unlock(&fhsm->fhsm_spin);
30818 +
30819 +       return pid;
30820 +}
30821 +
30822 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30823 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30824 +int au_fhsm_fd(struct super_block *sb, int oflags);
30825 +int au_fhsm_br_alloc(struct au_branch *br);
30826 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30827 +void au_fhsm_fin(struct super_block *sb);
30828 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30829 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30830 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30831 +#else
30832 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30833 +          int force)
30834 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30835 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30836 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30837 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30838 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30839 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30840 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30841 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30842 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30843 +#endif
30844 +
30845 +/* ---------------------------------------------------------------------- */
30846 +
30847 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30848 +{
30849 +       return sb->s_fs_info;
30850 +}
30851 +
30852 +/* ---------------------------------------------------------------------- */
30853 +
30854 +#ifdef CONFIG_AUFS_EXPORT
30855 +int au_test_nfsd(void);
30856 +void au_export_init(struct super_block *sb);
30857 +void au_xigen_inc(struct inode *inode);
30858 +int au_xigen_new(struct inode *inode);
30859 +int au_xigen_set(struct super_block *sb, struct path *path);
30860 +void au_xigen_clr(struct super_block *sb);
30861 +
30862 +static inline int au_busy_or_stale(void)
30863 +{
30864 +       if (!au_test_nfsd())
30865 +               return -EBUSY;
30866 +       return -ESTALE;
30867 +}
30868 +#else
30869 +AuStubInt0(au_test_nfsd, void)
30870 +AuStubVoid(au_export_init, struct super_block *sb)
30871 +AuStubVoid(au_xigen_inc, struct inode *inode)
30872 +AuStubInt0(au_xigen_new, struct inode *inode)
30873 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30874 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30875 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30876 +#endif /* CONFIG_AUFS_EXPORT */
30877 +
30878 +/* ---------------------------------------------------------------------- */
30879 +
30880 +#ifdef CONFIG_AUFS_SBILIST
30881 +/* module.c */
30882 +extern struct hlist_bl_head au_sbilist;
30883 +
30884 +static inline void au_sbilist_init(void)
30885 +{
30886 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30887 +}
30888 +
30889 +static inline void au_sbilist_add(struct super_block *sb)
30890 +{
30891 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30892 +}
30893 +
30894 +static inline void au_sbilist_del(struct super_block *sb)
30895 +{
30896 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30897 +}
30898 +
30899 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30900 +static inline void au_sbilist_lock(void)
30901 +{
30902 +       hlist_bl_lock(&au_sbilist);
30903 +}
30904 +
30905 +static inline void au_sbilist_unlock(void)
30906 +{
30907 +       hlist_bl_unlock(&au_sbilist);
30908 +}
30909 +#define AuGFP_SBILIST  GFP_ATOMIC
30910 +#else
30911 +AuStubVoid(au_sbilist_lock, void)
30912 +AuStubVoid(au_sbilist_unlock, void)
30913 +#define AuGFP_SBILIST  GFP_NOFS
30914 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30915 +#else
30916 +AuStubVoid(au_sbilist_init, void)
30917 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30918 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30919 +AuStubVoid(au_sbilist_lock, void)
30920 +AuStubVoid(au_sbilist_unlock, void)
30921 +#define AuGFP_SBILIST  GFP_NOFS
30922 +#endif
30923 +
30924 +/* ---------------------------------------------------------------------- */
30925 +
30926 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30927 +{
30928 +       /*
30929 +        * This function is a dynamic '__init' function actually,
30930 +        * so the tiny check for si_rwsem is unnecessary.
30931 +        */
30932 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30933 +#ifdef CONFIG_DEBUG_FS
30934 +       sbinfo->si_dbgaufs = NULL;
30935 +       sbinfo->si_dbgaufs_plink = NULL;
30936 +       sbinfo->si_dbgaufs_xib = NULL;
30937 +#ifdef CONFIG_AUFS_EXPORT
30938 +       sbinfo->si_dbgaufs_xigen = NULL;
30939 +#endif
30940 +#endif
30941 +}
30942 +
30943 +/* ---------------------------------------------------------------------- */
30944 +
30945 +/* current->atomic_flags */
30946 +/* this value should never corrupt the ones defined in linux/sched.h */
30947 +#define PFA_AUFS       0x10
30948 +
30949 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30950 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30951 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30952 +
30953 +static inline int si_pid_test(struct super_block *sb)
30954 +{
30955 +       return !!task_test_aufs(current);
30956 +}
30957 +
30958 +static inline void si_pid_clr(struct super_block *sb)
30959 +{
30960 +       AuDebugOn(!task_test_aufs(current));
30961 +       task_clear_aufs(current);
30962 +}
30963 +
30964 +static inline void si_pid_set(struct super_block *sb)
30965 +{
30966 +       AuDebugOn(task_test_aufs(current));
30967 +       task_set_aufs(current);
30968 +}
30969 +
30970 +/* ---------------------------------------------------------------------- */
30971 +
30972 +/* lock superblock. mainly for entry point functions */
30973 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30974 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30975 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30976 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30977 +/*
30978 +#define __si_read_trylock_nested(sb) \
30979 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30980 +#define __si_write_trylock_nested(sb) \
30981 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30982 +*/
30983 +
30984 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30985 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30986 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30987 +
30988 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30989 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30990 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30991 +
30992 +static inline void si_noflush_read_lock(struct super_block *sb)
30993 +{
30994 +       __si_read_lock(sb);
30995 +       si_pid_set(sb);
30996 +}
30997 +
30998 +static inline int si_noflush_read_trylock(struct super_block *sb)
30999 +{
31000 +       int locked;
31001 +
31002 +       locked = __si_read_trylock(sb);
31003 +       if (locked)
31004 +               si_pid_set(sb);
31005 +       return locked;
31006 +}
31007 +
31008 +static inline void si_noflush_write_lock(struct super_block *sb)
31009 +{
31010 +       __si_write_lock(sb);
31011 +       si_pid_set(sb);
31012 +}
31013 +
31014 +static inline int si_noflush_write_trylock(struct super_block *sb)
31015 +{
31016 +       int locked;
31017 +
31018 +       locked = __si_write_trylock(sb);
31019 +       if (locked)
31020 +               si_pid_set(sb);
31021 +       return locked;
31022 +}
31023 +
31024 +#if 0 /* reserved */
31025 +static inline int si_read_trylock(struct super_block *sb, int flags)
31026 +{
31027 +       if (au_ftest_lock(flags, FLUSH))
31028 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31029 +       return si_noflush_read_trylock(sb);
31030 +}
31031 +#endif
31032 +
31033 +static inline void si_read_unlock(struct super_block *sb)
31034 +{
31035 +       si_pid_clr(sb);
31036 +       __si_read_unlock(sb);
31037 +}
31038 +
31039 +#if 0 /* reserved */
31040 +static inline int si_write_trylock(struct super_block *sb, int flags)
31041 +{
31042 +       if (au_ftest_lock(flags, FLUSH))
31043 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31044 +       return si_noflush_write_trylock(sb);
31045 +}
31046 +#endif
31047 +
31048 +static inline void si_write_unlock(struct super_block *sb)
31049 +{
31050 +       si_pid_clr(sb);
31051 +       __si_write_unlock(sb);
31052 +}
31053 +
31054 +#if 0 /* reserved */
31055 +static inline void si_downgrade_lock(struct super_block *sb)
31056 +{
31057 +       __si_downgrade_lock(sb);
31058 +}
31059 +#endif
31060 +
31061 +/* ---------------------------------------------------------------------- */
31062 +
31063 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31064 +{
31065 +       SiMustAnyLock(sb);
31066 +       return au_sbi(sb)->si_bbot;
31067 +}
31068 +
31069 +static inline unsigned int au_mntflags(struct super_block *sb)
31070 +{
31071 +       SiMustAnyLock(sb);
31072 +       return au_sbi(sb)->si_mntflags;
31073 +}
31074 +
31075 +static inline unsigned int au_sigen(struct super_block *sb)
31076 +{
31077 +       SiMustAnyLock(sb);
31078 +       return au_sbi(sb)->si_generation;
31079 +}
31080 +
31081 +static inline struct au_branch *au_sbr(struct super_block *sb,
31082 +                                      aufs_bindex_t bindex)
31083 +{
31084 +       SiMustAnyLock(sb);
31085 +       return au_sbi(sb)->si_branch[0 + bindex];
31086 +}
31087 +
31088 +static inline loff_t au_xi_maxent(struct super_block *sb)
31089 +{
31090 +       SiMustAnyLock(sb);
31091 +       return au_sbi(sb)->si_ximaxent;
31092 +}
31093 +
31094 +#endif /* __KERNEL__ */
31095 +#endif /* __AUFS_SUPER_H__ */
31096 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31097 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31098 +++ linux/fs/aufs/sysaufs.c     2019-07-11 15:42:14.472238057 +0200
31099 @@ -0,0 +1,93 @@
31100 +// SPDX-License-Identifier: GPL-2.0
31101 +/*
31102 + * Copyright (C) 2005-2019 Junjiro R. Okajima
31103 + *
31104 + * This program, aufs is free software; you can redistribute it and/or modify
31105 + * it under the terms of the GNU General Public License as published by
31106 + * the Free Software Foundation; either version 2 of the License, or
31107 + * (at your option) any later version.
31108 + *
31109 + * This program is distributed in the hope that it will be useful,
31110 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31111 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31112 + * GNU General Public License for more details.
31113 + *
31114 + * You should have received a copy of the GNU General Public License
31115 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31116 + */
31117 +
31118 +/*
31119 + * sysfs interface and lifetime management
31120 + * they are necessary regardless sysfs is disabled.
31121 + */
31122 +
31123 +#include <linux/random.h>
31124 +#include "aufs.h"
31125 +
31126 +unsigned long sysaufs_si_mask;
31127 +struct kset *sysaufs_kset;
31128 +
31129 +#define AuSiAttr(_name) { \
31130 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31131 +       .show   = sysaufs_si_##_name,                           \
31132 +}
31133 +
31134 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31135 +struct attribute *sysaufs_si_attrs[] = {
31136 +       &sysaufs_si_attr_xi_path.attr,
31137 +       NULL,
31138 +};
31139 +
31140 +static const struct sysfs_ops au_sbi_ops = {
31141 +       .show   = sysaufs_si_show
31142 +};
31143 +
31144 +static struct kobj_type au_sbi_ktype = {
31145 +       .release        = au_si_free,
31146 +       .sysfs_ops      = &au_sbi_ops,
31147 +       .default_attrs  = sysaufs_si_attrs
31148 +};
31149 +
31150 +/* ---------------------------------------------------------------------- */
31151 +
31152 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31153 +{
31154 +       int err;
31155 +
31156 +       sbinfo->si_kobj.kset = sysaufs_kset;
31157 +       /* cf. sysaufs_name() */
31158 +       err = kobject_init_and_add
31159 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31160 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31161 +
31162 +       return err;
31163 +}
31164 +
31165 +void sysaufs_fin(void)
31166 +{
31167 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31168 +       kset_unregister(sysaufs_kset);
31169 +}
31170 +
31171 +int __init sysaufs_init(void)
31172 +{
31173 +       int err;
31174 +
31175 +       do {
31176 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31177 +       } while (!sysaufs_si_mask);
31178 +
31179 +       err = -EINVAL;
31180 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31181 +       if (unlikely(!sysaufs_kset))
31182 +               goto out;
31183 +       err = PTR_ERR(sysaufs_kset);
31184 +       if (IS_ERR(sysaufs_kset))
31185 +               goto out;
31186 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31187 +       if (unlikely(err))
31188 +               kset_unregister(sysaufs_kset);
31189 +
31190 +out:
31191 +       return err;
31192 +}
31193 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31194 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31195 +++ linux/fs/aufs/sysaufs.h     2019-07-11 15:42:14.475571481 +0200
31196 @@ -0,0 +1,102 @@
31197 +/* SPDX-License-Identifier: GPL-2.0 */
31198 +/*
31199 + * Copyright (C) 2005-2019 Junjiro R. Okajima
31200 + *
31201 + * This program, aufs is free software; you can redistribute it and/or modify
31202 + * it under the terms of the GNU General Public License as published by
31203 + * the Free Software Foundation; either version 2 of the License, or
31204 + * (at your option) any later version.
31205 + *
31206 + * This program is distributed in the hope that it will be useful,
31207 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31208 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31209 + * GNU General Public License for more details.
31210 + *
31211 + * You should have received a copy of the GNU General Public License
31212 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31213 + */
31214 +
31215 +/*
31216 + * sysfs interface and mount lifetime management
31217 + */
31218 +
31219 +#ifndef __SYSAUFS_H__
31220 +#define __SYSAUFS_H__
31221 +
31222 +#ifdef __KERNEL__
31223 +
31224 +#include <linux/sysfs.h>
31225 +#include "module.h"
31226 +
31227 +struct super_block;
31228 +struct au_sbinfo;
31229 +
31230 +struct sysaufs_si_attr {
31231 +       struct attribute attr;
31232 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31233 +};
31234 +
31235 +/* ---------------------------------------------------------------------- */
31236 +
31237 +/* sysaufs.c */
31238 +extern unsigned long sysaufs_si_mask;
31239 +extern struct kset *sysaufs_kset;
31240 +extern struct attribute *sysaufs_si_attrs[];
31241 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31242 +int __init sysaufs_init(void);
31243 +void sysaufs_fin(void);
31244 +
31245 +/* ---------------------------------------------------------------------- */
31246 +
31247 +/* some people doesn't like to show a pointer in kernel */
31248 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31249 +{
31250 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31251 +}
31252 +
31253 +#define SysaufsSiNamePrefix    "si_"
31254 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31255 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31256 +{
31257 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31258 +                sysaufs_si_id(sbinfo));
31259 +}
31260 +
31261 +struct au_branch;
31262 +#ifdef CONFIG_SYSFS
31263 +/* sysfs.c */
31264 +extern struct attribute_group *sysaufs_attr_group;
31265 +
31266 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31267 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31268 +                        char *buf);
31269 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31270 +#ifdef CONFIG_COMPAT
31271 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31272 +#endif
31273 +
31274 +void sysaufs_br_init(struct au_branch *br);
31275 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31276 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31277 +
31278 +#define sysaufs_brs_init()     do {} while (0)
31279 +
31280 +#else
31281 +#define sysaufs_attr_group     NULL
31282 +
31283 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31284 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31285 +       struct attribute *attr, char *buf)
31286 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31287 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31288 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31289 +
31290 +static inline void sysaufs_brs_init(void)
31291 +{
31292 +       sysaufs_brs = 0;
31293 +}
31294 +
31295 +#endif /* CONFIG_SYSFS */
31296 +
31297 +#endif /* __KERNEL__ */
31298 +#endif /* __SYSAUFS_H__ */
31299 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31300 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31301 +++ linux/fs/aufs/sysfs.c       2019-07-11 15:42:14.475571481 +0200
31302 @@ -0,0 +1,374 @@
31303 +// SPDX-License-Identifier: GPL-2.0
31304 +/*
31305 + * Copyright (C) 2005-2019 Junjiro R. Okajima
31306 + *
31307 + * This program, aufs is free software; you can redistribute it and/or modify
31308 + * it under the terms of the GNU General Public License as published by
31309 + * the Free Software Foundation; either version 2 of the License, or
31310 + * (at your option) any later version.
31311 + *
31312 + * This program is distributed in the hope that it will be useful,
31313 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31314 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31315 + * GNU General Public License for more details.
31316 + *
31317 + * You should have received a copy of the GNU General Public License
31318 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31319 + */
31320 +
31321 +/*
31322 + * sysfs interface
31323 + */
31324 +
31325 +#include <linux/compat.h>
31326 +#include <linux/seq_file.h>
31327 +#include "aufs.h"
31328 +
31329 +#ifdef CONFIG_AUFS_FS_MODULE
31330 +/* this entry violates the "one line per file" policy of sysfs */
31331 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31332 +                          char *buf)
31333 +{
31334 +       ssize_t err;
31335 +       static char *conf =
31336 +/* this file is generated at compiling */
31337 +#include "conf.str"
31338 +               ;
31339 +
31340 +       err = snprintf(buf, PAGE_SIZE, conf);
31341 +       if (unlikely(err >= PAGE_SIZE))
31342 +               err = -EFBIG;
31343 +       return err;
31344 +}
31345 +
31346 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31347 +#endif
31348 +
31349 +static struct attribute *au_attr[] = {
31350 +#ifdef CONFIG_AUFS_FS_MODULE
31351 +       &au_config_attr.attr,
31352 +#endif
31353 +       NULL,   /* need to NULL terminate the list of attributes */
31354 +};
31355 +
31356 +static struct attribute_group sysaufs_attr_group_body = {
31357 +       .attrs = au_attr
31358 +};
31359 +
31360 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31361 +
31362 +/* ---------------------------------------------------------------------- */
31363 +
31364 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31365 +{
31366 +       int err;
31367 +
31368 +       SiMustAnyLock(sb);
31369 +
31370 +       err = 0;
31371 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31372 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31373 +               seq_putc(seq, '\n');
31374 +       }
31375 +       return err;
31376 +}
31377 +
31378 +/*
31379 + * the lifetime of branch is independent from the entry under sysfs.
31380 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31381 + * unlinked.
31382 + */
31383 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31384 +                        aufs_bindex_t bindex, int idx)
31385 +{
31386 +       int err;
31387 +       struct path path;
31388 +       struct dentry *root;
31389 +       struct au_branch *br;
31390 +       au_br_perm_str_t perm;
31391 +
31392 +       AuDbg("b%d\n", bindex);
31393 +
31394 +       err = 0;
31395 +       root = sb->s_root;
31396 +       di_read_lock_parent(root, !AuLock_IR);
31397 +       br = au_sbr(sb, bindex);
31398 +
31399 +       switch (idx) {
31400 +       case AuBrSysfs_BR:
31401 +               path.mnt = au_br_mnt(br);
31402 +               path.dentry = au_h_dptr(root, bindex);
31403 +               err = au_seq_path(seq, &path);
31404 +               if (!err) {
31405 +                       au_optstr_br_perm(&perm, br->br_perm);
31406 +                       seq_printf(seq, "=%s\n", perm.a);
31407 +               }
31408 +               break;
31409 +       case AuBrSysfs_BRID:
31410 +               seq_printf(seq, "%d\n", br->br_id);
31411 +               break;
31412 +       }
31413 +       di_read_unlock(root, !AuLock_IR);
31414 +       if (unlikely(err || seq_has_overflowed(seq)))
31415 +               err = -E2BIG;
31416 +
31417 +       return err;
31418 +}
31419 +
31420 +/* ---------------------------------------------------------------------- */
31421 +
31422 +static struct seq_file *au_seq(char *p, ssize_t len)
31423 +{
31424 +       struct seq_file *seq;
31425 +
31426 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31427 +       if (seq) {
31428 +               /* mutex_init(&seq.lock); */
31429 +               seq->buf = p;
31430 +               seq->size = len;
31431 +               return seq; /* success */
31432 +       }
31433 +
31434 +       seq = ERR_PTR(-ENOMEM);
31435 +       return seq;
31436 +}
31437 +
31438 +#define SysaufsBr_PREFIX       "br"
31439 +#define SysaufsBrid_PREFIX     "brid"
31440 +
31441 +/* todo: file size may exceed PAGE_SIZE */
31442 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31443 +                       char *buf)
31444 +{
31445 +       ssize_t err;
31446 +       int idx;
31447 +       long l;
31448 +       aufs_bindex_t bbot;
31449 +       struct au_sbinfo *sbinfo;
31450 +       struct super_block *sb;
31451 +       struct seq_file *seq;
31452 +       char *name;
31453 +       struct attribute **cattr;
31454 +
31455 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31456 +       sb = sbinfo->si_sb;
31457 +
31458 +       /*
31459 +        * prevent a race condition between sysfs and aufs.
31460 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31461 +        * prohibits maintaining the sysfs entries.
31462 +        * hew we acquire read lock after sysfs_get_active_two().
31463 +        * on the other hand, the remount process may maintain the sysfs/aufs
31464 +        * entries after acquiring write lock.
31465 +        * it can cause a deadlock.
31466 +        * simply we gave up processing read here.
31467 +        */
31468 +       err = -EBUSY;
31469 +       if (unlikely(!si_noflush_read_trylock(sb)))
31470 +               goto out;
31471 +
31472 +       seq = au_seq(buf, PAGE_SIZE);
31473 +       err = PTR_ERR(seq);
31474 +       if (IS_ERR(seq))
31475 +               goto out_unlock;
31476 +
31477 +       name = (void *)attr->name;
31478 +       cattr = sysaufs_si_attrs;
31479 +       while (*cattr) {
31480 +               if (!strcmp(name, (*cattr)->name)) {
31481 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31482 +                               ->show(seq, sb);
31483 +                       goto out_seq;
31484 +               }
31485 +               cattr++;
31486 +       }
31487 +
31488 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31489 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31490 +               idx = AuBrSysfs_BRID;
31491 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31492 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31493 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31494 +               idx = AuBrSysfs_BR;
31495 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31496 +       } else
31497 +                 BUG();
31498 +
31499 +       err = kstrtol(name, 10, &l);
31500 +       if (!err) {
31501 +               bbot = au_sbbot(sb);
31502 +               if (l <= bbot)
31503 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31504 +               else
31505 +                       err = -ENOENT;
31506 +       }
31507 +
31508 +out_seq:
31509 +       if (!err) {
31510 +               err = seq->count;
31511 +               /* sysfs limit */
31512 +               if (unlikely(err == PAGE_SIZE))
31513 +                       err = -EFBIG;
31514 +       }
31515 +       au_kfree_rcu(seq);
31516 +out_unlock:
31517 +       si_read_unlock(sb);
31518 +out:
31519 +       return err;
31520 +}
31521 +
31522 +/* ---------------------------------------------------------------------- */
31523 +
31524 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31525 +{
31526 +       int err;
31527 +       int16_t brid;
31528 +       aufs_bindex_t bindex, bbot;
31529 +       size_t sz;
31530 +       char *buf;
31531 +       struct seq_file *seq;
31532 +       struct au_branch *br;
31533 +
31534 +       si_read_lock(sb, AuLock_FLUSH);
31535 +       bbot = au_sbbot(sb);
31536 +       err = bbot + 1;
31537 +       if (!arg)
31538 +               goto out;
31539 +
31540 +       err = -ENOMEM;
31541 +       buf = (void *)__get_free_page(GFP_NOFS);
31542 +       if (unlikely(!buf))
31543 +               goto out;
31544 +
31545 +       seq = au_seq(buf, PAGE_SIZE);
31546 +       err = PTR_ERR(seq);
31547 +       if (IS_ERR(seq))
31548 +               goto out_buf;
31549 +
31550 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31551 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31552 +               /* VERIFY_WRITE */
31553 +               err = !access_ok(arg, sizeof(*arg));
31554 +               if (unlikely(err))
31555 +                       break;
31556 +
31557 +               br = au_sbr(sb, bindex);
31558 +               brid = br->br_id;
31559 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31560 +               err = __put_user(brid, &arg->id);
31561 +               if (unlikely(err))
31562 +                       break;
31563 +
31564 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31565 +               err = __put_user(br->br_perm, &arg->perm);
31566 +               if (unlikely(err))
31567 +                       break;
31568 +
31569 +               err = au_seq_path(seq, &br->br_path);
31570 +               if (unlikely(err))
31571 +                       break;
31572 +               seq_putc(seq, '\0');
31573 +               if (!seq_has_overflowed(seq)) {
31574 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31575 +                       seq->count = 0;
31576 +                       if (unlikely(err))
31577 +                               break;
31578 +               } else {
31579 +                       err = -E2BIG;
31580 +                       goto out_seq;
31581 +               }
31582 +       }
31583 +       if (unlikely(err))
31584 +               err = -EFAULT;
31585 +
31586 +out_seq:
31587 +       au_kfree_rcu(seq);
31588 +out_buf:
31589 +       free_page((unsigned long)buf);
31590 +out:
31591 +       si_read_unlock(sb);
31592 +       return err;
31593 +}
31594 +
31595 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31596 +{
31597 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31598 +}
31599 +
31600 +#ifdef CONFIG_COMPAT
31601 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31602 +{
31603 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31604 +}
31605 +#endif
31606 +
31607 +/* ---------------------------------------------------------------------- */
31608 +
31609 +void sysaufs_br_init(struct au_branch *br)
31610 +{
31611 +       int i;
31612 +       struct au_brsysfs *br_sysfs;
31613 +       struct attribute *attr;
31614 +
31615 +       br_sysfs = br->br_sysfs;
31616 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31617 +               attr = &br_sysfs->attr;
31618 +               sysfs_attr_init(attr);
31619 +               attr->name = br_sysfs->name;
31620 +               attr->mode = 0444;
31621 +               br_sysfs++;
31622 +       }
31623 +}
31624 +
31625 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31626 +{
31627 +       struct au_branch *br;
31628 +       struct kobject *kobj;
31629 +       struct au_brsysfs *br_sysfs;
31630 +       int i;
31631 +       aufs_bindex_t bbot;
31632 +
31633 +       if (!sysaufs_brs)
31634 +               return;
31635 +
31636 +       kobj = &au_sbi(sb)->si_kobj;
31637 +       bbot = au_sbbot(sb);
31638 +       for (; bindex <= bbot; bindex++) {
31639 +               br = au_sbr(sb, bindex);
31640 +               br_sysfs = br->br_sysfs;
31641 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31642 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31643 +                       br_sysfs++;
31644 +               }
31645 +       }
31646 +}
31647 +
31648 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31649 +{
31650 +       int err, i;
31651 +       aufs_bindex_t bbot;
31652 +       struct kobject *kobj;
31653 +       struct au_branch *br;
31654 +       struct au_brsysfs *br_sysfs;
31655 +
31656 +       if (!sysaufs_brs)
31657 +               return;
31658 +
31659 +       kobj = &au_sbi(sb)->si_kobj;
31660 +       bbot = au_sbbot(sb);
31661 +       for (; bindex <= bbot; bindex++) {
31662 +               br = au_sbr(sb, bindex);
31663 +               br_sysfs = br->br_sysfs;
31664 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31665 +                        SysaufsBr_PREFIX "%d", bindex);
31666 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31667 +                        SysaufsBrid_PREFIX "%d", bindex);
31668 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31669 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31670 +                       if (unlikely(err))
31671 +                               pr_warn("failed %s under sysfs(%d)\n",
31672 +                                       br_sysfs->name, err);
31673 +                       br_sysfs++;
31674 +               }
31675 +       }
31676 +}
31677 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31678 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31679 +++ linux/fs/aufs/sysrq.c       2019-07-11 15:42:14.475571481 +0200
31680 @@ -0,0 +1,160 @@
31681 +// SPDX-License-Identifier: GPL-2.0
31682 +/*
31683 + * Copyright (C) 2005-2019 Junjiro R. Okajima
31684 + *
31685 + * This program, aufs is free software; you can redistribute it and/or modify
31686 + * it under the terms of the GNU General Public License as published by
31687 + * the Free Software Foundation; either version 2 of the License, or
31688 + * (at your option) any later version.
31689 + *
31690 + * This program is distributed in the hope that it will be useful,
31691 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31692 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31693 + * GNU General Public License for more details.
31694 + *
31695 + * You should have received a copy of the GNU General Public License
31696 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31697 + */
31698 +
31699 +/*
31700 + * magic sysrq handler
31701 + */
31702 +
31703 +/* #include <linux/sysrq.h> */
31704 +#include <linux/writeback.h>
31705 +#include "aufs.h"
31706 +
31707 +/* ---------------------------------------------------------------------- */
31708 +
31709 +static void sysrq_sb(struct super_block *sb)
31710 +{
31711 +       char *plevel;
31712 +       struct au_sbinfo *sbinfo;
31713 +       struct file *file;
31714 +       struct hlist_bl_head *files;
31715 +       struct hlist_bl_node *pos;
31716 +       struct au_finfo *finfo;
31717 +
31718 +       plevel = au_plevel;
31719 +       au_plevel = KERN_WARNING;
31720 +
31721 +       /* since we define pr_fmt, call printk directly */
31722 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31723 +
31724 +       sbinfo = au_sbi(sb);
31725 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31726 +       pr("superblock\n");
31727 +       au_dpri_sb(sb);
31728 +
31729 +#if 0
31730 +       pr("root dentry\n");
31731 +       au_dpri_dentry(sb->s_root);
31732 +       pr("root inode\n");
31733 +       au_dpri_inode(d_inode(sb->s_root));
31734 +#endif
31735 +
31736 +#if 0
31737 +       do {
31738 +               int err, i, j, ndentry;
31739 +               struct au_dcsub_pages dpages;
31740 +               struct au_dpage *dpage;
31741 +
31742 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31743 +               if (unlikely(err))
31744 +                       break;
31745 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31746 +               if (!err)
31747 +                       for (i = 0; i < dpages.ndpage; i++) {
31748 +                               dpage = dpages.dpages + i;
31749 +                               ndentry = dpage->ndentry;
31750 +                               for (j = 0; j < ndentry; j++)
31751 +                                       au_dpri_dentry(dpage->dentries[j]);
31752 +                       }
31753 +               au_dpages_free(&dpages);
31754 +       } while (0);
31755 +#endif
31756 +
31757 +#if 1
31758 +       {
31759 +               struct inode *i;
31760 +
31761 +               pr("isolated inode\n");
31762 +               spin_lock(&sb->s_inode_list_lock);
31763 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31764 +                       spin_lock(&i->i_lock);
31765 +                       if (1 || hlist_empty(&i->i_dentry))
31766 +                               au_dpri_inode(i);
31767 +                       spin_unlock(&i->i_lock);
31768 +               }
31769 +               spin_unlock(&sb->s_inode_list_lock);
31770 +       }
31771 +#endif
31772 +       pr("files\n");
31773 +       files = &au_sbi(sb)->si_files;
31774 +       hlist_bl_lock(files);
31775 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31776 +               umode_t mode;
31777 +
31778 +               file = finfo->fi_file;
31779 +               mode = file_inode(file)->i_mode;
31780 +               if (!special_file(mode))
31781 +                       au_dpri_file(file);
31782 +       }
31783 +       hlist_bl_unlock(files);
31784 +       pr("done\n");
31785 +
31786 +#undef pr
31787 +       au_plevel = plevel;
31788 +}
31789 +
31790 +/* ---------------------------------------------------------------------- */
31791 +
31792 +/* module parameter */
31793 +static char *aufs_sysrq_key = "a";
31794 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31795 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31796 +
31797 +static void au_sysrq(int key __maybe_unused)
31798 +{
31799 +       struct au_sbinfo *sbinfo;
31800 +       struct hlist_bl_node *pos;
31801 +
31802 +       lockdep_off();
31803 +       au_sbilist_lock();
31804 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31805 +               sysrq_sb(sbinfo->si_sb);
31806 +       au_sbilist_unlock();
31807 +       lockdep_on();
31808 +}
31809 +
31810 +static struct sysrq_key_op au_sysrq_op = {
31811 +       .handler        = au_sysrq,
31812 +       .help_msg       = "Aufs",
31813 +       .action_msg     = "Aufs",
31814 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31815 +};
31816 +
31817 +/* ---------------------------------------------------------------------- */
31818 +
31819 +int __init au_sysrq_init(void)
31820 +{
31821 +       int err;
31822 +       char key;
31823 +
31824 +       err = -1;
31825 +       key = *aufs_sysrq_key;
31826 +       if ('a' <= key && key <= 'z')
31827 +               err = register_sysrq_key(key, &au_sysrq_op);
31828 +       if (unlikely(err))
31829 +               pr_err("err %d, sysrq=%c\n", err, key);
31830 +       return err;
31831 +}
31832 +
31833 +void au_sysrq_fin(void)
31834 +{
31835 +       int err;
31836 +
31837 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31838 +       if (unlikely(err))
31839 +               pr_err("err %d (ignored)\n", err);
31840 +}
31841 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31842 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31843 +++ linux/fs/aufs/vdir.c        2019-07-11 15:42:14.475571481 +0200
31844 @@ -0,0 +1,896 @@
31845 +// SPDX-License-Identifier: GPL-2.0
31846 +/*
31847 + * Copyright (C) 2005-2019 Junjiro R. Okajima
31848 + *
31849 + * This program, aufs is free software; you can redistribute it and/or modify
31850 + * it under the terms of the GNU General Public License as published by
31851 + * the Free Software Foundation; either version 2 of the License, or
31852 + * (at your option) any later version.
31853 + *
31854 + * This program is distributed in the hope that it will be useful,
31855 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31856 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31857 + * GNU General Public License for more details.
31858 + *
31859 + * You should have received a copy of the GNU General Public License
31860 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31861 + */
31862 +
31863 +/*
31864 + * virtual or vertical directory
31865 + */
31866 +
31867 +#include <linux/iversion.h>
31868 +#include "aufs.h"
31869 +
31870 +static unsigned int calc_size(int nlen)
31871 +{
31872 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31873 +}
31874 +
31875 +static int set_deblk_end(union au_vdir_deblk_p *p,
31876 +                        union au_vdir_deblk_p *deblk_end)
31877 +{
31878 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31879 +               p->de->de_str.len = 0;
31880 +               /* smp_mb(); */
31881 +               return 0;
31882 +       }
31883 +       return -1; /* error */
31884 +}
31885 +
31886 +/* returns true or false */
31887 +static int is_deblk_end(union au_vdir_deblk_p *p,
31888 +                       union au_vdir_deblk_p *deblk_end)
31889 +{
31890 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31891 +               return !p->de->de_str.len;
31892 +       return 1;
31893 +}
31894 +
31895 +static unsigned char *last_deblk(struct au_vdir *vdir)
31896 +{
31897 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31898 +}
31899 +
31900 +/* ---------------------------------------------------------------------- */
31901 +
31902 +/* estimate the appropriate size for name hash table */
31903 +unsigned int au_rdhash_est(loff_t sz)
31904 +{
31905 +       unsigned int n;
31906 +
31907 +       n = UINT_MAX;
31908 +       sz >>= 10;
31909 +       if (sz < n)
31910 +               n = sz;
31911 +       if (sz < AUFS_RDHASH_DEF)
31912 +               n = AUFS_RDHASH_DEF;
31913 +       /* pr_info("n %u\n", n); */
31914 +       return n;
31915 +}
31916 +
31917 +/*
31918 + * the allocated memory has to be freed by
31919 + * au_nhash_wh_free() or au_nhash_de_free().
31920 + */
31921 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31922 +{
31923 +       struct hlist_head *head;
31924 +       unsigned int u;
31925 +       size_t sz;
31926 +
31927 +       sz = sizeof(*nhash->nh_head) * num_hash;
31928 +       head = kmalloc(sz, gfp);
31929 +       if (head) {
31930 +               nhash->nh_num = num_hash;
31931 +               nhash->nh_head = head;
31932 +               for (u = 0; u < num_hash; u++)
31933 +                       INIT_HLIST_HEAD(head++);
31934 +               return 0; /* success */
31935 +       }
31936 +
31937 +       return -ENOMEM;
31938 +}
31939 +
31940 +static void nhash_count(struct hlist_head *head)
31941 +{
31942 +#if 0
31943 +       unsigned long n;
31944 +       struct hlist_node *pos;
31945 +
31946 +       n = 0;
31947 +       hlist_for_each(pos, head)
31948 +               n++;
31949 +       pr_info("%lu\n", n);
31950 +#endif
31951 +}
31952 +
31953 +static void au_nhash_wh_do_free(struct hlist_head *head)
31954 +{
31955 +       struct au_vdir_wh *pos;
31956 +       struct hlist_node *node;
31957 +
31958 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31959 +               au_kfree_rcu(pos);
31960 +}
31961 +
31962 +static void au_nhash_de_do_free(struct hlist_head *head)
31963 +{
31964 +       struct au_vdir_dehstr *pos;
31965 +       struct hlist_node *node;
31966 +
31967 +       hlist_for_each_entry_safe(pos, node, head, hash)
31968 +               au_cache_free_vdir_dehstr(pos);
31969 +}
31970 +
31971 +static void au_nhash_do_free(struct au_nhash *nhash,
31972 +                            void (*free)(struct hlist_head *head))
31973 +{
31974 +       unsigned int n;
31975 +       struct hlist_head *head;
31976 +
31977 +       n = nhash->nh_num;
31978 +       if (!n)
31979 +               return;
31980 +
31981 +       head = nhash->nh_head;
31982 +       while (n-- > 0) {
31983 +               nhash_count(head);
31984 +               free(head++);
31985 +       }
31986 +       au_kfree_try_rcu(nhash->nh_head);
31987 +}
31988 +
31989 +void au_nhash_wh_free(struct au_nhash *whlist)
31990 +{
31991 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31992 +}
31993 +
31994 +static void au_nhash_de_free(struct au_nhash *delist)
31995 +{
31996 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31997 +}
31998 +
31999 +/* ---------------------------------------------------------------------- */
32000 +
32001 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32002 +                           int limit)
32003 +{
32004 +       int num;
32005 +       unsigned int u, n;
32006 +       struct hlist_head *head;
32007 +       struct au_vdir_wh *pos;
32008 +
32009 +       num = 0;
32010 +       n = whlist->nh_num;
32011 +       head = whlist->nh_head;
32012 +       for (u = 0; u < n; u++, head++)
32013 +               hlist_for_each_entry(pos, head, wh_hash)
32014 +                       if (pos->wh_bindex == btgt && ++num > limit)
32015 +                               return 1;
32016 +       return 0;
32017 +}
32018 +
32019 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32020 +                                      unsigned char *name,
32021 +                                      unsigned int len)
32022 +{
32023 +       unsigned int v;
32024 +       /* const unsigned int magic_bit = 12; */
32025 +
32026 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32027 +
32028 +       v = 0;
32029 +       if (len > 8)
32030 +               len = 8;
32031 +       while (len--)
32032 +               v += *name++;
32033 +       /* v = hash_long(v, magic_bit); */
32034 +       v %= nhash->nh_num;
32035 +       return nhash->nh_head + v;
32036 +}
32037 +
32038 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32039 +                             int nlen)
32040 +{
32041 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32042 +}
32043 +
32044 +/* returns found or not */
32045 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32046 +{
32047 +       struct hlist_head *head;
32048 +       struct au_vdir_wh *pos;
32049 +       struct au_vdir_destr *str;
32050 +
32051 +       head = au_name_hash(whlist, name, nlen);
32052 +       hlist_for_each_entry(pos, head, wh_hash) {
32053 +               str = &pos->wh_str;
32054 +               AuDbg("%.*s\n", str->len, str->name);
32055 +               if (au_nhash_test_name(str, name, nlen))
32056 +                       return 1;
32057 +       }
32058 +       return 0;
32059 +}
32060 +
32061 +/* returns found(true) or not */
32062 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32063 +{
32064 +       struct hlist_head *head;
32065 +       struct au_vdir_dehstr *pos;
32066 +       struct au_vdir_destr *str;
32067 +
32068 +       head = au_name_hash(delist, name, nlen);
32069 +       hlist_for_each_entry(pos, head, hash) {
32070 +               str = pos->str;
32071 +               AuDbg("%.*s\n", str->len, str->name);
32072 +               if (au_nhash_test_name(str, name, nlen))
32073 +                       return 1;
32074 +       }
32075 +       return 0;
32076 +}
32077 +
32078 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32079 +                           unsigned char d_type)
32080 +{
32081 +#ifdef CONFIG_AUFS_SHWH
32082 +       wh->wh_ino = ino;
32083 +       wh->wh_type = d_type;
32084 +#endif
32085 +}
32086 +
32087 +/* ---------------------------------------------------------------------- */
32088 +
32089 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32090 +                      unsigned int d_type, aufs_bindex_t bindex,
32091 +                      unsigned char shwh)
32092 +{
32093 +       int err;
32094 +       struct au_vdir_destr *str;
32095 +       struct au_vdir_wh *wh;
32096 +
32097 +       AuDbg("%.*s\n", nlen, name);
32098 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32099 +
32100 +       err = -ENOMEM;
32101 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32102 +       if (unlikely(!wh))
32103 +               goto out;
32104 +
32105 +       err = 0;
32106 +       wh->wh_bindex = bindex;
32107 +       if (shwh)
32108 +               au_shwh_init_wh(wh, ino, d_type);
32109 +       str = &wh->wh_str;
32110 +       str->len = nlen;
32111 +       memcpy(str->name, name, nlen);
32112 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32113 +       /* smp_mb(); */
32114 +
32115 +out:
32116 +       return err;
32117 +}
32118 +
32119 +static int append_deblk(struct au_vdir *vdir)
32120 +{
32121 +       int err;
32122 +       unsigned long ul;
32123 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32124 +       union au_vdir_deblk_p p, deblk_end;
32125 +       unsigned char **o;
32126 +
32127 +       err = -ENOMEM;
32128 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32129 +                       GFP_NOFS, /*may_shrink*/0);
32130 +       if (unlikely(!o))
32131 +               goto out;
32132 +
32133 +       vdir->vd_deblk = o;
32134 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32135 +       if (p.deblk) {
32136 +               ul = vdir->vd_nblk++;
32137 +               vdir->vd_deblk[ul] = p.deblk;
32138 +               vdir->vd_last.ul = ul;
32139 +               vdir->vd_last.p.deblk = p.deblk;
32140 +               deblk_end.deblk = p.deblk + deblk_sz;
32141 +               err = set_deblk_end(&p, &deblk_end);
32142 +       }
32143 +
32144 +out:
32145 +       return err;
32146 +}
32147 +
32148 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32149 +                    unsigned int d_type, struct au_nhash *delist)
32150 +{
32151 +       int err;
32152 +       unsigned int sz;
32153 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32154 +       union au_vdir_deblk_p p, *room, deblk_end;
32155 +       struct au_vdir_dehstr *dehstr;
32156 +
32157 +       p.deblk = last_deblk(vdir);
32158 +       deblk_end.deblk = p.deblk + deblk_sz;
32159 +       room = &vdir->vd_last.p;
32160 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32161 +                 || !is_deblk_end(room, &deblk_end));
32162 +
32163 +       sz = calc_size(nlen);
32164 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32165 +               err = append_deblk(vdir);
32166 +               if (unlikely(err))
32167 +                       goto out;
32168 +
32169 +               p.deblk = last_deblk(vdir);
32170 +               deblk_end.deblk = p.deblk + deblk_sz;
32171 +               /* smp_mb(); */
32172 +               AuDebugOn(room->deblk != p.deblk);
32173 +       }
32174 +
32175 +       err = -ENOMEM;
32176 +       dehstr = au_cache_alloc_vdir_dehstr();
32177 +       if (unlikely(!dehstr))
32178 +               goto out;
32179 +
32180 +       dehstr->str = &room->de->de_str;
32181 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32182 +       room->de->de_ino = ino;
32183 +       room->de->de_type = d_type;
32184 +       room->de->de_str.len = nlen;
32185 +       memcpy(room->de->de_str.name, name, nlen);
32186 +
32187 +       err = 0;
32188 +       room->deblk += sz;
32189 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32190 +               err = append_deblk(vdir);
32191 +       /* smp_mb(); */
32192 +
32193 +out:
32194 +       return err;
32195 +}
32196 +
32197 +/* ---------------------------------------------------------------------- */
32198 +
32199 +void au_vdir_free(struct au_vdir *vdir)
32200 +{
32201 +       unsigned char **deblk;
32202 +
32203 +       deblk = vdir->vd_deblk;
32204 +       while (vdir->vd_nblk--)
32205 +               au_kfree_try_rcu(*deblk++);
32206 +       au_kfree_try_rcu(vdir->vd_deblk);
32207 +       au_cache_free_vdir(vdir);
32208 +}
32209 +
32210 +static struct au_vdir *alloc_vdir(struct file *file)
32211 +{
32212 +       struct au_vdir *vdir;
32213 +       struct super_block *sb;
32214 +       int err;
32215 +
32216 +       sb = file->f_path.dentry->d_sb;
32217 +       SiMustAnyLock(sb);
32218 +
32219 +       err = -ENOMEM;
32220 +       vdir = au_cache_alloc_vdir();
32221 +       if (unlikely(!vdir))
32222 +               goto out;
32223 +
32224 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32225 +       if (unlikely(!vdir->vd_deblk))
32226 +               goto out_free;
32227 +
32228 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32229 +       if (!vdir->vd_deblk_sz) {
32230 +               /* estimate the appropriate size for deblk */
32231 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32232 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32233 +       }
32234 +       vdir->vd_nblk = 0;
32235 +       vdir->vd_version = 0;
32236 +       vdir->vd_jiffy = 0;
32237 +       err = append_deblk(vdir);
32238 +       if (!err)
32239 +               return vdir; /* success */
32240 +
32241 +       au_kfree_try_rcu(vdir->vd_deblk);
32242 +
32243 +out_free:
32244 +       au_cache_free_vdir(vdir);
32245 +out:
32246 +       vdir = ERR_PTR(err);
32247 +       return vdir;
32248 +}
32249 +
32250 +static int reinit_vdir(struct au_vdir *vdir)
32251 +{
32252 +       int err;
32253 +       union au_vdir_deblk_p p, deblk_end;
32254 +
32255 +       while (vdir->vd_nblk > 1) {
32256 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32257 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32258 +               vdir->vd_nblk--;
32259 +       }
32260 +       p.deblk = vdir->vd_deblk[0];
32261 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32262 +       err = set_deblk_end(&p, &deblk_end);
32263 +       /* keep vd_dblk_sz */
32264 +       vdir->vd_last.ul = 0;
32265 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32266 +       vdir->vd_version = 0;
32267 +       vdir->vd_jiffy = 0;
32268 +       /* smp_mb(); */
32269 +       return err;
32270 +}
32271 +
32272 +/* ---------------------------------------------------------------------- */
32273 +
32274 +#define AuFillVdir_CALLED      1
32275 +#define AuFillVdir_WHABLE      (1 << 1)
32276 +#define AuFillVdir_SHWH                (1 << 2)
32277 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32278 +#define au_fset_fillvdir(flags, name) \
32279 +       do { (flags) |= AuFillVdir_##name; } while (0)
32280 +#define au_fclr_fillvdir(flags, name) \
32281 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32282 +
32283 +#ifndef CONFIG_AUFS_SHWH
32284 +#undef AuFillVdir_SHWH
32285 +#define AuFillVdir_SHWH                0
32286 +#endif
32287 +
32288 +struct fillvdir_arg {
32289 +       struct dir_context      ctx;
32290 +       struct file             *file;
32291 +       struct au_vdir          *vdir;
32292 +       struct au_nhash         delist;
32293 +       struct au_nhash         whlist;
32294 +       aufs_bindex_t           bindex;
32295 +       unsigned int            flags;
32296 +       int                     err;
32297 +};
32298 +
32299 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32300 +                   loff_t offset __maybe_unused, u64 h_ino,
32301 +                   unsigned int d_type)
32302 +{
32303 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32304 +       char *name = (void *)__name;
32305 +       struct super_block *sb;
32306 +       ino_t ino;
32307 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32308 +
32309 +       arg->err = 0;
32310 +       sb = arg->file->f_path.dentry->d_sb;
32311 +       au_fset_fillvdir(arg->flags, CALLED);
32312 +       /* smp_mb(); */
32313 +       if (nlen <= AUFS_WH_PFX_LEN
32314 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32315 +               if (test_known(&arg->delist, name, nlen)
32316 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32317 +                       goto out; /* already exists or whiteouted */
32318 +
32319 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32320 +               if (!arg->err) {
32321 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32322 +                               d_type = DT_UNKNOWN;
32323 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32324 +                                            d_type, &arg->delist);
32325 +               }
32326 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32327 +               name += AUFS_WH_PFX_LEN;
32328 +               nlen -= AUFS_WH_PFX_LEN;
32329 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32330 +                       goto out; /* already whiteouted */
32331 +
32332 +               ino = 0; /* just to suppress a warning */
32333 +               if (shwh)
32334 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32335 +                                            &ino);
32336 +               if (!arg->err) {
32337 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32338 +                               d_type = DT_UNKNOWN;
32339 +                       arg->err = au_nhash_append_wh
32340 +                               (&arg->whlist, name, nlen, ino, d_type,
32341 +                                arg->bindex, shwh);
32342 +               }
32343 +       }
32344 +
32345 +out:
32346 +       if (!arg->err)
32347 +               arg->vdir->vd_jiffy = jiffies;
32348 +       /* smp_mb(); */
32349 +       AuTraceErr(arg->err);
32350 +       return arg->err;
32351 +}
32352 +
32353 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32354 +                         struct au_nhash *whlist, struct au_nhash *delist)
32355 +{
32356 +#ifdef CONFIG_AUFS_SHWH
32357 +       int err;
32358 +       unsigned int nh, u;
32359 +       struct hlist_head *head;
32360 +       struct au_vdir_wh *pos;
32361 +       struct hlist_node *n;
32362 +       char *p, *o;
32363 +       struct au_vdir_destr *destr;
32364 +
32365 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32366 +
32367 +       err = -ENOMEM;
32368 +       o = p = (void *)__get_free_page(GFP_NOFS);
32369 +       if (unlikely(!p))
32370 +               goto out;
32371 +
32372 +       err = 0;
32373 +       nh = whlist->nh_num;
32374 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32375 +       p += AUFS_WH_PFX_LEN;
32376 +       for (u = 0; u < nh; u++) {
32377 +               head = whlist->nh_head + u;
32378 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32379 +                       destr = &pos->wh_str;
32380 +                       memcpy(p, destr->name, destr->len);
32381 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32382 +                                       pos->wh_ino, pos->wh_type, delist);
32383 +                       if (unlikely(err))
32384 +                               break;
32385 +               }
32386 +       }
32387 +
32388 +       free_page((unsigned long)o);
32389 +
32390 +out:
32391 +       AuTraceErr(err);
32392 +       return err;
32393 +#else
32394 +       return 0;
32395 +#endif
32396 +}
32397 +
32398 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32399 +{
32400 +       int err;
32401 +       unsigned int rdhash;
32402 +       loff_t offset;
32403 +       aufs_bindex_t bbot, bindex, btop;
32404 +       unsigned char shwh;
32405 +       struct file *hf, *file;
32406 +       struct super_block *sb;
32407 +
32408 +       file = arg->file;
32409 +       sb = file->f_path.dentry->d_sb;
32410 +       SiMustAnyLock(sb);
32411 +
32412 +       rdhash = au_sbi(sb)->si_rdhash;
32413 +       if (!rdhash)
32414 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32415 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32416 +       if (unlikely(err))
32417 +               goto out;
32418 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32419 +       if (unlikely(err))
32420 +               goto out_delist;
32421 +
32422 +       err = 0;
32423 +       arg->flags = 0;
32424 +       shwh = 0;
32425 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32426 +               shwh = 1;
32427 +               au_fset_fillvdir(arg->flags, SHWH);
32428 +       }
32429 +       btop = au_fbtop(file);
32430 +       bbot = au_fbbot_dir(file);
32431 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32432 +               hf = au_hf_dir(file, bindex);
32433 +               if (!hf)
32434 +                       continue;
32435 +
32436 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32437 +               err = offset;
32438 +               if (unlikely(offset))
32439 +                       break;
32440 +
32441 +               arg->bindex = bindex;
32442 +               au_fclr_fillvdir(arg->flags, WHABLE);
32443 +               if (shwh
32444 +                   || (bindex != bbot
32445 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32446 +                       au_fset_fillvdir(arg->flags, WHABLE);
32447 +               do {
32448 +                       arg->err = 0;
32449 +                       au_fclr_fillvdir(arg->flags, CALLED);
32450 +                       /* smp_mb(); */
32451 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32452 +                       if (err >= 0)
32453 +                               err = arg->err;
32454 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32455 +
32456 +               /*
32457 +                * dir_relax() may be good for concurrency, but aufs should not
32458 +                * use it since it will cause a lockdep problem.
32459 +                */
32460 +       }
32461 +
32462 +       if (!err && shwh)
32463 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32464 +
32465 +       au_nhash_wh_free(&arg->whlist);
32466 +
32467 +out_delist:
32468 +       au_nhash_de_free(&arg->delist);
32469 +out:
32470 +       return err;
32471 +}
32472 +
32473 +static int read_vdir(struct file *file, int may_read)
32474 +{
32475 +       int err;
32476 +       unsigned long expire;
32477 +       unsigned char do_read;
32478 +       struct fillvdir_arg arg = {
32479 +               .ctx = {
32480 +                       .actor = fillvdir
32481 +               }
32482 +       };
32483 +       struct inode *inode;
32484 +       struct au_vdir *vdir, *allocated;
32485 +
32486 +       err = 0;
32487 +       inode = file_inode(file);
32488 +       IMustLock(inode);
32489 +       IiMustWriteLock(inode);
32490 +       SiMustAnyLock(inode->i_sb);
32491 +
32492 +       allocated = NULL;
32493 +       do_read = 0;
32494 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32495 +       vdir = au_ivdir(inode);
32496 +       if (!vdir) {
32497 +               do_read = 1;
32498 +               vdir = alloc_vdir(file);
32499 +               err = PTR_ERR(vdir);
32500 +               if (IS_ERR(vdir))
32501 +                       goto out;
32502 +               err = 0;
32503 +               allocated = vdir;
32504 +       } else if (may_read
32505 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32506 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32507 +               do_read = 1;
32508 +               err = reinit_vdir(vdir);
32509 +               if (unlikely(err))
32510 +                       goto out;
32511 +       }
32512 +
32513 +       if (!do_read)
32514 +               return 0; /* success */
32515 +
32516 +       arg.file = file;
32517 +       arg.vdir = vdir;
32518 +       err = au_do_read_vdir(&arg);
32519 +       if (!err) {
32520 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32521 +               vdir->vd_version = inode_query_iversion(inode);
32522 +               vdir->vd_last.ul = 0;
32523 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32524 +               if (allocated)
32525 +                       au_set_ivdir(inode, allocated);
32526 +       } else if (allocated)
32527 +               au_vdir_free(allocated);
32528 +
32529 +out:
32530 +       return err;
32531 +}
32532 +
32533 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32534 +{
32535 +       int err, rerr;
32536 +       unsigned long ul, n;
32537 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32538 +
32539 +       AuDebugOn(tgt->vd_nblk != 1);
32540 +
32541 +       err = -ENOMEM;
32542 +       if (tgt->vd_nblk < src->vd_nblk) {
32543 +               unsigned char **p;
32544 +
32545 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32546 +                               GFP_NOFS, /*may_shrink*/0);
32547 +               if (unlikely(!p))
32548 +                       goto out;
32549 +               tgt->vd_deblk = p;
32550 +       }
32551 +
32552 +       if (tgt->vd_deblk_sz != deblk_sz) {
32553 +               unsigned char *p;
32554 +
32555 +               tgt->vd_deblk_sz = deblk_sz;
32556 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32557 +                               /*may_shrink*/1);
32558 +               if (unlikely(!p))
32559 +                       goto out;
32560 +               tgt->vd_deblk[0] = p;
32561 +       }
32562 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32563 +       tgt->vd_version = src->vd_version;
32564 +       tgt->vd_jiffy = src->vd_jiffy;
32565 +
32566 +       n = src->vd_nblk;
32567 +       for (ul = 1; ul < n; ul++) {
32568 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32569 +                                           GFP_NOFS);
32570 +               if (unlikely(!tgt->vd_deblk[ul]))
32571 +                       goto out;
32572 +               tgt->vd_nblk++;
32573 +       }
32574 +       tgt->vd_nblk = n;
32575 +       tgt->vd_last.ul = tgt->vd_last.ul;
32576 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32577 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32578 +               - src->vd_deblk[src->vd_last.ul];
32579 +       /* smp_mb(); */
32580 +       return 0; /* success */
32581 +
32582 +out:
32583 +       rerr = reinit_vdir(tgt);
32584 +       BUG_ON(rerr);
32585 +       return err;
32586 +}
32587 +
32588 +int au_vdir_init(struct file *file)
32589 +{
32590 +       int err;
32591 +       struct inode *inode;
32592 +       struct au_vdir *vdir_cache, *allocated;
32593 +
32594 +       /* test file->f_pos here instead of ctx->pos */
32595 +       err = read_vdir(file, !file->f_pos);
32596 +       if (unlikely(err))
32597 +               goto out;
32598 +
32599 +       allocated = NULL;
32600 +       vdir_cache = au_fvdir_cache(file);
32601 +       if (!vdir_cache) {
32602 +               vdir_cache = alloc_vdir(file);
32603 +               err = PTR_ERR(vdir_cache);
32604 +               if (IS_ERR(vdir_cache))
32605 +                       goto out;
32606 +               allocated = vdir_cache;
32607 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32608 +               /* test file->f_pos here instead of ctx->pos */
32609 +               err = reinit_vdir(vdir_cache);
32610 +               if (unlikely(err))
32611 +                       goto out;
32612 +       } else
32613 +               return 0; /* success */
32614 +
32615 +       inode = file_inode(file);
32616 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32617 +       if (!err) {
32618 +               file->f_version = inode_query_iversion(inode);
32619 +               if (allocated)
32620 +                       au_set_fvdir_cache(file, allocated);
32621 +       } else if (allocated)
32622 +               au_vdir_free(allocated);
32623 +
32624 +out:
32625 +       return err;
32626 +}
32627 +
32628 +static loff_t calc_offset(struct au_vdir *vdir)
32629 +{
32630 +       loff_t offset;
32631 +       union au_vdir_deblk_p p;
32632 +
32633 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32634 +       offset = vdir->vd_last.p.deblk - p.deblk;
32635 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32636 +       return offset;
32637 +}
32638 +
32639 +/* returns true or false */
32640 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32641 +{
32642 +       int valid;
32643 +       unsigned int deblk_sz;
32644 +       unsigned long ul, n;
32645 +       loff_t offset;
32646 +       union au_vdir_deblk_p p, deblk_end;
32647 +       struct au_vdir *vdir_cache;
32648 +
32649 +       valid = 1;
32650 +       vdir_cache = au_fvdir_cache(file);
32651 +       offset = calc_offset(vdir_cache);
32652 +       AuDbg("offset %lld\n", offset);
32653 +       if (ctx->pos == offset)
32654 +               goto out;
32655 +
32656 +       vdir_cache->vd_last.ul = 0;
32657 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32658 +       if (!ctx->pos)
32659 +               goto out;
32660 +
32661 +       valid = 0;
32662 +       deblk_sz = vdir_cache->vd_deblk_sz;
32663 +       ul = div64_u64(ctx->pos, deblk_sz);
32664 +       AuDbg("ul %lu\n", ul);
32665 +       if (ul >= vdir_cache->vd_nblk)
32666 +               goto out;
32667 +
32668 +       n = vdir_cache->vd_nblk;
32669 +       for (; ul < n; ul++) {
32670 +               p.deblk = vdir_cache->vd_deblk[ul];
32671 +               deblk_end.deblk = p.deblk + deblk_sz;
32672 +               offset = ul;
32673 +               offset *= deblk_sz;
32674 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32675 +                       unsigned int l;
32676 +
32677 +                       l = calc_size(p.de->de_str.len);
32678 +                       offset += l;
32679 +                       p.deblk += l;
32680 +               }
32681 +               if (!is_deblk_end(&p, &deblk_end)) {
32682 +                       valid = 1;
32683 +                       vdir_cache->vd_last.ul = ul;
32684 +                       vdir_cache->vd_last.p = p;
32685 +                       break;
32686 +               }
32687 +       }
32688 +
32689 +out:
32690 +       /* smp_mb(); */
32691 +       if (!valid)
32692 +               AuDbg("valid %d\n", !valid);
32693 +       return valid;
32694 +}
32695 +
32696 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32697 +{
32698 +       unsigned int l, deblk_sz;
32699 +       union au_vdir_deblk_p deblk_end;
32700 +       struct au_vdir *vdir_cache;
32701 +       struct au_vdir_de *de;
32702 +
32703 +       if (!seek_vdir(file, ctx))
32704 +               return 0;
32705 +
32706 +       vdir_cache = au_fvdir_cache(file);
32707 +       deblk_sz = vdir_cache->vd_deblk_sz;
32708 +       while (1) {
32709 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32710 +               deblk_end.deblk += deblk_sz;
32711 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32712 +                       de = vdir_cache->vd_last.p.de;
32713 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32714 +                             de->de_str.len, de->de_str.name, ctx->pos,
32715 +                             (unsigned long)de->de_ino, de->de_type);
32716 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32717 +                                              de->de_str.len, de->de_ino,
32718 +                                              de->de_type))) {
32719 +                               /* todo: ignore the error caused by udba? */
32720 +                               /* return err; */
32721 +                               return 0;
32722 +                       }
32723 +
32724 +                       l = calc_size(de->de_str.len);
32725 +                       vdir_cache->vd_last.p.deblk += l;
32726 +                       ctx->pos += l;
32727 +               }
32728 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32729 +                       vdir_cache->vd_last.ul++;
32730 +                       vdir_cache->vd_last.p.deblk
32731 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32732 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32733 +                       continue;
32734 +               }
32735 +               break;
32736 +       }
32737 +
32738 +       /* smp_mb(); */
32739 +       return 0;
32740 +}
32741 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32742 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32743 +++ linux/fs/aufs/vfsub.c       2019-07-11 15:42:14.475571481 +0200
32744 @@ -0,0 +1,902 @@
32745 +// SPDX-License-Identifier: GPL-2.0
32746 +/*
32747 + * Copyright (C) 2005-2019 Junjiro R. Okajima
32748 + *
32749 + * This program, aufs is free software; you can redistribute it and/or modify
32750 + * it under the terms of the GNU General Public License as published by
32751 + * the Free Software Foundation; either version 2 of the License, or
32752 + * (at your option) any later version.
32753 + *
32754 + * This program is distributed in the hope that it will be useful,
32755 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32756 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32757 + * GNU General Public License for more details.
32758 + *
32759 + * You should have received a copy of the GNU General Public License
32760 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32761 + */
32762 +
32763 +/*
32764 + * sub-routines for VFS
32765 + */
32766 +
32767 +#include <linux/mnt_namespace.h>
32768 +#include <linux/namei.h>
32769 +#include <linux/nsproxy.h>
32770 +#include <linux/security.h>
32771 +#include <linux/splice.h>
32772 +#include "aufs.h"
32773 +
32774 +#ifdef CONFIG_AUFS_BR_FUSE
32775 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32776 +{
32777 +       if (!au_test_fuse(h_sb) || !au_userns)
32778 +               return 0;
32779 +
32780 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32781 +}
32782 +#endif
32783 +
32784 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32785 +{
32786 +       int err;
32787 +
32788 +       lockdep_off();
32789 +       down_read(&h_sb->s_umount);
32790 +       err = __sync_filesystem(h_sb, wait);
32791 +       up_read(&h_sb->s_umount);
32792 +       lockdep_on();
32793 +
32794 +       return err;
32795 +}
32796 +
32797 +/* ---------------------------------------------------------------------- */
32798 +
32799 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32800 +{
32801 +       int err;
32802 +       struct kstat st;
32803 +       struct super_block *h_sb;
32804 +
32805 +       /* for remote fs, leave work for its getattr or d_revalidate */
32806 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32807 +       /* still some fs may acquire i_mutex. we need to skip them */
32808 +       err = 0;
32809 +       if (!did)
32810 +               did = &err;
32811 +       h_sb = h_path->dentry->d_sb;
32812 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32813 +       if (*did)
32814 +               err = vfsub_getattr(h_path, &st);
32815 +
32816 +       return err;
32817 +}
32818 +
32819 +/* ---------------------------------------------------------------------- */
32820 +
32821 +struct file *vfsub_dentry_open(struct path *path, int flags)
32822 +{
32823 +       struct file *file;
32824 +
32825 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32826 +                          current_cred());
32827 +       if (!IS_ERR_OR_NULL(file)
32828 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
32829 +               i_readcount_inc(d_inode(path->dentry));
32830 +
32831 +       return file;
32832 +}
32833 +
32834 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32835 +{
32836 +       struct file *file;
32837 +
32838 +       lockdep_off();
32839 +       file = filp_open(path,
32840 +                        oflags /* | __FMODE_NONOTIFY */,
32841 +                        mode);
32842 +       lockdep_on();
32843 +       if (IS_ERR(file))
32844 +               goto out;
32845 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32846 +
32847 +out:
32848 +       return file;
32849 +}
32850 +
32851 +/*
32852 + * Ideally this function should call VFS:do_last() in order to keep all its
32853 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32854 + * structure such as nameidata. This is a second (or third) best approach.
32855 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32856 + */
32857 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32858 +                     struct vfsub_aopen_args *args)
32859 +{
32860 +       int err;
32861 +       struct au_branch *br = args->br;
32862 +       struct file *file = args->file;
32863 +       /* copied from linux/fs/namei.c:atomic_open() */
32864 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32865 +
32866 +       IMustLock(dir);
32867 +       AuDebugOn(!dir->i_op->atomic_open);
32868 +
32869 +       err = au_br_test_oflag(args->open_flag, br);
32870 +       if (unlikely(err))
32871 +               goto out;
32872 +
32873 +       au_lcnt_inc(&br->br_nfiles);
32874 +       file->f_path.dentry = DENTRY_NOT_SET;
32875 +       file->f_path.mnt = au_br_mnt(br);
32876 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32877 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32878 +                                    args->create_mode);
32879 +       if (unlikely(err < 0)) {
32880 +               au_lcnt_dec(&br->br_nfiles);
32881 +               goto out;
32882 +       }
32883 +
32884 +       /* temporary workaround for nfsv4 branch */
32885 +       if (au_test_nfs(dir->i_sb))
32886 +               nfs_mark_for_revalidate(dir);
32887 +
32888 +       if (file->f_mode & FMODE_CREATED)
32889 +               fsnotify_create(dir, dentry);
32890 +       if (!(file->f_mode & FMODE_OPENED)) {
32891 +               au_lcnt_dec(&br->br_nfiles);
32892 +               goto out;
32893 +       }
32894 +
32895 +       /* todo: call VFS:may_open() here */
32896 +       /* todo: ima_file_check() too? */
32897 +       if (!err && (args->open_flag & __FMODE_EXEC))
32898 +               err = deny_write_access(file);
32899 +       if (!err)
32900 +               fsnotify_open(file);
32901 +       else
32902 +               au_lcnt_dec(&br->br_nfiles);
32903 +       /* note that the file is created and still opened */
32904 +
32905 +out:
32906 +       return err;
32907 +}
32908 +
32909 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32910 +{
32911 +       int err;
32912 +
32913 +       err = kern_path(name, flags, path);
32914 +       if (!err && d_is_positive(path->dentry))
32915 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32916 +       return err;
32917 +}
32918 +
32919 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32920 +                                            struct dentry *parent, int len)
32921 +{
32922 +       struct path path = {
32923 +               .mnt = NULL
32924 +       };
32925 +
32926 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32927 +       if (IS_ERR(path.dentry))
32928 +               goto out;
32929 +       if (d_is_positive(path.dentry))
32930 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32931 +
32932 +out:
32933 +       AuTraceErrPtr(path.dentry);
32934 +       return path.dentry;
32935 +}
32936 +
32937 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32938 +                                   int len)
32939 +{
32940 +       struct path path = {
32941 +               .mnt = NULL
32942 +       };
32943 +
32944 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32945 +       IMustLock(d_inode(parent));
32946 +
32947 +       path.dentry = lookup_one_len(name, parent, len);
32948 +       if (IS_ERR(path.dentry))
32949 +               goto out;
32950 +       if (d_is_positive(path.dentry))
32951 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32952 +
32953 +out:
32954 +       AuTraceErrPtr(path.dentry);
32955 +       return path.dentry;
32956 +}
32957 +
32958 +void vfsub_call_lkup_one(void *args)
32959 +{
32960 +       struct vfsub_lkup_one_args *a = args;
32961 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32962 +}
32963 +
32964 +/* ---------------------------------------------------------------------- */
32965 +
32966 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32967 +                                struct dentry *d2, struct au_hinode *hdir2)
32968 +{
32969 +       struct dentry *d;
32970 +
32971 +       lockdep_off();
32972 +       d = lock_rename(d1, d2);
32973 +       lockdep_on();
32974 +       au_hn_suspend(hdir1);
32975 +       if (hdir1 != hdir2)
32976 +               au_hn_suspend(hdir2);
32977 +
32978 +       return d;
32979 +}
32980 +
32981 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32982 +                        struct dentry *d2, struct au_hinode *hdir2)
32983 +{
32984 +       au_hn_resume(hdir1);
32985 +       if (hdir1 != hdir2)
32986 +               au_hn_resume(hdir2);
32987 +       lockdep_off();
32988 +       unlock_rename(d1, d2);
32989 +       lockdep_on();
32990 +}
32991 +
32992 +/* ---------------------------------------------------------------------- */
32993 +
32994 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32995 +{
32996 +       int err;
32997 +       struct dentry *d;
32998 +
32999 +       IMustLock(dir);
33000 +
33001 +       d = path->dentry;
33002 +       path->dentry = d->d_parent;
33003 +       err = security_path_mknod(path, d, mode, 0);
33004 +       path->dentry = d;
33005 +       if (unlikely(err))
33006 +               goto out;
33007 +
33008 +       lockdep_off();
33009 +       err = vfs_create(dir, path->dentry, mode, want_excl);
33010 +       lockdep_on();
33011 +       if (!err) {
33012 +               struct path tmp = *path;
33013 +               int did;
33014 +
33015 +               vfsub_update_h_iattr(&tmp, &did);
33016 +               if (did) {
33017 +                       tmp.dentry = path->dentry->d_parent;
33018 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33019 +               }
33020 +               /*ignore*/
33021 +       }
33022 +
33023 +out:
33024 +       return err;
33025 +}
33026 +
33027 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33028 +{
33029 +       int err;
33030 +       struct dentry *d;
33031 +
33032 +       IMustLock(dir);
33033 +
33034 +       d = path->dentry;
33035 +       path->dentry = d->d_parent;
33036 +       err = security_path_symlink(path, d, symname);
33037 +       path->dentry = d;
33038 +       if (unlikely(err))
33039 +               goto out;
33040 +
33041 +       lockdep_off();
33042 +       err = vfs_symlink(dir, path->dentry, symname);
33043 +       lockdep_on();
33044 +       if (!err) {
33045 +               struct path tmp = *path;
33046 +               int did;
33047 +
33048 +               vfsub_update_h_iattr(&tmp, &did);
33049 +               if (did) {
33050 +                       tmp.dentry = path->dentry->d_parent;
33051 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33052 +               }
33053 +               /*ignore*/
33054 +       }
33055 +
33056 +out:
33057 +       return err;
33058 +}
33059 +
33060 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33061 +{
33062 +       int err;
33063 +       struct dentry *d;
33064 +
33065 +       IMustLock(dir);
33066 +
33067 +       d = path->dentry;
33068 +       path->dentry = d->d_parent;
33069 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33070 +       path->dentry = d;
33071 +       if (unlikely(err))
33072 +               goto out;
33073 +
33074 +       lockdep_off();
33075 +       err = vfs_mknod(dir, path->dentry, mode, dev);
33076 +       lockdep_on();
33077 +       if (!err) {
33078 +               struct path tmp = *path;
33079 +               int did;
33080 +
33081 +               vfsub_update_h_iattr(&tmp, &did);
33082 +               if (did) {
33083 +                       tmp.dentry = path->dentry->d_parent;
33084 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33085 +               }
33086 +               /*ignore*/
33087 +       }
33088 +
33089 +out:
33090 +       return err;
33091 +}
33092 +
33093 +static int au_test_nlink(struct inode *inode)
33094 +{
33095 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33096 +
33097 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33098 +           || inode->i_nlink < link_max)
33099 +               return 0;
33100 +       return -EMLINK;
33101 +}
33102 +
33103 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33104 +              struct inode **delegated_inode)
33105 +{
33106 +       int err;
33107 +       struct dentry *d;
33108 +
33109 +       IMustLock(dir);
33110 +
33111 +       err = au_test_nlink(d_inode(src_dentry));
33112 +       if (unlikely(err))
33113 +               return err;
33114 +
33115 +       /* we don't call may_linkat() */
33116 +       d = path->dentry;
33117 +       path->dentry = d->d_parent;
33118 +       err = security_path_link(src_dentry, path, d);
33119 +       path->dentry = d;
33120 +       if (unlikely(err))
33121 +               goto out;
33122 +
33123 +       lockdep_off();
33124 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
33125 +       lockdep_on();
33126 +       if (!err) {
33127 +               struct path tmp = *path;
33128 +               int did;
33129 +
33130 +               /* fuse has different memory inode for the same inumber */
33131 +               vfsub_update_h_iattr(&tmp, &did);
33132 +               if (did) {
33133 +                       tmp.dentry = path->dentry->d_parent;
33134 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33135 +                       tmp.dentry = src_dentry;
33136 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33137 +               }
33138 +               /*ignore*/
33139 +       }
33140 +
33141 +out:
33142 +       return err;
33143 +}
33144 +
33145 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33146 +                struct inode *dir, struct path *path,
33147 +                struct inode **delegated_inode, unsigned int flags)
33148 +{
33149 +       int err;
33150 +       struct path tmp = {
33151 +               .mnt    = path->mnt
33152 +       };
33153 +       struct dentry *d;
33154 +
33155 +       IMustLock(dir);
33156 +       IMustLock(src_dir);
33157 +
33158 +       d = path->dentry;
33159 +       path->dentry = d->d_parent;
33160 +       tmp.dentry = src_dentry->d_parent;
33161 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33162 +       path->dentry = d;
33163 +       if (unlikely(err))
33164 +               goto out;
33165 +
33166 +       lockdep_off();
33167 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33168 +                        delegated_inode, flags);
33169 +       lockdep_on();
33170 +       if (!err) {
33171 +               int did;
33172 +
33173 +               tmp.dentry = d->d_parent;
33174 +               vfsub_update_h_iattr(&tmp, &did);
33175 +               if (did) {
33176 +                       tmp.dentry = src_dentry;
33177 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33178 +                       tmp.dentry = src_dentry->d_parent;
33179 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33180 +               }
33181 +               /*ignore*/
33182 +       }
33183 +
33184 +out:
33185 +       return err;
33186 +}
33187 +
33188 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33189 +{
33190 +       int err;
33191 +       struct dentry *d;
33192 +
33193 +       IMustLock(dir);
33194 +
33195 +       d = path->dentry;
33196 +       path->dentry = d->d_parent;
33197 +       err = security_path_mkdir(path, d, mode);
33198 +       path->dentry = d;
33199 +       if (unlikely(err))
33200 +               goto out;
33201 +
33202 +       lockdep_off();
33203 +       err = vfs_mkdir(dir, path->dentry, mode);
33204 +       lockdep_on();
33205 +       if (!err) {
33206 +               struct path tmp = *path;
33207 +               int did;
33208 +
33209 +               vfsub_update_h_iattr(&tmp, &did);
33210 +               if (did) {
33211 +                       tmp.dentry = path->dentry->d_parent;
33212 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33213 +               }
33214 +               /*ignore*/
33215 +       }
33216 +
33217 +out:
33218 +       return err;
33219 +}
33220 +
33221 +int vfsub_rmdir(struct inode *dir, struct path *path)
33222 +{
33223 +       int err;
33224 +       struct dentry *d;
33225 +
33226 +       IMustLock(dir);
33227 +
33228 +       d = path->dentry;
33229 +       path->dentry = d->d_parent;
33230 +       err = security_path_rmdir(path, d);
33231 +       path->dentry = d;
33232 +       if (unlikely(err))
33233 +               goto out;
33234 +
33235 +       lockdep_off();
33236 +       err = vfs_rmdir(dir, path->dentry);
33237 +       lockdep_on();
33238 +       if (!err) {
33239 +               struct path tmp = {
33240 +                       .dentry = path->dentry->d_parent,
33241 +                       .mnt    = path->mnt
33242 +               };
33243 +
33244 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33245 +       }
33246 +
33247 +out:
33248 +       return err;
33249 +}
33250 +
33251 +/* ---------------------------------------------------------------------- */
33252 +
33253 +/* todo: support mmap_sem? */
33254 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33255 +                    loff_t *ppos)
33256 +{
33257 +       ssize_t err;
33258 +
33259 +       lockdep_off();
33260 +       err = vfs_read(file, ubuf, count, ppos);
33261 +       lockdep_on();
33262 +       if (err >= 0)
33263 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33264 +       return err;
33265 +}
33266 +
33267 +/* todo: kernel_read()? */
33268 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33269 +                    loff_t *ppos)
33270 +{
33271 +       ssize_t err;
33272 +       mm_segment_t oldfs;
33273 +       union {
33274 +               void *k;
33275 +               char __user *u;
33276 +       } buf;
33277 +
33278 +       buf.k = kbuf;
33279 +       oldfs = get_fs();
33280 +       set_fs(KERNEL_DS);
33281 +       err = vfsub_read_u(file, buf.u, count, ppos);
33282 +       set_fs(oldfs);
33283 +       return err;
33284 +}
33285 +
33286 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33287 +                     loff_t *ppos)
33288 +{
33289 +       ssize_t err;
33290 +
33291 +       lockdep_off();
33292 +       err = vfs_write(file, ubuf, count, ppos);
33293 +       lockdep_on();
33294 +       if (err >= 0)
33295 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33296 +       return err;
33297 +}
33298 +
33299 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33300 +{
33301 +       ssize_t err;
33302 +       mm_segment_t oldfs;
33303 +       union {
33304 +               void *k;
33305 +               const char __user *u;
33306 +       } buf;
33307 +
33308 +       buf.k = kbuf;
33309 +       oldfs = get_fs();
33310 +       set_fs(KERNEL_DS);
33311 +       err = vfsub_write_u(file, buf.u, count, ppos);
33312 +       set_fs(oldfs);
33313 +       return err;
33314 +}
33315 +
33316 +int vfsub_flush(struct file *file, fl_owner_t id)
33317 +{
33318 +       int err;
33319 +
33320 +       err = 0;
33321 +       if (file->f_op->flush) {
33322 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33323 +                       err = file->f_op->flush(file, id);
33324 +               else {
33325 +                       lockdep_off();
33326 +                       err = file->f_op->flush(file, id);
33327 +                       lockdep_on();
33328 +               }
33329 +               if (!err)
33330 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33331 +               /*ignore*/
33332 +       }
33333 +       return err;
33334 +}
33335 +
33336 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33337 +{
33338 +       int err;
33339 +
33340 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33341 +
33342 +       lockdep_off();
33343 +       err = iterate_dir(file, ctx);
33344 +       lockdep_on();
33345 +       if (err >= 0)
33346 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33347 +
33348 +       return err;
33349 +}
33350 +
33351 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33352 +                    struct pipe_inode_info *pipe, size_t len,
33353 +                    unsigned int flags)
33354 +{
33355 +       long err;
33356 +
33357 +       lockdep_off();
33358 +       err = do_splice_to(in, ppos, pipe, len, flags);
33359 +       lockdep_on();
33360 +       file_accessed(in);
33361 +       if (err >= 0)
33362 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33363 +       return err;
33364 +}
33365 +
33366 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33367 +                      loff_t *ppos, size_t len, unsigned int flags)
33368 +{
33369 +       long err;
33370 +
33371 +       lockdep_off();
33372 +       err = do_splice_from(pipe, out, ppos, len, flags);
33373 +       lockdep_on();
33374 +       if (err >= 0)
33375 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33376 +       return err;
33377 +}
33378 +
33379 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33380 +{
33381 +       int err;
33382 +
33383 +       /* file can be NULL */
33384 +       lockdep_off();
33385 +       err = vfs_fsync(file, datasync);
33386 +       lockdep_on();
33387 +       if (!err) {
33388 +               if (!path) {
33389 +                       AuDebugOn(!file);
33390 +                       path = &file->f_path;
33391 +               }
33392 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33393 +       }
33394 +       return err;
33395 +}
33396 +
33397 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33398 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33399 +               struct file *h_file)
33400 +{
33401 +       int err;
33402 +       struct inode *h_inode;
33403 +       struct super_block *h_sb;
33404 +
33405 +       if (!h_file) {
33406 +               err = vfsub_truncate(h_path, length);
33407 +               goto out;
33408 +       }
33409 +
33410 +       h_inode = d_inode(h_path->dentry);
33411 +       h_sb = h_inode->i_sb;
33412 +       lockdep_off();
33413 +       sb_start_write(h_sb);
33414 +       lockdep_on();
33415 +       err = locks_verify_truncate(h_inode, h_file, length);
33416 +       if (!err)
33417 +               err = security_path_truncate(h_path);
33418 +       if (!err) {
33419 +               lockdep_off();
33420 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33421 +               lockdep_on();
33422 +       }
33423 +       lockdep_off();
33424 +       sb_end_write(h_sb);
33425 +       lockdep_on();
33426 +
33427 +out:
33428 +       return err;
33429 +}
33430 +
33431 +/* ---------------------------------------------------------------------- */
33432 +
33433 +struct au_vfsub_mkdir_args {
33434 +       int *errp;
33435 +       struct inode *dir;
33436 +       struct path *path;
33437 +       int mode;
33438 +};
33439 +
33440 +static void au_call_vfsub_mkdir(void *args)
33441 +{
33442 +       struct au_vfsub_mkdir_args *a = args;
33443 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33444 +}
33445 +
33446 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33447 +{
33448 +       int err, do_sio, wkq_err;
33449 +
33450 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33451 +       if (!do_sio) {
33452 +               lockdep_off();
33453 +               err = vfsub_mkdir(dir, path, mode);
33454 +               lockdep_on();
33455 +       } else {
33456 +               struct au_vfsub_mkdir_args args = {
33457 +                       .errp   = &err,
33458 +                       .dir    = dir,
33459 +                       .path   = path,
33460 +                       .mode   = mode
33461 +               };
33462 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33463 +               if (unlikely(wkq_err))
33464 +                       err = wkq_err;
33465 +       }
33466 +
33467 +       return err;
33468 +}
33469 +
33470 +struct au_vfsub_rmdir_args {
33471 +       int *errp;
33472 +       struct inode *dir;
33473 +       struct path *path;
33474 +};
33475 +
33476 +static void au_call_vfsub_rmdir(void *args)
33477 +{
33478 +       struct au_vfsub_rmdir_args *a = args;
33479 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33480 +}
33481 +
33482 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33483 +{
33484 +       int err, do_sio, wkq_err;
33485 +
33486 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33487 +       if (!do_sio) {
33488 +               lockdep_off();
33489 +               err = vfsub_rmdir(dir, path);
33490 +               lockdep_on();
33491 +       } else {
33492 +               struct au_vfsub_rmdir_args args = {
33493 +                       .errp   = &err,
33494 +                       .dir    = dir,
33495 +                       .path   = path
33496 +               };
33497 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33498 +               if (unlikely(wkq_err))
33499 +                       err = wkq_err;
33500 +       }
33501 +
33502 +       return err;
33503 +}
33504 +
33505 +/* ---------------------------------------------------------------------- */
33506 +
33507 +struct notify_change_args {
33508 +       int *errp;
33509 +       struct path *path;
33510 +       struct iattr *ia;
33511 +       struct inode **delegated_inode;
33512 +};
33513 +
33514 +static void call_notify_change(void *args)
33515 +{
33516 +       struct notify_change_args *a = args;
33517 +       struct inode *h_inode;
33518 +
33519 +       h_inode = d_inode(a->path->dentry);
33520 +       IMustLock(h_inode);
33521 +
33522 +       *a->errp = -EPERM;
33523 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33524 +               lockdep_off();
33525 +               *a->errp = notify_change(a->path->dentry, a->ia,
33526 +                                        a->delegated_inode);
33527 +               lockdep_on();
33528 +               if (!*a->errp)
33529 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33530 +       }
33531 +       AuTraceErr(*a->errp);
33532 +}
33533 +
33534 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33535 +                       struct inode **delegated_inode)
33536 +{
33537 +       int err;
33538 +       struct notify_change_args args = {
33539 +               .errp                   = &err,
33540 +               .path                   = path,
33541 +               .ia                     = ia,
33542 +               .delegated_inode        = delegated_inode
33543 +       };
33544 +
33545 +       call_notify_change(&args);
33546 +
33547 +       return err;
33548 +}
33549 +
33550 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33551 +                           struct inode **delegated_inode)
33552 +{
33553 +       int err, wkq_err;
33554 +       struct notify_change_args args = {
33555 +               .errp                   = &err,
33556 +               .path                   = path,
33557 +               .ia                     = ia,
33558 +               .delegated_inode        = delegated_inode
33559 +       };
33560 +
33561 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33562 +       if (unlikely(wkq_err))
33563 +               err = wkq_err;
33564 +
33565 +       return err;
33566 +}
33567 +
33568 +/* ---------------------------------------------------------------------- */
33569 +
33570 +struct unlink_args {
33571 +       int *errp;
33572 +       struct inode *dir;
33573 +       struct path *path;
33574 +       struct inode **delegated_inode;
33575 +};
33576 +
33577 +static void call_unlink(void *args)
33578 +{
33579 +       struct unlink_args *a = args;
33580 +       struct dentry *d = a->path->dentry;
33581 +       struct inode *h_inode;
33582 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33583 +                                     && au_dcount(d) == 1);
33584 +
33585 +       IMustLock(a->dir);
33586 +
33587 +       a->path->dentry = d->d_parent;
33588 +       *a->errp = security_path_unlink(a->path, d);
33589 +       a->path->dentry = d;
33590 +       if (unlikely(*a->errp))
33591 +               return;
33592 +
33593 +       if (!stop_sillyrename)
33594 +               dget(d);
33595 +       h_inode = NULL;
33596 +       if (d_is_positive(d)) {
33597 +               h_inode = d_inode(d);
33598 +               ihold(h_inode);
33599 +       }
33600 +
33601 +       lockdep_off();
33602 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33603 +       lockdep_on();
33604 +       if (!*a->errp) {
33605 +               struct path tmp = {
33606 +                       .dentry = d->d_parent,
33607 +                       .mnt    = a->path->mnt
33608 +               };
33609 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33610 +       }
33611 +
33612 +       if (!stop_sillyrename)
33613 +               dput(d);
33614 +       if (h_inode)
33615 +               iput(h_inode);
33616 +
33617 +       AuTraceErr(*a->errp);
33618 +}
33619 +
33620 +/*
33621 + * @dir: must be locked.
33622 + * @dentry: target dentry.
33623 + */
33624 +int vfsub_unlink(struct inode *dir, struct path *path,
33625 +                struct inode **delegated_inode, int force)
33626 +{
33627 +       int err;
33628 +       struct unlink_args args = {
33629 +               .errp                   = &err,
33630 +               .dir                    = dir,
33631 +               .path                   = path,
33632 +               .delegated_inode        = delegated_inode
33633 +       };
33634 +
33635 +       if (!force)
33636 +               call_unlink(&args);
33637 +       else {
33638 +               int wkq_err;
33639 +
33640 +               wkq_err = au_wkq_wait(call_unlink, &args);
33641 +               if (unlikely(wkq_err))
33642 +                       err = wkq_err;
33643 +       }
33644 +
33645 +       return err;
33646 +}
33647 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33648 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33649 +++ linux/fs/aufs/vfsub.h       2019-07-11 15:42:14.475571481 +0200
33650 @@ -0,0 +1,354 @@
33651 +/* SPDX-License-Identifier: GPL-2.0 */
33652 +/*
33653 + * Copyright (C) 2005-2019 Junjiro R. Okajima
33654 + *
33655 + * This program, aufs is free software; you can redistribute it and/or modify
33656 + * it under the terms of the GNU General Public License as published by
33657 + * the Free Software Foundation; either version 2 of the License, or
33658 + * (at your option) any later version.
33659 + *
33660 + * This program is distributed in the hope that it will be useful,
33661 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33662 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33663 + * GNU General Public License for more details.
33664 + *
33665 + * You should have received a copy of the GNU General Public License
33666 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33667 + */
33668 +
33669 +/*
33670 + * sub-routines for VFS
33671 + */
33672 +
33673 +#ifndef __AUFS_VFSUB_H__
33674 +#define __AUFS_VFSUB_H__
33675 +
33676 +#ifdef __KERNEL__
33677 +
33678 +#include <linux/fs.h>
33679 +#include <linux/mount.h>
33680 +#include <linux/posix_acl.h>
33681 +#include <linux/xattr.h>
33682 +#include "debug.h"
33683 +
33684 +/* copied from linux/fs/internal.h */
33685 +/* todo: BAD approach!! */
33686 +extern void __mnt_drop_write(struct vfsmount *);
33687 +extern struct file *alloc_empty_file(int, const struct cred *);
33688 +
33689 +/* ---------------------------------------------------------------------- */
33690 +
33691 +/* lock subclass for lower inode */
33692 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33693 +/* reduce? gave up. */
33694 +enum {
33695 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33696 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33697 +       AuLsc_I_PARENT2,        /* copyup dirs */
33698 +       AuLsc_I_PARENT3,        /* copyup wh */
33699 +       AuLsc_I_CHILD,
33700 +       AuLsc_I_CHILD2,
33701 +       AuLsc_I_End
33702 +};
33703 +
33704 +/* to debug easier, do not make them inlined functions */
33705 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33706 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33707 +
33708 +/* ---------------------------------------------------------------------- */
33709 +
33710 +static inline void vfsub_drop_nlink(struct inode *inode)
33711 +{
33712 +       AuDebugOn(!inode->i_nlink);
33713 +       drop_nlink(inode);
33714 +}
33715 +
33716 +static inline void vfsub_dead_dir(struct inode *inode)
33717 +{
33718 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33719 +       inode->i_flags |= S_DEAD;
33720 +       clear_nlink(inode);
33721 +}
33722 +
33723 +static inline int vfsub_native_ro(struct inode *inode)
33724 +{
33725 +       return sb_rdonly(inode->i_sb)
33726 +               || IS_RDONLY(inode)
33727 +               /* || IS_APPEND(inode) */
33728 +               || IS_IMMUTABLE(inode);
33729 +}
33730 +
33731 +#ifdef CONFIG_AUFS_BR_FUSE
33732 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33733 +#else
33734 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33735 +#endif
33736 +
33737 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33738 +
33739 +/* ---------------------------------------------------------------------- */
33740 +
33741 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33742 +struct file *vfsub_dentry_open(struct path *path, int flags);
33743 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33744 +struct au_branch;
33745 +struct vfsub_aopen_args {
33746 +       struct file             *file;
33747 +       unsigned int            open_flag;
33748 +       umode_t                 create_mode;
33749 +       struct au_branch        *br;
33750 +};
33751 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33752 +                     struct vfsub_aopen_args *args);
33753 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33754 +
33755 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33756 +                                            struct dentry *parent, int len);
33757 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33758 +                                   int len);
33759 +
33760 +struct vfsub_lkup_one_args {
33761 +       struct dentry **errp;
33762 +       struct qstr *name;
33763 +       struct dentry *parent;
33764 +};
33765 +
33766 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33767 +                                           struct dentry *parent)
33768 +{
33769 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33770 +}
33771 +
33772 +void vfsub_call_lkup_one(void *args);
33773 +
33774 +/* ---------------------------------------------------------------------- */
33775 +
33776 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33777 +{
33778 +       int err;
33779 +
33780 +       lockdep_off();
33781 +       err = mnt_want_write(mnt);
33782 +       lockdep_on();
33783 +       return err;
33784 +}
33785 +
33786 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33787 +{
33788 +       lockdep_off();
33789 +       mnt_drop_write(mnt);
33790 +       lockdep_on();
33791 +}
33792 +
33793 +#if 0 /* reserved */
33794 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33795 +{
33796 +       lockdep_off();
33797 +       mnt_drop_write_file(file);
33798 +       lockdep_on();
33799 +}
33800 +#endif
33801 +
33802 +/* ---------------------------------------------------------------------- */
33803 +
33804 +struct au_hinode;
33805 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33806 +                                struct dentry *d2, struct au_hinode *hdir2);
33807 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33808 +                        struct dentry *d2, struct au_hinode *hdir2);
33809 +
33810 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33811 +                bool want_excl);
33812 +int vfsub_symlink(struct inode *dir, struct path *path,
33813 +                 const char *symname);
33814 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33815 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33816 +              struct path *path, struct inode **delegated_inode);
33817 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33818 +                struct inode *hdir, struct path *path,
33819 +                struct inode **delegated_inode, unsigned int flags);
33820 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33821 +int vfsub_rmdir(struct inode *dir, struct path *path);
33822 +
33823 +/* ---------------------------------------------------------------------- */
33824 +
33825 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33826 +                    loff_t *ppos);
33827 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33828 +                       loff_t *ppos);
33829 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33830 +                     loff_t *ppos);
33831 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33832 +                     loff_t *ppos);
33833 +int vfsub_flush(struct file *file, fl_owner_t id);
33834 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33835 +
33836 +static inline loff_t vfsub_f_size_read(struct file *file)
33837 +{
33838 +       return i_size_read(file_inode(file));
33839 +}
33840 +
33841 +static inline unsigned int vfsub_file_flags(struct file *file)
33842 +{
33843 +       unsigned int flags;
33844 +
33845 +       spin_lock(&file->f_lock);
33846 +       flags = file->f_flags;
33847 +       spin_unlock(&file->f_lock);
33848 +
33849 +       return flags;
33850 +}
33851 +
33852 +static inline int vfsub_file_execed(struct file *file)
33853 +{
33854 +       /* todo: direct access f_flags */
33855 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33856 +}
33857 +
33858 +#if 0 /* reserved */
33859 +static inline void vfsub_file_accessed(struct file *h_file)
33860 +{
33861 +       file_accessed(h_file);
33862 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33863 +}
33864 +#endif
33865 +
33866 +#if 0 /* reserved */
33867 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33868 +                                    struct dentry *h_dentry)
33869 +{
33870 +       struct path h_path = {
33871 +               .dentry = h_dentry,
33872 +               .mnt    = h_mnt
33873 +       };
33874 +       touch_atime(&h_path);
33875 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33876 +}
33877 +#endif
33878 +
33879 +static inline int vfsub_update_time(struct inode *h_inode,
33880 +                                   struct timespec64 *ts, int flags)
33881 +{
33882 +       return update_time(h_inode, ts, flags);
33883 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33884 +}
33885 +
33886 +#ifdef CONFIG_FS_POSIX_ACL
33887 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33888 +{
33889 +       int err;
33890 +
33891 +       err = posix_acl_chmod(h_inode, h_mode);
33892 +       if (err == -EOPNOTSUPP)
33893 +               err = 0;
33894 +       return err;
33895 +}
33896 +#else
33897 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33898 +#endif
33899 +
33900 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33901 +                    struct pipe_inode_info *pipe, size_t len,
33902 +                    unsigned int flags);
33903 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33904 +                      loff_t *ppos, size_t len, unsigned int flags);
33905 +
33906 +static inline long vfsub_truncate(struct path *path, loff_t length)
33907 +{
33908 +       long err;
33909 +
33910 +       lockdep_off();
33911 +       err = vfs_truncate(path, length);
33912 +       lockdep_on();
33913 +       return err;
33914 +}
33915 +
33916 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33917 +               struct file *h_file);
33918 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33919 +
33920 +/*
33921 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33922 + * ioctl.
33923 + */
33924 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33925 +                                           loff_t len)
33926 +{
33927 +       loff_t err;
33928 +
33929 +       lockdep_off();
33930 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33931 +       lockdep_on();
33932 +
33933 +       return err;
33934 +}
33935 +
33936 +/* copy_file_range(2) is a systemcall */
33937 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33938 +                                           struct file *dst, loff_t dst_pos,
33939 +                                           size_t len, unsigned int flags)
33940 +{
33941 +       ssize_t ssz;
33942 +
33943 +       lockdep_off();
33944 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33945 +       lockdep_on();
33946 +
33947 +       return ssz;
33948 +}
33949 +
33950 +/* ---------------------------------------------------------------------- */
33951 +
33952 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33953 +{
33954 +       loff_t err;
33955 +
33956 +       lockdep_off();
33957 +       err = vfs_llseek(file, offset, origin);
33958 +       lockdep_on();
33959 +       return err;
33960 +}
33961 +
33962 +/* ---------------------------------------------------------------------- */
33963 +
33964 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33965 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33966 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33967 +                           struct inode **delegated_inode);
33968 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33969 +                       struct inode **delegated_inode);
33970 +int vfsub_unlink(struct inode *dir, struct path *path,
33971 +                struct inode **delegated_inode, int force);
33972 +
33973 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33974 +{
33975 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33976 +}
33977 +
33978 +/* ---------------------------------------------------------------------- */
33979 +
33980 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33981 +                                const void *value, size_t size, int flags)
33982 +{
33983 +       int err;
33984 +
33985 +       lockdep_off();
33986 +       err = vfs_setxattr(dentry, name, value, size, flags);
33987 +       lockdep_on();
33988 +
33989 +       return err;
33990 +}
33991 +
33992 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33993 +{
33994 +       int err;
33995 +
33996 +       lockdep_off();
33997 +       err = vfs_removexattr(dentry, name);
33998 +       lockdep_on();
33999 +
34000 +       return err;
34001 +}
34002 +
34003 +#endif /* __KERNEL__ */
34004 +#endif /* __AUFS_VFSUB_H__ */
34005 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34006 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34007 +++ linux/fs/aufs/wbr_policy.c  2019-07-11 15:42:14.475571481 +0200
34008 @@ -0,0 +1,830 @@
34009 +// SPDX-License-Identifier: GPL-2.0
34010 +/*
34011 + * Copyright (C) 2005-2019 Junjiro R. Okajima
34012 + *
34013 + * This program, aufs is free software; you can redistribute it and/or modify
34014 + * it under the terms of the GNU General Public License as published by
34015 + * the Free Software Foundation; either version 2 of the License, or
34016 + * (at your option) any later version.
34017 + *
34018 + * This program is distributed in the hope that it will be useful,
34019 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34020 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34021 + * GNU General Public License for more details.
34022 + *
34023 + * You should have received a copy of the GNU General Public License
34024 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34025 + */
34026 +
34027 +/*
34028 + * policies for selecting one among multiple writable branches
34029 + */
34030 +
34031 +#include <linux/statfs.h>
34032 +#include "aufs.h"
34033 +
34034 +/* subset of cpup_attr() */
34035 +static noinline_for_stack
34036 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34037 +{
34038 +       int err, sbits;
34039 +       struct iattr ia;
34040 +       struct inode *h_isrc;
34041 +
34042 +       h_isrc = d_inode(h_src);
34043 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34044 +       ia.ia_mode = h_isrc->i_mode;
34045 +       ia.ia_uid = h_isrc->i_uid;
34046 +       ia.ia_gid = h_isrc->i_gid;
34047 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34048 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34049 +       /* no delegation since it is just created */
34050 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34051 +
34052 +       /* is this nfs only? */
34053 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34054 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34055 +               ia.ia_mode = h_isrc->i_mode;
34056 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34057 +       }
34058 +
34059 +       return err;
34060 +}
34061 +
34062 +#define AuCpdown_PARENT_OPQ    1
34063 +#define AuCpdown_WHED          (1 << 1)
34064 +#define AuCpdown_MADE_DIR      (1 << 2)
34065 +#define AuCpdown_DIROPQ                (1 << 3)
34066 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34067 +#define au_fset_cpdown(flags, name) \
34068 +       do { (flags) |= AuCpdown_##name; } while (0)
34069 +#define au_fclr_cpdown(flags, name) \
34070 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34071 +
34072 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34073 +                            unsigned int *flags)
34074 +{
34075 +       int err;
34076 +       struct dentry *opq_dentry;
34077 +
34078 +       opq_dentry = au_diropq_create(dentry, bdst);
34079 +       err = PTR_ERR(opq_dentry);
34080 +       if (IS_ERR(opq_dentry))
34081 +               goto out;
34082 +       dput(opq_dentry);
34083 +       au_fset_cpdown(*flags, DIROPQ);
34084 +
34085 +out:
34086 +       return err;
34087 +}
34088 +
34089 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34090 +                           struct inode *dir, aufs_bindex_t bdst)
34091 +{
34092 +       int err;
34093 +       struct path h_path;
34094 +       struct au_branch *br;
34095 +
34096 +       br = au_sbr(dentry->d_sb, bdst);
34097 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34098 +       err = PTR_ERR(h_path.dentry);
34099 +       if (IS_ERR(h_path.dentry))
34100 +               goto out;
34101 +
34102 +       err = 0;
34103 +       if (d_is_positive(h_path.dentry)) {
34104 +               h_path.mnt = au_br_mnt(br);
34105 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34106 +                                         dentry);
34107 +       }
34108 +       dput(h_path.dentry);
34109 +
34110 +out:
34111 +       return err;
34112 +}
34113 +
34114 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34115 +                        struct au_pin *pin,
34116 +                        struct dentry *h_parent, void *arg)
34117 +{
34118 +       int err, rerr;
34119 +       aufs_bindex_t bopq, btop;
34120 +       struct path h_path;
34121 +       struct dentry *parent;
34122 +       struct inode *h_dir, *h_inode, *inode, *dir;
34123 +       unsigned int *flags = arg;
34124 +
34125 +       btop = au_dbtop(dentry);
34126 +       /* dentry is di-locked */
34127 +       parent = dget_parent(dentry);
34128 +       dir = d_inode(parent);
34129 +       h_dir = d_inode(h_parent);
34130 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34131 +       IMustLock(h_dir);
34132 +
34133 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34134 +       if (unlikely(err < 0))
34135 +               goto out;
34136 +       h_path.dentry = au_h_dptr(dentry, bdst);
34137 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34138 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34139 +       if (unlikely(err))
34140 +               goto out_put;
34141 +       au_fset_cpdown(*flags, MADE_DIR);
34142 +
34143 +       bopq = au_dbdiropq(dentry);
34144 +       au_fclr_cpdown(*flags, WHED);
34145 +       au_fclr_cpdown(*flags, DIROPQ);
34146 +       if (au_dbwh(dentry) == bdst)
34147 +               au_fset_cpdown(*flags, WHED);
34148 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34149 +               au_fset_cpdown(*flags, PARENT_OPQ);
34150 +       h_inode = d_inode(h_path.dentry);
34151 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34152 +       if (au_ftest_cpdown(*flags, WHED)) {
34153 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34154 +               if (unlikely(err)) {
34155 +                       inode_unlock(h_inode);
34156 +                       goto out_dir;
34157 +               }
34158 +       }
34159 +
34160 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34161 +       inode_unlock(h_inode);
34162 +       if (unlikely(err))
34163 +               goto out_opq;
34164 +
34165 +       if (au_ftest_cpdown(*flags, WHED)) {
34166 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34167 +               if (unlikely(err))
34168 +                       goto out_opq;
34169 +       }
34170 +
34171 +       inode = d_inode(dentry);
34172 +       if (au_ibbot(inode) < bdst)
34173 +               au_set_ibbot(inode, bdst);
34174 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34175 +                     au_hi_flags(inode, /*isdir*/1));
34176 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34177 +       goto out; /* success */
34178 +
34179 +       /* revert */
34180 +out_opq:
34181 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34182 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34183 +               rerr = au_diropq_remove(dentry, bdst);
34184 +               inode_unlock(h_inode);
34185 +               if (unlikely(rerr)) {
34186 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34187 +                               dentry, bdst, rerr);
34188 +                       err = -EIO;
34189 +                       goto out;
34190 +               }
34191 +       }
34192 +out_dir:
34193 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34194 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34195 +               if (unlikely(rerr)) {
34196 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34197 +                               dentry, bdst, rerr);
34198 +                       err = -EIO;
34199 +               }
34200 +       }
34201 +out_put:
34202 +       au_set_h_dptr(dentry, bdst, NULL);
34203 +       if (au_dbbot(dentry) == bdst)
34204 +               au_update_dbbot(dentry);
34205 +out:
34206 +       dput(parent);
34207 +       return err;
34208 +}
34209 +
34210 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34211 +{
34212 +       int err;
34213 +       unsigned int flags;
34214 +
34215 +       flags = 0;
34216 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34217 +
34218 +       return err;
34219 +}
34220 +
34221 +/* ---------------------------------------------------------------------- */
34222 +
34223 +/* policies for create */
34224 +
34225 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34226 +{
34227 +       int err, i, j, ndentry;
34228 +       aufs_bindex_t bopq;
34229 +       struct au_dcsub_pages dpages;
34230 +       struct au_dpage *dpage;
34231 +       struct dentry **dentries, *parent, *d;
34232 +
34233 +       err = au_dpages_init(&dpages, GFP_NOFS);
34234 +       if (unlikely(err))
34235 +               goto out;
34236 +       parent = dget_parent(dentry);
34237 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34238 +       if (unlikely(err))
34239 +               goto out_free;
34240 +
34241 +       err = bindex;
34242 +       for (i = 0; i < dpages.ndpage; i++) {
34243 +               dpage = dpages.dpages + i;
34244 +               dentries = dpage->dentries;
34245 +               ndentry = dpage->ndentry;
34246 +               for (j = 0; j < ndentry; j++) {
34247 +                       d = dentries[j];
34248 +                       di_read_lock_parent2(d, !AuLock_IR);
34249 +                       bopq = au_dbdiropq(d);
34250 +                       di_read_unlock(d, !AuLock_IR);
34251 +                       if (bopq >= 0 && bopq < err)
34252 +                               err = bopq;
34253 +               }
34254 +       }
34255 +
34256 +out_free:
34257 +       dput(parent);
34258 +       au_dpages_free(&dpages);
34259 +out:
34260 +       return err;
34261 +}
34262 +
34263 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34264 +{
34265 +       for (; bindex >= 0; bindex--)
34266 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34267 +                       return bindex;
34268 +       return -EROFS;
34269 +}
34270 +
34271 +/* top down parent */
34272 +static int au_wbr_create_tdp(struct dentry *dentry,
34273 +                            unsigned int flags __maybe_unused)
34274 +{
34275 +       int err;
34276 +       aufs_bindex_t btop, bindex;
34277 +       struct super_block *sb;
34278 +       struct dentry *parent, *h_parent;
34279 +
34280 +       sb = dentry->d_sb;
34281 +       btop = au_dbtop(dentry);
34282 +       err = btop;
34283 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34284 +               goto out;
34285 +
34286 +       err = -EROFS;
34287 +       parent = dget_parent(dentry);
34288 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34289 +               h_parent = au_h_dptr(parent, bindex);
34290 +               if (!h_parent || d_is_negative(h_parent))
34291 +                       continue;
34292 +
34293 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34294 +                       err = bindex;
34295 +                       break;
34296 +               }
34297 +       }
34298 +       dput(parent);
34299 +
34300 +       /* bottom up here */
34301 +       if (unlikely(err < 0)) {
34302 +               err = au_wbr_bu(sb, btop - 1);
34303 +               if (err >= 0)
34304 +                       err = au_wbr_nonopq(dentry, err);
34305 +       }
34306 +
34307 +out:
34308 +       AuDbg("b%d\n", err);
34309 +       return err;
34310 +}
34311 +
34312 +/* ---------------------------------------------------------------------- */
34313 +
34314 +/* an exception for the policy other than tdp */
34315 +static int au_wbr_create_exp(struct dentry *dentry)
34316 +{
34317 +       int err;
34318 +       aufs_bindex_t bwh, bdiropq;
34319 +       struct dentry *parent;
34320 +
34321 +       err = -1;
34322 +       bwh = au_dbwh(dentry);
34323 +       parent = dget_parent(dentry);
34324 +       bdiropq = au_dbdiropq(parent);
34325 +       if (bwh >= 0) {
34326 +               if (bdiropq >= 0)
34327 +                       err = min(bdiropq, bwh);
34328 +               else
34329 +                       err = bwh;
34330 +               AuDbg("%d\n", err);
34331 +       } else if (bdiropq >= 0) {
34332 +               err = bdiropq;
34333 +               AuDbg("%d\n", err);
34334 +       }
34335 +       dput(parent);
34336 +
34337 +       if (err >= 0)
34338 +               err = au_wbr_nonopq(dentry, err);
34339 +
34340 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34341 +               err = -1;
34342 +
34343 +       AuDbg("%d\n", err);
34344 +       return err;
34345 +}
34346 +
34347 +/* ---------------------------------------------------------------------- */
34348 +
34349 +/* round robin */
34350 +static int au_wbr_create_init_rr(struct super_block *sb)
34351 +{
34352 +       int err;
34353 +
34354 +       err = au_wbr_bu(sb, au_sbbot(sb));
34355 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34356 +       /* smp_mb(); */
34357 +
34358 +       AuDbg("b%d\n", err);
34359 +       return err;
34360 +}
34361 +
34362 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34363 +{
34364 +       int err, nbr;
34365 +       unsigned int u;
34366 +       aufs_bindex_t bindex, bbot;
34367 +       struct super_block *sb;
34368 +       atomic_t *next;
34369 +
34370 +       err = au_wbr_create_exp(dentry);
34371 +       if (err >= 0)
34372 +               goto out;
34373 +
34374 +       sb = dentry->d_sb;
34375 +       next = &au_sbi(sb)->si_wbr_rr_next;
34376 +       bbot = au_sbbot(sb);
34377 +       nbr = bbot + 1;
34378 +       for (bindex = 0; bindex <= bbot; bindex++) {
34379 +               if (!au_ftest_wbr(flags, DIR)) {
34380 +                       err = atomic_dec_return(next) + 1;
34381 +                       /* modulo for 0 is meaningless */
34382 +                       if (unlikely(!err))
34383 +                               err = atomic_dec_return(next) + 1;
34384 +               } else
34385 +                       err = atomic_read(next);
34386 +               AuDbg("%d\n", err);
34387 +               u = err;
34388 +               err = u % nbr;
34389 +               AuDbg("%d\n", err);
34390 +               if (!au_br_rdonly(au_sbr(sb, err)))
34391 +                       break;
34392 +               err = -EROFS;
34393 +       }
34394 +
34395 +       if (err >= 0)
34396 +               err = au_wbr_nonopq(dentry, err);
34397 +
34398 +out:
34399 +       AuDbg("%d\n", err);
34400 +       return err;
34401 +}
34402 +
34403 +/* ---------------------------------------------------------------------- */
34404 +
34405 +/* most free space */
34406 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34407 +{
34408 +       struct super_block *sb;
34409 +       struct au_branch *br;
34410 +       struct au_wbr_mfs *mfs;
34411 +       struct dentry *h_parent;
34412 +       aufs_bindex_t bindex, bbot;
34413 +       int err;
34414 +       unsigned long long b, bavail;
34415 +       struct path h_path;
34416 +       /* reduce the stack usage */
34417 +       struct kstatfs *st;
34418 +
34419 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34420 +       if (unlikely(!st)) {
34421 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34422 +               return;
34423 +       }
34424 +
34425 +       bavail = 0;
34426 +       sb = dentry->d_sb;
34427 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34428 +       MtxMustLock(&mfs->mfs_lock);
34429 +       mfs->mfs_bindex = -EROFS;
34430 +       mfs->mfsrr_bytes = 0;
34431 +       if (!parent) {
34432 +               bindex = 0;
34433 +               bbot = au_sbbot(sb);
34434 +       } else {
34435 +               bindex = au_dbtop(parent);
34436 +               bbot = au_dbtaildir(parent);
34437 +       }
34438 +
34439 +       for (; bindex <= bbot; bindex++) {
34440 +               if (parent) {
34441 +                       h_parent = au_h_dptr(parent, bindex);
34442 +                       if (!h_parent || d_is_negative(h_parent))
34443 +                               continue;
34444 +               }
34445 +               br = au_sbr(sb, bindex);
34446 +               if (au_br_rdonly(br))
34447 +                       continue;
34448 +
34449 +               /* sb->s_root for NFS is unreliable */
34450 +               h_path.mnt = au_br_mnt(br);
34451 +               h_path.dentry = h_path.mnt->mnt_root;
34452 +               err = vfs_statfs(&h_path, st);
34453 +               if (unlikely(err)) {
34454 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34455 +                       continue;
34456 +               }
34457 +
34458 +               /* when the available size is equal, select the lower one */
34459 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34460 +                            || sizeof(b) < sizeof(st->f_bsize));
34461 +               b = st->f_bavail * st->f_bsize;
34462 +               br->br_wbr->wbr_bytes = b;
34463 +               if (b >= bavail) {
34464 +                       bavail = b;
34465 +                       mfs->mfs_bindex = bindex;
34466 +                       mfs->mfs_jiffy = jiffies;
34467 +               }
34468 +       }
34469 +
34470 +       mfs->mfsrr_bytes = bavail;
34471 +       AuDbg("b%d\n", mfs->mfs_bindex);
34472 +       au_kfree_rcu(st);
34473 +}
34474 +
34475 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34476 +{
34477 +       int err;
34478 +       struct dentry *parent;
34479 +       struct super_block *sb;
34480 +       struct au_wbr_mfs *mfs;
34481 +
34482 +       err = au_wbr_create_exp(dentry);
34483 +       if (err >= 0)
34484 +               goto out;
34485 +
34486 +       sb = dentry->d_sb;
34487 +       parent = NULL;
34488 +       if (au_ftest_wbr(flags, PARENT))
34489 +               parent = dget_parent(dentry);
34490 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34491 +       mutex_lock(&mfs->mfs_lock);
34492 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34493 +           || mfs->mfs_bindex < 0
34494 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34495 +               au_mfs(dentry, parent);
34496 +       mutex_unlock(&mfs->mfs_lock);
34497 +       err = mfs->mfs_bindex;
34498 +       dput(parent);
34499 +
34500 +       if (err >= 0)
34501 +               err = au_wbr_nonopq(dentry, err);
34502 +
34503 +out:
34504 +       AuDbg("b%d\n", err);
34505 +       return err;
34506 +}
34507 +
34508 +static int au_wbr_create_init_mfs(struct super_block *sb)
34509 +{
34510 +       struct au_wbr_mfs *mfs;
34511 +
34512 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34513 +       mutex_init(&mfs->mfs_lock);
34514 +       mfs->mfs_jiffy = 0;
34515 +       mfs->mfs_bindex = -EROFS;
34516 +
34517 +       return 0;
34518 +}
34519 +
34520 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34521 +{
34522 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34523 +       return 0;
34524 +}
34525 +
34526 +/* ---------------------------------------------------------------------- */
34527 +
34528 +/* top down regardless parent, and then mfs */
34529 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34530 +                              unsigned int flags __maybe_unused)
34531 +{
34532 +       int err;
34533 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34534 +       unsigned long long watermark;
34535 +       struct super_block *sb;
34536 +       struct au_wbr_mfs *mfs;
34537 +       struct au_branch *br;
34538 +       struct dentry *parent;
34539 +
34540 +       sb = dentry->d_sb;
34541 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34542 +       mutex_lock(&mfs->mfs_lock);
34543 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34544 +           || mfs->mfs_bindex < 0)
34545 +               au_mfs(dentry, /*parent*/NULL);
34546 +       watermark = mfs->mfsrr_watermark;
34547 +       bmfs = mfs->mfs_bindex;
34548 +       mutex_unlock(&mfs->mfs_lock);
34549 +
34550 +       /* another style of au_wbr_create_exp() */
34551 +       bwh = au_dbwh(dentry);
34552 +       parent = dget_parent(dentry);
34553 +       btail = au_dbtaildir(parent);
34554 +       if (bwh >= 0 && bwh < btail)
34555 +               btail = bwh;
34556 +
34557 +       err = au_wbr_nonopq(dentry, btail);
34558 +       if (unlikely(err < 0))
34559 +               goto out;
34560 +       btail = err;
34561 +       bfound = -1;
34562 +       for (bindex = 0; bindex <= btail; bindex++) {
34563 +               br = au_sbr(sb, bindex);
34564 +               if (au_br_rdonly(br))
34565 +                       continue;
34566 +               if (br->br_wbr->wbr_bytes > watermark) {
34567 +                       bfound = bindex;
34568 +                       break;
34569 +               }
34570 +       }
34571 +       err = bfound;
34572 +       if (err < 0)
34573 +               err = bmfs;
34574 +
34575 +out:
34576 +       dput(parent);
34577 +       AuDbg("b%d\n", err);
34578 +       return err;
34579 +}
34580 +
34581 +/* ---------------------------------------------------------------------- */
34582 +
34583 +/* most free space and then round robin */
34584 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34585 +{
34586 +       int err;
34587 +       struct au_wbr_mfs *mfs;
34588 +
34589 +       err = au_wbr_create_mfs(dentry, flags);
34590 +       if (err >= 0) {
34591 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34592 +               mutex_lock(&mfs->mfs_lock);
34593 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34594 +                       err = au_wbr_create_rr(dentry, flags);
34595 +               mutex_unlock(&mfs->mfs_lock);
34596 +       }
34597 +
34598 +       AuDbg("b%d\n", err);
34599 +       return err;
34600 +}
34601 +
34602 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34603 +{
34604 +       int err;
34605 +
34606 +       au_wbr_create_init_mfs(sb); /* ignore */
34607 +       err = au_wbr_create_init_rr(sb);
34608 +
34609 +       return err;
34610 +}
34611 +
34612 +/* ---------------------------------------------------------------------- */
34613 +
34614 +/* top down parent and most free space */
34615 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34616 +{
34617 +       int err, e2;
34618 +       unsigned long long b;
34619 +       aufs_bindex_t bindex, btop, bbot;
34620 +       struct super_block *sb;
34621 +       struct dentry *parent, *h_parent;
34622 +       struct au_branch *br;
34623 +
34624 +       err = au_wbr_create_tdp(dentry, flags);
34625 +       if (unlikely(err < 0))
34626 +               goto out;
34627 +       parent = dget_parent(dentry);
34628 +       btop = au_dbtop(parent);
34629 +       bbot = au_dbtaildir(parent);
34630 +       if (btop == bbot)
34631 +               goto out_parent; /* success */
34632 +
34633 +       e2 = au_wbr_create_mfs(dentry, flags);
34634 +       if (e2 < 0)
34635 +               goto out_parent; /* success */
34636 +
34637 +       /* when the available size is equal, select upper one */
34638 +       sb = dentry->d_sb;
34639 +       br = au_sbr(sb, err);
34640 +       b = br->br_wbr->wbr_bytes;
34641 +       AuDbg("b%d, %llu\n", err, b);
34642 +
34643 +       for (bindex = btop; bindex <= bbot; bindex++) {
34644 +               h_parent = au_h_dptr(parent, bindex);
34645 +               if (!h_parent || d_is_negative(h_parent))
34646 +                       continue;
34647 +
34648 +               br = au_sbr(sb, bindex);
34649 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34650 +                       b = br->br_wbr->wbr_bytes;
34651 +                       err = bindex;
34652 +                       AuDbg("b%d, %llu\n", err, b);
34653 +               }
34654 +       }
34655 +
34656 +       if (err >= 0)
34657 +               err = au_wbr_nonopq(dentry, err);
34658 +
34659 +out_parent:
34660 +       dput(parent);
34661 +out:
34662 +       AuDbg("b%d\n", err);
34663 +       return err;
34664 +}
34665 +
34666 +/* ---------------------------------------------------------------------- */
34667 +
34668 +/*
34669 + * - top down parent
34670 + * - most free space with parent
34671 + * - most free space round-robin regardless parent
34672 + */
34673 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34674 +{
34675 +       int err;
34676 +       unsigned long long watermark;
34677 +       struct super_block *sb;
34678 +       struct au_branch *br;
34679 +       struct au_wbr_mfs *mfs;
34680 +
34681 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34682 +       if (unlikely(err < 0))
34683 +               goto out;
34684 +
34685 +       sb = dentry->d_sb;
34686 +       br = au_sbr(sb, err);
34687 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34688 +       mutex_lock(&mfs->mfs_lock);
34689 +       watermark = mfs->mfsrr_watermark;
34690 +       mutex_unlock(&mfs->mfs_lock);
34691 +       if (br->br_wbr->wbr_bytes < watermark)
34692 +               /* regardless the parent dir */
34693 +               err = au_wbr_create_mfsrr(dentry, flags);
34694 +
34695 +out:
34696 +       AuDbg("b%d\n", err);
34697 +       return err;
34698 +}
34699 +
34700 +/* ---------------------------------------------------------------------- */
34701 +
34702 +/* policies for copyup */
34703 +
34704 +/* top down parent */
34705 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34706 +{
34707 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34708 +}
34709 +
34710 +/* bottom up parent */
34711 +static int au_wbr_copyup_bup(struct dentry *dentry)
34712 +{
34713 +       int err;
34714 +       aufs_bindex_t bindex, btop;
34715 +       struct dentry *parent, *h_parent;
34716 +       struct super_block *sb;
34717 +
34718 +       err = -EROFS;
34719 +       sb = dentry->d_sb;
34720 +       parent = dget_parent(dentry);
34721 +       btop = au_dbtop(parent);
34722 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34723 +               h_parent = au_h_dptr(parent, bindex);
34724 +               if (!h_parent || d_is_negative(h_parent))
34725 +                       continue;
34726 +
34727 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34728 +                       err = bindex;
34729 +                       break;
34730 +               }
34731 +       }
34732 +       dput(parent);
34733 +
34734 +       /* bottom up here */
34735 +       if (unlikely(err < 0))
34736 +               err = au_wbr_bu(sb, btop - 1);
34737 +
34738 +       AuDbg("b%d\n", err);
34739 +       return err;
34740 +}
34741 +
34742 +/* bottom up */
34743 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34744 +{
34745 +       int err;
34746 +
34747 +       err = au_wbr_bu(dentry->d_sb, btop);
34748 +       AuDbg("b%d\n", err);
34749 +       if (err > btop)
34750 +               err = au_wbr_nonopq(dentry, err);
34751 +
34752 +       AuDbg("b%d\n", err);
34753 +       return err;
34754 +}
34755 +
34756 +static int au_wbr_copyup_bu(struct dentry *dentry)
34757 +{
34758 +       int err;
34759 +       aufs_bindex_t btop;
34760 +
34761 +       btop = au_dbtop(dentry);
34762 +       err = au_wbr_do_copyup_bu(dentry, btop);
34763 +       return err;
34764 +}
34765 +
34766 +/* ---------------------------------------------------------------------- */
34767 +
34768 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34769 +       [AuWbrCopyup_TDP] = {
34770 +               .copyup = au_wbr_copyup_tdp
34771 +       },
34772 +       [AuWbrCopyup_BUP] = {
34773 +               .copyup = au_wbr_copyup_bup
34774 +       },
34775 +       [AuWbrCopyup_BU] = {
34776 +               .copyup = au_wbr_copyup_bu
34777 +       }
34778 +};
34779 +
34780 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34781 +       [AuWbrCreate_TDP] = {
34782 +               .create = au_wbr_create_tdp
34783 +       },
34784 +       [AuWbrCreate_RR] = {
34785 +               .create = au_wbr_create_rr,
34786 +               .init   = au_wbr_create_init_rr
34787 +       },
34788 +       [AuWbrCreate_MFS] = {
34789 +               .create = au_wbr_create_mfs,
34790 +               .init   = au_wbr_create_init_mfs,
34791 +               .fin    = au_wbr_create_fin_mfs
34792 +       },
34793 +       [AuWbrCreate_MFSV] = {
34794 +               .create = au_wbr_create_mfs,
34795 +               .init   = au_wbr_create_init_mfs,
34796 +               .fin    = au_wbr_create_fin_mfs
34797 +       },
34798 +       [AuWbrCreate_MFSRR] = {
34799 +               .create = au_wbr_create_mfsrr,
34800 +               .init   = au_wbr_create_init_mfsrr,
34801 +               .fin    = au_wbr_create_fin_mfs
34802 +       },
34803 +       [AuWbrCreate_MFSRRV] = {
34804 +               .create = au_wbr_create_mfsrr,
34805 +               .init   = au_wbr_create_init_mfsrr,
34806 +               .fin    = au_wbr_create_fin_mfs
34807 +       },
34808 +       [AuWbrCreate_TDMFS] = {
34809 +               .create = au_wbr_create_tdmfs,
34810 +               .init   = au_wbr_create_init_mfs,
34811 +               .fin    = au_wbr_create_fin_mfs
34812 +       },
34813 +       [AuWbrCreate_TDMFSV] = {
34814 +               .create = au_wbr_create_tdmfs,
34815 +               .init   = au_wbr_create_init_mfs,
34816 +               .fin    = au_wbr_create_fin_mfs
34817 +       },
34818 +       [AuWbrCreate_PMFS] = {
34819 +               .create = au_wbr_create_pmfs,
34820 +               .init   = au_wbr_create_init_mfs,
34821 +               .fin    = au_wbr_create_fin_mfs
34822 +       },
34823 +       [AuWbrCreate_PMFSV] = {
34824 +               .create = au_wbr_create_pmfs,
34825 +               .init   = au_wbr_create_init_mfs,
34826 +               .fin    = au_wbr_create_fin_mfs
34827 +       },
34828 +       [AuWbrCreate_PMFSRR] = {
34829 +               .create = au_wbr_create_pmfsrr,
34830 +               .init   = au_wbr_create_init_mfsrr,
34831 +               .fin    = au_wbr_create_fin_mfs
34832 +       },
34833 +       [AuWbrCreate_PMFSRRV] = {
34834 +               .create = au_wbr_create_pmfsrr,
34835 +               .init   = au_wbr_create_init_mfsrr,
34836 +               .fin    = au_wbr_create_fin_mfs
34837 +       }
34838 +};
34839 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34840 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34841 +++ linux/fs/aufs/whout.c       2019-07-11 15:42:14.475571481 +0200
34842 @@ -0,0 +1,1062 @@
34843 +// SPDX-License-Identifier: GPL-2.0
34844 +/*
34845 + * Copyright (C) 2005-2019 Junjiro R. Okajima
34846 + *
34847 + * This program, aufs is free software; you can redistribute it and/or modify
34848 + * it under the terms of the GNU General Public License as published by
34849 + * the Free Software Foundation; either version 2 of the License, or
34850 + * (at your option) any later version.
34851 + *
34852 + * This program is distributed in the hope that it will be useful,
34853 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34854 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34855 + * GNU General Public License for more details.
34856 + *
34857 + * You should have received a copy of the GNU General Public License
34858 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34859 + */
34860 +
34861 +/*
34862 + * whiteout for logical deletion and opaque directory
34863 + */
34864 +
34865 +#include "aufs.h"
34866 +
34867 +#define WH_MASK                        0444
34868 +
34869 +/*
34870 + * If a directory contains this file, then it is opaque.  We start with the
34871 + * .wh. flag so that it is blocked by lookup.
34872 + */
34873 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34874 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34875 +
34876 +/*
34877 + * generate whiteout name, which is NOT terminated by NULL.
34878 + * @name: original d_name.name
34879 + * @len: original d_name.len
34880 + * @wh: whiteout qstr
34881 + * returns zero when succeeds, otherwise error.
34882 + * succeeded value as wh->name should be freed by kfree().
34883 + */
34884 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34885 +{
34886 +       char *p;
34887 +
34888 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34889 +               return -ENAMETOOLONG;
34890 +
34891 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34892 +       p = kmalloc(wh->len, GFP_NOFS);
34893 +       wh->name = p;
34894 +       if (p) {
34895 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34896 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34897 +               /* smp_mb(); */
34898 +               return 0;
34899 +       }
34900 +       return -ENOMEM;
34901 +}
34902 +
34903 +/* ---------------------------------------------------------------------- */
34904 +
34905 +/*
34906 + * test if the @wh_name exists under @h_parent.
34907 + * @try_sio specifies the necessary of super-io.
34908 + */
34909 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34910 +{
34911 +       int err;
34912 +       struct dentry *wh_dentry;
34913 +
34914 +       if (!try_sio)
34915 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34916 +       else
34917 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34918 +       err = PTR_ERR(wh_dentry);
34919 +       if (IS_ERR(wh_dentry)) {
34920 +               if (err == -ENAMETOOLONG)
34921 +                       err = 0;
34922 +               goto out;
34923 +       }
34924 +
34925 +       err = 0;
34926 +       if (d_is_negative(wh_dentry))
34927 +               goto out_wh; /* success */
34928 +
34929 +       err = 1;
34930 +       if (d_is_reg(wh_dentry))
34931 +               goto out_wh; /* success */
34932 +
34933 +       err = -EIO;
34934 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34935 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34936 +
34937 +out_wh:
34938 +       dput(wh_dentry);
34939 +out:
34940 +       return err;
34941 +}
34942 +
34943 +/*
34944 + * test if the @h_dentry sets opaque or not.
34945 + */
34946 +int au_diropq_test(struct dentry *h_dentry)
34947 +{
34948 +       int err;
34949 +       struct inode *h_dir;
34950 +
34951 +       h_dir = d_inode(h_dentry);
34952 +       err = au_wh_test(h_dentry, &diropq_name,
34953 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34954 +       return err;
34955 +}
34956 +
34957 +/*
34958 + * returns a negative dentry whose name is unique and temporary.
34959 + */
34960 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34961 +                            struct qstr *prefix)
34962 +{
34963 +       struct dentry *dentry;
34964 +       int i;
34965 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34966 +               *name, *p;
34967 +       /* strict atomic_t is unnecessary here */
34968 +       static unsigned short cnt;
34969 +       struct qstr qs;
34970 +
34971 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34972 +
34973 +       name = defname;
34974 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34975 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34976 +               dentry = ERR_PTR(-ENAMETOOLONG);
34977 +               if (unlikely(qs.len > NAME_MAX))
34978 +                       goto out;
34979 +               dentry = ERR_PTR(-ENOMEM);
34980 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34981 +               if (unlikely(!name))
34982 +                       goto out;
34983 +       }
34984 +
34985 +       /* doubly whiteout-ed */
34986 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34987 +       p = name + AUFS_WH_PFX_LEN * 2;
34988 +       memcpy(p, prefix->name, prefix->len);
34989 +       p += prefix->len;
34990 +       *p++ = '.';
34991 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34992 +
34993 +       qs.name = name;
34994 +       for (i = 0; i < 3; i++) {
34995 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34996 +               dentry = au_sio_lkup_one(&qs, h_parent);
34997 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34998 +                       goto out_name;
34999 +               dput(dentry);
35000 +       }
35001 +       /* pr_warn("could not get random name\n"); */
35002 +       dentry = ERR_PTR(-EEXIST);
35003 +       AuDbg("%.*s\n", AuLNPair(&qs));
35004 +       BUG();
35005 +
35006 +out_name:
35007 +       if (name != defname)
35008 +               au_kfree_try_rcu(name);
35009 +out:
35010 +       AuTraceErrPtr(dentry);
35011 +       return dentry;
35012 +}
35013 +
35014 +/*
35015 + * rename the @h_dentry on @br to the whiteouted temporary name.
35016 + */
35017 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35018 +{
35019 +       int err;
35020 +       struct path h_path = {
35021 +               .mnt = au_br_mnt(br)
35022 +       };
35023 +       struct inode *h_dir, *delegated;
35024 +       struct dentry *h_parent;
35025 +
35026 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35027 +       h_dir = d_inode(h_parent);
35028 +       IMustLock(h_dir);
35029 +
35030 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35031 +       err = PTR_ERR(h_path.dentry);
35032 +       if (IS_ERR(h_path.dentry))
35033 +               goto out;
35034 +
35035 +       /* under the same dir, no need to lock_rename() */
35036 +       delegated = NULL;
35037 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35038 +                          /*flags*/0);
35039 +       AuTraceErr(err);
35040 +       if (unlikely(err == -EWOULDBLOCK)) {
35041 +               pr_warn("cannot retry for NFSv4 delegation"
35042 +                       " for an internal rename\n");
35043 +               iput(delegated);
35044 +       }
35045 +       dput(h_path.dentry);
35046 +
35047 +out:
35048 +       AuTraceErr(err);
35049 +       return err;
35050 +}
35051 +
35052 +/* ---------------------------------------------------------------------- */
35053 +/*
35054 + * functions for removing a whiteout
35055 + */
35056 +
35057 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35058 +{
35059 +       int err, force;
35060 +       struct inode *delegated;
35061 +
35062 +       /*
35063 +        * forces superio when the dir has a sticky bit.
35064 +        * this may be a violation of unix fs semantics.
35065 +        */
35066 +       force = (h_dir->i_mode & S_ISVTX)
35067 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35068 +       delegated = NULL;
35069 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35070 +       if (unlikely(err == -EWOULDBLOCK)) {
35071 +               pr_warn("cannot retry for NFSv4 delegation"
35072 +                       " for an internal unlink\n");
35073 +               iput(delegated);
35074 +       }
35075 +       return err;
35076 +}
35077 +
35078 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35079 +                       struct dentry *dentry)
35080 +{
35081 +       int err;
35082 +
35083 +       err = do_unlink_wh(h_dir, h_path);
35084 +       if (!err && dentry)
35085 +               au_set_dbwh(dentry, -1);
35086 +
35087 +       return err;
35088 +}
35089 +
35090 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35091 +                         struct au_branch *br)
35092 +{
35093 +       int err;
35094 +       struct path h_path = {
35095 +               .mnt = au_br_mnt(br)
35096 +       };
35097 +
35098 +       err = 0;
35099 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
35100 +       if (IS_ERR(h_path.dentry))
35101 +               err = PTR_ERR(h_path.dentry);
35102 +       else {
35103 +               if (d_is_reg(h_path.dentry))
35104 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
35105 +               dput(h_path.dentry);
35106 +       }
35107 +
35108 +       return err;
35109 +}
35110 +
35111 +/* ---------------------------------------------------------------------- */
35112 +/*
35113 + * initialize/clean whiteout for a branch
35114 + */
35115 +
35116 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35117 +                       const int isdir)
35118 +{
35119 +       int err;
35120 +       struct inode *delegated;
35121 +
35122 +       if (d_is_negative(whpath->dentry))
35123 +               return;
35124 +
35125 +       if (isdir)
35126 +               err = vfsub_rmdir(h_dir, whpath);
35127 +       else {
35128 +               delegated = NULL;
35129 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35130 +               if (unlikely(err == -EWOULDBLOCK)) {
35131 +                       pr_warn("cannot retry for NFSv4 delegation"
35132 +                               " for an internal unlink\n");
35133 +                       iput(delegated);
35134 +               }
35135 +       }
35136 +       if (unlikely(err))
35137 +               pr_warn("failed removing %pd (%d), ignored.\n",
35138 +                       whpath->dentry, err);
35139 +}
35140 +
35141 +static int test_linkable(struct dentry *h_root)
35142 +{
35143 +       struct inode *h_dir = d_inode(h_root);
35144 +
35145 +       if (h_dir->i_op->link)
35146 +               return 0;
35147 +
35148 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35149 +              h_root, au_sbtype(h_root->d_sb));
35150 +       return -ENOSYS;
35151 +}
35152 +
35153 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35154 +static int au_whdir(struct inode *h_dir, struct path *path)
35155 +{
35156 +       int err;
35157 +
35158 +       err = -EEXIST;
35159 +       if (d_is_negative(path->dentry)) {
35160 +               int mode = 0700;
35161 +
35162 +               if (au_test_nfs(path->dentry->d_sb))
35163 +                       mode |= 0111;
35164 +               err = vfsub_mkdir(h_dir, path, mode);
35165 +       } else if (d_is_dir(path->dentry))
35166 +               err = 0;
35167 +       else
35168 +               pr_err("unknown %pd exists\n", path->dentry);
35169 +
35170 +       return err;
35171 +}
35172 +
35173 +struct au_wh_base {
35174 +       const struct qstr *name;
35175 +       struct dentry *dentry;
35176 +};
35177 +
35178 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35179 +                         struct path *h_path)
35180 +{
35181 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35182 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35183 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35184 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35185 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35186 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35187 +}
35188 +
35189 +/*
35190 + * returns tri-state,
35191 + * minus: error, caller should print the message
35192 + * zero: success
35193 + * plus: error, caller should NOT print the message
35194 + */
35195 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35196 +                               int do_plink, struct au_wh_base base[],
35197 +                               struct path *h_path)
35198 +{
35199 +       int err;
35200 +       struct inode *h_dir;
35201 +
35202 +       h_dir = d_inode(h_root);
35203 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35204 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35205 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35206 +       if (do_plink) {
35207 +               err = test_linkable(h_root);
35208 +               if (unlikely(err)) {
35209 +                       err = 1;
35210 +                       goto out;
35211 +               }
35212 +
35213 +               err = au_whdir(h_dir, h_path);
35214 +               if (unlikely(err))
35215 +                       goto out;
35216 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35217 +       } else
35218 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35219 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35220 +       err = au_whdir(h_dir, h_path);
35221 +       if (unlikely(err))
35222 +               goto out;
35223 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35224 +
35225 +out:
35226 +       return err;
35227 +}
35228 +
35229 +/*
35230 + * for the moment, aufs supports the branch filesystem which does not support
35231 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35232 + * copyup failed. finally, such filesystem will not be used as the writable
35233 + * branch.
35234 + *
35235 + * returns tri-state, see above.
35236 + */
35237 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35238 +                        int do_plink, struct au_wh_base base[],
35239 +                        struct path *h_path)
35240 +{
35241 +       int err;
35242 +       struct inode *h_dir;
35243 +
35244 +       WbrWhMustWriteLock(wbr);
35245 +
35246 +       err = test_linkable(h_root);
35247 +       if (unlikely(err)) {
35248 +               err = 1;
35249 +               goto out;
35250 +       }
35251 +
35252 +       /*
35253 +        * todo: should this create be done in /sbin/mount.aufs helper?
35254 +        */
35255 +       err = -EEXIST;
35256 +       h_dir = d_inode(h_root);
35257 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35258 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35259 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35260 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35261 +               err = 0;
35262 +       else
35263 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35264 +       if (unlikely(err))
35265 +               goto out;
35266 +
35267 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35268 +       if (do_plink) {
35269 +               err = au_whdir(h_dir, h_path);
35270 +               if (unlikely(err))
35271 +                       goto out;
35272 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35273 +       } else
35274 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35275 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35276 +
35277 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35278 +       err = au_whdir(h_dir, h_path);
35279 +       if (unlikely(err))
35280 +               goto out;
35281 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35282 +
35283 +out:
35284 +       return err;
35285 +}
35286 +
35287 +/*
35288 + * initialize the whiteout base file/dir for @br.
35289 + */
35290 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35291 +{
35292 +       int err, i;
35293 +       const unsigned char do_plink
35294 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35295 +       struct inode *h_dir;
35296 +       struct path path = br->br_path;
35297 +       struct dentry *h_root = path.dentry;
35298 +       struct au_wbr *wbr = br->br_wbr;
35299 +       static const struct qstr base_name[] = {
35300 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35301 +                                         sizeof(AUFS_BASE_NAME) - 1),
35302 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35303 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35304 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35305 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35306 +       };
35307 +       struct au_wh_base base[] = {
35308 +               [AuBrWh_BASE] = {
35309 +                       .name   = base_name + AuBrWh_BASE,
35310 +                       .dentry = NULL
35311 +               },
35312 +               [AuBrWh_PLINK] = {
35313 +                       .name   = base_name + AuBrWh_PLINK,
35314 +                       .dentry = NULL
35315 +               },
35316 +               [AuBrWh_ORPH] = {
35317 +                       .name   = base_name + AuBrWh_ORPH,
35318 +                       .dentry = NULL
35319 +               }
35320 +       };
35321 +
35322 +       if (wbr)
35323 +               WbrWhMustWriteLock(wbr);
35324 +
35325 +       for (i = 0; i < AuBrWh_Last; i++) {
35326 +               /* doubly whiteouted */
35327 +               struct dentry *d;
35328 +
35329 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35330 +               err = PTR_ERR(d);
35331 +               if (IS_ERR(d))
35332 +                       goto out;
35333 +
35334 +               base[i].dentry = d;
35335 +               AuDebugOn(wbr
35336 +                         && wbr->wbr_wh[i]
35337 +                         && wbr->wbr_wh[i] != base[i].dentry);
35338 +       }
35339 +
35340 +       if (wbr)
35341 +               for (i = 0; i < AuBrWh_Last; i++) {
35342 +                       dput(wbr->wbr_wh[i]);
35343 +                       wbr->wbr_wh[i] = NULL;
35344 +               }
35345 +
35346 +       err = 0;
35347 +       if (!au_br_writable(br->br_perm)) {
35348 +               h_dir = d_inode(h_root);
35349 +               au_wh_init_ro(h_dir, base, &path);
35350 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35351 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35352 +               if (err > 0)
35353 +                       goto out;
35354 +               else if (err)
35355 +                       goto out_err;
35356 +       } else {
35357 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35358 +               if (err > 0)
35359 +                       goto out;
35360 +               else if (err)
35361 +                       goto out_err;
35362 +       }
35363 +       goto out; /* success */
35364 +
35365 +out_err:
35366 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35367 +              err, h_root, au_sbtype(h_root->d_sb));
35368 +out:
35369 +       for (i = 0; i < AuBrWh_Last; i++)
35370 +               dput(base[i].dentry);
35371 +       return err;
35372 +}
35373 +
35374 +/* ---------------------------------------------------------------------- */
35375 +/*
35376 + * whiteouts are all hard-linked usually.
35377 + * when its link count reaches a ceiling, we create a new whiteout base
35378 + * asynchronously.
35379 + */
35380 +
35381 +struct reinit_br_wh {
35382 +       struct super_block *sb;
35383 +       struct au_branch *br;
35384 +};
35385 +
35386 +static void reinit_br_wh(void *arg)
35387 +{
35388 +       int err;
35389 +       aufs_bindex_t bindex;
35390 +       struct path h_path;
35391 +       struct reinit_br_wh *a = arg;
35392 +       struct au_wbr *wbr;
35393 +       struct inode *dir, *delegated;
35394 +       struct dentry *h_root;
35395 +       struct au_hinode *hdir;
35396 +
35397 +       err = 0;
35398 +       wbr = a->br->br_wbr;
35399 +       /* big aufs lock */
35400 +       si_noflush_write_lock(a->sb);
35401 +       if (!au_br_writable(a->br->br_perm))
35402 +               goto out;
35403 +       bindex = au_br_index(a->sb, a->br->br_id);
35404 +       if (unlikely(bindex < 0))
35405 +               goto out;
35406 +
35407 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35408 +       dir = d_inode(a->sb->s_root);
35409 +       hdir = au_hi(dir, bindex);
35410 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35411 +       AuDebugOn(h_root != au_br_dentry(a->br));
35412 +
35413 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35414 +       wbr_wh_write_lock(wbr);
35415 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35416 +                         h_root, a->br);
35417 +       if (!err) {
35418 +               h_path.dentry = wbr->wbr_whbase;
35419 +               h_path.mnt = au_br_mnt(a->br);
35420 +               delegated = NULL;
35421 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35422 +                                  /*force*/0);
35423 +               if (unlikely(err == -EWOULDBLOCK)) {
35424 +                       pr_warn("cannot retry for NFSv4 delegation"
35425 +                               " for an internal unlink\n");
35426 +                       iput(delegated);
35427 +               }
35428 +       } else {
35429 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35430 +               err = 0;
35431 +       }
35432 +       dput(wbr->wbr_whbase);
35433 +       wbr->wbr_whbase = NULL;
35434 +       if (!err)
35435 +               err = au_wh_init(a->br, a->sb);
35436 +       wbr_wh_write_unlock(wbr);
35437 +       au_hn_inode_unlock(hdir);
35438 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35439 +       if (!err)
35440 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35441 +
35442 +out:
35443 +       if (wbr)
35444 +               atomic_dec(&wbr->wbr_wh_running);
35445 +       au_lcnt_dec(&a->br->br_count);
35446 +       si_write_unlock(a->sb);
35447 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35448 +       au_kfree_rcu(a);
35449 +       if (unlikely(err))
35450 +               AuIOErr("err %d\n", err);
35451 +}
35452 +
35453 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35454 +{
35455 +       int do_dec, wkq_err;
35456 +       struct reinit_br_wh *arg;
35457 +
35458 +       do_dec = 1;
35459 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35460 +               goto out;
35461 +
35462 +       /* ignore ENOMEM */
35463 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35464 +       if (arg) {
35465 +               /*
35466 +                * dec(wh_running), kfree(arg) and dec(br_count)
35467 +                * in reinit function
35468 +                */
35469 +               arg->sb = sb;
35470 +               arg->br = br;
35471 +               au_lcnt_inc(&br->br_count);
35472 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35473 +               if (unlikely(wkq_err)) {
35474 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35475 +                       au_lcnt_dec(&br->br_count);
35476 +                       au_kfree_rcu(arg);
35477 +               }
35478 +               do_dec = 0;
35479 +       }
35480 +
35481 +out:
35482 +       if (do_dec)
35483 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35484 +}
35485 +
35486 +/* ---------------------------------------------------------------------- */
35487 +
35488 +/*
35489 + * create the whiteout @wh.
35490 + */
35491 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35492 +                            struct dentry *wh)
35493 +{
35494 +       int err;
35495 +       struct path h_path = {
35496 +               .dentry = wh
35497 +       };
35498 +       struct au_branch *br;
35499 +       struct au_wbr *wbr;
35500 +       struct dentry *h_parent;
35501 +       struct inode *h_dir, *delegated;
35502 +
35503 +       h_parent = wh->d_parent; /* dir inode is locked */
35504 +       h_dir = d_inode(h_parent);
35505 +       IMustLock(h_dir);
35506 +
35507 +       br = au_sbr(sb, bindex);
35508 +       h_path.mnt = au_br_mnt(br);
35509 +       wbr = br->br_wbr;
35510 +       wbr_wh_read_lock(wbr);
35511 +       if (wbr->wbr_whbase) {
35512 +               delegated = NULL;
35513 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35514 +               if (unlikely(err == -EWOULDBLOCK)) {
35515 +                       pr_warn("cannot retry for NFSv4 delegation"
35516 +                               " for an internal link\n");
35517 +                       iput(delegated);
35518 +               }
35519 +               if (!err || err != -EMLINK)
35520 +                       goto out;
35521 +
35522 +               /* link count full. re-initialize br_whbase. */
35523 +               kick_reinit_br_wh(sb, br);
35524 +       }
35525 +
35526 +       /* return this error in this context */
35527 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35528 +       if (!err)
35529 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35530 +
35531 +out:
35532 +       wbr_wh_read_unlock(wbr);
35533 +       return err;
35534 +}
35535 +
35536 +/* ---------------------------------------------------------------------- */
35537 +
35538 +/*
35539 + * create or remove the diropq.
35540 + */
35541 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35542 +                               unsigned int flags)
35543 +{
35544 +       struct dentry *opq_dentry, *h_dentry;
35545 +       struct super_block *sb;
35546 +       struct au_branch *br;
35547 +       int err;
35548 +
35549 +       sb = dentry->d_sb;
35550 +       br = au_sbr(sb, bindex);
35551 +       h_dentry = au_h_dptr(dentry, bindex);
35552 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35553 +       if (IS_ERR(opq_dentry))
35554 +               goto out;
35555 +
35556 +       if (au_ftest_diropq(flags, CREATE)) {
35557 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35558 +               if (!err) {
35559 +                       au_set_dbdiropq(dentry, bindex);
35560 +                       goto out; /* success */
35561 +               }
35562 +       } else {
35563 +               struct path tmp = {
35564 +                       .dentry = opq_dentry,
35565 +                       .mnt    = au_br_mnt(br)
35566 +               };
35567 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35568 +               if (!err)
35569 +                       au_set_dbdiropq(dentry, -1);
35570 +       }
35571 +       dput(opq_dentry);
35572 +       opq_dentry = ERR_PTR(err);
35573 +
35574 +out:
35575 +       return opq_dentry;
35576 +}
35577 +
35578 +struct do_diropq_args {
35579 +       struct dentry **errp;
35580 +       struct dentry *dentry;
35581 +       aufs_bindex_t bindex;
35582 +       unsigned int flags;
35583 +};
35584 +
35585 +static void call_do_diropq(void *args)
35586 +{
35587 +       struct do_diropq_args *a = args;
35588 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35589 +}
35590 +
35591 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35592 +                            unsigned int flags)
35593 +{
35594 +       struct dentry *diropq, *h_dentry;
35595 +
35596 +       h_dentry = au_h_dptr(dentry, bindex);
35597 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35598 +               diropq = do_diropq(dentry, bindex, flags);
35599 +       else {
35600 +               int wkq_err;
35601 +               struct do_diropq_args args = {
35602 +                       .errp           = &diropq,
35603 +                       .dentry         = dentry,
35604 +                       .bindex         = bindex,
35605 +                       .flags          = flags
35606 +               };
35607 +
35608 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35609 +               if (unlikely(wkq_err))
35610 +                       diropq = ERR_PTR(wkq_err);
35611 +       }
35612 +
35613 +       return diropq;
35614 +}
35615 +
35616 +/* ---------------------------------------------------------------------- */
35617 +
35618 +/*
35619 + * lookup whiteout dentry.
35620 + * @h_parent: lower parent dentry which must exist and be locked
35621 + * @base_name: name of dentry which will be whiteouted
35622 + * returns dentry for whiteout.
35623 + */
35624 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35625 +                         struct au_branch *br)
35626 +{
35627 +       int err;
35628 +       struct qstr wh_name;
35629 +       struct dentry *wh_dentry;
35630 +
35631 +       err = au_wh_name_alloc(&wh_name, base_name);
35632 +       wh_dentry = ERR_PTR(err);
35633 +       if (!err) {
35634 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35635 +               au_kfree_try_rcu(wh_name.name);
35636 +       }
35637 +       return wh_dentry;
35638 +}
35639 +
35640 +/*
35641 + * link/create a whiteout for @dentry on @bindex.
35642 + */
35643 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35644 +                           struct dentry *h_parent)
35645 +{
35646 +       struct dentry *wh_dentry;
35647 +       struct super_block *sb;
35648 +       int err;
35649 +
35650 +       sb = dentry->d_sb;
35651 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35652 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35653 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35654 +               if (!err) {
35655 +                       au_set_dbwh(dentry, bindex);
35656 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35657 +               } else {
35658 +                       dput(wh_dentry);
35659 +                       wh_dentry = ERR_PTR(err);
35660 +               }
35661 +       }
35662 +
35663 +       return wh_dentry;
35664 +}
35665 +
35666 +/* ---------------------------------------------------------------------- */
35667 +
35668 +/* Delete all whiteouts in this directory on branch bindex. */
35669 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35670 +                          aufs_bindex_t bindex, struct au_branch *br)
35671 +{
35672 +       int err;
35673 +       unsigned long ul, n;
35674 +       struct qstr wh_name;
35675 +       char *p;
35676 +       struct hlist_head *head;
35677 +       struct au_vdir_wh *pos;
35678 +       struct au_vdir_destr *str;
35679 +
35680 +       err = -ENOMEM;
35681 +       p = (void *)__get_free_page(GFP_NOFS);
35682 +       wh_name.name = p;
35683 +       if (unlikely(!wh_name.name))
35684 +               goto out;
35685 +
35686 +       err = 0;
35687 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35688 +       p += AUFS_WH_PFX_LEN;
35689 +       n = whlist->nh_num;
35690 +       head = whlist->nh_head;
35691 +       for (ul = 0; !err && ul < n; ul++, head++) {
35692 +               hlist_for_each_entry(pos, head, wh_hash) {
35693 +                       if (pos->wh_bindex != bindex)
35694 +                               continue;
35695 +
35696 +                       str = &pos->wh_str;
35697 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35698 +                               memcpy(p, str->name, str->len);
35699 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35700 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35701 +                               if (!err)
35702 +                                       continue;
35703 +                               break;
35704 +                       }
35705 +                       AuIOErr("whiteout name too long %.*s\n",
35706 +                               str->len, str->name);
35707 +                       err = -EIO;
35708 +                       break;
35709 +               }
35710 +       }
35711 +       free_page((unsigned long)wh_name.name);
35712 +
35713 +out:
35714 +       return err;
35715 +}
35716 +
35717 +struct del_wh_children_args {
35718 +       int *errp;
35719 +       struct dentry *h_dentry;
35720 +       struct au_nhash *whlist;
35721 +       aufs_bindex_t bindex;
35722 +       struct au_branch *br;
35723 +};
35724 +
35725 +static void call_del_wh_children(void *args)
35726 +{
35727 +       struct del_wh_children_args *a = args;
35728 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35729 +}
35730 +
35731 +/* ---------------------------------------------------------------------- */
35732 +
35733 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35734 +{
35735 +       struct au_whtmp_rmdir *whtmp;
35736 +       int err;
35737 +       unsigned int rdhash;
35738 +
35739 +       SiMustAnyLock(sb);
35740 +
35741 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35742 +       if (unlikely(!whtmp)) {
35743 +               whtmp = ERR_PTR(-ENOMEM);
35744 +               goto out;
35745 +       }
35746 +
35747 +       /* no estimation for dir size */
35748 +       rdhash = au_sbi(sb)->si_rdhash;
35749 +       if (!rdhash)
35750 +               rdhash = AUFS_RDHASH_DEF;
35751 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35752 +       if (unlikely(err)) {
35753 +               au_kfree_rcu(whtmp);
35754 +               whtmp = ERR_PTR(err);
35755 +       }
35756 +
35757 +out:
35758 +       return whtmp;
35759 +}
35760 +
35761 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35762 +{
35763 +       if (whtmp->br)
35764 +               au_lcnt_dec(&whtmp->br->br_count);
35765 +       dput(whtmp->wh_dentry);
35766 +       iput(whtmp->dir);
35767 +       au_nhash_wh_free(&whtmp->whlist);
35768 +       au_kfree_rcu(whtmp);
35769 +}
35770 +
35771 +/*
35772 + * rmdir the whiteouted temporary named dir @h_dentry.
35773 + * @whlist: whiteouted children.
35774 + */
35775 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35776 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35777 +{
35778 +       int err;
35779 +       unsigned int h_nlink;
35780 +       struct path h_tmp;
35781 +       struct inode *wh_inode, *h_dir;
35782 +       struct au_branch *br;
35783 +
35784 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35785 +       IMustLock(h_dir);
35786 +
35787 +       br = au_sbr(dir->i_sb, bindex);
35788 +       wh_inode = d_inode(wh_dentry);
35789 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35790 +
35791 +       /*
35792 +        * someone else might change some whiteouts while we were sleeping.
35793 +        * it means this whlist may have an obsoleted entry.
35794 +        */
35795 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35796 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35797 +       else {
35798 +               int wkq_err;
35799 +               struct del_wh_children_args args = {
35800 +                       .errp           = &err,
35801 +                       .h_dentry       = wh_dentry,
35802 +                       .whlist         = whlist,
35803 +                       .bindex         = bindex,
35804 +                       .br             = br
35805 +               };
35806 +
35807 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35808 +               if (unlikely(wkq_err))
35809 +                       err = wkq_err;
35810 +       }
35811 +       inode_unlock(wh_inode);
35812 +
35813 +       if (!err) {
35814 +               h_tmp.dentry = wh_dentry;
35815 +               h_tmp.mnt = au_br_mnt(br);
35816 +               h_nlink = h_dir->i_nlink;
35817 +               err = vfsub_rmdir(h_dir, &h_tmp);
35818 +               /* some fs doesn't change the parent nlink in some cases */
35819 +               h_nlink -= h_dir->i_nlink;
35820 +       }
35821 +
35822 +       if (!err) {
35823 +               if (au_ibtop(dir) == bindex) {
35824 +                       /* todo: dir->i_mutex is necessary */
35825 +                       au_cpup_attr_timesizes(dir);
35826 +                       if (h_nlink)
35827 +                               vfsub_drop_nlink(dir);
35828 +               }
35829 +               return 0; /* success */
35830 +       }
35831 +
35832 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35833 +       return err;
35834 +}
35835 +
35836 +static void call_rmdir_whtmp(void *args)
35837 +{
35838 +       int err;
35839 +       aufs_bindex_t bindex;
35840 +       struct au_whtmp_rmdir *a = args;
35841 +       struct super_block *sb;
35842 +       struct dentry *h_parent;
35843 +       struct inode *h_dir;
35844 +       struct au_hinode *hdir;
35845 +
35846 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35847 +       /* inode_lock(a->dir); */
35848 +       err = -EROFS;
35849 +       sb = a->dir->i_sb;
35850 +       si_read_lock(sb, !AuLock_FLUSH);
35851 +       if (!au_br_writable(a->br->br_perm))
35852 +               goto out;
35853 +       bindex = au_br_index(sb, a->br->br_id);
35854 +       if (unlikely(bindex < 0))
35855 +               goto out;
35856 +
35857 +       err = -EIO;
35858 +       ii_write_lock_parent(a->dir);
35859 +       h_parent = dget_parent(a->wh_dentry);
35860 +       h_dir = d_inode(h_parent);
35861 +       hdir = au_hi(a->dir, bindex);
35862 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35863 +       if (unlikely(err))
35864 +               goto out_mnt;
35865 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35866 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35867 +                         a->br);
35868 +       if (!err)
35869 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35870 +       au_hn_inode_unlock(hdir);
35871 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35872 +
35873 +out_mnt:
35874 +       dput(h_parent);
35875 +       ii_write_unlock(a->dir);
35876 +out:
35877 +       /* inode_unlock(a->dir); */
35878 +       au_whtmp_rmdir_free(a);
35879 +       si_read_unlock(sb);
35880 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35881 +       if (unlikely(err))
35882 +               AuIOErr("err %d\n", err);
35883 +}
35884 +
35885 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35886 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35887 +{
35888 +       int wkq_err;
35889 +       struct super_block *sb;
35890 +
35891 +       IMustLock(dir);
35892 +
35893 +       /* all post-process will be done in do_rmdir_whtmp(). */
35894 +       sb = dir->i_sb;
35895 +       args->dir = au_igrab(dir);
35896 +       args->br = au_sbr(sb, bindex);
35897 +       au_lcnt_inc(&args->br->br_count);
35898 +       args->wh_dentry = dget(wh_dentry);
35899 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35900 +       if (unlikely(wkq_err)) {
35901 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35902 +               au_whtmp_rmdir_free(args);
35903 +       }
35904 +}
35905 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35906 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35907 +++ linux/fs/aufs/whout.h       2019-07-11 15:42:14.475571481 +0200
35908 @@ -0,0 +1,86 @@
35909 +/* SPDX-License-Identifier: GPL-2.0 */
35910 +/*
35911 + * Copyright (C) 2005-2019 Junjiro R. Okajima
35912 + *
35913 + * This program, aufs is free software; you can redistribute it and/or modify
35914 + * it under the terms of the GNU General Public License as published by
35915 + * the Free Software Foundation; either version 2 of the License, or
35916 + * (at your option) any later version.
35917 + *
35918 + * This program is distributed in the hope that it will be useful,
35919 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35920 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35921 + * GNU General Public License for more details.
35922 + *
35923 + * You should have received a copy of the GNU General Public License
35924 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35925 + */
35926 +
35927 +/*
35928 + * whiteout for logical deletion and opaque directory
35929 + */
35930 +
35931 +#ifndef __AUFS_WHOUT_H__
35932 +#define __AUFS_WHOUT_H__
35933 +
35934 +#ifdef __KERNEL__
35935 +
35936 +#include "dir.h"
35937 +
35938 +/* whout.c */
35939 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35940 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35941 +int au_diropq_test(struct dentry *h_dentry);
35942 +struct au_branch;
35943 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35944 +                            struct qstr *prefix);
35945 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35946 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35947 +                       struct dentry *dentry);
35948 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35949 +
35950 +/* diropq flags */
35951 +#define AuDiropq_CREATE        1
35952 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35953 +#define au_fset_diropq(flags, name) \
35954 +       do { (flags) |= AuDiropq_##name; } while (0)
35955 +#define au_fclr_diropq(flags, name) \
35956 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35957 +
35958 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35959 +                            unsigned int flags);
35960 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35961 +                         struct au_branch *br);
35962 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35963 +                           struct dentry *h_parent);
35964 +
35965 +/* real rmdir for the whiteout-ed dir */
35966 +struct au_whtmp_rmdir {
35967 +       struct inode *dir;
35968 +       struct au_branch *br;
35969 +       struct dentry *wh_dentry;
35970 +       struct au_nhash whlist;
35971 +};
35972 +
35973 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35974 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35975 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35976 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35977 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35978 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35979 +
35980 +/* ---------------------------------------------------------------------- */
35981 +
35982 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35983 +                                             aufs_bindex_t bindex)
35984 +{
35985 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35986 +}
35987 +
35988 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35989 +{
35990 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35991 +}
35992 +
35993 +#endif /* __KERNEL__ */
35994 +#endif /* __AUFS_WHOUT_H__ */
35995 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35996 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35997 +++ linux/fs/aufs/wkq.c 2019-07-11 15:42:14.475571481 +0200
35998 @@ -0,0 +1,392 @@
35999 +// SPDX-License-Identifier: GPL-2.0
36000 +/*
36001 + * Copyright (C) 2005-2019 Junjiro R. Okajima
36002 + *
36003 + * This program, aufs is free software; you can redistribute it and/or modify
36004 + * it under the terms of the GNU General Public License as published by
36005 + * the Free Software Foundation; either version 2 of the License, or
36006 + * (at your option) any later version.
36007 + *
36008 + * This program is distributed in the hope that it will be useful,
36009 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36010 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36011 + * GNU General Public License for more details.
36012 + *
36013 + * You should have received a copy of the GNU General Public License
36014 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36015 + */
36016 +
36017 +/*
36018 + * workqueue for asynchronous/super-io operations
36019 + * todo: try new credential scheme
36020 + */
36021 +
36022 +#include <linux/module.h>
36023 +#include "aufs.h"
36024 +
36025 +/* internal workqueue named AUFS_WKQ_NAME */
36026 +
36027 +static struct workqueue_struct *au_wkq;
36028 +
36029 +struct au_wkinfo {
36030 +       struct work_struct wk;
36031 +       struct kobject *kobj;
36032 +
36033 +       unsigned int flags; /* see wkq.h */
36034 +
36035 +       au_wkq_func_t func;
36036 +       void *args;
36037 +
36038 +#ifdef CONFIG_LOCKDEP
36039 +       int dont_check;
36040 +       struct held_lock **hlock;
36041 +#endif
36042 +
36043 +       struct completion *comp;
36044 +};
36045 +
36046 +/* ---------------------------------------------------------------------- */
36047 +/*
36048 + * Aufs passes some operations to the workqueue such as the internal copyup.
36049 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36050 + * job run by workqueue depends upon the locks acquired in the other task.
36051 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36052 + * information too. And the job in the workqueue restores the info in order to
36053 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36054 + * correctly and expectedly.
36055 + */
36056 +
36057 +#ifndef CONFIG_LOCKDEP
36058 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36059 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36060 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36061 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36062 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36063 +#else
36064 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36065 +{
36066 +       wkinfo->hlock = NULL;
36067 +       wkinfo->dont_check = 0;
36068 +}
36069 +
36070 +/*
36071 + * 1: matched
36072 + * 0: unmatched
36073 + */
36074 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36075 +{
36076 +       static DEFINE_SPINLOCK(spin);
36077 +       static struct {
36078 +               char *name;
36079 +               struct lock_class_key *key;
36080 +       } a[] = {
36081 +               { .name = "&sbinfo->si_rwsem" },
36082 +               { .name = "&finfo->fi_rwsem" },
36083 +               { .name = "&dinfo->di_rwsem" },
36084 +               { .name = "&iinfo->ii_rwsem" }
36085 +       };
36086 +       static int set;
36087 +       int i;
36088 +
36089 +       /* lockless read from 'set.' see below */
36090 +       if (set == ARRAY_SIZE(a)) {
36091 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36092 +                       if (a[i].key == key)
36093 +                               goto match;
36094 +               goto unmatch;
36095 +       }
36096 +
36097 +       spin_lock(&spin);
36098 +       if (set)
36099 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36100 +                       if (a[i].key == key) {
36101 +                               spin_unlock(&spin);
36102 +                               goto match;
36103 +                       }
36104 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36105 +               if (a[i].key) {
36106 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36107 +                               spin_unlock(&spin);
36108 +                               goto match;
36109 +                       } else
36110 +                               continue;
36111 +               }
36112 +               if (strstr(a[i].name, name)) {
36113 +                       /*
36114 +                        * the order of these three lines is important for the
36115 +                        * lockless read above.
36116 +                        */
36117 +                       a[i].key = key;
36118 +                       spin_unlock(&spin);
36119 +                       set++;
36120 +                       /* AuDbg("%d, %s\n", set, name); */
36121 +                       goto match;
36122 +               }
36123 +       }
36124 +       spin_unlock(&spin);
36125 +       goto unmatch;
36126 +
36127 +match:
36128 +       return 1;
36129 +unmatch:
36130 +       return 0;
36131 +}
36132 +
36133 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36134 +{
36135 +       int err, n;
36136 +       struct task_struct *curr;
36137 +       struct held_lock **hl, *held_locks, *p;
36138 +
36139 +       err = 0;
36140 +       curr = current;
36141 +       wkinfo->dont_check = lockdep_recursing(curr);
36142 +       if (wkinfo->dont_check)
36143 +               goto out;
36144 +       n = curr->lockdep_depth;
36145 +       if (!n)
36146 +               goto out;
36147 +
36148 +       err = -ENOMEM;
36149 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36150 +       if (unlikely(!wkinfo->hlock))
36151 +               goto out;
36152 +
36153 +       err = 0;
36154 +#if 0
36155 +       if (0 && au_debug_test()) /* left for debugging */
36156 +               lockdep_print_held_locks(curr);
36157 +#endif
36158 +       held_locks = curr->held_locks;
36159 +       hl = wkinfo->hlock;
36160 +       while (n--) {
36161 +               p = held_locks++;
36162 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36163 +                       *hl++ = p;
36164 +       }
36165 +       *hl = NULL;
36166 +
36167 +out:
36168 +       return err;
36169 +}
36170 +
36171 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36172 +{
36173 +       au_kfree_try_rcu(wkinfo->hlock);
36174 +}
36175 +
36176 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36177 +{
36178 +       struct held_lock *p, **hl = wkinfo->hlock;
36179 +       int subclass;
36180 +
36181 +       if (wkinfo->dont_check)
36182 +               lockdep_off();
36183 +       if (!hl)
36184 +               return;
36185 +       while ((p = *hl++)) { /* assignment */
36186 +               subclass = lockdep_hlock_class(p)->subclass;
36187 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36188 +               if (p->read)
36189 +                       rwsem_acquire_read(p->instance, subclass, 0,
36190 +                                          /*p->acquire_ip*/_RET_IP_);
36191 +               else
36192 +                       rwsem_acquire(p->instance, subclass, 0,
36193 +                                     /*p->acquire_ip*/_RET_IP_);
36194 +       }
36195 +}
36196 +
36197 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36198 +{
36199 +       struct held_lock *p, **hl = wkinfo->hlock;
36200 +
36201 +       if (wkinfo->dont_check)
36202 +               lockdep_on();
36203 +       if (!hl)
36204 +               return;
36205 +       while ((p = *hl++)) /* assignment */
36206 +               rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36207 +}
36208 +#endif
36209 +
36210 +static void wkq_func(struct work_struct *wk)
36211 +{
36212 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36213 +
36214 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36215 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36216 +
36217 +       au_wkq_lockdep_pre(wkinfo);
36218 +       wkinfo->func(wkinfo->args);
36219 +       au_wkq_lockdep_post(wkinfo);
36220 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36221 +               complete(wkinfo->comp);
36222 +       else {
36223 +               kobject_put(wkinfo->kobj);
36224 +               module_put(THIS_MODULE); /* todo: ?? */
36225 +               au_kfree_rcu(wkinfo);
36226 +       }
36227 +}
36228 +
36229 +/*
36230 + * Since struct completion is large, try allocating it dynamically.
36231 + */
36232 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
36233 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36234 +
36235 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36236 +{
36237 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36238 +       if (*comp) {
36239 +               init_completion(*comp);
36240 +               wkinfo->comp = *comp;
36241 +               return 0;
36242 +       }
36243 +       return -ENOMEM;
36244 +}
36245 +
36246 +static void au_wkq_comp_free(struct completion *comp)
36247 +{
36248 +       au_kfree_rcu(comp);
36249 +}
36250 +
36251 +#else
36252 +
36253 +/* no braces */
36254 +#define AuWkqCompDeclare(name) \
36255 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
36256 +       struct completion *comp = &_ ## name
36257 +
36258 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36259 +{
36260 +       wkinfo->comp = *comp;
36261 +       return 0;
36262 +}
36263 +
36264 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36265 +{
36266 +       /* empty */
36267 +}
36268 +#endif /* 4KSTACKS */
36269 +
36270 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36271 +{
36272 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36273 +               if (au_wkq_test()) {
36274 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36275 +                               " due to a dead dir by UDBA,"
36276 +                               " or async xino write?\n");
36277 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36278 +               }
36279 +       } else
36280 +               au_dbg_verify_kthread();
36281 +
36282 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36283 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36284 +               queue_work(au_wkq, &wkinfo->wk);
36285 +       } else {
36286 +               INIT_WORK(&wkinfo->wk, wkq_func);
36287 +               schedule_work(&wkinfo->wk);
36288 +       }
36289 +}
36290 +
36291 +/*
36292 + * Be careful. It is easy to make deadlock happen.
36293 + * processA: lock, wkq and wait
36294 + * processB: wkq and wait, lock in wkq
36295 + * --> deadlock
36296 + */
36297 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36298 +{
36299 +       int err;
36300 +       AuWkqCompDeclare(comp);
36301 +       struct au_wkinfo wkinfo = {
36302 +               .flags  = flags,
36303 +               .func   = func,
36304 +               .args   = args
36305 +       };
36306 +
36307 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36308 +       if (unlikely(err))
36309 +               goto out;
36310 +       err = au_wkq_lockdep_alloc(&wkinfo);
36311 +       if (unlikely(err))
36312 +               goto out_comp;
36313 +       if (!err) {
36314 +               au_wkq_run(&wkinfo);
36315 +               /* no timeout, no interrupt */
36316 +               wait_for_completion(wkinfo.comp);
36317 +       }
36318 +       au_wkq_lockdep_free(&wkinfo);
36319 +
36320 +out_comp:
36321 +       au_wkq_comp_free(comp);
36322 +out:
36323 +       destroy_work_on_stack(&wkinfo.wk);
36324 +       return err;
36325 +}
36326 +
36327 +/*
36328 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36329 + * problem in a concurrent umounting.
36330 + */
36331 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36332 +                 unsigned int flags)
36333 +{
36334 +       int err;
36335 +       struct au_wkinfo *wkinfo;
36336 +
36337 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36338 +
36339 +       /*
36340 +        * wkq_func() must free this wkinfo.
36341 +        * it highly depends upon the implementation of workqueue.
36342 +        */
36343 +       err = 0;
36344 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36345 +       if (wkinfo) {
36346 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36347 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36348 +               wkinfo->func = func;
36349 +               wkinfo->args = args;
36350 +               wkinfo->comp = NULL;
36351 +               au_wkq_lockdep_init(wkinfo);
36352 +               kobject_get(wkinfo->kobj);
36353 +               __module_get(THIS_MODULE); /* todo: ?? */
36354 +
36355 +               au_wkq_run(wkinfo);
36356 +       } else {
36357 +               err = -ENOMEM;
36358 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36359 +       }
36360 +
36361 +       return err;
36362 +}
36363 +
36364 +/* ---------------------------------------------------------------------- */
36365 +
36366 +void au_nwt_init(struct au_nowait_tasks *nwt)
36367 +{
36368 +       atomic_set(&nwt->nw_len, 0);
36369 +       /* smp_mb(); */ /* atomic_set */
36370 +       init_waitqueue_head(&nwt->nw_wq);
36371 +}
36372 +
36373 +void au_wkq_fin(void)
36374 +{
36375 +       destroy_workqueue(au_wkq);
36376 +}
36377 +
36378 +int __init au_wkq_init(void)
36379 +{
36380 +       int err;
36381 +
36382 +       err = 0;
36383 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36384 +       if (IS_ERR(au_wkq))
36385 +               err = PTR_ERR(au_wkq);
36386 +       else if (!au_wkq)
36387 +               err = -ENOMEM;
36388 +
36389 +       return err;
36390 +}
36391 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36392 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36393 +++ linux/fs/aufs/wkq.h 2019-07-11 15:42:14.475571481 +0200
36394 @@ -0,0 +1,89 @@
36395 +/* SPDX-License-Identifier: GPL-2.0 */
36396 +/*
36397 + * Copyright (C) 2005-2019 Junjiro R. Okajima
36398 + *
36399 + * This program, aufs is free software; you can redistribute it and/or modify
36400 + * it under the terms of the GNU General Public License as published by
36401 + * the Free Software Foundation; either version 2 of the License, or
36402 + * (at your option) any later version.
36403 + *
36404 + * This program is distributed in the hope that it will be useful,
36405 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36406 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36407 + * GNU General Public License for more details.
36408 + *
36409 + * You should have received a copy of the GNU General Public License
36410 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36411 + */
36412 +
36413 +/*
36414 + * workqueue for asynchronous/super-io operations
36415 + * todo: try new credentials management scheme
36416 + */
36417 +
36418 +#ifndef __AUFS_WKQ_H__
36419 +#define __AUFS_WKQ_H__
36420 +
36421 +#ifdef __KERNEL__
36422 +
36423 +#include <linux/wait.h>
36424 +
36425 +struct super_block;
36426 +
36427 +/* ---------------------------------------------------------------------- */
36428 +
36429 +/*
36430 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36431 + */
36432 +struct au_nowait_tasks {
36433 +       atomic_t                nw_len;
36434 +       wait_queue_head_t       nw_wq;
36435 +};
36436 +
36437 +/* ---------------------------------------------------------------------- */
36438 +
36439 +typedef void (*au_wkq_func_t)(void *args);
36440 +
36441 +/* wkq flags */
36442 +#define AuWkq_WAIT     1
36443 +#define AuWkq_NEST     (1 << 1)
36444 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36445 +#define au_fset_wkq(flags, name) \
36446 +       do { (flags) |= AuWkq_##name; } while (0)
36447 +#define au_fclr_wkq(flags, name) \
36448 +       do { (flags) &= ~AuWkq_##name; } while (0)
36449 +
36450 +/* wkq.c */
36451 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36452 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36453 +                 unsigned int flags);
36454 +void au_nwt_init(struct au_nowait_tasks *nwt);
36455 +int __init au_wkq_init(void);
36456 +void au_wkq_fin(void);
36457 +
36458 +/* ---------------------------------------------------------------------- */
36459 +
36460 +static inline int au_wkq_test(void)
36461 +{
36462 +       return current->flags & PF_WQ_WORKER;
36463 +}
36464 +
36465 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36466 +{
36467 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36468 +}
36469 +
36470 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36471 +{
36472 +       if (atomic_dec_and_test(&nwt->nw_len))
36473 +               wake_up_all(&nwt->nw_wq);
36474 +}
36475 +
36476 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36477 +{
36478 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36479 +       return 0;
36480 +}
36481 +
36482 +#endif /* __KERNEL__ */
36483 +#endif /* __AUFS_WKQ_H__ */
36484 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36485 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36486 +++ linux/fs/aufs/xattr.c       2019-07-11 15:42:14.475571481 +0200
36487 @@ -0,0 +1,356 @@
36488 +// SPDX-License-Identifier: GPL-2.0
36489 +/*
36490 + * Copyright (C) 2014-2019 Junjiro R. Okajima
36491 + *
36492 + * This program, aufs is free software; you can redistribute it and/or modify
36493 + * it under the terms of the GNU General Public License as published by
36494 + * the Free Software Foundation; either version 2 of the License, or
36495 + * (at your option) any later version.
36496 + *
36497 + * This program is distributed in the hope that it will be useful,
36498 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36499 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36500 + * GNU General Public License for more details.
36501 + *
36502 + * You should have received a copy of the GNU General Public License
36503 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36504 + */
36505 +
36506 +/*
36507 + * handling xattr functions
36508 + */
36509 +
36510 +#include <linux/fs.h>
36511 +#include <linux/posix_acl_xattr.h>
36512 +#include <linux/xattr.h>
36513 +#include "aufs.h"
36514 +
36515 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36516 +{
36517 +       if (!ignore_flags)
36518 +               goto out;
36519 +       switch (err) {
36520 +       case -ENOMEM:
36521 +       case -EDQUOT:
36522 +               goto out;
36523 +       }
36524 +
36525 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36526 +               err = 0;
36527 +               goto out;
36528 +       }
36529 +
36530 +#define cmp(brattr, prefix) do {                                       \
36531 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36532 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36533 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36534 +                               err = 0;                                \
36535 +                       goto out;                                       \
36536 +               }                                                       \
36537 +       } while (0)
36538 +
36539 +       cmp(SEC, SECURITY);
36540 +       cmp(SYS, SYSTEM);
36541 +       cmp(TR, TRUSTED);
36542 +       cmp(USR, USER);
36543 +#undef cmp
36544 +
36545 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36546 +               err = 0;
36547 +
36548 +out:
36549 +       return err;
36550 +}
36551 +
36552 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36553 +
36554 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36555 +                           char *name, char **buf, unsigned int ignore_flags,
36556 +                           unsigned int verbose)
36557 +{
36558 +       int err;
36559 +       ssize_t ssz;
36560 +       struct inode *h_idst;
36561 +
36562 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36563 +       err = ssz;
36564 +       if (unlikely(err <= 0)) {
36565 +               if (err == -ENODATA
36566 +                   || (err == -EOPNOTSUPP
36567 +                       && ((ignore_flags & au_xattr_out_of_list)
36568 +                           || (au_test_nfs_noacl(d_inode(h_src))
36569 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36570 +                                   || !strcmp(name,
36571 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36572 +                           ))
36573 +                       err = 0;
36574 +               if (err && (verbose || au_debug_test()))
36575 +                       pr_err("%s, err %d\n", name, err);
36576 +               goto out;
36577 +       }
36578 +
36579 +       /* unlock it temporary */
36580 +       h_idst = d_inode(h_dst);
36581 +       inode_unlock(h_idst);
36582 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36583 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36584 +       if (unlikely(err)) {
36585 +               if (verbose || au_debug_test())
36586 +                       pr_err("%s, err %d\n", name, err);
36587 +               err = au_xattr_ignore(err, name, ignore_flags);
36588 +       }
36589 +
36590 +out:
36591 +       return err;
36592 +}
36593 +
36594 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36595 +                 unsigned int verbose)
36596 +{
36597 +       int err, unlocked, acl_access, acl_default;
36598 +       ssize_t ssz;
36599 +       struct inode *h_isrc, *h_idst;
36600 +       char *value, *p, *o, *e;
36601 +
36602 +       /* try stopping to update the source inode while we are referencing */
36603 +       /* there should not be the parent-child relationship between them */
36604 +       h_isrc = d_inode(h_src);
36605 +       h_idst = d_inode(h_dst);
36606 +       inode_unlock(h_idst);
36607 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36608 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36609 +       unlocked = 0;
36610 +
36611 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36612 +       ssz = vfs_listxattr(h_src, NULL, 0);
36613 +       err = ssz;
36614 +       if (unlikely(err < 0)) {
36615 +               AuTraceErr(err);
36616 +               if (err == -ENODATA
36617 +                   || err == -EOPNOTSUPP)
36618 +                       err = 0;        /* ignore */
36619 +               goto out;
36620 +       }
36621 +
36622 +       err = 0;
36623 +       p = NULL;
36624 +       o = NULL;
36625 +       if (ssz) {
36626 +               err = -ENOMEM;
36627 +               p = kmalloc(ssz, GFP_NOFS);
36628 +               o = p;
36629 +               if (unlikely(!p))
36630 +                       goto out;
36631 +               err = vfs_listxattr(h_src, p, ssz);
36632 +       }
36633 +       inode_unlock_shared(h_isrc);
36634 +       unlocked = 1;
36635 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36636 +       if (unlikely(err < 0))
36637 +               goto out_free;
36638 +
36639 +       err = 0;
36640 +       e = p + ssz;
36641 +       value = NULL;
36642 +       acl_access = 0;
36643 +       acl_default = 0;
36644 +       while (!err && p < e) {
36645 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36646 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36647 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36648 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36649 +                                       - 1);
36650 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36651 +                                      verbose);
36652 +               p += strlen(p) + 1;
36653 +       }
36654 +       AuTraceErr(err);
36655 +       ignore_flags |= au_xattr_out_of_list;
36656 +       if (!err && !acl_access) {
36657 +               err = au_do_cpup_xattr(h_dst, h_src,
36658 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36659 +                                      ignore_flags, verbose);
36660 +               AuTraceErr(err);
36661 +       }
36662 +       if (!err && !acl_default) {
36663 +               err = au_do_cpup_xattr(h_dst, h_src,
36664 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36665 +                                      ignore_flags, verbose);
36666 +               AuTraceErr(err);
36667 +       }
36668 +
36669 +       au_kfree_try_rcu(value);
36670 +
36671 +out_free:
36672 +       au_kfree_try_rcu(o);
36673 +out:
36674 +       if (!unlocked)
36675 +               inode_unlock_shared(h_isrc);
36676 +       AuTraceErr(err);
36677 +       return err;
36678 +}
36679 +
36680 +/* ---------------------------------------------------------------------- */
36681 +
36682 +static int au_smack_reentering(struct super_block *sb)
36683 +{
36684 +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36685 +       /*
36686 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36687 +        * i_op->getxattr(). ouch.
36688 +        */
36689 +       return si_pid_test(sb);
36690 +#else
36691 +       return 0;
36692 +#endif
36693 +}
36694 +
36695 +enum {
36696 +       AU_XATTR_LIST,
36697 +       AU_XATTR_GET
36698 +};
36699 +
36700 +struct au_lgxattr {
36701 +       int type;
36702 +       union {
36703 +               struct {
36704 +                       char    *list;
36705 +                       size_t  size;
36706 +               } list;
36707 +               struct {
36708 +                       const char      *name;
36709 +                       void            *value;
36710 +                       size_t          size;
36711 +               } get;
36712 +       } u;
36713 +};
36714 +
36715 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36716 +{
36717 +       ssize_t err;
36718 +       int reenter;
36719 +       struct path h_path;
36720 +       struct super_block *sb;
36721 +
36722 +       sb = dentry->d_sb;
36723 +       reenter = au_smack_reentering(sb);
36724 +       if (!reenter) {
36725 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36726 +               if (unlikely(err))
36727 +                       goto out;
36728 +       }
36729 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
36730 +       if (unlikely(err))
36731 +               goto out_si;
36732 +       if (unlikely(!h_path.dentry))
36733 +               /* illegally overlapped or something */
36734 +               goto out_di; /* pretending success */
36735 +
36736 +       /* always topmost entry only */
36737 +       switch (arg->type) {
36738 +       case AU_XATTR_LIST:
36739 +               err = vfs_listxattr(h_path.dentry,
36740 +                                   arg->u.list.list, arg->u.list.size);
36741 +               break;
36742 +       case AU_XATTR_GET:
36743 +               AuDebugOn(d_is_negative(h_path.dentry));
36744 +               err = vfs_getxattr(h_path.dentry,
36745 +                                  arg->u.get.name, arg->u.get.value,
36746 +                                  arg->u.get.size);
36747 +               break;
36748 +       }
36749 +
36750 +out_di:
36751 +       if (!reenter)
36752 +               di_read_unlock(dentry, AuLock_IR);
36753 +out_si:
36754 +       if (!reenter)
36755 +               si_read_unlock(sb);
36756 +out:
36757 +       AuTraceErr(err);
36758 +       return err;
36759 +}
36760 +
36761 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36762 +{
36763 +       struct au_lgxattr arg = {
36764 +               .type = AU_XATTR_LIST,
36765 +               .u.list = {
36766 +                       .list   = list,
36767 +                       .size   = size
36768 +               },
36769 +       };
36770 +
36771 +       return au_lgxattr(dentry, &arg);
36772 +}
36773 +
36774 +static ssize_t au_getxattr(struct dentry *dentry,
36775 +                          struct inode *inode __maybe_unused,
36776 +                          const char *name, void *value, size_t size)
36777 +{
36778 +       struct au_lgxattr arg = {
36779 +               .type = AU_XATTR_GET,
36780 +               .u.get = {
36781 +                       .name   = name,
36782 +                       .value  = value,
36783 +                       .size   = size
36784 +               },
36785 +       };
36786 +
36787 +       return au_lgxattr(dentry, &arg);
36788 +}
36789 +
36790 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36791 +                      const char *name, const void *value, size_t size,
36792 +                      int flags)
36793 +{
36794 +       struct au_sxattr arg = {
36795 +               .type = AU_XATTR_SET,
36796 +               .u.set = {
36797 +                       .name   = name,
36798 +                       .value  = value,
36799 +                       .size   = size,
36800 +                       .flags  = flags
36801 +               },
36802 +       };
36803 +
36804 +       return au_sxattr(dentry, inode, &arg);
36805 +}
36806 +
36807 +/* ---------------------------------------------------------------------- */
36808 +
36809 +static int au_xattr_get(const struct xattr_handler *handler,
36810 +                       struct dentry *dentry, struct inode *inode,
36811 +                       const char *name, void *buffer, size_t size)
36812 +{
36813 +       return au_getxattr(dentry, inode, name, buffer, size);
36814 +}
36815 +
36816 +static int au_xattr_set(const struct xattr_handler *handler,
36817 +                       struct dentry *dentry, struct inode *inode,
36818 +                       const char *name, const void *value, size_t size,
36819 +                       int flags)
36820 +{
36821 +       return au_setxattr(dentry, inode, name, value, size, flags);
36822 +}
36823 +
36824 +static const struct xattr_handler au_xattr_handler = {
36825 +       .name   = "",
36826 +       .prefix = "",
36827 +       .get    = au_xattr_get,
36828 +       .set    = au_xattr_set
36829 +};
36830 +
36831 +static const struct xattr_handler *au_xattr_handlers[] = {
36832 +#ifdef CONFIG_FS_POSIX_ACL
36833 +       &posix_acl_access_xattr_handler,
36834 +       &posix_acl_default_xattr_handler,
36835 +#endif
36836 +       &au_xattr_handler, /* must be last */
36837 +       NULL
36838 +};
36839 +
36840 +void au_xattr_init(struct super_block *sb)
36841 +{
36842 +       sb->s_xattr = au_xattr_handlers;
36843 +}
36844 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36845 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36846 +++ linux/fs/aufs/xino.c        2019-07-11 15:42:14.475571481 +0200
36847 @@ -0,0 +1,1965 @@
36848 +// SPDX-License-Identifier: GPL-2.0
36849 +/*
36850 + * Copyright (C) 2005-2019 Junjiro R. Okajima
36851 + *
36852 + * This program, aufs is free software; you can redistribute it and/or modify
36853 + * it under the terms of the GNU General Public License as published by
36854 + * the Free Software Foundation; either version 2 of the License, or
36855 + * (at your option) any later version.
36856 + *
36857 + * This program is distributed in the hope that it will be useful,
36858 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36859 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36860 + * GNU General Public License for more details.
36861 + *
36862 + * You should have received a copy of the GNU General Public License
36863 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36864 + */
36865 +
36866 +/*
36867 + * external inode number translation table and bitmap
36868 + *
36869 + * things to consider
36870 + * - the lifetime
36871 + *   + au_xino object
36872 + *   + XINO files (xino, xib, xigen)
36873 + *   + dynamic debugfs entries (xiN)
36874 + *   + static debugfs entries (xib, xigen)
36875 + *   + static sysfs entry (xi_path)
36876 + * - several entry points to handle them.
36877 + *   + mount(2) without xino option (default)
36878 + *   + mount(2) with xino option
36879 + *   + mount(2) with noxino option
36880 + *   + umount(2)
36881 + *   + remount with add/del branches
36882 + *   + remount with xino/noxino options
36883 + */
36884 +
36885 +#include <linux/seq_file.h>
36886 +#include <linux/statfs.h>
36887 +#include "aufs.h"
36888 +
36889 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36890 +                                    aufs_bindex_t bbot,
36891 +                                    struct super_block *h_sb)
36892 +{
36893 +       /* todo: try binary-search if the branches are many */
36894 +       for (; btop <= bbot; btop++)
36895 +               if (h_sb == au_sbr_sb(sb, btop))
36896 +                       return btop;
36897 +       return -1;
36898 +}
36899 +
36900 +/*
36901 + * find another branch who is on the same filesystem of the specified
36902 + * branch{@btgt}. search until @bbot.
36903 + */
36904 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36905 +                                 aufs_bindex_t bbot)
36906 +{
36907 +       aufs_bindex_t bindex;
36908 +       struct super_block *tgt_sb;
36909 +
36910 +       tgt_sb = au_sbr_sb(sb, btgt);
36911 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36912 +       if (bindex < 0)
36913 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36914 +
36915 +       return bindex;
36916 +}
36917 +
36918 +/* ---------------------------------------------------------------------- */
36919 +
36920 +/*
36921 + * stop unnecessary notify events at creating xino files
36922 + */
36923 +
36924 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36925 +{
36926 +       aufs_bindex_t bfound, bindex, bbot;
36927 +       struct dentry *parent;
36928 +       struct au_branch *br;
36929 +
36930 +       bfound = -1;
36931 +       parent = dentry->d_parent; /* safe d_parent access */
36932 +       bbot = au_sbbot(sb);
36933 +       for (bindex = 0; bindex <= bbot; bindex++) {
36934 +               br = au_sbr(sb, bindex);
36935 +               if (au_br_dentry(br) == parent) {
36936 +                       bfound = bindex;
36937 +                       break;
36938 +               }
36939 +       }
36940 +
36941 +       AuDbg("bfound b%d\n", bfound);
36942 +       return bfound;
36943 +}
36944 +
36945 +struct au_xino_lock_dir {
36946 +       struct au_hinode *hdir;
36947 +       struct dentry *parent;
36948 +       struct inode *dir;
36949 +};
36950 +
36951 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36952 +                                         unsigned int lsc)
36953 +{
36954 +       struct dentry *parent;
36955 +       struct inode *dir;
36956 +
36957 +       parent = dget_parent(dentry);
36958 +       dir = d_inode(parent);
36959 +       inode_lock_nested(dir, lsc);
36960 +#if 0 /* it should not happen */
36961 +       spin_lock(&dentry->d_lock);
36962 +       if (unlikely(dentry->d_parent != parent)) {
36963 +               spin_unlock(&dentry->d_lock);
36964 +               inode_unlock(dir);
36965 +               dput(parent);
36966 +               parent = NULL;
36967 +               goto out;
36968 +       }
36969 +       spin_unlock(&dentry->d_lock);
36970 +
36971 +out:
36972 +#endif
36973 +       return parent;
36974 +}
36975 +
36976 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36977 +                            struct au_xino_lock_dir *ldir)
36978 +{
36979 +       aufs_bindex_t bindex;
36980 +
36981 +       ldir->hdir = NULL;
36982 +       bindex = au_xi_root(sb, xipath->dentry);
36983 +       if (bindex >= 0) {
36984 +               /* rw branch root */
36985 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36986 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36987 +       } else {
36988 +               /* other */
36989 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36990 +                                                  AuLsc_I_PARENT);
36991 +               ldir->dir = d_inode(ldir->parent);
36992 +       }
36993 +}
36994 +
36995 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36996 +{
36997 +       if (ldir->hdir)
36998 +               au_hn_inode_unlock(ldir->hdir);
36999 +       else {
37000 +               inode_unlock(ldir->dir);
37001 +               dput(ldir->parent);
37002 +       }
37003 +}
37004 +
37005 +/* ---------------------------------------------------------------------- */
37006 +
37007 +/*
37008 + * create and set a new xino file
37009 + */
37010 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37011 +                           int wbrtop)
37012 +{
37013 +       struct file *file;
37014 +       struct dentry *h_parent, *d;
37015 +       struct inode *h_dir, *inode;
37016 +       int err;
37017 +       static DEFINE_MUTEX(mtx);
37018 +
37019 +       /*
37020 +        * at mount-time, and the xino file is the default path,
37021 +        * hnotify is disabled so we have no notify events to ignore.
37022 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37023 +        */
37024 +       if (!wbrtop)
37025 +               mutex_lock(&mtx);
37026 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37027 +                              /* | __FMODE_NONOTIFY */,
37028 +                              0666);
37029 +       if (IS_ERR(file)) {
37030 +               if (!wbrtop)
37031 +                       mutex_unlock(&mtx);
37032 +               if (!silent)
37033 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37034 +               return file;
37035 +       }
37036 +
37037 +       /* keep file count */
37038 +       err = 0;
37039 +       d = file->f_path.dentry;
37040 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37041 +       if (!wbrtop)
37042 +               mutex_unlock(&mtx);
37043 +       /* mnt_want_write() is unnecessary here */
37044 +       h_dir = d_inode(h_parent);
37045 +       inode = file_inode(file);
37046 +       /* no delegation since it is just created */
37047 +       if (inode->i_nlink)
37048 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37049 +                                  /*force*/0);
37050 +       inode_unlock(h_dir);
37051 +       dput(h_parent);
37052 +       if (unlikely(err)) {
37053 +               if (!silent)
37054 +                       pr_err("unlink %s(%d)\n", fpath, err);
37055 +               goto out;
37056 +       }
37057 +
37058 +       err = -EINVAL;
37059 +       if (unlikely(sb == d->d_sb)) {
37060 +               if (!silent)
37061 +                       pr_err("%s must be outside\n", fpath);
37062 +               goto out;
37063 +       }
37064 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37065 +               if (!silent)
37066 +                       pr_err("xino doesn't support %s(%s)\n",
37067 +                              fpath, au_sbtype(d->d_sb));
37068 +               goto out;
37069 +       }
37070 +       return file; /* success */
37071 +
37072 +out:
37073 +       fput(file);
37074 +       file = ERR_PTR(err);
37075 +       return file;
37076 +}
37077 +
37078 +/*
37079 + * create a new xinofile at the same place/path as @base.
37080 + */
37081 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37082 +                            struct file *copy_src)
37083 +{
37084 +       struct file *file;
37085 +       struct dentry *dentry, *parent;
37086 +       struct inode *dir, *delegated;
37087 +       struct qstr *name;
37088 +       struct path path;
37089 +       int err, do_unlock;
37090 +       struct au_xino_lock_dir ldir;
37091 +
37092 +       do_unlock = 1;
37093 +       au_xino_lock_dir(sb, base, &ldir);
37094 +       dentry = base->dentry;
37095 +       parent = dentry->d_parent; /* dir inode is locked */
37096 +       dir = d_inode(parent);
37097 +       IMustLock(dir);
37098 +
37099 +       name = &dentry->d_name;
37100 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37101 +       if (IS_ERR(path.dentry)) {
37102 +               file = (void *)path.dentry;
37103 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37104 +               goto out;
37105 +       }
37106 +
37107 +       /* no need to mnt_want_write() since we call dentry_open() later */
37108 +       err = vfs_create(dir, path.dentry, 0666, NULL);
37109 +       if (unlikely(err)) {
37110 +               file = ERR_PTR(err);
37111 +               pr_err("%pd create err %d\n", dentry, err);
37112 +               goto out_dput;
37113 +       }
37114 +
37115 +       path.mnt = base->mnt;
37116 +       file = vfsub_dentry_open(&path,
37117 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37118 +                                /* | __FMODE_NONOTIFY */);
37119 +       if (IS_ERR(file)) {
37120 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37121 +               goto out_dput;
37122 +       }
37123 +
37124 +       delegated = NULL;
37125 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37126 +       au_xino_unlock_dir(&ldir);
37127 +       do_unlock = 0;
37128 +       if (unlikely(err == -EWOULDBLOCK)) {
37129 +               pr_warn("cannot retry for NFSv4 delegation"
37130 +                       " for an internal unlink\n");
37131 +               iput(delegated);
37132 +       }
37133 +       if (unlikely(err)) {
37134 +               pr_err("%pd unlink err %d\n", dentry, err);
37135 +               goto out_fput;
37136 +       }
37137 +
37138 +       if (copy_src) {
37139 +               /* no one can touch copy_src xino */
37140 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37141 +               if (unlikely(err)) {
37142 +                       pr_err("%pd copy err %d\n", dentry, err);
37143 +                       goto out_fput;
37144 +               }
37145 +       }
37146 +       goto out_dput; /* success */
37147 +
37148 +out_fput:
37149 +       fput(file);
37150 +       file = ERR_PTR(err);
37151 +out_dput:
37152 +       dput(path.dentry);
37153 +out:
37154 +       if (do_unlock)
37155 +               au_xino_unlock_dir(&ldir);
37156 +       return file;
37157 +}
37158 +
37159 +struct file *au_xino_file1(struct au_xino *xi)
37160 +{
37161 +       struct file *file;
37162 +       unsigned int u, nfile;
37163 +
37164 +       file = NULL;
37165 +       nfile = xi->xi_nfile;
37166 +       for (u = 0; u < nfile; u++) {
37167 +               file = xi->xi_file[u];
37168 +               if (file)
37169 +                       break;
37170 +       }
37171 +
37172 +       return file;
37173 +}
37174 +
37175 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37176 +{
37177 +       int err;
37178 +       struct file *f;
37179 +       void *p;
37180 +
37181 +       if (file)
37182 +               get_file(file);
37183 +
37184 +       err = 0;
37185 +       f = NULL;
37186 +       if (idx < xi->xi_nfile) {
37187 +               f = xi->xi_file[idx];
37188 +               if (f)
37189 +                       fput(f);
37190 +       } else {
37191 +               p = au_kzrealloc(xi->xi_file,
37192 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37193 +                                sizeof(*xi->xi_file) * (idx + 1),
37194 +                                GFP_NOFS, /*may_shrink*/0);
37195 +               if (p) {
37196 +                       MtxMustLock(&xi->xi_mtx);
37197 +                       xi->xi_file = p;
37198 +                       xi->xi_nfile = idx + 1;
37199 +               } else {
37200 +                       err = -ENOMEM;
37201 +                       if (file)
37202 +                               fput(file);
37203 +                       goto out;
37204 +               }
37205 +       }
37206 +       xi->xi_file[idx] = file;
37207 +
37208 +out:
37209 +       return err;
37210 +}
37211 +
37212 +/*
37213 + * if @xinew->xi is not set, then create new xigen file.
37214 + */
37215 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37216 +{
37217 +       struct file *file;
37218 +       int err;
37219 +
37220 +       SiMustAnyLock(sb);
37221 +
37222 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37223 +       if (IS_ERR(file)) {
37224 +               err = PTR_ERR(file);
37225 +               pr_err("%s[%d], err %d\n",
37226 +                      xinew->xi ? "xino" : "xigen",
37227 +                      xinew->idx, err);
37228 +               goto out;
37229 +       }
37230 +
37231 +       if (xinew->xi)
37232 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37233 +       else {
37234 +               BUG();
37235 +               /* todo: make xigen file an array */
37236 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37237 +       }
37238 +       fput(file);
37239 +       if (unlikely(err))
37240 +               file = ERR_PTR(err);
37241 +
37242 +out:
37243 +       return file;
37244 +}
37245 +
37246 +/* ---------------------------------------------------------------------- */
37247 +
37248 +/*
37249 + * truncate xino files
37250 + */
37251 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37252 +                           int idx, struct kstatfs *st)
37253 +{
37254 +       int err;
37255 +       blkcnt_t blocks;
37256 +       struct file *file, *new_xino;
37257 +       struct au_xi_new xinew = {
37258 +               .idx = idx
37259 +       };
37260 +
37261 +       err = 0;
37262 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37263 +       file = au_xino_file(xinew.xi, idx);
37264 +       if (!file)
37265 +               goto out;
37266 +
37267 +       xinew.base = &file->f_path;
37268 +       err = vfs_statfs(xinew.base, st);
37269 +       if (unlikely(err)) {
37270 +               AuErr1("statfs err %d, ignored\n", err);
37271 +               err = 0;
37272 +               goto out;
37273 +       }
37274 +
37275 +       blocks = file_inode(file)->i_blocks;
37276 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37277 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37278 +
37279 +       xinew.copy_src = file;
37280 +       new_xino = au_xi_new(sb, &xinew);
37281 +       if (IS_ERR(new_xino)) {
37282 +               err = PTR_ERR(new_xino);
37283 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37284 +               goto out;
37285 +       }
37286 +
37287 +       err = vfs_statfs(&new_xino->f_path, st);
37288 +       if (!err)
37289 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37290 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37291 +                       st->f_bfree, st->f_blocks);
37292 +       else {
37293 +               AuErr1("statfs err %d, ignored\n", err);
37294 +               err = 0;
37295 +       }
37296 +
37297 +out:
37298 +       return err;
37299 +}
37300 +
37301 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37302 +{
37303 +       int err, i;
37304 +       unsigned long jiffy;
37305 +       aufs_bindex_t bbot;
37306 +       struct kstatfs *st;
37307 +       struct au_branch *br;
37308 +       struct au_xino *xi;
37309 +
37310 +       err = -ENOMEM;
37311 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37312 +       if (unlikely(!st))
37313 +               goto out;
37314 +
37315 +       err = -EINVAL;
37316 +       bbot = au_sbbot(sb);
37317 +       if (unlikely(bindex < 0 || bbot < bindex))
37318 +               goto out_st;
37319 +
37320 +       err = 0;
37321 +       jiffy = jiffies;
37322 +       br = au_sbr(sb, bindex);
37323 +       xi = br->br_xino;
37324 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37325 +               err = au_xino_do_trunc(sb, bindex, i, st);
37326 +       if (!err)
37327 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37328 +
37329 +out_st:
37330 +       au_kfree_rcu(st);
37331 +out:
37332 +       return err;
37333 +}
37334 +
37335 +struct xino_do_trunc_args {
37336 +       struct super_block *sb;
37337 +       struct au_branch *br;
37338 +       int idx;
37339 +};
37340 +
37341 +static void xino_do_trunc(void *_args)
37342 +{
37343 +       struct xino_do_trunc_args *args = _args;
37344 +       struct super_block *sb;
37345 +       struct au_branch *br;
37346 +       struct inode *dir;
37347 +       int err, idx;
37348 +       aufs_bindex_t bindex;
37349 +
37350 +       err = 0;
37351 +       sb = args->sb;
37352 +       dir = d_inode(sb->s_root);
37353 +       br = args->br;
37354 +       idx = args->idx;
37355 +
37356 +       si_noflush_write_lock(sb);
37357 +       ii_read_lock_parent(dir);
37358 +       bindex = au_br_index(sb, br->br_id);
37359 +       err = au_xino_trunc(sb, bindex, idx);
37360 +       ii_read_unlock(dir);
37361 +       if (unlikely(err))
37362 +               pr_warn("err b%d, (%d)\n", bindex, err);
37363 +       atomic_dec(&br->br_xino->xi_truncating);
37364 +       au_lcnt_dec(&br->br_count);
37365 +       si_write_unlock(sb);
37366 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37367 +       au_kfree_rcu(args);
37368 +}
37369 +
37370 +/*
37371 + * returns the index in the xi_file array whose corresponding file is necessary
37372 + * to truncate, or -1 which means no need to truncate.
37373 + */
37374 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37375 +{
37376 +       int err;
37377 +       unsigned int u;
37378 +       struct kstatfs st;
37379 +       struct au_sbinfo *sbinfo;
37380 +       struct au_xino *xi;
37381 +       struct file *file;
37382 +
37383 +       /* todo: si_xino_expire and the ratio should be customizable */
37384 +       sbinfo = au_sbi(sb);
37385 +       if (time_before(jiffies,
37386 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37387 +               return -1;
37388 +
37389 +       /* truncation border */
37390 +       xi = br->br_xino;
37391 +       for (u = 0; u < xi->xi_nfile; u++) {
37392 +               file = au_xino_file(xi, u);
37393 +               if (!file)
37394 +                       continue;
37395 +
37396 +               err = vfs_statfs(&file->f_path, &st);
37397 +               if (unlikely(err)) {
37398 +                       AuErr1("statfs err %d, ignored\n", err);
37399 +                       return -1;
37400 +               }
37401 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37402 +                   >= AUFS_XINO_DEF_TRUNC)
37403 +                       return u;
37404 +       }
37405 +
37406 +       return -1;
37407 +}
37408 +
37409 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37410 +{
37411 +       int idx;
37412 +       struct xino_do_trunc_args *args;
37413 +       int wkq_err;
37414 +
37415 +       idx = xino_trunc_test(sb, br);
37416 +       if (idx < 0)
37417 +               return;
37418 +
37419 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37420 +               goto out;
37421 +
37422 +       /* lock and kfree() will be called in trunc_xino() */
37423 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37424 +       if (unlikely(!args)) {
37425 +               AuErr1("no memory\n");
37426 +               goto out;
37427 +       }
37428 +
37429 +       au_lcnt_inc(&br->br_count);
37430 +       args->sb = sb;
37431 +       args->br = br;
37432 +       args->idx = idx;
37433 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37434 +       if (!wkq_err)
37435 +               return; /* success */
37436 +
37437 +       pr_err("wkq %d\n", wkq_err);
37438 +       au_lcnt_dec(&br->br_count);
37439 +       au_kfree_rcu(args);
37440 +
37441 +out:
37442 +       atomic_dec(&br->br_xino->xi_truncating);
37443 +}
37444 +
37445 +/* ---------------------------------------------------------------------- */
37446 +
37447 +struct au_xi_calc {
37448 +       int idx;
37449 +       loff_t pos;
37450 +};
37451 +
37452 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37453 +                      struct au_xi_calc *calc)
37454 +{
37455 +       loff_t maxent;
37456 +
37457 +       maxent = au_xi_maxent(sb);
37458 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37459 +       calc->pos *= sizeof(ino_t);
37460 +}
37461 +
37462 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37463 +                               struct au_xi_calc *calc)
37464 +{
37465 +       int err;
37466 +       struct file *file;
37467 +       struct au_xino *xi = br->br_xino;
37468 +       struct au_xi_new xinew = {
37469 +               .xi = xi
37470 +       };
37471 +
37472 +       SiMustAnyLock(sb);
37473 +
37474 +       err = 0;
37475 +       if (!xi)
37476 +               goto out;
37477 +
37478 +       mutex_lock(&xi->xi_mtx);
37479 +       file = au_xino_file(xi, calc->idx);
37480 +       if (file)
37481 +               goto out_mtx;
37482 +
37483 +       file = au_xino_file(xi, /*idx*/-1);
37484 +       AuDebugOn(!file);
37485 +       xinew.idx = calc->idx;
37486 +       xinew.base = &file->f_path;
37487 +       /* xinew.copy_src = NULL; */
37488 +       file = au_xi_new(sb, &xinew);
37489 +       if (IS_ERR(file))
37490 +               err = PTR_ERR(file);
37491 +
37492 +out_mtx:
37493 +       mutex_unlock(&xi->xi_mtx);
37494 +out:
37495 +       return err;
37496 +}
37497 +
37498 +struct au_xino_do_new_async_args {
37499 +       struct super_block *sb;
37500 +       struct au_branch *br;
37501 +       struct au_xi_calc calc;
37502 +       ino_t ino;
37503 +};
37504 +
37505 +struct au_xi_writing {
37506 +       struct hlist_bl_node node;
37507 +       ino_t h_ino, ino;
37508 +};
37509 +
37510 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37511 +                           struct au_xi_calc *calc, ino_t ino);
37512 +
37513 +static void au_xino_call_do_new_async(void *args)
37514 +{
37515 +       struct au_xino_do_new_async_args *a = args;
37516 +       struct au_branch *br;
37517 +       struct super_block *sb;
37518 +       struct au_sbinfo *sbi;
37519 +       struct inode *root;
37520 +       struct file *file;
37521 +       struct au_xi_writing *del, *p;
37522 +       struct hlist_bl_head *hbl;
37523 +       struct hlist_bl_node *pos;
37524 +       int err;
37525 +
37526 +       br = a->br;
37527 +       sb = a->sb;
37528 +       sbi = au_sbi(sb);
37529 +       si_noflush_read_lock(sb);
37530 +       root = d_inode(sb->s_root);
37531 +       ii_read_lock_child(root);
37532 +       err = au_xino_do_new_async(sb, br, &a->calc);
37533 +       if (unlikely(err)) {
37534 +               AuIOErr("err %d\n", err);
37535 +               goto out;
37536 +       }
37537 +
37538 +       file = au_xino_file(br->br_xino, a->calc.idx);
37539 +       AuDebugOn(!file);
37540 +       err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37541 +       if (unlikely(err)) {
37542 +               AuIOErr("err %d\n", err);
37543 +               goto out;
37544 +       }
37545 +
37546 +       del = NULL;
37547 +       hbl = &br->br_xino->xi_writing;
37548 +       hlist_bl_lock(hbl);
37549 +       au_hbl_for_each(pos, hbl) {
37550 +               p = container_of(pos, struct au_xi_writing, node);
37551 +               if (p->ino == a->ino) {
37552 +                       del = p;
37553 +                       hlist_bl_del(&p->node);
37554 +                       break;
37555 +               }
37556 +       }
37557 +       hlist_bl_unlock(hbl);
37558 +       au_kfree_rcu(del);
37559 +
37560 +out:
37561 +       au_lcnt_dec(&br->br_count);
37562 +       ii_read_unlock(root);
37563 +       si_read_unlock(sb);
37564 +       au_nwt_done(&sbi->si_nowait);
37565 +       au_kfree_rcu(a);
37566 +}
37567 +
37568 +/*
37569 + * create a new xino file asynchronously
37570 + */
37571 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37572 +                            struct au_xi_calc *calc, ino_t ino)
37573 +{
37574 +       int err;
37575 +       struct au_xino_do_new_async_args *arg;
37576 +
37577 +       err = -ENOMEM;
37578 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37579 +       if (unlikely(!arg))
37580 +               goto out;
37581 +
37582 +       arg->sb = sb;
37583 +       arg->br = br;
37584 +       arg->calc = *calc;
37585 +       arg->ino = ino;
37586 +       au_lcnt_inc(&br->br_count);
37587 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37588 +       if (unlikely(err)) {
37589 +               pr_err("wkq %d\n", err);
37590 +               au_lcnt_dec(&br->br_count);
37591 +               au_kfree_rcu(arg);
37592 +       }
37593 +
37594 +out:
37595 +       return err;
37596 +}
37597 +
37598 +/*
37599 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37600 + * at the position of @h_ino.
37601 + */
37602 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37603 +                ino_t *ino)
37604 +{
37605 +       int err;
37606 +       ssize_t sz;
37607 +       struct au_xi_calc calc;
37608 +       struct au_sbinfo *sbinfo;
37609 +       struct file *file;
37610 +       struct au_xino *xi;
37611 +       struct hlist_bl_head *hbl;
37612 +       struct hlist_bl_node *pos;
37613 +       struct au_xi_writing *p;
37614 +
37615 +       *ino = 0;
37616 +       if (!au_opt_test(au_mntflags(sb), XINO))
37617 +               return 0; /* no xino */
37618 +
37619 +       err = 0;
37620 +       au_xi_calc(sb, h_ino, &calc);
37621 +       xi = au_sbr(sb, bindex)->br_xino;
37622 +       file = au_xino_file(xi, calc.idx);
37623 +       if (!file) {
37624 +               hbl = &xi->xi_writing;
37625 +               hlist_bl_lock(hbl);
37626 +               au_hbl_for_each(pos, hbl) {
37627 +                       p = container_of(pos, struct au_xi_writing, node);
37628 +                       if (p->h_ino == h_ino) {
37629 +                               AuDbg("hi%llu, i%llu, found\n",
37630 +                                     (u64)p->h_ino, (u64)p->ino);
37631 +                               *ino = p->ino;
37632 +                               break;
37633 +                       }
37634 +               }
37635 +               hlist_bl_unlock(hbl);
37636 +               return 0;
37637 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37638 +               return 0; /* no xino */
37639 +
37640 +       sbinfo = au_sbi(sb);
37641 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
37642 +       if (sz == sizeof(*ino))
37643 +               return 0; /* success */
37644 +
37645 +       err = sz;
37646 +       if (unlikely(sz >= 0)) {
37647 +               err = -EIO;
37648 +               AuIOErr("xino read error (%zd)\n", sz);
37649 +       }
37650 +       return err;
37651 +}
37652 +
37653 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37654 +                           struct au_xi_calc *calc, ino_t ino)
37655 +{
37656 +       ssize_t sz;
37657 +
37658 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
37659 +       if (sz == sizeof(ino))
37660 +               return 0; /* success */
37661 +
37662 +       AuIOErr("write failed (%zd)\n", sz);
37663 +       return -EIO;
37664 +}
37665 +
37666 +/*
37667 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37668 + * at the position of @h_ino.
37669 + * even if @ino is zero, it is written to the xinofile and means no entry.
37670 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37671 + * try truncating it.
37672 + */
37673 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37674 +                 ino_t ino)
37675 +{
37676 +       int err;
37677 +       unsigned int mnt_flags;
37678 +       struct au_xi_calc calc;
37679 +       struct file *file;
37680 +       struct au_branch *br;
37681 +       struct au_xino *xi;
37682 +       struct au_xi_writing *p;
37683 +
37684 +       SiMustAnyLock(sb);
37685 +
37686 +       mnt_flags = au_mntflags(sb);
37687 +       if (!au_opt_test(mnt_flags, XINO))
37688 +               return 0;
37689 +
37690 +       au_xi_calc(sb, h_ino, &calc);
37691 +       br = au_sbr(sb, bindex);
37692 +       xi = br->br_xino;
37693 +       file = au_xino_file(xi, calc.idx);
37694 +       if (!file) {
37695 +               /* store the inum pair into the list */
37696 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37697 +               p->h_ino = h_ino;
37698 +               p->ino = ino;
37699 +               au_hbl_add(&p->node, &xi->xi_writing);
37700 +
37701 +               /* create and write a new xino file asynchronously */
37702 +               err = au_xino_new_async(sb, br, &calc, ino);
37703 +               if (!err)
37704 +                       return 0; /* success */
37705 +               goto out;
37706 +       }
37707 +
37708 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
37709 +       if (!err) {
37710 +               br = au_sbr(sb, bindex);
37711 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37712 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37713 +                       xino_try_trunc(sb, br);
37714 +               return 0; /* success */
37715 +       }
37716 +
37717 +out:
37718 +       AuIOErr("write failed (%d)\n", err);
37719 +       return -EIO;
37720 +}
37721 +
37722 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37723 +                             size_t size, loff_t *pos);
37724 +
37725 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37726 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37727 +                  loff_t *pos)
37728 +{
37729 +       ssize_t err;
37730 +       mm_segment_t oldfs;
37731 +       union {
37732 +               void *k;
37733 +               char __user *u;
37734 +       } buf;
37735 +       int i;
37736 +       const int prevent_endless = 10;
37737 +
37738 +       i = 0;
37739 +       buf.k = kbuf;
37740 +       oldfs = get_fs();
37741 +       set_fs(KERNEL_DS);
37742 +       do {
37743 +               err = func(file, buf.u, size, pos);
37744 +               if (err == -EINTR
37745 +                   && !au_wkq_test()
37746 +                   && fatal_signal_pending(current)) {
37747 +                       set_fs(oldfs);
37748 +                       err = xino_fread_wkq(func, file, kbuf, size, pos);
37749 +                       BUG_ON(err == -EINTR);
37750 +                       oldfs = get_fs();
37751 +                       set_fs(KERNEL_DS);
37752 +               }
37753 +       } while (i++ < prevent_endless
37754 +                && (err == -EAGAIN || err == -EINTR));
37755 +       set_fs(oldfs);
37756 +
37757 +#if 0 /* reserved for future use */
37758 +       if (err > 0)
37759 +               fsnotify_access(file->f_path.dentry);
37760 +#endif
37761 +
37762 +       return err;
37763 +}
37764 +
37765 +struct xino_fread_args {
37766 +       ssize_t *errp;
37767 +       vfs_readf_t func;
37768 +       struct file *file;
37769 +       void *buf;
37770 +       size_t size;
37771 +       loff_t *pos;
37772 +};
37773 +
37774 +static void call_xino_fread(void *args)
37775 +{
37776 +       struct xino_fread_args *a = args;
37777 +       *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37778 +}
37779 +
37780 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37781 +                             size_t size, loff_t *pos)
37782 +{
37783 +       ssize_t err;
37784 +       int wkq_err;
37785 +       struct xino_fread_args args = {
37786 +               .errp   = &err,
37787 +               .func   = func,
37788 +               .file   = file,
37789 +               .buf    = buf,
37790 +               .size   = size,
37791 +               .pos    = pos
37792 +       };
37793 +
37794 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37795 +       if (unlikely(wkq_err))
37796 +               err = wkq_err;
37797 +
37798 +       return err;
37799 +}
37800 +
37801 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37802 +                              size_t size, loff_t *pos);
37803 +
37804 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37805 +                             size_t size, loff_t *pos)
37806 +{
37807 +       ssize_t err;
37808 +       mm_segment_t oldfs;
37809 +       union {
37810 +               void *k;
37811 +               const char __user *u;
37812 +       } buf;
37813 +       int i;
37814 +       const int prevent_endless = 10;
37815 +
37816 +       i = 0;
37817 +       buf.k = kbuf;
37818 +       oldfs = get_fs();
37819 +       set_fs(KERNEL_DS);
37820 +       do {
37821 +               err = func(file, buf.u, size, pos);
37822 +               if (err == -EINTR
37823 +                   && !au_wkq_test()
37824 +                   && fatal_signal_pending(current)) {
37825 +                       set_fs(oldfs);
37826 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37827 +                       BUG_ON(err == -EINTR);
37828 +                       oldfs = get_fs();
37829 +                       set_fs(KERNEL_DS);
37830 +               }
37831 +       } while (i++ < prevent_endless
37832 +                && (err == -EAGAIN || err == -EINTR));
37833 +       set_fs(oldfs);
37834 +
37835 +#if 0 /* reserved for future use */
37836 +       if (err > 0)
37837 +               fsnotify_modify(file->f_path.dentry);
37838 +#endif
37839 +
37840 +       return err;
37841 +}
37842 +
37843 +struct do_xino_fwrite_args {
37844 +       ssize_t *errp;
37845 +       vfs_writef_t func;
37846 +       struct file *file;
37847 +       void *buf;
37848 +       size_t size;
37849 +       loff_t *pos;
37850 +};
37851 +
37852 +static void call_do_xino_fwrite(void *args)
37853 +{
37854 +       struct do_xino_fwrite_args *a = args;
37855 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37856 +}
37857 +
37858 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37859 +                              size_t size, loff_t *pos)
37860 +{
37861 +       ssize_t err;
37862 +       int wkq_err;
37863 +       struct do_xino_fwrite_args args = {
37864 +               .errp   = &err,
37865 +               .func   = func,
37866 +               .file   = file,
37867 +               .buf    = buf,
37868 +               .size   = size,
37869 +               .pos    = pos
37870 +       };
37871 +
37872 +       /*
37873 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37874 +        * users should care about quota and real 'filesystem full.'
37875 +        */
37876 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37877 +       if (unlikely(wkq_err))
37878 +               err = wkq_err;
37879 +
37880 +       return err;
37881 +}
37882 +
37883 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37884 +                   size_t size, loff_t *pos)
37885 +{
37886 +       ssize_t err;
37887 +
37888 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37889 +               lockdep_off();
37890 +               err = do_xino_fwrite(func, file, buf, size, pos);
37891 +               lockdep_on();
37892 +       } else {
37893 +               lockdep_off();
37894 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
37895 +               lockdep_on();
37896 +       }
37897 +
37898 +       return err;
37899 +}
37900 +
37901 +/* ---------------------------------------------------------------------- */
37902 +
37903 +/*
37904 + * inode number bitmap
37905 + */
37906 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37907 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37908 +{
37909 +       ino_t ino;
37910 +
37911 +       AuDebugOn(bit < 0 || page_bits <= bit);
37912 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37913 +       return ino;
37914 +}
37915 +
37916 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37917 +{
37918 +       AuDebugOn(ino < AUFS_FIRST_INO);
37919 +       ino -= AUFS_FIRST_INO;
37920 +       *pindex = ino / page_bits;
37921 +       *bit = ino % page_bits;
37922 +}
37923 +
37924 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37925 +{
37926 +       int err;
37927 +       loff_t pos;
37928 +       ssize_t sz;
37929 +       struct au_sbinfo *sbinfo;
37930 +       struct file *xib;
37931 +       unsigned long *p;
37932 +
37933 +       sbinfo = au_sbi(sb);
37934 +       MtxMustLock(&sbinfo->si_xib_mtx);
37935 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37936 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37937 +
37938 +       if (pindex == sbinfo->si_xib_last_pindex)
37939 +               return 0;
37940 +
37941 +       xib = sbinfo->si_xib;
37942 +       p = sbinfo->si_xib_buf;
37943 +       pos = sbinfo->si_xib_last_pindex;
37944 +       pos *= PAGE_SIZE;
37945 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37946 +       if (unlikely(sz != PAGE_SIZE))
37947 +               goto out;
37948 +
37949 +       pos = pindex;
37950 +       pos *= PAGE_SIZE;
37951 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37952 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37953 +       else {
37954 +               memset(p, 0, PAGE_SIZE);
37955 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37956 +       }
37957 +       if (sz == PAGE_SIZE) {
37958 +               sbinfo->si_xib_last_pindex = pindex;
37959 +               return 0; /* success */
37960 +       }
37961 +
37962 +out:
37963 +       AuIOErr1("write failed (%zd)\n", sz);
37964 +       err = sz;
37965 +       if (sz >= 0)
37966 +               err = -EIO;
37967 +       return err;
37968 +}
37969 +
37970 +static void au_xib_clear_bit(struct inode *inode)
37971 +{
37972 +       int err, bit;
37973 +       unsigned long pindex;
37974 +       struct super_block *sb;
37975 +       struct au_sbinfo *sbinfo;
37976 +
37977 +       AuDebugOn(inode->i_nlink);
37978 +
37979 +       sb = inode->i_sb;
37980 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37981 +       AuDebugOn(page_bits <= bit);
37982 +       sbinfo = au_sbi(sb);
37983 +       mutex_lock(&sbinfo->si_xib_mtx);
37984 +       err = xib_pindex(sb, pindex);
37985 +       if (!err) {
37986 +               clear_bit(bit, sbinfo->si_xib_buf);
37987 +               sbinfo->si_xib_next_bit = bit;
37988 +       }
37989 +       mutex_unlock(&sbinfo->si_xib_mtx);
37990 +}
37991 +
37992 +/* ---------------------------------------------------------------------- */
37993 +
37994 +/*
37995 + * truncate a xino bitmap file
37996 + */
37997 +
37998 +/* todo: slow */
37999 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38000 +{
38001 +       int err, bit;
38002 +       ssize_t sz;
38003 +       unsigned long pindex;
38004 +       loff_t pos, pend;
38005 +       struct au_sbinfo *sbinfo;
38006 +       vfs_readf_t func;
38007 +       ino_t *ino;
38008 +       unsigned long *p;
38009 +
38010 +       err = 0;
38011 +       sbinfo = au_sbi(sb);
38012 +       MtxMustLock(&sbinfo->si_xib_mtx);
38013 +       p = sbinfo->si_xib_buf;
38014 +       func = sbinfo->si_xread;
38015 +       pend = vfsub_f_size_read(file);
38016 +       pos = 0;
38017 +       while (pos < pend) {
38018 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
38019 +               err = sz;
38020 +               if (unlikely(sz <= 0))
38021 +                       goto out;
38022 +
38023 +               err = 0;
38024 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38025 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38026 +                               continue;
38027 +
38028 +                       xib_calc_bit(*ino, &pindex, &bit);
38029 +                       AuDebugOn(page_bits <= bit);
38030 +                       err = xib_pindex(sb, pindex);
38031 +                       if (!err)
38032 +                               set_bit(bit, p);
38033 +                       else
38034 +                               goto out;
38035 +               }
38036 +       }
38037 +
38038 +out:
38039 +       return err;
38040 +}
38041 +
38042 +static int xib_restore(struct super_block *sb)
38043 +{
38044 +       int err, i;
38045 +       unsigned int nfile;
38046 +       aufs_bindex_t bindex, bbot;
38047 +       void *page;
38048 +       struct au_branch *br;
38049 +       struct au_xino *xi;
38050 +       struct file *file;
38051 +
38052 +       err = -ENOMEM;
38053 +       page = (void *)__get_free_page(GFP_NOFS);
38054 +       if (unlikely(!page))
38055 +               goto out;
38056 +
38057 +       err = 0;
38058 +       bbot = au_sbbot(sb);
38059 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38060 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38061 +                       br = au_sbr(sb, bindex);
38062 +                       xi = br->br_xino;
38063 +                       nfile = xi->xi_nfile;
38064 +                       for (i = 0; i < nfile; i++) {
38065 +                               file = au_xino_file(xi, i);
38066 +                               if (file)
38067 +                                       err = do_xib_restore(sb, file, page);
38068 +                       }
38069 +               } else
38070 +                       AuDbg("skip shared b%d\n", bindex);
38071 +       free_page((unsigned long)page);
38072 +
38073 +out:
38074 +       return err;
38075 +}
38076 +
38077 +int au_xib_trunc(struct super_block *sb)
38078 +{
38079 +       int err;
38080 +       ssize_t sz;
38081 +       loff_t pos;
38082 +       struct au_sbinfo *sbinfo;
38083 +       unsigned long *p;
38084 +       struct file *file;
38085 +
38086 +       SiMustWriteLock(sb);
38087 +
38088 +       err = 0;
38089 +       sbinfo = au_sbi(sb);
38090 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38091 +               goto out;
38092 +
38093 +       file = sbinfo->si_xib;
38094 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38095 +               goto out;
38096 +
38097 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38098 +       err = PTR_ERR(file);
38099 +       if (IS_ERR(file))
38100 +               goto out;
38101 +       fput(sbinfo->si_xib);
38102 +       sbinfo->si_xib = file;
38103 +
38104 +       p = sbinfo->si_xib_buf;
38105 +       memset(p, 0, PAGE_SIZE);
38106 +       pos = 0;
38107 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38108 +       if (unlikely(sz != PAGE_SIZE)) {
38109 +               err = sz;
38110 +               AuIOErr("err %d\n", err);
38111 +               if (sz >= 0)
38112 +                       err = -EIO;
38113 +               goto out;
38114 +       }
38115 +
38116 +       mutex_lock(&sbinfo->si_xib_mtx);
38117 +       /* mnt_want_write() is unnecessary here */
38118 +       err = xib_restore(sb);
38119 +       mutex_unlock(&sbinfo->si_xib_mtx);
38120 +
38121 +out:
38122 +       return err;
38123 +}
38124 +
38125 +/* ---------------------------------------------------------------------- */
38126 +
38127 +struct au_xino *au_xino_alloc(unsigned int nfile)
38128 +{
38129 +       struct au_xino *xi;
38130 +
38131 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38132 +       if (unlikely(!xi))
38133 +               goto out;
38134 +       xi->xi_nfile = nfile;
38135 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38136 +       if (unlikely(!xi->xi_file))
38137 +               goto out_free;
38138 +
38139 +       xi->xi_nondir.total = 8; /* initial size */
38140 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38141 +                                     GFP_NOFS);
38142 +       if (unlikely(!xi->xi_nondir.array))
38143 +               goto out_file;
38144 +
38145 +       spin_lock_init(&xi->xi_nondir.spin);
38146 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38147 +       mutex_init(&xi->xi_mtx);
38148 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38149 +       atomic_set(&xi->xi_truncating, 0);
38150 +       kref_init(&xi->xi_kref);
38151 +       goto out; /* success */
38152 +
38153 +out_file:
38154 +       au_kfree_try_rcu(xi->xi_file);
38155 +out_free:
38156 +       au_kfree_rcu(xi);
38157 +       xi = NULL;
38158 +out:
38159 +       return xi;
38160 +}
38161 +
38162 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38163 +{
38164 +       int err;
38165 +       struct au_xino *xi;
38166 +
38167 +       err = 0;
38168 +       xi = au_xino_alloc(idx + 1);
38169 +       if (unlikely(!xi)) {
38170 +               err = -ENOMEM;
38171 +               goto out;
38172 +       }
38173 +
38174 +       if (file)
38175 +               get_file(file);
38176 +       xi->xi_file[idx] = file;
38177 +       AuDebugOn(br->br_xino);
38178 +       br->br_xino = xi;
38179 +
38180 +out:
38181 +       return err;
38182 +}
38183 +
38184 +static void au_xino_release(struct kref *kref)
38185 +{
38186 +       struct au_xino *xi;
38187 +       int i;
38188 +       unsigned long ul;
38189 +       struct hlist_bl_head *hbl;
38190 +       struct hlist_bl_node *pos, *n;
38191 +       struct au_xi_writing *p;
38192 +
38193 +       xi = container_of(kref, struct au_xino, xi_kref);
38194 +       for (i = 0; i < xi->xi_nfile; i++)
38195 +               if (xi->xi_file[i])
38196 +                       fput(xi->xi_file[i]);
38197 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38198 +               AuDebugOn(xi->xi_nondir.array[i]);
38199 +       mutex_destroy(&xi->xi_mtx);
38200 +       hbl = &xi->xi_writing;
38201 +       ul = au_hbl_count(hbl);
38202 +       if (unlikely(ul)) {
38203 +               pr_warn("xi_writing %lu\n", ul);
38204 +               hlist_bl_lock(hbl);
38205 +               hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
38206 +                       hlist_bl_del(&p->node);
38207 +                       au_kfree_rcu(p);
38208 +               }
38209 +               hlist_bl_unlock(hbl);
38210 +       }
38211 +       au_kfree_try_rcu(xi->xi_file);
38212 +       au_kfree_try_rcu(xi->xi_nondir.array);
38213 +       au_kfree_rcu(xi);
38214 +}
38215 +
38216 +int au_xino_put(struct au_branch *br)
38217 +{
38218 +       int ret;
38219 +       struct au_xino *xi;
38220 +
38221 +       ret = 0;
38222 +       xi = br->br_xino;
38223 +       if (xi) {
38224 +               br->br_xino = NULL;
38225 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38226 +       }
38227 +
38228 +       return ret;
38229 +}
38230 +
38231 +/* ---------------------------------------------------------------------- */
38232 +
38233 +/*
38234 + * xino mount option handlers
38235 + */
38236 +
38237 +/* xino bitmap */
38238 +static void xino_clear_xib(struct super_block *sb)
38239 +{
38240 +       struct au_sbinfo *sbinfo;
38241 +
38242 +       SiMustWriteLock(sb);
38243 +
38244 +       sbinfo = au_sbi(sb);
38245 +       /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
38246 +       if (sbinfo->si_xib)
38247 +               fput(sbinfo->si_xib);
38248 +       sbinfo->si_xib = NULL;
38249 +       if (sbinfo->si_xib_buf)
38250 +               free_page((unsigned long)sbinfo->si_xib_buf);
38251 +       sbinfo->si_xib_buf = NULL;
38252 +}
38253 +
38254 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38255 +{
38256 +       int err;
38257 +       loff_t pos;
38258 +       struct au_sbinfo *sbinfo;
38259 +       struct file *file;
38260 +       struct super_block *xi_sb;
38261 +
38262 +       SiMustWriteLock(sb);
38263 +
38264 +       sbinfo = au_sbi(sb);
38265 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38266 +       err = PTR_ERR(file);
38267 +       if (IS_ERR(file))
38268 +               goto out;
38269 +       if (sbinfo->si_xib)
38270 +               fput(sbinfo->si_xib);
38271 +       sbinfo->si_xib = file;
38272 +       sbinfo->si_xread = vfs_readf(file);
38273 +       sbinfo->si_xwrite = vfs_writef(file);
38274 +       xi_sb = file_inode(file)->i_sb;
38275 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38276 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38277 +               err = -EIO;
38278 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38279 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38280 +               goto out_unset;
38281 +       }
38282 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38283 +
38284 +       err = -ENOMEM;
38285 +       if (!sbinfo->si_xib_buf)
38286 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38287 +       if (unlikely(!sbinfo->si_xib_buf))
38288 +               goto out_unset;
38289 +
38290 +       sbinfo->si_xib_last_pindex = 0;
38291 +       sbinfo->si_xib_next_bit = 0;
38292 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38293 +               pos = 0;
38294 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38295 +                                 PAGE_SIZE, &pos);
38296 +               if (unlikely(err != PAGE_SIZE))
38297 +                       goto out_free;
38298 +       }
38299 +       err = 0;
38300 +       goto out; /* success */
38301 +
38302 +out_free:
38303 +       if (sbinfo->si_xib_buf)
38304 +               free_page((unsigned long)sbinfo->si_xib_buf);
38305 +       sbinfo->si_xib_buf = NULL;
38306 +       if (err >= 0)
38307 +               err = -EIO;
38308 +out_unset:
38309 +       fput(sbinfo->si_xib);
38310 +       sbinfo->si_xib = NULL;
38311 +out:
38312 +       AuTraceErr(err);
38313 +       return err;
38314 +}
38315 +
38316 +/* xino for each branch */
38317 +static void xino_clear_br(struct super_block *sb)
38318 +{
38319 +       aufs_bindex_t bindex, bbot;
38320 +       struct au_branch *br;
38321 +
38322 +       bbot = au_sbbot(sb);
38323 +       for (bindex = 0; bindex <= bbot; bindex++) {
38324 +               br = au_sbr(sb, bindex);
38325 +               AuDebugOn(!br);
38326 +               au_xino_put(br);
38327 +       }
38328 +}
38329 +
38330 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38331 +                                 aufs_bindex_t bshared)
38332 +{
38333 +       struct au_branch *brshared;
38334 +
38335 +       brshared = au_sbr(sb, bshared);
38336 +       AuDebugOn(!brshared->br_xino);
38337 +       AuDebugOn(!brshared->br_xino->xi_file);
38338 +       if (br->br_xino != brshared->br_xino) {
38339 +               au_xino_get(brshared);
38340 +               au_xino_put(br);
38341 +               br->br_xino = brshared->br_xino;
38342 +       }
38343 +}
38344 +
38345 +struct au_xino_do_set_br {
38346 +       vfs_writef_t writef;
38347 +       struct au_branch *br;
38348 +       ino_t h_ino;
38349 +       aufs_bindex_t bshared;
38350 +};
38351 +
38352 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38353 +                            struct au_xino_do_set_br *args)
38354 +{
38355 +       int err;
38356 +       struct au_xi_calc calc;
38357 +       struct file *file;
38358 +       struct au_branch *br;
38359 +       struct au_xi_new xinew = {
38360 +               .base = path
38361 +       };
38362 +
38363 +       br = args->br;
38364 +       xinew.xi = br->br_xino;
38365 +       au_xi_calc(sb, args->h_ino, &calc);
38366 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38367 +       if (args->bshared >= 0)
38368 +               /* shared xino */
38369 +               au_xino_set_br_shared(sb, br, args->bshared);
38370 +       else if (!xinew.xi) {
38371 +               /* new xino */
38372 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38373 +               if (unlikely(err))
38374 +                       goto out;
38375 +       }
38376 +
38377 +       /* force re-creating */
38378 +       xinew.xi = br->br_xino;
38379 +       xinew.idx = calc.idx;
38380 +       mutex_lock(&xinew.xi->xi_mtx);
38381 +       file = au_xi_new(sb, &xinew);
38382 +       mutex_unlock(&xinew.xi->xi_mtx);
38383 +       err = PTR_ERR(file);
38384 +       if (IS_ERR(file))
38385 +               goto out;
38386 +       AuDebugOn(!file);
38387 +
38388 +       err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38389 +       if (unlikely(err))
38390 +               au_xino_put(br);
38391 +
38392 +out:
38393 +       AuTraceErr(err);
38394 +       return err;
38395 +}
38396 +
38397 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38398 +{
38399 +       int err;
38400 +       aufs_bindex_t bindex, bbot;
38401 +       struct au_xino_do_set_br args;
38402 +       struct inode *inode;
38403 +
38404 +       SiMustWriteLock(sb);
38405 +
38406 +       bbot = au_sbbot(sb);
38407 +       inode = d_inode(sb->s_root);
38408 +       args.writef = au_sbi(sb)->si_xwrite;
38409 +       for (bindex = 0; bindex <= bbot; bindex++) {
38410 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38411 +               args.br = au_sbr(sb, bindex);
38412 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38413 +               err = au_xino_do_set_br(sb, path, &args);
38414 +               if (unlikely(err))
38415 +                       break;
38416 +       }
38417 +
38418 +       AuTraceErr(err);
38419 +       return err;
38420 +}
38421 +
38422 +void au_xino_clr(struct super_block *sb)
38423 +{
38424 +       struct au_sbinfo *sbinfo;
38425 +
38426 +       au_xigen_clr(sb);
38427 +       xino_clear_xib(sb);
38428 +       xino_clear_br(sb);
38429 +       dbgaufs_brs_del(sb, 0);
38430 +       sbinfo = au_sbi(sb);
38431 +       /* lvalue, do not call au_mntflags() */
38432 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38433 +}
38434 +
38435 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38436 +{
38437 +       int err, skip;
38438 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38439 +       struct qstr *dname, *cur_name;
38440 +       struct file *cur_xino;
38441 +       struct au_sbinfo *sbinfo;
38442 +       struct path *path, *cur_path;
38443 +
38444 +       SiMustWriteLock(sb);
38445 +
38446 +       err = 0;
38447 +       sbinfo = au_sbi(sb);
38448 +       path = &xiopt->file->f_path;
38449 +       dentry = path->dentry;
38450 +       parent = dget_parent(dentry);
38451 +       if (remount) {
38452 +               skip = 0;
38453 +               cur_xino = sbinfo->si_xib;
38454 +               if (cur_xino) {
38455 +                       cur_path = &cur_xino->f_path;
38456 +                       cur_dentry = cur_path->dentry;
38457 +                       cur_parent = dget_parent(cur_dentry);
38458 +                       cur_name = &cur_dentry->d_name;
38459 +                       dname = &dentry->d_name;
38460 +                       skip = (cur_parent == parent
38461 +                               && au_qstreq(dname, cur_name));
38462 +                       dput(cur_parent);
38463 +               }
38464 +               if (skip)
38465 +                       goto out;
38466 +       }
38467 +
38468 +       au_opt_set(sbinfo->si_mntflags, XINO);
38469 +       err = au_xino_set_xib(sb, path);
38470 +       /* si_x{read,write} are set */
38471 +       if (!err)
38472 +               err = au_xigen_set(sb, path);
38473 +       if (!err)
38474 +               err = au_xino_set_br(sb, path);
38475 +       if (!err) {
38476 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38477 +               goto out; /* success */
38478 +       }
38479 +
38480 +       /* reset all */
38481 +       AuIOErr("failed setting xino(%d).\n", err);
38482 +       au_xino_clr(sb);
38483 +
38484 +out:
38485 +       dput(parent);
38486 +       return err;
38487 +}
38488 +
38489 +/*
38490 + * create a xinofile at the default place/path.
38491 + */
38492 +struct file *au_xino_def(struct super_block *sb)
38493 +{
38494 +       struct file *file;
38495 +       char *page, *p;
38496 +       struct au_branch *br;
38497 +       struct super_block *h_sb;
38498 +       struct path path;
38499 +       aufs_bindex_t bbot, bindex, bwr;
38500 +
38501 +       br = NULL;
38502 +       bbot = au_sbbot(sb);
38503 +       bwr = -1;
38504 +       for (bindex = 0; bindex <= bbot; bindex++) {
38505 +               br = au_sbr(sb, bindex);
38506 +               if (au_br_writable(br->br_perm)
38507 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38508 +                       bwr = bindex;
38509 +                       break;
38510 +               }
38511 +       }
38512 +
38513 +       if (bwr >= 0) {
38514 +               file = ERR_PTR(-ENOMEM);
38515 +               page = (void *)__get_free_page(GFP_NOFS);
38516 +               if (unlikely(!page))
38517 +                       goto out;
38518 +               path.mnt = au_br_mnt(br);
38519 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38520 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38521 +               file = (void *)p;
38522 +               if (!IS_ERR(p)) {
38523 +                       strcat(p, "/" AUFS_XINO_FNAME);
38524 +                       AuDbg("%s\n", p);
38525 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38526 +               }
38527 +               free_page((unsigned long)page);
38528 +       } else {
38529 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38530 +                                     /*wbrtop*/0);
38531 +               if (IS_ERR(file))
38532 +                       goto out;
38533 +               h_sb = file->f_path.dentry->d_sb;
38534 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38535 +                       pr_err("xino doesn't support %s(%s)\n",
38536 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38537 +                       fput(file);
38538 +                       file = ERR_PTR(-EINVAL);
38539 +               }
38540 +       }
38541 +
38542 +out:
38543 +       return file;
38544 +}
38545 +
38546 +/* ---------------------------------------------------------------------- */
38547 +
38548 +/*
38549 + * initialize the xinofile for the specified branch @br
38550 + * at the place/path where @base_file indicates.
38551 + * test whether another branch is on the same filesystem or not,
38552 + * if found then share the xinofile with another branch.
38553 + */
38554 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38555 +                   struct path *base)
38556 +{
38557 +       int err;
38558 +       struct au_xino_do_set_br args = {
38559 +               .h_ino  = h_ino,
38560 +               .br     = br
38561 +       };
38562 +
38563 +       args.writef = au_sbi(sb)->si_xwrite;
38564 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38565 +                                      au_br_sb(br));
38566 +       err = au_xino_do_set_br(sb, base, &args);
38567 +       if (unlikely(err))
38568 +               au_xino_put(br);
38569 +
38570 +       return err;
38571 +}
38572 +
38573 +/* ---------------------------------------------------------------------- */
38574 +
38575 +/*
38576 + * get an unused inode number from bitmap
38577 + */
38578 +ino_t au_xino_new_ino(struct super_block *sb)
38579 +{
38580 +       ino_t ino;
38581 +       unsigned long *p, pindex, ul, pend;
38582 +       struct au_sbinfo *sbinfo;
38583 +       struct file *file;
38584 +       int free_bit, err;
38585 +
38586 +       if (!au_opt_test(au_mntflags(sb), XINO))
38587 +               return iunique(sb, AUFS_FIRST_INO);
38588 +
38589 +       sbinfo = au_sbi(sb);
38590 +       mutex_lock(&sbinfo->si_xib_mtx);
38591 +       p = sbinfo->si_xib_buf;
38592 +       free_bit = sbinfo->si_xib_next_bit;
38593 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38594 +               goto out; /* success */
38595 +       free_bit = find_first_zero_bit(p, page_bits);
38596 +       if (free_bit < page_bits)
38597 +               goto out; /* success */
38598 +
38599 +       pindex = sbinfo->si_xib_last_pindex;
38600 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38601 +               err = xib_pindex(sb, ul);
38602 +               if (unlikely(err))
38603 +                       goto out_err;
38604 +               free_bit = find_first_zero_bit(p, page_bits);
38605 +               if (free_bit < page_bits)
38606 +                       goto out; /* success */
38607 +       }
38608 +
38609 +       file = sbinfo->si_xib;
38610 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38611 +       for (ul = pindex + 1; ul <= pend; ul++) {
38612 +               err = xib_pindex(sb, ul);
38613 +               if (unlikely(err))
38614 +                       goto out_err;
38615 +               free_bit = find_first_zero_bit(p, page_bits);
38616 +               if (free_bit < page_bits)
38617 +                       goto out; /* success */
38618 +       }
38619 +       BUG();
38620 +
38621 +out:
38622 +       set_bit(free_bit, p);
38623 +       sbinfo->si_xib_next_bit = free_bit + 1;
38624 +       pindex = sbinfo->si_xib_last_pindex;
38625 +       mutex_unlock(&sbinfo->si_xib_mtx);
38626 +       ino = xib_calc_ino(pindex, free_bit);
38627 +       AuDbg("i%lu\n", (unsigned long)ino);
38628 +       return ino;
38629 +out_err:
38630 +       mutex_unlock(&sbinfo->si_xib_mtx);
38631 +       AuDbg("i0\n");
38632 +       return 0;
38633 +}
38634 +
38635 +/* for s_op->delete_inode() */
38636 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38637 +{
38638 +       int err;
38639 +       unsigned int mnt_flags;
38640 +       aufs_bindex_t bindex, bbot, bi;
38641 +       unsigned char try_trunc;
38642 +       struct au_iinfo *iinfo;
38643 +       struct super_block *sb;
38644 +       struct au_hinode *hi;
38645 +       struct inode *h_inode;
38646 +       struct au_branch *br;
38647 +       vfs_writef_t xwrite;
38648 +       struct au_xi_calc calc;
38649 +       struct file *file;
38650 +
38651 +       AuDebugOn(au_is_bad_inode(inode));
38652 +
38653 +       sb = inode->i_sb;
38654 +       mnt_flags = au_mntflags(sb);
38655 +       if (!au_opt_test(mnt_flags, XINO)
38656 +           || inode->i_ino == AUFS_ROOT_INO)
38657 +               return;
38658 +
38659 +       if (unlinked) {
38660 +               au_xigen_inc(inode);
38661 +               au_xib_clear_bit(inode);
38662 +       }
38663 +
38664 +       iinfo = au_ii(inode);
38665 +       bindex = iinfo->ii_btop;
38666 +       if (bindex < 0)
38667 +               return;
38668 +
38669 +       xwrite = au_sbi(sb)->si_xwrite;
38670 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38671 +       hi = au_hinode(iinfo, bindex);
38672 +       bbot = iinfo->ii_bbot;
38673 +       for (; bindex <= bbot; bindex++, hi++) {
38674 +               h_inode = hi->hi_inode;
38675 +               if (!h_inode
38676 +                   || (!unlinked && h_inode->i_nlink))
38677 +                       continue;
38678 +
38679 +               /* inode may not be revalidated */
38680 +               bi = au_br_index(sb, hi->hi_id);
38681 +               if (bi < 0)
38682 +                       continue;
38683 +
38684 +               br = au_sbr(sb, bi);
38685 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38686 +               file = au_xino_file(br->br_xino, calc.idx);
38687 +               if (IS_ERR_OR_NULL(file))
38688 +                       continue;
38689 +
38690 +               err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
38691 +               if (!err && try_trunc
38692 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38693 +                       xino_try_trunc(sb, br);
38694 +       }
38695 +}
38696 +
38697 +/* ---------------------------------------------------------------------- */
38698 +
38699 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38700 +{
38701 +       int found, total, i;
38702 +
38703 +       found = -1;
38704 +       total = xi->xi_nondir.total;
38705 +       for (i = 0; i < total; i++) {
38706 +               if (xi->xi_nondir.array[i] != h_ino)
38707 +                       continue;
38708 +               found = i;
38709 +               break;
38710 +       }
38711 +
38712 +       return found;
38713 +}
38714 +
38715 +static int au_xinondir_expand(struct au_xino *xi)
38716 +{
38717 +       int err, sz;
38718 +       ino_t *p;
38719 +
38720 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38721 +
38722 +       err = -ENOMEM;
38723 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38724 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38725 +               goto out;
38726 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38727 +                        /*may_shrink*/0);
38728 +       if (p) {
38729 +               xi->xi_nondir.array = p;
38730 +               xi->xi_nondir.total <<= 1;
38731 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38732 +               err = 0;
38733 +       }
38734 +
38735 +out:
38736 +       return err;
38737 +}
38738 +
38739 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38740 +                      ino_t h_ino, int idx)
38741 +{
38742 +       struct au_xino *xi;
38743 +
38744 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38745 +       xi = au_sbr(sb, bindex)->br_xino;
38746 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38747 +
38748 +       spin_lock(&xi->xi_nondir.spin);
38749 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38750 +       xi->xi_nondir.array[idx] = 0;
38751 +       spin_unlock(&xi->xi_nondir.spin);
38752 +       wake_up_all(&xi->xi_nondir.wqh);
38753 +}
38754 +
38755 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38756 +                     int *idx)
38757 +{
38758 +       int err, found, empty;
38759 +       struct au_xino *xi;
38760 +
38761 +       err = 0;
38762 +       *idx = -1;
38763 +       if (!au_opt_test(au_mntflags(sb), XINO))
38764 +               goto out; /* no xino */
38765 +
38766 +       xi = au_sbr(sb, bindex)->br_xino;
38767 +
38768 +again:
38769 +       spin_lock(&xi->xi_nondir.spin);
38770 +       found = au_xinondir_find(xi, h_ino);
38771 +       if (found == -1) {
38772 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38773 +               if (empty == -1) {
38774 +                       empty = xi->xi_nondir.total;
38775 +                       err = au_xinondir_expand(xi);
38776 +                       if (unlikely(err))
38777 +                               goto out_unlock;
38778 +               }
38779 +               xi->xi_nondir.array[empty] = h_ino;
38780 +               *idx = empty;
38781 +       } else {
38782 +               spin_unlock(&xi->xi_nondir.spin);
38783 +               wait_event(xi->xi_nondir.wqh,
38784 +                          xi->xi_nondir.array[found] != h_ino);
38785 +               goto again;
38786 +       }
38787 +
38788 +out_unlock:
38789 +       spin_unlock(&xi->xi_nondir.spin);
38790 +out:
38791 +       return err;
38792 +}
38793 +
38794 +/* ---------------------------------------------------------------------- */
38795 +
38796 +int au_xino_path(struct seq_file *seq, struct file *file)
38797 +{
38798 +       int err;
38799 +
38800 +       err = au_seq_path(seq, &file->f_path);
38801 +       if (unlikely(err))
38802 +               goto out;
38803 +
38804 +#define Deleted "\\040(deleted)"
38805 +       seq->count -= sizeof(Deleted) - 1;
38806 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38807 +                        sizeof(Deleted) - 1));
38808 +#undef Deleted
38809 +
38810 +out:
38811 +       return err;
38812 +}
38813 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38814 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38815 +++ linux/include/uapi/linux/aufs_type.h        2019-11-25 12:42:00.275843147 +0100
38816 @@ -0,0 +1,452 @@
38817 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38818 +/*
38819 + * Copyright (C) 2005-2019 Junjiro R. Okajima
38820 + *
38821 + * This program, aufs is free software; you can redistribute it and/or modify
38822 + * it under the terms of the GNU General Public License as published by
38823 + * the Free Software Foundation; either version 2 of the License, or
38824 + * (at your option) any later version.
38825 + *
38826 + * This program is distributed in the hope that it will be useful,
38827 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38828 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38829 + * GNU General Public License for more details.
38830 + *
38831 + * You should have received a copy of the GNU General Public License
38832 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38833 + */
38834 +
38835 +#ifndef __AUFS_TYPE_H__
38836 +#define __AUFS_TYPE_H__
38837 +
38838 +#define AUFS_NAME      "aufs"
38839 +
38840 +#ifdef __KERNEL__
38841 +/*
38842 + * define it before including all other headers.
38843 + * sched.h may use pr_* macros before defining "current", so define the
38844 + * no-current version first, and re-define later.
38845 + */
38846 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38847 +#include <linux/sched.h>
38848 +#undef pr_fmt
38849 +#define pr_fmt(fmt) \
38850 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38851 +               (int)sizeof(current->comm), current->comm, current->pid
38852 +#else
38853 +#include <stdint.h>
38854 +#include <sys/types.h>
38855 +#endif /* __KERNEL__ */
38856 +
38857 +#include <linux/limits.h>
38858 +
38859 +#define AUFS_VERSION   "5.x-rcN-20191021"
38860 +
38861 +/* todo? move this to linux-2.6.19/include/magic.h */
38862 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38863 +
38864 +/* ---------------------------------------------------------------------- */
38865 +
38866 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38867 +typedef int8_t aufs_bindex_t;
38868 +#define AUFS_BRANCH_MAX 127
38869 +#else
38870 +typedef int16_t aufs_bindex_t;
38871 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38872 +#define AUFS_BRANCH_MAX 511
38873 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38874 +#define AUFS_BRANCH_MAX 1023
38875 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38876 +#define AUFS_BRANCH_MAX 32767
38877 +#endif
38878 +#endif
38879 +
38880 +#ifdef __KERNEL__
38881 +#ifndef AUFS_BRANCH_MAX
38882 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38883 +#endif
38884 +#endif /* __KERNEL__ */
38885 +
38886 +/* ---------------------------------------------------------------------- */
38887 +
38888 +#define AUFS_FSTYPE            AUFS_NAME
38889 +
38890 +#define AUFS_ROOT_INO          2
38891 +#define AUFS_FIRST_INO         11
38892 +
38893 +#define AUFS_WH_PFX            ".wh."
38894 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38895 +#define AUFS_WH_TMP_LEN                4
38896 +/* a limit for rmdir/rename a dir and copyup */
38897 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38898 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38899 +                               - 1                     /* dot */\
38900 +                               - AUFS_WH_TMP_LEN)      /* hex */
38901 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38902 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38903 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38904 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38905 +#define AUFS_DIRWH_DEF         3
38906 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38907 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38908 +#define AUFS_RDBLK_DEF         512 /* bytes */
38909 +#define AUFS_RDHASH_DEF                32
38910 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38911 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38912 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38913 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38914 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38915 +
38916 +/* pseudo-link maintenace under /proc */
38917 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38918 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38919 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38920 +
38921 +/* dirren, renamed dir */
38922 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38923 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38924 +/* whiteouted doubly */
38925 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38926 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38927 +
38928 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38929 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38930 +
38931 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38932 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38933 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38934 +
38935 +/* doubly whiteouted */
38936 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38937 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38938 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38939 +
38940 +/* branch permissions and attributes */
38941 +#define AUFS_BRPERM_RW         "rw"
38942 +#define AUFS_BRPERM_RO         "ro"
38943 +#define AUFS_BRPERM_RR         "rr"
38944 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38945 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38946 +#define AUFS_BRATTR_FHSM       "fhsm"
38947 +#define AUFS_BRATTR_UNPIN      "unpin"
38948 +#define AUFS_BRATTR_ICEX       "icex"
38949 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38950 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38951 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38952 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38953 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38954 +#define AUFS_BRRATTR_WH                "wh"
38955 +#define AUFS_BRWATTR_NLWH      "nolwh"
38956 +#define AUFS_BRWATTR_MOO       "moo"
38957 +
38958 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38959 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38960 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38961 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38962 +
38963 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38964 +#define AuBrAttr_COO_ALL       (1 << 4)
38965 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38966 +
38967 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38968 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38969 +                                                  branch. meaningless since
38970 +                                                  linux-3.18-rc1 */
38971 +
38972 +/* ignore error in copying XATTR */
38973 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38974 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38975 +#define AuBrAttr_ICEX_TR       (1 << 9)
38976 +#define AuBrAttr_ICEX_USR      (1 << 10)
38977 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38978 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38979 +                                | AuBrAttr_ICEX_SYS    \
38980 +                                | AuBrAttr_ICEX_TR     \
38981 +                                | AuBrAttr_ICEX_USR    \
38982 +                                | AuBrAttr_ICEX_OTH)
38983 +
38984 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38985 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38986 +
38987 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38988 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38989 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38990 +
38991 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38992 +
38993 +/* #warning test userspace */
38994 +#ifdef __KERNEL__
38995 +#ifndef CONFIG_AUFS_FHSM
38996 +#undef AuBrAttr_FHSM
38997 +#define AuBrAttr_FHSM          0
38998 +#endif
38999 +#ifndef CONFIG_AUFS_XATTR
39000 +#undef AuBrAttr_ICEX
39001 +#define AuBrAttr_ICEX          0
39002 +#undef AuBrAttr_ICEX_SEC
39003 +#define AuBrAttr_ICEX_SEC      0
39004 +#undef AuBrAttr_ICEX_SYS
39005 +#define AuBrAttr_ICEX_SYS      0
39006 +#undef AuBrAttr_ICEX_TR
39007 +#define AuBrAttr_ICEX_TR       0
39008 +#undef AuBrAttr_ICEX_USR
39009 +#define AuBrAttr_ICEX_USR      0
39010 +#undef AuBrAttr_ICEX_OTH
39011 +#define AuBrAttr_ICEX_OTH      0
39012 +#endif
39013 +#endif
39014 +
39015 +/* the longest combination */
39016 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39017 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39018 +                              "+" AUFS_BRATTR_COO_REG          \
39019 +                              "+" AUFS_BRATTR_FHSM             \
39020 +                              "+" AUFS_BRATTR_UNPIN            \
39021 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39022 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39023 +                              "+" AUFS_BRATTR_ICEX_USR         \
39024 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39025 +                              "+" AUFS_BRWATTR_NLWH)
39026 +
39027 +typedef struct {
39028 +       char a[AuBrPermStrSz];
39029 +} au_br_perm_str_t;
39030 +
39031 +static inline int au_br_writable(int brperm)
39032 +{
39033 +       return brperm & AuBrPerm_RW;
39034 +}
39035 +
39036 +static inline int au_br_whable(int brperm)
39037 +{
39038 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39039 +}
39040 +
39041 +static inline int au_br_wh_linkable(int brperm)
39042 +{
39043 +       return !(brperm & AuBrWAttr_NoLinkWH);
39044 +}
39045 +
39046 +static inline int au_br_cmoo(int brperm)
39047 +{
39048 +       return brperm & AuBrAttr_CMOO_Mask;
39049 +}
39050 +
39051 +static inline int au_br_fhsm(int brperm)
39052 +{
39053 +       return brperm & AuBrAttr_FHSM;
39054 +}
39055 +
39056 +/* ---------------------------------------------------------------------- */
39057 +
39058 +/* ioctl */
39059 +enum {
39060 +       /* readdir in userspace */
39061 +       AuCtl_RDU,
39062 +       AuCtl_RDU_INO,
39063 +
39064 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39065 +       AuCtl_IBUSY,    /* busy inode */
39066 +       AuCtl_MVDOWN,   /* move-down */
39067 +       AuCtl_BR,       /* info about branches */
39068 +       AuCtl_FHSM_FD   /* connection for fhsm */
39069 +};
39070 +
39071 +/* borrowed from linux/include/linux/kernel.h */
39072 +#ifndef ALIGN
39073 +#ifdef _GNU_SOURCE
39074 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39075 +#else
39076 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39077 +#endif
39078 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39079 +#endif
39080 +
39081 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39082 +#ifndef __aligned
39083 +#define __aligned(x)                   __attribute__((aligned(x)))
39084 +#endif
39085 +
39086 +#ifdef __KERNEL__
39087 +#ifndef __packed
39088 +#define __packed                       __attribute__((packed))
39089 +#endif
39090 +#endif
39091 +
39092 +struct au_rdu_cookie {
39093 +       uint64_t        h_pos;
39094 +       int16_t         bindex;
39095 +       uint8_t         flags;
39096 +       uint8_t         pad;
39097 +       uint32_t        generation;
39098 +} __aligned(8);
39099 +
39100 +struct au_rdu_ent {
39101 +       uint64_t        ino;
39102 +       int16_t         bindex;
39103 +       uint8_t         type;
39104 +       uint8_t         nlen;
39105 +       uint8_t         wh;
39106 +       char            name[0];
39107 +} __aligned(8);
39108 +
39109 +static inline int au_rdu_len(int nlen)
39110 +{
39111 +       /* include the terminating NULL */
39112 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39113 +                    sizeof(uint64_t));
39114 +}
39115 +
39116 +union au_rdu_ent_ul {
39117 +       struct au_rdu_ent __user        *e;
39118 +       uint64_t                        ul;
39119 +};
39120 +
39121 +enum {
39122 +       AufsCtlRduV_SZ,
39123 +       AufsCtlRduV_End
39124 +};
39125 +
39126 +struct aufs_rdu {
39127 +       /* input */
39128 +       union {
39129 +               uint64_t        sz;     /* AuCtl_RDU */
39130 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39131 +       };
39132 +       union au_rdu_ent_ul     ent;
39133 +       uint16_t                verify[AufsCtlRduV_End];
39134 +
39135 +       /* input/output */
39136 +       uint32_t                blk;
39137 +
39138 +       /* output */
39139 +       union au_rdu_ent_ul     tail;
39140 +       /* number of entries which were added in a single call */
39141 +       uint64_t                rent;
39142 +       uint8_t                 full;
39143 +       uint8_t                 shwh;
39144 +
39145 +       struct au_rdu_cookie    cookie;
39146 +} __aligned(8);
39147 +
39148 +/* ---------------------------------------------------------------------- */
39149 +
39150 +/* dirren. the branch is identified by the filename who contains this */
39151 +struct au_drinfo {
39152 +       uint64_t ino;
39153 +       union {
39154 +               uint8_t oldnamelen;
39155 +               uint64_t _padding;
39156 +       };
39157 +       uint8_t oldname[0];
39158 +} __aligned(8);
39159 +
39160 +struct au_drinfo_fdata {
39161 +       uint32_t magic;
39162 +       struct au_drinfo drinfo;
39163 +} __aligned(8);
39164 +
39165 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39166 +/* future */
39167 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39168 +
39169 +/* ---------------------------------------------------------------------- */
39170 +
39171 +struct aufs_wbr_fd {
39172 +       uint32_t        oflags;
39173 +       int16_t         brid;
39174 +} __aligned(8);
39175 +
39176 +/* ---------------------------------------------------------------------- */
39177 +
39178 +struct aufs_ibusy {
39179 +       uint64_t        ino, h_ino;
39180 +       int16_t         bindex;
39181 +} __aligned(8);
39182 +
39183 +/* ---------------------------------------------------------------------- */
39184 +
39185 +/* error code for move-down */
39186 +/* the actual message strings are implemented in aufs-util.git */
39187 +enum {
39188 +       EAU_MVDOWN_OPAQUE = 1,
39189 +       EAU_MVDOWN_WHITEOUT,
39190 +       EAU_MVDOWN_UPPER,
39191 +       EAU_MVDOWN_BOTTOM,
39192 +       EAU_MVDOWN_NOUPPER,
39193 +       EAU_MVDOWN_NOLOWERBR,
39194 +       EAU_Last
39195 +};
39196 +
39197 +/* flags for move-down */
39198 +#define AUFS_MVDOWN_DMSG       1
39199 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39200 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39201 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39202 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39203 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39204 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39205 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39206 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39207 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39208 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39209 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39210 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39211 +
39212 +/* index for move-down */
39213 +enum {
39214 +       AUFS_MVDOWN_UPPER,
39215 +       AUFS_MVDOWN_LOWER,
39216 +       AUFS_MVDOWN_NARRAY
39217 +};
39218 +
39219 +/*
39220 + * additional info of move-down
39221 + * number of free blocks and inodes.
39222 + * subset of struct kstatfs, but smaller and always 64bit.
39223 + */
39224 +struct aufs_stfs {
39225 +       uint64_t        f_blocks;
39226 +       uint64_t        f_bavail;
39227 +       uint64_t        f_files;
39228 +       uint64_t        f_ffree;
39229 +};
39230 +
39231 +struct aufs_stbr {
39232 +       int16_t                 brid;   /* optional input */
39233 +       int16_t                 bindex; /* output */
39234 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39235 +} __aligned(8);
39236 +
39237 +struct aufs_mvdown {
39238 +       uint32_t                flags;                  /* input/output */
39239 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39240 +       int8_t                  au_errno;               /* output */
39241 +} __aligned(8);
39242 +
39243 +/* ---------------------------------------------------------------------- */
39244 +
39245 +union aufs_brinfo {
39246 +       /* PATH_MAX may differ between kernel-space and user-space */
39247 +       char    _spacer[4096];
39248 +       struct {
39249 +               int16_t id;
39250 +               int     perm;
39251 +               char    path[0];
39252 +       };
39253 +} __aligned(8);
39254 +
39255 +/* ---------------------------------------------------------------------- */
39256 +
39257 +#define AuCtlType              'A'
39258 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39259 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39260 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39261 +                                    struct aufs_wbr_fd)
39262 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39263 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39264 +                                     struct aufs_mvdown)
39265 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39266 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39267 +
39268 +#endif /* __AUFS_TYPE_H__ */
39269 SPDX-License-Identifier: GPL-2.0
39270 aufs5.x-rcN loopback patch
39271
39272 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39273 index 80d06084b043..a83cdfec7359 100644
39274 --- a/drivers/block/loop.c
39275 +++ b/drivers/block/loop.c
39276 @@ -625,6 +625,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39277                         lo->use_dio);
39278  }
39279  
39280 +static struct file *loop_real_file(struct file *file)
39281 +{
39282 +       struct file *f = NULL;
39283 +
39284 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39285 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39286 +       return f;
39287 +}
39288 +
39289  static void loop_reread_partitions(struct loop_device *lo,
39290                                    struct block_device *bdev)
39291  {
39292 @@ -678,6 +687,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39293                           unsigned int arg)
39294  {
39295         struct file     *file = NULL, *old_file;
39296 +       struct file     *f, *virt_file = NULL, *old_virt_file;
39297         int             error;
39298         bool            partscan;
39299  
39300 @@ -697,12 +707,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39301         file = fget(arg);
39302         if (!file)
39303                 goto out_err;
39304 +       f = loop_real_file(file);
39305 +       if (f) {
39306 +               virt_file = file;
39307 +               file = f;
39308 +               get_file(file);
39309 +       }
39310  
39311         error = loop_validate_file(file, bdev);
39312         if (error)
39313                 goto out_err;
39314  
39315         old_file = lo->lo_backing_file;
39316 +       old_virt_file = lo->lo_backing_virt_file;
39317  
39318         error = -EINVAL;
39319  
39320 @@ -714,6 +731,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39321         blk_mq_freeze_queue(lo->lo_queue);
39322         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39323         lo->lo_backing_file = file;
39324 +       lo->lo_backing_virt_file = virt_file;
39325         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39326         mapping_set_gfp_mask(file->f_mapping,
39327                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39328 @@ -727,6 +745,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39329          * dependency.
39330          */
39331         fput(old_file);
39332 +       if (old_virt_file)
39333 +               fput(old_virt_file);
39334         if (partscan)
39335                 loop_reread_partitions(lo, bdev);
39336         return 0;
39337 @@ -735,6 +755,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39338         mutex_unlock(&loop_ctl_mutex);
39339         if (file)
39340                 fput(file);
39341 +       if (virt_file)
39342 +               fput(virt_file);
39343         return error;
39344  }
39345  
39346 @@ -939,7 +961,7 @@ static void loop_update_rotational(struct loop_device *lo)
39347  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39348                        struct block_device *bdev, unsigned int arg)
39349  {
39350 -       struct file     *file;
39351 +       struct file     *file, *f, *virt_file = NULL;
39352         struct inode    *inode;
39353         struct address_space *mapping;
39354         struct block_device *claimed_bdev = NULL;
39355 @@ -955,6 +977,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39356         file = fget(arg);
39357         if (!file)
39358                 goto out;
39359 +       f = loop_real_file(file);
39360 +       if (f) {
39361 +               virt_file = file;
39362 +               file = f;
39363 +               get_file(file);
39364 +       }
39365  
39366         /*
39367          * If we don't hold exclusive handle for the device, upgrade to it
39368 @@ -1003,6 +1031,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39369         lo->lo_device = bdev;
39370         lo->lo_flags = lo_flags;
39371         lo->lo_backing_file = file;
39372 +       lo->lo_backing_virt_file = virt_file;
39373         lo->transfer = NULL;
39374         lo->ioctl = NULL;
39375         lo->lo_sizelimit = 0;
39376 @@ -1046,6 +1075,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39377                 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
39378  out_putf:
39379         fput(file);
39380 +       if (virt_file)
39381 +               fput(virt_file);
39382  out:
39383         /* This is safe: open() is still holding a reference. */
39384         module_put(THIS_MODULE);
39385 @@ -1092,6 +1123,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
39386  static int __loop_clr_fd(struct loop_device *lo, bool release)
39387  {
39388         struct file *filp = NULL;
39389 +       struct file *virt_filp = lo->lo_backing_virt_file;
39390         gfp_t gfp = lo->old_gfp_mask;
39391         struct block_device *bdev = lo->lo_device;
39392         int err = 0;
39393 @@ -1115,6 +1147,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39394  
39395         spin_lock_irq(&lo->lo_lock);
39396         lo->lo_backing_file = NULL;
39397 +       lo->lo_backing_virt_file = NULL;
39398         spin_unlock_irq(&lo->lo_lock);
39399  
39400         loop_release_xfer(lo);
39401 @@ -1197,6 +1230,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39402          */
39403         if (filp)
39404                 fput(filp);
39405 +       if (virt_filp)
39406 +               fput(virt_filp);
39407         return err;
39408  }
39409  
39410 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39411 index af75a5ee4094..1e6ee5a4f623 100644
39412 --- a/drivers/block/loop.h
39413 +++ b/drivers/block/loop.h
39414 @@ -46,7 +46,7 @@ struct loop_device {
39415         int             (*ioctl)(struct loop_device *, int cmd, 
39416                                  unsigned long arg); 
39417  
39418 -       struct file *   lo_backing_file;
39419 +       struct file *   lo_backing_file, *lo_backing_virt_file;
39420         struct block_device *lo_device;
39421         void            *key_data; 
39422  
39423 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39424 index 0309f0d502ff..19feb4f3cb5f 100644
39425 --- a/fs/aufs/f_op.c
39426 +++ b/fs/aufs/f_op.c
39427 @@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39428         if (IS_ERR(h_file))
39429                 goto out;
39430  
39431 -       if (au_test_loopback_kthread()) {
39432 +       if (0 && au_test_loopback_kthread()) {
39433                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39434                 if (file->f_mapping != h_file->f_mapping) {
39435                         file->f_mapping = h_file->f_mapping;
39436 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39437 index 9ba35a878ecd..4ed0ff03d5ab 100644
39438 --- a/fs/aufs/loop.c
39439 +++ b/fs/aufs/loop.c
39440 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39441                 symbol_put(loop_backing_file);
39442         au_kfree_try_rcu(au_warn_loopback_array);
39443  }
39444 +
39445 +/* ---------------------------------------------------------------------- */
39446 +
39447 +/* support the loopback block device insude aufs */
39448 +
39449 +struct file *aufs_real_loop(struct file *file)
39450 +{
39451 +       struct file *f;
39452 +
39453 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39454 +       fi_read_lock(file);
39455 +       f = au_hf_top(file);
39456 +       fi_read_unlock(file);
39457 +       AuDebugOn(!f);
39458 +       return f;
39459 +}
39460 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39461 index f31e40aff267..e13fb1a0717a 100644
39462 --- a/fs/aufs/loop.h
39463 +++ b/fs/aufs/loop.h
39464 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39465  
39466  int au_loopback_init(void);
39467  void au_loopback_fin(void);
39468 +
39469 +struct file *aufs_real_loop(struct file *file);
39470  #else
39471  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39472  
39473 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39474  
39475  AuStubInt0(au_loopback_init, void)
39476  AuStubVoid(au_loopback_fin, void)
39477 +
39478 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39479  #endif /* BLK_DEV_LOOP */
39480  
39481  #endif /* __KERNEL__ */
39482 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39483 index a97e2921cb09..f74eb6962684 100644
39484 --- a/fs/aufs/super.c
39485 +++ b/fs/aufs/super.c
39486 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39487         .statfs         = aufs_statfs,
39488         .put_super      = aufs_put_super,
39489         .sync_fs        = aufs_sync_fs,
39490 -       .remount_fs     = aufs_remount_fs
39491 +       .remount_fs     = aufs_remount_fs,
39492 +#ifdef CONFIG_AUFS_BDEV_LOOP
39493 +       .real_loop      = aufs_real_loop
39494 +#endif
39495  };
39496  
39497  /* ---------------------------------------------------------------------- */
39498 diff --git a/include/linux/fs.h b/include/linux/fs.h
39499 index 3dbec51c2037..38ab7b6c7c6d 100644
39500 --- a/include/linux/fs.h
39501 +++ b/include/linux/fs.h
39502 @@ -1948,6 +1948,10 @@ struct super_operations {
39503                                   struct shrink_control *);
39504         long (*free_cached_objects)(struct super_block *,
39505                                     struct shrink_control *);
39506 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
39507 +       /* and aufs */
39508 +       struct file *(*real_loop)(struct file *);
39509 +#endif
39510  };
39511  
39512  /*
This page took 3.704607 seconds and 3 git commands to generate.