]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
- rediff
[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 f08fbbfafd9a0..1c597cddc351c 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -267,6 +267,7 @@ source "fs/sysv/Kconfig"
9  source "fs/ufs/Kconfig"
10  source "fs/erofs/Kconfig"
11  source "fs/vboxsf/Kconfig"
12 +source "fs/aufs/Kconfig"
13  
14  endif # MISC_FILESYSTEMS
15  
16 diff --git a/fs/Makefile b/fs/Makefile
17 index 2ce5112b02c86..d9b4a51985538 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -136,3 +136,4 @@ obj-$(CONFIG_EFIVAR_FS)             += efivarfs/
21  obj-$(CONFIG_EROFS_FS)         += erofs/
22  obj-$(CONFIG_VBOXSF_FS)                += vboxsf/
23  obj-$(CONFIG_ZONEFS_FS)                += zonefs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs5.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 50659d76976b7..80f67140e4f70 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2946,6 +2946,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 da693e6a834e5..0479902a0cd50 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -753,6 +753,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 b280e07e162b1..046000653e4d8 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1266,7 +1266,7 @@ enum d_walk_ret {
86   *
87   * The @enter() callbacks are called with d_lock held.
88   */
89 -static void d_walk(struct dentry *parent, void *data,
90 +void d_walk(struct dentry *parent, void *data,
91                    enum d_walk_ret (*enter)(void *, struct dentry *))
92  {
93         struct dentry *this_parent;
94 diff --git a/fs/fcntl.c b/fs/fcntl.c
95 index 2e4c0fa2074b0..0b28a37f7e505 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 93d9252a00ab4..7700c5e4783b9 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1688,7 +1688,7 @@ EXPORT_SYMBOL(generic_update_time);
121   * This does the actual work of updating an inodes time or version.  Must have
122   * had called mnt_want_write() before calling this.
123   */
124 -static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125 +int update_time(struct inode *inode, struct timespec64 *time, int flags)
126  {
127         if (inode->i_op->update_time)
128                 return inode->i_op->update_time(inode, time, flags);
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index a28e4db075ede..12e725e34c6ca 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 bbfa9b12b15eb..5d684d7d29207 100644
148 --- a/fs/read_write.c
149 +++ b/fs/read_write.c
150 @@ -499,6 +499,28 @@ static ssize_t __vfs_write(struct file *file, const char __user *p,
151         return ret;
152  }
153  
154 +vfs_readf_t vfs_readf(struct file *file)
155 +{
156 +       const struct file_operations *fop = file->f_op;
157 +
158 +       if (fop->read)
159 +               return fop->read;
160 +       if (fop->read_iter)
161 +               return new_sync_read;
162 +       return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
163 +}
164 +
165 +vfs_writef_t vfs_writef(struct file *file)
166 +{
167 +       const struct file_operations *fop = file->f_op;
168 +
169 +       if (fop->write)
170 +               return fop->write;
171 +       if (fop->write_iter)
172 +               return new_sync_write;
173 +       return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
174 +}
175 +
176  /* caller is responsible for file_start_write/file_end_write */
177  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
178  {
179 diff --git a/fs/splice.c b/fs/splice.c
180 index 4e53efbd621db..9f230ee854e20 100644
181 --- a/fs/splice.c
182 +++ b/fs/splice.c
183 @@ -849,8 +849,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
184  /*
185   * Attempt to initiate a splice from pipe to file.
186   */
187 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188 -                          loff_t *ppos, size_t len, unsigned int flags)
189 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
190 +                   loff_t *ppos, size_t len, unsigned int flags)
191  {
192         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
193                                 loff_t *, size_t, unsigned int);
194 @@ -866,9 +866,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
195  /*
196   * Attempt to initiate a splice from a file to a pipe.
197   */
198 -static long do_splice_to(struct file *in, loff_t *ppos,
199 -                        struct pipe_inode_info *pipe, size_t len,
200 -                        unsigned int flags)
201 +long do_splice_to(struct file *in, loff_t *ppos,
202 +                 struct pipe_inode_info *pipe, size_t len,
203 +                 unsigned int flags)
204  {
205         ssize_t (*splice_read)(struct file *, loff_t *,
206                                struct pipe_inode_info *, size_t, unsigned int);
207 diff --git a/fs/sync.c b/fs/sync.c
208 index 4d1ff010bc5af..457f4e4a5cc1f 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 45cc10cdf6ddd..916efd7e612b3 100644
222 --- a/include/linux/fs.h
223 +++ b/include/linux/fs.h
224 @@ -1355,6 +1355,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 @@ -1847,6 +1848,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 @@ -1917,6 +1919,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_write(struct file *, const char __user *, size_t, loff_t *);
252  extern ssize_t vfs_readv(struct file *, const struct iovec __user *,
253 @@ -2349,6 +2357,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 @@ -2633,6 +2642,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
262  }
263  
264  void emergency_thaw_all(void);
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 206774ac69460..e5eda5226fbc7 100644
271 --- a/include/linux/lockdep.h
272 +++ b/include/linux/lockdep.h
273 @@ -376,6 +376,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 @@ -521,6 +523,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 35942084cd40d..24f5fd1a789de 100644
292 --- a/include/linux/mnt_namespace.h
293 +++ b/include/linux/mnt_namespace.h
294 @@ -6,12 +6,15 @@
295  struct fs_struct;
296  struct user_namespace;
297  struct ns_common;
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  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
304  
305 +extern int is_current_mnt_ns(struct vfsmount *mnt);
306 +
307  extern const struct file_operations proc_mounts_operations;
308  extern const struct file_operations proc_mountinfo_operations;
309  extern const struct file_operations proc_mountstats_operations;
310 diff --git a/include/linux/splice.h b/include/linux/splice.h
311 index ebbbfea48aa0b..d68d574250283 100644
312 --- a/include/linux/splice.h
313 +++ b/include/linux/splice.h
314 @@ -90,4 +90,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
315  
316  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
317  extern const struct pipe_buf_operations default_pipe_buf_ops;
318 +
319 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
320 +                          loff_t *ppos, size_t len, unsigned int flags);
321 +extern long do_splice_to(struct file *in, loff_t *ppos,
322 +                        struct pipe_inode_info *pipe, size_t len,
323 +                        unsigned int flags);
324  #endif
325 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
326 index ac10db66cc63f..837a3358e77ca 100644
327 --- a/kernel/locking/lockdep.c
328 +++ b/kernel/locking/lockdep.c
329 @@ -169,7 +169,7 @@ static
330  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
331  static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
332  
333 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
334 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
335  {
336         unsigned int class_idx = hlock->class_idx;
337  
338 @@ -190,6 +190,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
339          */
340         return lock_classes + class_idx;
341  }
342 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
343  
344  #ifdef CONFIG_LOCK_STAT
345  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
346 SPDX-License-Identifier: GPL-2.0
347 aufs5.x-rcN mmap patch
348
349 diff --git a/fs/proc/base.c b/fs/proc/base.c
350 index eb2255e95f62b..92f6062423c0c 100644
351 --- a/fs/proc/base.c
352 +++ b/fs/proc/base.c
353 @@ -2162,7 +2162,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
354         rc = -ENOENT;
355         vma = find_exact_vma(mm, vm_start, vm_end);
356         if (vma && vma->vm_file) {
357 -               *path = vma->vm_file->f_path;
358 +               *path = vma_pr_or_file(vma)->f_path;
359                 path_get(path);
360                 rc = 0;
361         }
362 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
363 index 14c2badb8fd93..65afe5287e43e 100644
364 --- a/fs/proc/nommu.c
365 +++ b/fs/proc/nommu.c
366 @@ -41,7 +41,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
367         file = region->vm_file;
368  
369         if (file) {
370 -               struct inode *inode = file_inode(region->vm_file);
371 +               struct inode *inode;
372 +
373 +               file = vmr_pr_or_file(region);
374 +               inode = file_inode(file);
375                 dev = inode->i_sb->s_dev;
376                 ino = inode->i_ino;
377         }
378 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
379 index 8d382d4ec0672..93a768f754c91 100644
380 --- a/fs/proc/task_mmu.c
381 +++ b/fs/proc/task_mmu.c
382 @@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
383         const char *name = NULL;
384  
385         if (file) {
386 -               struct inode *inode = file_inode(vma->vm_file);
387 +               struct inode *inode;
388 +
389 +               file = vma_pr_or_file(vma);
390 +               inode = file_inode(file);
391                 dev = inode->i_sb->s_dev;
392                 ino = inode->i_ino;
393                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
394 @@ -1787,7 +1790,7 @@ static int show_numa_map(struct seq_file *m, void *v)
395         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
396         struct vm_area_struct *vma = v;
397         struct numa_maps *md = &numa_priv->md;
398 -       struct file *file = vma->vm_file;
399 +       struct file *file = vma_pr_or_file(vma);
400         struct mm_struct *mm = vma->vm_mm;
401         struct mempolicy *pol;
402         char buffer[64];
403 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
404 index 7907e6419e572..d17209cf52bc3 100644
405 --- a/fs/proc/task_nommu.c
406 +++ b/fs/proc/task_nommu.c
407 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
408         file = vma->vm_file;
409  
410         if (file) {
411 -               struct inode *inode = file_inode(vma->vm_file);
412 +               struct inode *inode;
413 +
414 +               file = vma_pr_or_file(vma);
415 +               inode = file_inode(file);
416                 dev = inode->i_sb->s_dev;
417                 ino = inode->i_ino;
418                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
419 diff --git a/include/linux/mm.h b/include/linux/mm.h
420 index f3fe7371855ce..a5b9751a5b331 100644
421 --- a/include/linux/mm.h
422 +++ b/include/linux/mm.h
423 @@ -1688,6 +1688,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
424         unmap_mapping_range(mapping, holebegin, holelen, 0);
425  }
426  
427 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
428 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
429 +                                     int);
430 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
431 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
432 +
433 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
434 +                                                               __LINE__)
435 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
436 +                                                         __LINE__)
437 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
438 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
439 +
440 +#ifndef CONFIG_MMU
441 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
442 +extern void vmr_do_fput(struct vm_region *, const char[], int);
443 +
444 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
445 +                                                         __LINE__)
446 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
447 +#endif /* !CONFIG_MMU */
448 +
449  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
450                 void *buf, int len, unsigned int gup_flags);
451  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
452 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
453 index 4aba6c0c2ba80..974802987f69d 100644
454 --- a/include/linux/mm_types.h
455 +++ b/include/linux/mm_types.h
456 @@ -272,6 +272,7 @@ struct vm_region {
457         unsigned long   vm_top;         /* region allocated to here */
458         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
459         struct file     *vm_file;       /* the backing file or NULL */
460 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
461  
462         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
463         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
464 @@ -351,6 +352,7 @@ struct vm_area_struct {
465         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
466                                            units */
467         struct file * vm_file;          /* File we map to (can be NULL). */
468 +       struct file *vm_prfile;         /* shadow of vm_file */
469         void * vm_private_data;         /* was vm_pte (shared mem) */
470  
471  #ifdef CONFIG_SWAP
472 diff --git a/kernel/fork.c b/kernel/fork.c
473 index 48ed22774efaa..d7439388c1c52 100644
474 --- a/kernel/fork.c
475 +++ b/kernel/fork.c
476 @@ -568,7 +568,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
477                         struct inode *inode = file_inode(file);
478                         struct address_space *mapping = file->f_mapping;
479  
480 -                       get_file(file);
481 +                       vma_get_file(tmp);
482                         if (tmp->vm_flags & VM_DENYWRITE)
483                                 atomic_dec(&inode->i_writecount);
484                         i_mmap_lock_write(mapping);
485 diff --git a/mm/Makefile b/mm/Makefile
486 index fccd3756b25f0..51485ceec650a 100644
487 --- a/mm/Makefile
488 +++ b/mm/Makefile
489 @@ -44,7 +44,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
490                            mm_init.o mmu_context.o percpu.o slab_common.o \
491                            compaction.o vmacache.o \
492                            interval_tree.o list_lru.o workingset.o \
493 -                          debug.o gup.o $(mmu-y)
494 +                          prfile.o debug.o gup.o $(mmu-y)
495  
496  # Give 'page_alloc' its own module-parameter namespace
497  page-alloc-y := page_alloc.o
498 diff --git a/mm/filemap.c b/mm/filemap.c
499 index 23a051a7ef0fb..e2fe9132feb21 100644
500 --- a/mm/filemap.c
501 +++ b/mm/filemap.c
502 @@ -2660,7 +2660,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
503         vm_fault_t ret = VM_FAULT_LOCKED;
504  
505         sb_start_pagefault(inode->i_sb);
506 -       file_update_time(vmf->vma->vm_file);
507 +       vma_file_update_time(vmf->vma);
508         lock_page(page);
509         if (page->mapping != inode->i_mapping) {
510                 unlock_page(page);
511 diff --git a/mm/mmap.c b/mm/mmap.c
512 index f609e9ec4a253..d554b349c653c 100644
513 --- a/mm/mmap.c
514 +++ b/mm/mmap.c
515 @@ -179,7 +179,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
516         if (vma->vm_ops && vma->vm_ops->close)
517                 vma->vm_ops->close(vma);
518         if (vma->vm_file)
519 -               fput(vma->vm_file);
520 +               vma_fput(vma);
521         mpol_put(vma_policy(vma));
522         vm_area_free(vma);
523         return next;
524 @@ -910,7 +910,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
525         if (remove_next) {
526                 if (file) {
527                         uprobe_munmap(next, next->vm_start, next->vm_end);
528 -                       fput(file);
529 +                       vma_fput(vma);
530                 }
531                 if (next->anon_vma)
532                         anon_vma_merge(vma, next);
533 @@ -1831,8 +1831,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
534         return addr;
535  
536  unmap_and_free_vma:
537 +       vma_fput(vma);
538         vma->vm_file = NULL;
539 -       fput(file);
540  
541         /* Undo any partial mapping done by a device driver. */
542         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
543 @@ -2683,7 +2683,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
544                 goto out_free_mpol;
545  
546         if (new->vm_file)
547 -               get_file(new->vm_file);
548 +               vma_get_file(new);
549  
550         if (new->vm_ops && new->vm_ops->open)
551                 new->vm_ops->open(new);
552 @@ -2702,7 +2702,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
553         if (new->vm_ops && new->vm_ops->close)
554                 new->vm_ops->close(new);
555         if (new->vm_file)
556 -               fput(new->vm_file);
557 +               vma_fput(new);
558         unlink_anon_vmas(new);
559   out_free_mpol:
560         mpol_put(vma_policy(new));
561 @@ -2894,7 +2894,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
562         struct vm_area_struct *vma;
563         unsigned long populate = 0;
564         unsigned long ret = -EINVAL;
565 -       struct file *file;
566 +       struct file *file, *prfile;
567  
568         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
569                      current->comm, current->pid);
570 @@ -2969,10 +2969,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
571                 }
572         }
573  
574 -       file = get_file(vma->vm_file);
575 +       vma_get_file(vma);
576 +       file = vma->vm_file;
577 +       prfile = vma->vm_prfile;
578         ret = do_mmap(vma->vm_file, start, size,
579                         prot, flags, pgoff, &populate, NULL);
580 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
581 +               struct vm_area_struct *new_vma;
582 +
583 +               new_vma = find_vma(mm, ret);
584 +               if (!new_vma->vm_prfile)
585 +                       new_vma->vm_prfile = prfile;
586 +               if (new_vma != vma)
587 +                       get_file(prfile);
588 +       }
589 +       /*
590 +        * two fput()s instead of vma_fput(vma),
591 +        * coz vma may not be available anymore.
592 +        */
593         fput(file);
594 +       if (prfile)
595 +               fput(prfile);
596  out:
597         up_write(&mm->mmap_sem);
598         if (populate)
599 @@ -3263,7 +3280,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
600                 if (anon_vma_clone(new_vma, vma))
601                         goto out_free_mempol;
602                 if (new_vma->vm_file)
603 -                       get_file(new_vma->vm_file);
604 +                       vma_get_file(new_vma);
605                 if (new_vma->vm_ops && new_vma->vm_ops->open)
606                         new_vma->vm_ops->open(new_vma);
607                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
608 diff --git a/mm/nommu.c b/mm/nommu.c
609 index 318df4e236c99..7f051e86ea1dd 100644
610 --- a/mm/nommu.c
611 +++ b/mm/nommu.c
612 @@ -567,7 +567,7 @@ static void __put_nommu_region(struct vm_region *region)
613                 up_write(&nommu_region_sem);
614  
615                 if (region->vm_file)
616 -                       fput(region->vm_file);
617 +                       vmr_fput(region);
618  
619                 /* IO memory and memory shared directly out of the pagecache
620                  * from ramfs/tmpfs mustn't be released here */
621 @@ -699,7 +699,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
622         if (vma->vm_ops && vma->vm_ops->close)
623                 vma->vm_ops->close(vma);
624         if (vma->vm_file)
625 -               fput(vma->vm_file);
626 +               vma_fput(vma);
627         put_nommu_region(vma->vm_region);
628         vm_area_free(vma);
629  }
630 @@ -1222,7 +1222,7 @@ unsigned long do_mmap(struct file *file,
631                                         goto error_just_free;
632                                 }
633                         }
634 -                       fput(region->vm_file);
635 +                       vmr_fput(region);
636                         kmem_cache_free(vm_region_jar, region);
637                         region = pregion;
638                         result = start;
639 @@ -1299,10 +1299,10 @@ unsigned long do_mmap(struct file *file,
640         up_write(&nommu_region_sem);
641  error:
642         if (region->vm_file)
643 -               fput(region->vm_file);
644 +               vmr_fput(region);
645         kmem_cache_free(vm_region_jar, region);
646         if (vma->vm_file)
647 -               fput(vma->vm_file);
648 +               vma_fput(vma);
649         vm_area_free(vma);
650         return ret;
651  
652 diff --git a/mm/prfile.c b/mm/prfile.c
653 new file mode 100644
654 index 0000000000000..00d51187c3250
655 --- /dev/null
656 +++ b/mm/prfile.c
657 @@ -0,0 +1,86 @@
658 +// SPDX-License-Identifier: GPL-2.0
659 +/*
660 + * Mainly for aufs which mmap(2) different file and wants to print different
661 + * path in /proc/PID/maps.
662 + * Call these functions via macros defined in linux/mm.h.
663 + *
664 + * See Documentation/filesystems/aufs/design/06mmap.txt
665 + *
666 + * Copyright (c) 2014-2020 Junjro R. Okajima
667 + * Copyright (c) 2014 Ian Campbell
668 + */
669 +
670 +#include <linux/mm.h>
671 +#include <linux/file.h>
672 +#include <linux/fs.h>
673 +
674 +/* #define PRFILE_TRACE */
675 +static inline void prfile_trace(struct file *f, struct file *pr,
676 +                             const char func[], int line, const char func2[])
677 +{
678 +#ifdef PRFILE_TRACE
679 +       if (pr)
680 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
681 +#endif
682 +}
683 +
684 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
685 +                            int line)
686 +{
687 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
688 +
689 +       prfile_trace(f, pr, func, line, __func__);
690 +       file_update_time(f);
691 +       if (f && pr)
692 +               file_update_time(pr);
693 +}
694 +
695 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
696 +                              int line)
697 +{
698 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
699 +
700 +       prfile_trace(f, pr, func, line, __func__);
701 +       return (f && pr) ? pr : f;
702 +}
703 +
704 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
705 +{
706 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
707 +
708 +       prfile_trace(f, pr, func, line, __func__);
709 +       get_file(f);
710 +       if (f && pr)
711 +               get_file(pr);
712 +}
713 +
714 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
715 +{
716 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
717 +
718 +       prfile_trace(f, pr, func, line, __func__);
719 +       fput(f);
720 +       if (f && pr)
721 +               fput(pr);
722 +}
723 +
724 +#ifndef CONFIG_MMU
725 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
726 +                              int line)
727 +{
728 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
729 +
730 +       prfile_trace(f, pr, func, line, __func__);
731 +       return (f && pr) ? pr : f;
732 +}
733 +
734 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
735 +{
736 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
737 +
738 +       prfile_trace(f, pr, func, line, __func__);
739 +       fput(f);
740 +       if (f && pr)
741 +               fput(pr);
742 +}
743 +#endif /* !CONFIG_MMU */
744 SPDX-License-Identifier: GPL-2.0
745 aufs5.x-rcN standalone patch
746
747 diff --git a/fs/dcache.c b/fs/dcache.c
748 index 046000653e4d8..15aa871d1b450 100644
749 --- a/fs/dcache.c
750 +++ b/fs/dcache.c
751 @@ -1371,6 +1371,7 @@ void d_walk(struct dentry *parent, void *data,
752         seq = 1;
753         goto again;
754  }
755 +EXPORT_SYMBOL_GPL(d_walk);
756  
757  struct check_mount {
758         struct vfsmount *mnt;
759 @@ -2916,6 +2917,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
760  
761         write_sequnlock(&rename_lock);
762  }
763 +EXPORT_SYMBOL_GPL(d_exchange);
764  
765  /**
766   * d_ancestor - search for an ancestor
767 diff --git a/fs/exec.c b/fs/exec.c
768 index 2c465119affcc..239bcb8906e78 100644
769 --- a/fs/exec.c
770 +++ b/fs/exec.c
771 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
772         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
773                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
774  }
775 +EXPORT_SYMBOL_GPL(path_noexec);
776  
777  #ifdef CONFIG_USELIB
778  /*
779 diff --git a/fs/fcntl.c b/fs/fcntl.c
780 index 0b28a37f7e505..f2c90a416b751 100644
781 --- a/fs/fcntl.c
782 +++ b/fs/fcntl.c
783 @@ -85,6 +85,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
784   out:
785         return error;
786  }
787 +EXPORT_SYMBOL_GPL(setfl);
788  
789  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
790                       int force)
791 diff --git a/fs/file_table.c b/fs/file_table.c
792 index 30d55c9a1744a..34b9bbf4c5566 100644
793 --- a/fs/file_table.c
794 +++ b/fs/file_table.c
795 @@ -162,6 +162,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
796         }
797         return ERR_PTR(-ENFILE);
798  }
799 +EXPORT_SYMBOL_GPL(alloc_empty_file);
800  
801  /*
802   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
803 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
804  }
805  
806  EXPORT_SYMBOL(fput);
807 +EXPORT_SYMBOL_GPL(__fput_sync);
808  
809  void __init files_init(void)
810  {
811 diff --git a/fs/inode.c b/fs/inode.c
812 index 7700c5e4783b9..6284b41fcf3f0 100644
813 --- a/fs/inode.c
814 +++ b/fs/inode.c
815 @@ -1694,6 +1694,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
816                 return inode->i_op->update_time(inode, time, flags);
817         return generic_update_time(inode, time, flags);
818  }
819 +EXPORT_SYMBOL_GPL(update_time);
820  
821  /**
822   *     touch_atime     -       update the access time
823 diff --git a/fs/namespace.c b/fs/namespace.c
824 index 12e725e34c6ca..fa17b9d5926ba 100644
825 --- a/fs/namespace.c
826 +++ b/fs/namespace.c
827 @@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
828         mnt_dec_writers(real_mount(mnt));
829         preempt_enable();
830  }
831 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
832  
833  /**
834   * mnt_drop_write - give up write access to a mount
835 @@ -781,6 +782,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
836  {
837         return check_mnt(real_mount(mnt));
838  }
839 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
840  
841  /*
842   * vfsmount lock must be held for write
843 @@ -1903,6 +1905,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
844         }
845         return 0;
846  }
847 +EXPORT_SYMBOL_GPL(iterate_mounts);
848  
849  static void lock_mnt_tree(struct mount *mnt)
850  {
851 diff --git a/fs/notify/group.c b/fs/notify/group.c
852 index 133f723aca070..0b9f7f6d8390f 100644
853 --- a/fs/notify/group.c
854 +++ b/fs/notify/group.c
855 @@ -99,6 +99,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
856  {
857         refcount_inc(&group->refcnt);
858  }
859 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
860  
861  /*
862   * Drop a reference to a group.  Free it if it's through.
863 diff --git a/fs/open.c b/fs/open.c
864 index 719b320ede52b..f88ce55c1c998 100644
865 --- a/fs/open.c
866 +++ b/fs/open.c
867 @@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
868         inode_unlock(dentry->d_inode);
869         return ret;
870  }
871 +EXPORT_SYMBOL_GPL(do_truncate);
872  
873  long vfs_truncate(const struct path *path, loff_t length)
874  {
875 diff --git a/fs/read_write.c b/fs/read_write.c
876 index 5d684d7d29207..6f61c0b6aa664 100644
877 --- a/fs/read_write.c
878 +++ b/fs/read_write.c
879 @@ -469,6 +469,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
880  
881         return ret;
882  }
883 +EXPORT_SYMBOL_GPL(vfs_read);
884  
885  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
886  {
887 @@ -509,6 +510,7 @@ vfs_readf_t vfs_readf(struct file *file)
888                 return new_sync_read;
889         return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
890  }
891 +EXPORT_SYMBOL_GPL(vfs_readf);
892  
893  vfs_writef_t vfs_writef(struct file *file)
894  {
895 @@ -520,6 +522,7 @@ vfs_writef_t vfs_writef(struct file *file)
896                 return new_sync_write;
897         return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
898  }
899 +EXPORT_SYMBOL_GPL(vfs_writef);
900  
901  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
902  {
903 @@ -589,6 +592,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
904  
905         return ret;
906  }
907 +EXPORT_SYMBOL_GPL(vfs_write);
908  
909  /* file_ppos returns &file->f_pos or NULL if file is stream */
910  static inline loff_t *file_ppos(struct file *file)
911 diff --git a/fs/splice.c b/fs/splice.c
912 index 9f230ee854e20..a6bd05004433c 100644
913 --- a/fs/splice.c
914 +++ b/fs/splice.c
915 @@ -862,6 +862,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
916  
917         return splice_write(pipe, out, ppos, len, flags);
918  }
919 +EXPORT_SYMBOL_GPL(do_splice_from);
920  
921  /*
922   * Attempt to initiate a splice from a file to a pipe.
923 @@ -891,6 +892,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
924  
925         return splice_read(in, ppos, pipe, len, flags);
926  }
927 +EXPORT_SYMBOL_GPL(do_splice_to);
928  
929  /**
930   * splice_direct_to_actor - splices data directly between two non-pipes
931 diff --git a/fs/sync.c b/fs/sync.c
932 index 457f4e4a5cc1f..67c66358f3fe4 100644
933 --- a/fs/sync.c
934 +++ b/fs/sync.c
935 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
936                 sb->s_op->sync_fs(sb, wait);
937         return __sync_blockdev(sb->s_bdev, wait);
938  }
939 +EXPORT_SYMBOL_GPL(__sync_filesystem);
940  
941  /*
942   * Write out and wait upon all dirty data associated with this
943 diff --git a/fs/xattr.c b/fs/xattr.c
944 index 91608d9bfc6aa..02d19ab3ba540 100644
945 --- a/fs/xattr.c
946 +++ b/fs/xattr.c
947 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
948         *xattr_value = value;
949         return error;
950  }
951 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
952  
953  ssize_t
954  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
955 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
956 index 837a3358e77ca..715ba9c1b91ae 100644
957 --- a/kernel/locking/lockdep.c
958 +++ b/kernel/locking/lockdep.c
959 @@ -190,6 +190,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
960          */
961         return lock_classes + class_idx;
962  }
963 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
964  #define hlock_class(hlock) lockdep_hlock_class(hlock)
965  
966  #ifdef CONFIG_LOCK_STAT
967 diff --git a/kernel/task_work.c b/kernel/task_work.c
968 index 825f28259a19a..b77593b29c1a5 100644
969 --- a/kernel/task_work.c
970 +++ b/kernel/task_work.c
971 @@ -126,3 +126,4 @@ void task_work_run(void)
972                 } while (work);
973         }
974  }
975 +EXPORT_SYMBOL_GPL(task_work_run);
976 diff --git a/security/security.c b/security/security.c
977 index 51de970fbb1ed..b21f1ffd90051 100644
978 --- a/security/security.c
979 +++ b/security/security.c
980 @@ -1087,6 +1087,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
981                 return 0;
982         return call_int_hook(path_rmdir, 0, dir, dentry);
983  }
984 +EXPORT_SYMBOL_GPL(security_path_rmdir);
985  
986  int security_path_unlink(const struct path *dir, struct dentry *dentry)
987  {
988 @@ -1103,6 +1104,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
989                 return 0;
990         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
991  }
992 +EXPORT_SYMBOL_GPL(security_path_symlink);
993  
994  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
995                        struct dentry *new_dentry)
996 @@ -1111,6 +1113,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
997                 return 0;
998         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
999  }
1000 +EXPORT_SYMBOL_GPL(security_path_link);
1001  
1002  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1003                          const struct path *new_dir, struct dentry *new_dentry,
1004 @@ -1138,6 +1141,7 @@ int security_path_truncate(const struct path *path)
1005                 return 0;
1006         return call_int_hook(path_truncate, 0, path);
1007  }
1008 +EXPORT_SYMBOL_GPL(security_path_truncate);
1009  
1010  int security_path_chmod(const struct path *path, umode_t mode)
1011  {
1012 @@ -1145,6 +1149,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1013                 return 0;
1014         return call_int_hook(path_chmod, 0, path, mode);
1015  }
1016 +EXPORT_SYMBOL_GPL(security_path_chmod);
1017  
1018  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1019  {
1020 @@ -1152,6 +1157,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1021                 return 0;
1022         return call_int_hook(path_chown, 0, path, uid, gid);
1023  }
1024 +EXPORT_SYMBOL_GPL(security_path_chown);
1025  
1026  int security_path_chroot(const struct path *path)
1027  {
1028 @@ -1252,6 +1258,7 @@ int security_inode_permission(struct inode *inode, int mask)
1029                 return 0;
1030         return call_int_hook(inode_permission, 0, inode, mask);
1031  }
1032 +EXPORT_SYMBOL_GPL(security_inode_permission);
1033  
1034  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1035  {
1036 @@ -1429,6 +1436,7 @@ int security_file_permission(struct file *file, int mask)
1037  
1038         return fsnotify_perm(file, mask);
1039  }
1040 +EXPORT_SYMBOL_GPL(security_file_permission);
1041  
1042  int security_file_alloc(struct file *file)
1043  {
1044 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1045 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1046 +++ linux/Documentation/ABI/testing/debugfs-aufs        2019-07-11 15:42:14.455570938 +0200
1047 @@ -0,0 +1,55 @@
1048 +What:          /debug/aufs/si_<id>/
1049 +Date:          March 2009
1050 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1051 +Description:
1052 +               Under /debug/aufs, a directory named si_<id> is created
1053 +               per aufs mount, where <id> is a unique id generated
1054 +               internally.
1055 +
1056 +What:          /debug/aufs/si_<id>/plink
1057 +Date:          Apr 2013
1058 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1059 +Description:
1060 +               It has three lines and shows the information about the
1061 +               pseudo-link. The first line is a single number
1062 +               representing a number of buckets. The second line is a
1063 +               number of pseudo-links per buckets (separated by a
1064 +               blank). The last line is a single number representing a
1065 +               total number of psedo-links.
1066 +               When the aufs mount option 'noplink' is specified, it
1067 +               will show "1\n0\n0\n".
1068 +
1069 +What:          /debug/aufs/si_<id>/xib
1070 +Date:          March 2009
1071 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1072 +Description:
1073 +               It shows the consumed blocks by xib (External Inode Number
1074 +               Bitmap), its block size and file size.
1075 +               When the aufs mount option 'noxino' is specified, it
1076 +               will be empty. About XINO files, see the aufs manual.
1077 +
1078 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1079 +Date:          March 2009
1080 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1081 +Description:
1082 +               It shows the consumed blocks by xino (External Inode Number
1083 +               Translation Table), its link count, block size and file
1084 +               size.
1085 +               Due to the file size limit, there may exist multiple
1086 +               xino files per branch.  In this case, "-N" is added to
1087 +               the filename and it corresponds to the index of the
1088 +               internal xino array.  "-0" is omitted.
1089 +               When the aufs mount option 'noxino' is specified, Those
1090 +               entries won't exist.  About XINO files, see the aufs
1091 +               manual.
1092 +
1093 +What:          /debug/aufs/si_<id>/xigen
1094 +Date:          March 2009
1095 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1096 +Description:
1097 +               It shows the consumed blocks by xigen (External Inode
1098 +               Generation Table), its block size and file size.
1099 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1100 +               be created.
1101 +               When the aufs mount option 'noxino' is specified, it
1102 +               will be empty. About XINO files, see the aufs manual.
1103 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1104 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1105 +++ linux/Documentation/ABI/testing/sysfs-aufs  2019-07-11 15:42:14.455570938 +0200
1106 @@ -0,0 +1,31 @@
1107 +What:          /sys/fs/aufs/si_<id>/
1108 +Date:          March 2009
1109 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1110 +Description:
1111 +               Under /sys/fs/aufs, a directory named si_<id> is created
1112 +               per aufs mount, where <id> is a unique id generated
1113 +               internally.
1114 +
1115 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1116 +Date:          March 2009
1117 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1118 +Description:
1119 +               It shows the abolute path of a member directory (which
1120 +               is called branch) in aufs, and its permission.
1121 +
1122 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1123 +Date:          July 2013
1124 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1125 +Description:
1126 +               It shows the id of a member directory (which is called
1127 +               branch) in aufs.
1128 +
1129 +What:          /sys/fs/aufs/si_<id>/xi_path
1130 +Date:          March 2009
1131 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1132 +Description:
1133 +               It shows the abolute path of XINO (External Inode Number
1134 +               Bitmap, Translation Table and Generation Table) file
1135 +               even if it is the default path.
1136 +               When the aufs mount option 'noxino' is specified, it
1137 +               will be empty. About XINO files, see the aufs manual.
1138 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1139 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1140 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2020-01-27 10:57:18.162204582 +0100
1141 @@ -0,0 +1,171 @@
1142 +
1143 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1144 +# 
1145 +# This program is free software; you can redistribute it and/or modify
1146 +# it under the terms of the GNU General Public License as published by
1147 +# the Free Software Foundation; either version 2 of the License, or
1148 +# (at your option) any later version.
1149 +# 
1150 +# This program is distributed in the hope that it will be useful,
1151 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1152 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1153 +# GNU General Public License for more details.
1154 +# 
1155 +# You should have received a copy of the GNU General Public License
1156 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1157 +
1158 +Introduction
1159 +----------------------------------------
1160 +
1161 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1162 +1. abbrev. for "advanced multi-layered unification filesystem".
1163 +2. abbrev. for "another unionfs".
1164 +3. abbrev. for "auf das" in German which means "on the" in English.
1165 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1166 +   But "Filesystem aufs Filesystem" is hard to understand.
1167 +4. abbrev. for "African Urban Fashion Show".
1168 +
1169 +AUFS is a filesystem with features:
1170 +- multi layered stackable unification filesystem, the member directory
1171 +  is called as a branch.
1172 +- branch permission and attribute, 'readonly', 'real-readonly',
1173 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1174 +  combination.
1175 +- internal "file copy-on-write".
1176 +- logical deletion, whiteout.
1177 +- dynamic branch manipulation, adding, deleting and changing permission.
1178 +- allow bypassing aufs, user's direct branch access.
1179 +- external inode number translation table and bitmap which maintains the
1180 +  persistent aufs inode number.
1181 +- seekable directory, including NFS readdir.
1182 +- file mapping, mmap and sharing pages.
1183 +- pseudo-link, hardlink over branches.
1184 +- loopback mounted filesystem as a branch.
1185 +- several policies to select one among multiple writable branches.
1186 +- revert a single systemcall when an error occurs in aufs.
1187 +- and more...
1188 +
1189 +
1190 +Multi Layered Stackable Unification Filesystem
1191 +----------------------------------------------------------------------
1192 +Most people already knows what it is.
1193 +It is a filesystem which unifies several directories and provides a
1194 +merged single directory. When users access a file, the access will be
1195 +passed/re-directed/converted (sorry, I am not sure which English word is
1196 +correct) to the real file on the member filesystem. The member
1197 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1198 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1199 +readonly branch is handled by creating 'whiteout' on the upper writable
1200 +branch.
1201 +
1202 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1203 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1204 +different approaches to implement the merged-view.
1205 +The former tries putting it into VFS, and the latter implements as a
1206 +separate filesystem.
1207 +(If I misunderstand about these implementations, please let me know and
1208 +I shall correct it. Because it is a long time ago when I read their
1209 +source files last time).
1210 +
1211 +UnionMount's approach will be able to small, but may be hard to share
1212 +branches between several UnionMount since the whiteout in it is
1213 +implemented in the inode on branch filesystem and always
1214 +shared. According to Bharata's post, readdir does not seems to be
1215 +finished yet.
1216 +There are several missing features known in this implementations such as
1217 +- for users, the inode number may change silently. eg. copy-up.
1218 +- link(2) may break by copy-up.
1219 +- read(2) may get an obsoleted filedata (fstat(2) too).
1220 +- fcntl(F_SETLK) may be broken by copy-up.
1221 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1222 +  open(O_RDWR).
1223 +
1224 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1225 +merged into mainline. This is another implementation of UnionMount as a
1226 +separated filesystem. All the limitations and known problems which
1227 +UnionMount are equally inherited to "overlay" filesystem.
1228 +
1229 +Unionfs has a longer history. When I started implementing a stackable
1230 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1231 +inode, dentry and file objects and they have an array pointing lower
1232 +same kind objects. After contributing many patches for Unionfs, I
1233 +re-started my project AUFS (Jun 2006).
1234 +
1235 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1236 +implemented my own ideas, approaches and enhancements and it became
1237 +totally different one.
1238 +
1239 +Comparing DM snapshot and fs based implementation
1240 +- the number of bytes to be copied between devices is much smaller.
1241 +- the type of filesystem must be one and only.
1242 +- the fs must be writable, no readonly fs, even for the lower original
1243 +  device. so the compression fs will not be usable. but if we use
1244 +  loopback mount, we may address this issue.
1245 +  for instance,
1246 +       mount /cdrom/squashfs.img /sq
1247 +       losetup /sq/ext2.img
1248 +       losetup /somewhere/cow
1249 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1250 +- it will be difficult (or needs more operations) to extract the
1251 +  difference between the original device and COW.
1252 +- DM snapshot-merge may help a lot when users try merging. in the
1253 +  fs-layer union, users will use rsync(1).
1254 +
1255 +You may want to read my old paper "Filesystems in LiveCD"
1256 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1257 +
1258 +
1259 +Several characters/aspects/persona of aufs
1260 +----------------------------------------------------------------------
1261 +
1262 +Aufs has several characters, aspects or persona.
1263 +1. a filesystem, callee of VFS helper
1264 +2. sub-VFS, caller of VFS helper for branches
1265 +3. a virtual filesystem which maintains persistent inode number
1266 +4. reader/writer of files on branches such like an application
1267 +
1268 +1. Callee of VFS Helper
1269 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1270 +unlink(2) from an application reaches sys_unlink() kernel function and
1271 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1272 +calls filesystem specific unlink operation. Actually aufs implements the
1273 +unlink operation but it behaves like a redirector.
1274 +
1275 +2. Caller of VFS Helper for Branches
1276 +aufs_unlink() passes the unlink request to the branch filesystem as if
1277 +it were called from VFS. So the called unlink operation of the branch
1278 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1279 +every necessary pre/post operation for the branch filesystem.
1280 +- acquire the lock for the parent dir on a branch
1281 +- lookup in a branch
1282 +- revalidate dentry on a branch
1283 +- mnt_want_write() for a branch
1284 +- vfs_unlink() for a branch
1285 +- mnt_drop_write() for a branch
1286 +- release the lock on a branch
1287 +
1288 +3. Persistent Inode Number
1289 +One of the most important issue for a filesystem is to maintain inode
1290 +numbers. This is particularly important to support exporting a
1291 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1292 +backend block device for its own. But some storage is necessary to
1293 +keep and maintain the inode numbers. It may be a large space and may not
1294 +suit to keep in memory. Aufs rents some space from its first writable
1295 +branch filesystem (by default) and creates file(s) on it. These files
1296 +are created by aufs internally and removed soon (currently) keeping
1297 +opened.
1298 +Note: Because these files are removed, they are totally gone after
1299 +      unmounting aufs. It means the inode numbers are not persistent
1300 +      across unmount or reboot. I have a plan to make them really
1301 +      persistent which will be important for aufs on NFS server.
1302 +
1303 +4. Read/Write Files Internally (copy-on-write)
1304 +Because a branch can be readonly, when you write a file on it, aufs will
1305 +"copy-up" it to the upper writable branch internally. And then write the
1306 +originally requested thing to the file. Generally kernel doesn't
1307 +open/read/write file actively. In aufs, even a single write may cause a
1308 +internal "file copy". This behaviour is very similar to cp(1) command.
1309 +
1310 +Some people may think it is better to pass such work to user space
1311 +helper, instead of doing in kernel space. Actually I am still thinking
1312 +about it. But currently I have implemented it in kernel space.
1313 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1314 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1315 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2020-01-27 10:57:18.162204582 +0100
1316 @@ -0,0 +1,258 @@
1317 +
1318 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1319 +# 
1320 +# This program is free software; you can redistribute it and/or modify
1321 +# it under the terms of the GNU General Public License as published by
1322 +# the Free Software Foundation; either version 2 of the License, or
1323 +# (at your option) any later version.
1324 +# 
1325 +# This program is distributed in the hope that it will be useful,
1326 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1327 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1328 +# GNU General Public License for more details.
1329 +# 
1330 +# You should have received a copy of the GNU General Public License
1331 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1332 +
1333 +Basic Aufs Internal Structure
1334 +
1335 +Superblock/Inode/Dentry/File Objects
1336 +----------------------------------------------------------------------
1337 +As like an ordinary filesystem, aufs has its own
1338 +superblock/inode/dentry/file objects. All these objects have a
1339 +dynamically allocated array and store the same kind of pointers to the
1340 +lower filesystem, branch.
1341 +For example, when you build a union with one readwrite branch and one
1342 +readonly, mounted /au, /rw and /ro respectively.
1343 +- /au = /rw + /ro
1344 +- /ro/fileA exists but /rw/fileA
1345 +
1346 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1347 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1348 +- [0] = NULL (because /rw/fileA doesn't exist)
1349 +- [1] = /ro/fileA
1350 +
1351 +This style of an array is essentially same to the aufs
1352 +superblock/inode/dentry/file objects.
1353 +
1354 +Because aufs supports manipulating branches, ie. add/delete/change
1355 +branches dynamically, these objects has its own generation. When
1356 +branches are changed, the generation in aufs superblock is
1357 +incremented. And a generation in other object are compared when it is
1358 +accessed. When a generation in other objects are obsoleted, aufs
1359 +refreshes the internal array.
1360 +
1361 +
1362 +Superblock
1363 +----------------------------------------------------------------------
1364 +Additionally aufs superblock has some data for policies to select one
1365 +among multiple writable branches, XIB files, pseudo-links and kobject.
1366 +See below in detail.
1367 +About the policies which supports copy-down a directory, see
1368 +wbr_policy.txt too.
1369 +
1370 +
1371 +Branch and XINO(External Inode Number Translation Table)
1372 +----------------------------------------------------------------------
1373 +Every branch has its own xino (external inode number translation table)
1374 +file. The xino file is created and unlinked by aufs internally. When two
1375 +members of a union exist on the same filesystem, they share the single
1376 +xino file.
1377 +The struct of a xino file is simple, just a sequence of aufs inode
1378 +numbers which is indexed by the lower inode number.
1379 +In the above sample, assume the inode number of /ro/fileA is i111 and
1380 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1381 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1382 +
1383 +When the inode numbers are not contiguous, the xino file will be sparse
1384 +which has a hole in it and doesn't consume as much disk space as it
1385 +might appear. If your branch filesystem consumes disk space for such
1386 +holes, then you should specify 'xino=' option at mounting aufs.
1387 +
1388 +Aufs has a mount option to free the disk blocks for such holes in XINO
1389 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1390 +meet a problem of disk shortage due to XINO files, then you should try
1391 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1392 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1393 +the holes in XINO files.
1394 +
1395 +Also a writable branch has three kinds of "whiteout bases". All these
1396 +are existed when the branch is joined to aufs, and their names are
1397 +whiteout-ed doubly, so that users will never see their names in aufs
1398 +hierarchy.
1399 +1. a regular file which will be hardlinked to all whiteouts.
1400 +2. a directory to store a pseudo-link.
1401 +3. a directory to store an "orphan"-ed file temporary.
1402 +
1403 +1. Whiteout Base
1404 +   When you remove a file on a readonly branch, aufs handles it as a
1405 +   logical deletion and creates a whiteout on the upper writable branch
1406 +   as a hardlink of this file in order not to consume inode on the
1407 +   writable branch.
1408 +2. Pseudo-link Dir
1409 +   See below, Pseudo-link.
1410 +3. Step-Parent Dir
1411 +   When "fileC" exists on the lower readonly branch only and it is
1412 +   opened and removed with its parent dir, and then user writes
1413 +   something into it, then aufs copies-up fileC to this
1414 +   directory. Because there is no other dir to store fileC. After
1415 +   creating a file under this dir, the file is unlinked.
1416 +
1417 +Because aufs supports manipulating branches, ie. add/delete/change
1418 +dynamically, a branch has its own id. When the branch order changes,
1419 +aufs finds the new index by searching the branch id.
1420 +
1421 +
1422 +Pseudo-link
1423 +----------------------------------------------------------------------
1424 +Assume "fileA" exists on the lower readonly branch only and it is
1425 +hardlinked to "fileB" on the branch. When you write something to fileA,
1426 +aufs copies-up it to the upper writable branch. Additionally aufs
1427 +creates a hardlink under the Pseudo-link Directory of the writable
1428 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1429 +simple list. If fileB is read after unlinking fileA, aufs returns
1430 +filedata from the pseudo-link instead of the lower readonly
1431 +branch. Because the pseudo-link is based upon the inode, to keep the
1432 +inode number by xino (see above) is essentially necessary.
1433 +
1434 +All the hardlinks under the Pseudo-link Directory of the writable branch
1435 +should be restored in a proper location later. Aufs provides a utility
1436 +to do this. The userspace helpers executed at remounting and unmounting
1437 +aufs by default.
1438 +During this utility is running, it puts aufs into the pseudo-link
1439 +maintenance mode. In this mode, only the process which began the
1440 +maintenance mode (and its child processes) is allowed to operate in
1441 +aufs. Some other processes which are not related to the pseudo-link will
1442 +be allowed to run too, but the rest have to return an error or wait
1443 +until the maintenance mode ends. If a process already acquires an inode
1444 +mutex (in VFS), it has to return an error.
1445 +
1446 +
1447 +XIB(external inode number bitmap)
1448 +----------------------------------------------------------------------
1449 +Addition to the xino file per a branch, aufs has an external inode number
1450 +bitmap in a superblock object. It is also an internal file such like a
1451 +xino file.
1452 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1453 +not.
1454 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1455 +
1456 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1457 +reduce the number of consumed disk blocks for these files.
1458 +
1459 +
1460 +Virtual or Vertical Dir, and Readdir in Userspace
1461 +----------------------------------------------------------------------
1462 +In order to support multiple layers (branches), aufs readdir operation
1463 +constructs a virtual dir block on memory. For readdir, aufs calls
1464 +vfs_readdir() internally for each dir on branches, merges their entries
1465 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1466 +object. So the file object has its entry list until it is closed. The
1467 +entry list will be updated when the file position is zero and becomes
1468 +obsoleted. This decision is made in aufs automatically.
1469 +
1470 +The dynamically allocated memory block for the name of entries has a
1471 +unit of 512 bytes (by default) and stores the names contiguously (no
1472 +padding). Another block for each entry is handled by kmem_cache too.
1473 +During building dir blocks, aufs creates hash list and judging whether
1474 +the entry is whiteouted by its upper branch or already listed.
1475 +The merged result is cached in the corresponding inode object and
1476 +maintained by a customizable life-time option.
1477 +
1478 +Some people may call it can be a security hole or invite DoS attack
1479 +since the opened and once readdir-ed dir (file object) holds its entry
1480 +list and becomes a pressure for system memory. But I'd say it is similar
1481 +to files under /proc or /sys. The virtual files in them also holds a
1482 +memory page (generally) while they are opened. When an idea to reduce
1483 +memory for them is introduced, it will be applied to aufs too.
1484 +For those who really hate this situation, I've developed readdir(3)
1485 +library which operates this merging in userspace. You just need to set
1486 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1487 +kernel space for readdir(3).
1488 +
1489 +
1490 +Workqueue
1491 +----------------------------------------------------------------------
1492 +Aufs sometimes requires privilege access to a branch. For instance,
1493 +in copy-up/down operation. When a user process is going to make changes
1494 +to a file which exists in the lower readonly branch only, and the mode
1495 +of one of ancestor directories may not be writable by a user
1496 +process. Here aufs copy-up the file with its ancestors and they may
1497 +require privilege to set its owner/group/mode/etc.
1498 +This is a typical case of a application character of aufs (see
1499 +Introduction).
1500 +
1501 +Aufs uses workqueue synchronously for this case. It creates its own
1502 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1503 +passes the request to call mkdir or write (for example), and wait for
1504 +its completion. This approach solves a problem of a signal handler
1505 +simply.
1506 +If aufs didn't adopt the workqueue and changed the privilege of the
1507 +process, then the process may receive the unexpected SIGXFSZ or other
1508 +signals.
1509 +
1510 +Also aufs uses the system global workqueue ("events" kernel thread) too
1511 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1512 +whiteout base and etc. This is unrelated to a privilege.
1513 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1514 +superblock at the beginning, at the same time waits for the completion
1515 +of all queued asynchronous tasks.
1516 +
1517 +
1518 +Whiteout
1519 +----------------------------------------------------------------------
1520 +The whiteout in aufs is very similar to Unionfs's. That is represented
1521 +by its filename. UnionMount takes an approach of a file mode, but I am
1522 +afraid several utilities (find(1) or something) will have to support it.
1523 +
1524 +Basically the whiteout represents "logical deletion" which stops aufs to
1525 +lookup further, but also it represents "dir is opaque" which also stop
1526 +further lookup.
1527 +
1528 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1529 +In order to make several functions in a single systemcall to be
1530 +revertible, aufs adopts an approach to rename a directory to a temporary
1531 +unique whiteouted name.
1532 +For example, in rename(2) dir where the target dir already existed, aufs
1533 +renames the target dir to a temporary unique whiteouted name before the
1534 +actual rename on a branch, and then handles other actions (make it opaque,
1535 +update the attributes, etc). If an error happens in these actions, aufs
1536 +simply renames the whiteouted name back and returns an error. If all are
1537 +succeeded, aufs registers a function to remove the whiteouted unique
1538 +temporary name completely and asynchronously to the system global
1539 +workqueue.
1540 +
1541 +
1542 +Copy-up
1543 +----------------------------------------------------------------------
1544 +It is a well-known feature or concept.
1545 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1546 +internally and makes change to the new file on the upper writable branch.
1547 +When the trigger systemcall does not update the timestamps of the parent
1548 +dir, aufs reverts it after copy-up.
1549 +
1550 +
1551 +Move-down (aufs3.9 and later)
1552 +----------------------------------------------------------------------
1553 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1554 +the lower readonly branch to the upper writable branch when a user
1555 +changes something about the file.
1556 +"Move-down" is an opposite action of copy-up. Basically this action is
1557 +ran manually instead of automatically and internally.
1558 +For desgin and implementation, aufs has to consider these issues.
1559 +- whiteout for the file may exist on the lower branch.
1560 +- ancestor directories may not exist on the lower branch.
1561 +- diropq for the ancestor directories may exist on the upper branch.
1562 +- free space on the lower branch will reduce.
1563 +- another access to the file may happen during moving-down, including
1564 +  UDBA (see "Revalidate Dentry and UDBA").
1565 +- the file should not be hard-linked nor pseudo-linked. they should be
1566 +  handled by auplink utility later.
1567 +
1568 +Sometimes users want to move-down a file from the upper writable branch
1569 +to the lower readonly or writable branch. For instance,
1570 +- the free space of the upper writable branch is going to run out.
1571 +- create a new intermediate branch between the upper and lower branch.
1572 +- etc.
1573 +
1574 +For this purpose, use "aumvdown" command in aufs-util.git.
1575 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1576 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1577 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2020-01-27 10:57:18.162204582 +0100
1578 @@ -0,0 +1,85 @@
1579 +
1580 +# Copyright (C) 2015-2020 Junjiro R. Okajima
1581 +# 
1582 +# This program is free software; you can redistribute it and/or modify
1583 +# it under the terms of the GNU General Public License as published by
1584 +# the Free Software Foundation; either version 2 of the License, or
1585 +# (at your option) any later version.
1586 +# 
1587 +# This program is distributed in the hope that it will be useful,
1588 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1589 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1590 +# GNU General Public License for more details.
1591 +# 
1592 +# You should have received a copy of the GNU General Public License
1593 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1594 +
1595 +Support for a branch who has its ->atomic_open()
1596 +----------------------------------------------------------------------
1597 +The filesystems who implement its ->atomic_open() are not majority. For
1598 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1599 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1600 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1601 +sure whether all filesystems who have ->atomic_open() behave like this,
1602 +but NFSv4 surely returns the error.
1603 +
1604 +In order to support ->atomic_open() for aufs, there are a few
1605 +approaches.
1606 +
1607 +A. Introduce aufs_atomic_open()
1608 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1609 +     branch fs.
1610 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1611 +   an aufs user Pip Cet's approach
1612 +   - calls aufs_create(), VFS finish_open() and notify_change().
1613 +   - pass fake-mode to finish_open(), and then correct the mode by
1614 +     notify_change().
1615 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1616 +   - no aufs_atomic_open().
1617 +   - aufs_lookup() registers the TID to an aufs internal object.
1618 +   - aufs_create() does nothing when the matching TID is registered, but
1619 +     registers the mode.
1620 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1621 +     TID is registered.
1622 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1623 +   credential
1624 +   - no aufs_atomic_open().
1625 +   - aufs_create() registers the TID to an internal object. this info
1626 +     represents "this process created this file just now."
1627 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1628 +     registered TID and re-try open() with superuser's credential.
1629 +
1630 +Pros and cons for each approach.
1631 +
1632 +A.
1633 +   - straightforward but highly depends upon VFS internal.
1634 +   - the atomic behavaiour is kept.
1635 +   - some of parameters such as nameidata are hard to reproduce for
1636 +     branch fs.
1637 +   - large overhead.
1638 +B.
1639 +   - easy to implement.
1640 +   - the atomic behavaiour is lost.
1641 +C.
1642 +   - the atomic behavaiour is kept.
1643 +   - dirty and tricky.
1644 +   - VFS checks whether the file is created correctly after calling
1645 +     ->create(), which means this approach doesn't work.
1646 +D.
1647 +   - easy to implement.
1648 +   - the atomic behavaiour is lost.
1649 +   - to open a file with superuser's credential and give it to a user
1650 +     process is a bad idea, since the file object keeps the credential
1651 +     in it. It may affect LSM or something. This approach doesn't work
1652 +     either.
1653 +
1654 +The approach A is ideal, but it hard to implement. So here is a
1655 +variation of A, which is to be implemented.
1656 +
1657 +A-1. Introduce aufs_atomic_open()
1658 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1659 +       vfs_create() and finish_open().
1660 +     - the demerit is that the several checks after branch fs
1661 +       ->atomic_open() are lost. in the ordinary case, the checks are
1662 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1663 +       be implemented in aufs, but not all I am afraid.
1664 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1665 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1666 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2020-01-27 10:57:18.165538015 +0100
1667 @@ -0,0 +1,113 @@
1668 +
1669 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1670 +# 
1671 +# This program is free software; you can redistribute it and/or modify
1672 +# it under the terms of the GNU General Public License as published by
1673 +# the Free Software Foundation; either version 2 of the License, or
1674 +# (at your option) any later version.
1675 +# 
1676 +# This program is distributed in the hope that it will be useful,
1677 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1678 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1679 +# GNU General Public License for more details.
1680 +# 
1681 +# You should have received a copy of the GNU General Public License
1682 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1683 +
1684 +Lookup in a Branch
1685 +----------------------------------------------------------------------
1686 +Since aufs has a character of sub-VFS (see Introduction), it operates
1687 +lookup for branches as VFS does. It may be a heavy work. But almost all
1688 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1689 +directly connected to its parent. Digging down the directory hierarchy
1690 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1691 +aufs calls it.
1692 +
1693 +When a branch is a remote filesystem, aufs basically relies upon its
1694 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1695 +them.
1696 +For d_revalidate, aufs implements three levels of revalidate tests. See
1697 +"Revalidate Dentry and UDBA" in detail.
1698 +
1699 +
1700 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1701 +----------------------------------------------------------------------
1702 +Let's try case study.
1703 +- aufs has two branches, upper readwrite and lower readonly.
1704 +  /au = /rw + /ro
1705 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1706 +- user invoked "chmod a+rx /au/dirA"
1707 +- the internal copy-up is activated and "/rw/dirA" is created and its
1708 +  permission bits are set to world readable.
1709 +- then "/au/dirA" becomes world readable?
1710 +
1711 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1712 +or it may be a natively readonly filesystem. If aufs respects the lower
1713 +branch, it should not respond readdir request from other users. But user
1714 +allowed it by chmod. Should really aufs rejects showing the entries
1715 +under /ro/dirA?
1716 +
1717 +To be honest, I don't have a good solution for this case. So aufs
1718 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1719 +users.
1720 +When dirperm1 is specified, aufs checks only the highest one for the
1721 +directory permission, and shows the entries. Otherwise, as usual, checks
1722 +every dir existing on all branches and rejects the request.
1723 +
1724 +As a side effect, dirperm1 option improves the performance of aufs
1725 +because the number of permission check is reduced when the number of
1726 +branch is many.
1727 +
1728 +
1729 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1730 +----------------------------------------------------------------------
1731 +Generally VFS helpers re-validate a dentry as a part of lookup.
1732 +0. digging down the directory hierarchy.
1733 +1. lock the parent dir by its i_mutex.
1734 +2. lookup the final (child) entry.
1735 +3. revalidate it.
1736 +4. call the actual operation (create, unlink, etc.)
1737 +5. unlock the parent dir
1738 +
1739 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1740 +called. Actually aufs implements it and checks the dentry on a branch is
1741 +still valid.
1742 +But it is not enough. Because aufs has to release the lock for the
1743 +parent dir on a branch at the end of ->lookup() (step 2) and
1744 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1745 +held by VFS.
1746 +If the file on a branch is changed directly, eg. bypassing aufs, after
1747 +aufs released the lock, then the subsequent operation may cause
1748 +something unpleasant result.
1749 +
1750 +This situation is a result of VFS architecture, ->lookup() and
1751 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1752 +design from VFS's point of view. It is just not suitable for sub-VFS
1753 +character in aufs.
1754 +
1755 +Aufs supports such case by three level of revalidation which is
1756 +selectable by user.
1757 +1. Simple Revalidate
1758 +   Addition to the native flow in VFS's, confirm the child-parent
1759 +   relationship on the branch just after locking the parent dir on the
1760 +   branch in the "actual operation" (step 4). When this validation
1761 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1762 +   checks the validation of the dentry on branches.
1763 +2. Monitor Changes Internally by Inotify/Fsnotify
1764 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1765 +   the dentry on the branch, and returns EBUSY if it finds different
1766 +   dentry.
1767 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1768 +   during it is in cache. When the event is notified, aufs registers a
1769 +   function to kernel 'events' thread by schedule_work(). And the
1770 +   function sets some special status to the cached aufs dentry and inode
1771 +   private data. If they are not cached, then aufs has nothing to
1772 +   do. When the same file is accessed through aufs (step 0-3) later,
1773 +   aufs will detect the status and refresh all necessary data.
1774 +   In this mode, aufs has to ignore the event which is fired by aufs
1775 +   itself.
1776 +3. No Extra Validation
1777 +   This is the simplest test and doesn't add any additional revalidation
1778 +   test, and skip the revalidation in step 4. It is useful and improves
1779 +   aufs performance when system surely hide the aufs branches from user,
1780 +   by over-mounting something (or another method).
1781 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1782 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1783 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2020-01-27 10:57:18.165538015 +0100
1784 @@ -0,0 +1,74 @@
1785 +
1786 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1787 +# 
1788 +# This program is free software; you can redistribute it and/or modify
1789 +# it under the terms of the GNU General Public License as published by
1790 +# the Free Software Foundation; either version 2 of the License, or
1791 +# (at your option) any later version.
1792 +# 
1793 +# This program is distributed in the hope that it will be useful,
1794 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1795 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1796 +# GNU General Public License for more details.
1797 +# 
1798 +# You should have received a copy of the GNU General Public License
1799 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1800 +
1801 +Branch Manipulation
1802 +
1803 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1804 +and changing its permission/attribute, there are a lot of works to do.
1805 +
1806 +
1807 +Add a Branch
1808 +----------------------------------------------------------------------
1809 +o Confirm the adding dir exists outside of aufs, including loopback
1810 +  mount, and its various attributes.
1811 +o Initialize the xino file and whiteout bases if necessary.
1812 +  See struct.txt.
1813 +
1814 +o Check the owner/group/mode of the directory
1815 +  When the owner/group/mode of the adding directory differs from the
1816 +  existing branch, aufs issues a warning because it may impose a
1817 +  security risk.
1818 +  For example, when a upper writable branch has a world writable empty
1819 +  top directory, a malicious user can create any files on the writable
1820 +  branch directly, like copy-up and modify manually. If something like
1821 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1822 +  writable branch, and the writable branch is world-writable, then a
1823 +  malicious guy may create /etc/passwd on the writable branch directly
1824 +  and the infected file will be valid in aufs.
1825 +  I am afraid it can be a security issue, but aufs can do nothing except
1826 +  producing a warning.
1827 +
1828 +
1829 +Delete a Branch
1830 +----------------------------------------------------------------------
1831 +o Confirm the deleting branch is not busy
1832 +  To be general, there is one merit to adopt "remount" interface to
1833 +  manipulate branches. It is to discard caches. At deleting a branch,
1834 +  aufs checks the still cached (and connected) dentries and inodes. If
1835 +  there are any, then they are all in-use. An inode without its
1836 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1837 +
1838 +  For the cached one, aufs checks whether the same named entry exists on
1839 +  other branches.
1840 +  If the cached one is a directory, because aufs provides a merged view
1841 +  to users, as long as one dir is left on any branch aufs can show the
1842 +  dir to users. In this case, the branch can be removed from aufs.
1843 +  Otherwise aufs rejects deleting the branch.
1844 +
1845 +  If any file on the deleting branch is opened by aufs, then aufs
1846 +  rejects deleting.
1847 +
1848 +
1849 +Modify the Permission of a Branch
1850 +----------------------------------------------------------------------
1851 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1852 +  See struct.txt.
1853 +
1854 +o rw --> ro: Confirm the modifying branch is not busy
1855 +  Aufs rejects the request if any of these conditions are true.
1856 +  - a file on the branch is mmap-ed.
1857 +  - a regular file on the branch is opened for write and there is no
1858 +    same named entry on the upper branch.
1859 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1860 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1861 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2020-01-27 10:57:18.165538015 +0100
1862 @@ -0,0 +1,64 @@
1863 +
1864 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1865 +# 
1866 +# This program is free software; you can redistribute it and/or modify
1867 +# it under the terms of the GNU General Public License as published by
1868 +# the Free Software Foundation; either version 2 of the License, or
1869 +# (at your option) any later version.
1870 +# 
1871 +# This program is distributed in the hope that it will be useful,
1872 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1873 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1874 +# GNU General Public License for more details.
1875 +# 
1876 +# You should have received a copy of the GNU General Public License
1877 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1878 +
1879 +Policies to Select One among Multiple Writable Branches
1880 +----------------------------------------------------------------------
1881 +When the number of writable branch is more than one, aufs has to decide
1882 +the target branch for file creation or copy-up. By default, the highest
1883 +writable branch which has the parent (or ancestor) dir of the target
1884 +file is chosen (top-down-parent policy).
1885 +By user's request, aufs implements some other policies to select the
1886 +writable branch, for file creation several policies, round-robin,
1887 +most-free-space, and other policies. For copy-up, top-down-parent,
1888 +bottom-up-parent, bottom-up and others.
1889 +
1890 +As expected, the round-robin policy selects the branch in circular. When
1891 +you have two writable branches and creates 10 new files, 5 files will be
1892 +created for each branch. mkdir(2) systemcall is an exception. When you
1893 +create 10 new directories, all will be created on the same branch.
1894 +And the most-free-space policy selects the one which has most free
1895 +space among the writable branches. The amount of free space will be
1896 +checked by aufs internally, and users can specify its time interval.
1897 +
1898 +The policies for copy-up is more simple,
1899 +top-down-parent is equivalent to the same named on in create policy,
1900 +bottom-up-parent selects the writable branch where the parent dir
1901 +exists and the nearest upper one from the copyup-source,
1902 +bottom-up selects the nearest upper writable branch from the
1903 +copyup-source, regardless the existence of the parent dir.
1904 +
1905 +There are some rules or exceptions to apply these policies.
1906 +- If there is a readonly branch above the policy-selected branch and
1907 +  the parent dir is marked as opaque (a variation of whiteout), or the
1908 +  target (creating) file is whiteout-ed on the upper readonly branch,
1909 +  then the result of the policy is ignored and the target file will be
1910 +  created on the nearest upper writable branch than the readonly branch.
1911 +- If there is a writable branch above the policy-selected branch and
1912 +  the parent dir is marked as opaque or the target file is whiteouted
1913 +  on the branch, then the result of the policy is ignored and the target
1914 +  file will be created on the highest one among the upper writable
1915 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1916 +  it as usual.
1917 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1918 +  They try selecting the branch where the source exists as possible
1919 +  since copyup a large file will take long time. If it can't be,
1920 +  ie. the branch where the source exists is readonly, then they will
1921 +  follow the copyup policy.
1922 +- There is an exception for rename(2) when the target exists.
1923 +  If the rename target exists, aufs compares the index of the branches
1924 +  where the source and the target exists and selects the higher
1925 +  one. If the selected branch is readonly, then aufs follows the
1926 +  copyup policy.
1927 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1928 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1929 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2019-07-11 15:42:14.458904362 +0200
1930 @@ -0,0 +1,31 @@
1931 +
1932 +// to view this graph, run dot(1) command in GRAPHVIZ.
1933 +
1934 +digraph G {
1935 +node [shape=box];
1936 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1937 +
1938 +node [shape=oval];
1939 +
1940 +aufs_rename -> whinfo [label="store/remove"];
1941 +
1942 +node [shape=oval];
1943 +inode_list [label="h_inum list in branch\ncache"];
1944 +
1945 +node [shape=box];
1946 +whinode [label="h_inum list file"];
1947 +
1948 +node [shape=oval];
1949 +brmgmt [label="br_add/del/mod/umount"];
1950 +
1951 +brmgmt -> inode_list [label="create/remove"];
1952 +brmgmt -> whinode [label="load/store"];
1953 +
1954 +inode_list -> whinode [style=dashed,dir=both];
1955 +
1956 +aufs_rename -> inode_list [label="add/del"];
1957 +
1958 +aufs_lookup -> inode_list [label="search"];
1959 +
1960 +aufs_lookup -> whinfo [label="load/remove"];
1961 +}
1962 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1963 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1964 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2020-01-27 10:57:18.165538015 +0100
1965 @@ -0,0 +1,102 @@
1966 +
1967 +# Copyright (C) 2017-2020 Junjiro R. Okajima
1968 +#
1969 +# This program is free software; you can redistribute it and/or modify
1970 +# it under the terms of the GNU General Public License as published by
1971 +# the Free Software Foundation; either version 2 of the License, or
1972 +# (at your option) any later version.
1973 +#
1974 +# This program is distributed in the hope that it will be useful,
1975 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1976 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1977 +# GNU General Public License for more details.
1978 +#
1979 +# You should have received a copy of the GNU General Public License
1980 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1981 +
1982 +Special handling for renaming a directory (DIRREN)
1983 +----------------------------------------------------------------------
1984 +First, let's assume we have a simple usecase.
1985 +
1986 +- /u = /rw + /ro
1987 +- /rw/dirA exists
1988 +- /ro/dirA and /ro/dirA/file exist too
1989 +- there is no dirB on both branches
1990 +- a user issues rename("dirA", "dirB")
1991 +
1992 +Now, what should aufs behave against this rename(2)?
1993 +There are a few possible cases.
1994 +
1995 +A. returns EROFS.
1996 +   since dirA exists on a readonly branch which cannot be renamed.
1997 +B. returns EXDEV.
1998 +   it is possible to copy-up dirA (only the dir itself), but the child
1999 +   entries ("file" in this case) should not be. it must be a bad
2000 +   approach to copy-up recursively.
2001 +C. returns a success.
2002 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
2003 +   is a violation of aufs' policy.
2004 +D. construct an extra information which indicates that /ro/dirA should
2005 +   be handled as the name of dirB.
2006 +   overlayfs has a similar feature called REDIRECT.
2007 +
2008 +Until now, aufs implements the case B only which returns EXDEV, and
2009 +expects the userspace application behaves like mv(1) which tries
2010 +issueing rename(2) recursively.
2011 +
2012 +A new aufs feature called DIRREN is introduced which implements the case
2013 +D. There are several "extra information" added.
2014 +
2015 +1. detailed info per renamed directory
2016 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2017 +2. the inode-number list of directories on a branch
2018 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
2019 +
2020 +The filename of "detailed info per directory" represents the lower
2021 +branch, and its format is
2022 +- a type of the branch id
2023 +  one of these.
2024 +  + uuid (not implemented yet)
2025 +  + fsid
2026 +  + dev
2027 +- the inode-number of the branch root dir
2028 +
2029 +And it contains these info in a single regular file.
2030 +- magic number
2031 +- branch's inode-number of the logically renamed dir
2032 +- the name of the before-renamed dir
2033 +
2034 +The "detailed info per directory" file is created in aufs rename(2), and
2035 +loaded in any lookup.
2036 +The info is considered in lookup for the matching case only. Here
2037 +"matching" means that the root of branch (in the info filename) is same
2038 +to the current looking-up branch. After looking-up the before-renamed
2039 +name, the inode-number is compared. And the matched dentry is used.
2040 +
2041 +The "inode-number list of directories" is a regular file which contains
2042 +simply the inode-numbers on the branch. The file is created or updated
2043 +in removing the branch, and loaded in adding the branch. Its lifetime is
2044 +equal to the branch.
2045 +The list is refered in lookup, and when the current target inode is
2046 +found in the list, the aufs tries loading the "detailed info per
2047 +directory" and get the changed and valid name of the dir.
2048 +
2049 +Theoretically these "extra informaiton" may be able to be put into XATTR
2050 +in the dir inode. But aufs doesn't choose this way because
2051 +1. XATTR may not be supported by the branch (or its configuration)
2052 +2. XATTR may have its size limit.
2053 +3. XATTR may be less easy to convert than a regular file, when the
2054 +   format of the info is changed in the future.
2055 +At the same time, I agree that the regular file approach is much slower
2056 +than XATTR approach. So, in the future, aufs may take the XATTR or other
2057 +better approach.
2058 +
2059 +This DIRREN feature is enabled by aufs configuration, and is activated
2060 +by a new mount option.
2061 +
2062 +For the more complicated case, there is a work with UDBA option, which
2063 +is to dected the direct access to the branches (by-passing aufs) and to
2064 +maintain the cashes in aufs. Since a single cached aufs dentry may
2065 +contains two names, before- and after-rename, the name comparision in
2066 +UDBA handler may not work correctly. In this case, the behaviour will be
2067 +equivalen to udba=reval case.
2068 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2069 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2070 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2020-01-27 10:57:18.165538015 +0100
2071 @@ -0,0 +1,120 @@
2072 +
2073 +# Copyright (C) 2011-2020 Junjiro R. Okajima
2074 +# 
2075 +# This program is free software; you can redistribute it and/or modify
2076 +# it under the terms of the GNU General Public License as published by
2077 +# the Free Software Foundation; either version 2 of the License, or
2078 +# (at your option) any later version.
2079 +# 
2080 +# This program is distributed in the hope that it will be useful,
2081 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2082 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2083 +# GNU General Public License for more details.
2084 +# 
2085 +# You should have received a copy of the GNU General Public License
2086 +# along with this program; if not, write to the Free Software
2087 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2088 +
2089 +
2090 +File-based Hierarchical Storage Management (FHSM)
2091 +----------------------------------------------------------------------
2092 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2093 +storage world. Aufs provides this feature as file-based with multiple
2094 +writable branches, based upon the principle of "Colder, the Lower".
2095 +Here the word "colder" means that the less used files, and "lower" means
2096 +that the position in the order of the stacked branches vertically.
2097 +These multiple writable branches are prioritized, ie. the topmost one
2098 +should be the fastest drive and be used heavily.
2099 +
2100 +o Characters in aufs FHSM story
2101 +- aufs itself and a new branch attribute.
2102 +- a new ioctl interface to move-down and to establish a connection with
2103 +  the daemon ("move-down" is a converse of "copy-up").
2104 +- userspace tool and daemon.
2105 +
2106 +The userspace daemon establishes a connection with aufs and waits for
2107 +the notification. The notified information is very similar to struct
2108 +statfs containing the number of consumed blocks and inodes.
2109 +When the consumed blocks/inodes of a branch exceeds the user-specified
2110 +upper watermark, the daemon activates its move-down process until the
2111 +consumed blocks/inodes reaches the user-specified lower watermark.
2112 +
2113 +The actual move-down is done by aufs based upon the request from
2114 +user-space since we need to maintain the inode number and the internal
2115 +pointer arrays in aufs.
2116 +
2117 +Currently aufs FHSM handles the regular files only. Additionally they
2118 +must not be hard-linked nor pseudo-linked.
2119 +
2120 +
2121 +o Cowork of aufs and the user-space daemon
2122 +  During the userspace daemon established the connection, aufs sends a
2123 +  small notification to it whenever aufs writes something into the
2124 +  writable branch. But it may cost high since aufs issues statfs(2)
2125 +  internally. So user can specify a new option to cache the
2126 +  info. Actually the notification is controlled by these factors.
2127 +  + the specified cache time.
2128 +  + classified as "force" by aufs internally.
2129 +  Until the specified time expires, aufs doesn't send the info
2130 +  except the forced cases. When aufs decide forcing, the info is always
2131 +  notified to userspace.
2132 +  For example, the number of free inodes is generally large enough and
2133 +  the shortage of it happens rarely. So aufs doesn't force the
2134 +  notification when creating a new file, directory and others. This is
2135 +  the typical case which aufs doesn't force.
2136 +  When aufs writes the actual filedata and the files consumes any of new
2137 +  blocks, the aufs forces notifying.
2138 +
2139 +
2140 +o Interfaces in aufs
2141 +- New branch attribute.
2142 +  + fhsm
2143 +    Specifies that the branch is managed by FHSM feature. In other word,
2144 +    participant in the FHSM.
2145 +    When nofhsm is set to the branch, it will not be the source/target
2146 +    branch of the move-down operation. This attribute is set
2147 +    independently from coo and moo attributes, and if you want full
2148 +    FHSM, you should specify them as well.
2149 +- New mount option.
2150 +  + fhsm_sec
2151 +    Specifies a second to suppress many less important info to be
2152 +    notified.
2153 +- New ioctl.
2154 +  + AUFS_CTL_FHSM_FD
2155 +    create a new file descriptor which userspace can read the notification
2156 +    (a subset of struct statfs) from aufs.
2157 +- Module parameter 'brs'
2158 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2159 +  be set.
2160 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2161 +  When there are two or more branches with fhsm attributes,
2162 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2163 +  terminates it. As a result of remounting and branch-manipulation, the
2164 +  number of branches with fhsm attribute can be one. In this case,
2165 +  /sbin/mount.aufs will terminate the user-space daemon.
2166 +
2167 +
2168 +Finally the operation is done as these steps in kernel-space.
2169 +- make sure that,
2170 +  + no one else is using the file.
2171 +  + the file is not hard-linked.
2172 +  + the file is not pseudo-linked.
2173 +  + the file is a regular file.
2174 +  + the parent dir is not opaqued.
2175 +- find the target writable branch.
2176 +- make sure the file is not whiteout-ed by the upper (than the target)
2177 +  branch.
2178 +- make the parent dir on the target branch.
2179 +- mutex lock the inode on the branch.
2180 +- unlink the whiteout on the target branch (if exists).
2181 +- lookup and create the whiteout-ed temporary name on the target branch.
2182 +- copy the file as the whiteout-ed temporary name on the target branch.
2183 +- rename the whiteout-ed temporary name to the original name.
2184 +- unlink the file on the source branch.
2185 +- maintain the internal pointer array and the external inode number
2186 +  table (XINO).
2187 +- maintain the timestamps and other attributes of the parent dir and the
2188 +  file.
2189 +
2190 +And of course, in every step, an error may happen. So the operation
2191 +should restore the original file state after an error happens.
2192 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2193 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2194 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2020-01-27 10:57:18.165538015 +0100
2195 @@ -0,0 +1,72 @@
2196 +
2197 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2198 +# 
2199 +# This program is free software; you can redistribute it and/or modify
2200 +# it under the terms of the GNU General Public License as published by
2201 +# the Free Software Foundation; either version 2 of the License, or
2202 +# (at your option) any later version.
2203 +# 
2204 +# This program is distributed in the hope that it will be useful,
2205 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2206 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2207 +# GNU General Public License for more details.
2208 +# 
2209 +# You should have received a copy of the GNU General Public License
2210 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2211 +
2212 +mmap(2) -- File Memory Mapping
2213 +----------------------------------------------------------------------
2214 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2215 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2216 +->mmap().
2217 +This approach is simple and good, but there is one problem.
2218 +Under /proc, several entries show the mmapped files by its path (with
2219 +device and inode number), and the printed path will be the path on the
2220 +branch fs's instead of virtual aufs's.
2221 +This is not a problem in most cases, but some utilities lsof(1) (and its
2222 +user) may expect the path on aufs.
2223 +
2224 +To address this issue, aufs adds a new member called vm_prfile in struct
2225 +vm_area_struct (and struct vm_region). The original vm_file points to
2226 +the file on the branch fs in order to handle everything correctly as
2227 +usual. The new vm_prfile points to a virtual file in aufs, and the
2228 +show-functions in procfs refers to vm_prfile if it is set.
2229 +Also we need to maintain several other places where touching vm_file
2230 +such like
2231 +- fork()/clone() copies vma and the reference count of vm_file is
2232 +  incremented.
2233 +- merging vma maintains the ref count too.
2234 +
2235 +This is not a good approach. It just fakes the printed path. But it
2236 +leaves all behaviour around f_mapping unchanged. This is surely an
2237 +advantage.
2238 +Actually aufs had adopted another complicated approach which calls
2239 +generic_file_mmap() and handles struct vm_operations_struct. In this
2240 +approach, aufs met a hard problem and I could not solve it without
2241 +switching the approach.
2242 +
2243 +There may be one more another approach which is
2244 +- bind-mount the branch-root onto the aufs-root internally
2245 +- grab the new vfsmount (ie. struct mount)
2246 +- lazy-umount the branch-root internally
2247 +- in open(2) the aufs-file, open the branch-file with the hidden
2248 +  vfsmount (instead of the original branch's vfsmount)
2249 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2250 +  but it may be possible from userspace by the mount helper.
2251 +
2252 +Adding the internal hidden vfsmount and using it in opening a file, the
2253 +file path under /proc will be printed correctly. This approach looks
2254 +smarter, but is not possible I am afraid.
2255 +- aufs-root may be bind-mount later. when it happens, another hidden
2256 +  vfsmount will be required.
2257 +- it is hard to get the chance to bind-mount and lazy-umount
2258 +  + in kernel-space, FS can have vfsmount in open(2) via
2259 +    file->f_path, and aufs can know its vfsmount. But several locks are
2260 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2261 +    here, then it may cause a deadlock.
2262 +  + in user-space, bind-mount doesn't invoke the mount helper.
2263 +- since /proc shows dev and ino, aufs has to give vma these info. it
2264 +  means a new member vm_prinode will be necessary. this is essentially
2265 +  equivalent to vm_prfile described above.
2266 +
2267 +I have to give up this "looks-smater" approach.
2268 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2269 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2270 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2020-01-27 10:57:18.165538015 +0100
2271 @@ -0,0 +1,96 @@
2272 +
2273 +# Copyright (C) 2014-2020 Junjiro R. Okajima
2274 +#
2275 +# This program is free software; you can redistribute it and/or modify
2276 +# it under the terms of the GNU General Public License as published by
2277 +# the Free Software Foundation; either version 2 of the License, or
2278 +# (at your option) any later version.
2279 +#
2280 +# This program is distributed in the hope that it will be useful,
2281 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2282 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2283 +# GNU General Public License for more details.
2284 +#
2285 +# You should have received a copy of the GNU General Public License
2286 +# along with this program; if not, write to the Free Software
2287 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2288 +
2289 +
2290 +Listing XATTR/EA and getting the value
2291 +----------------------------------------------------------------------
2292 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2293 +shows the values from the topmost existing file. This behaviour is good
2294 +for the non-dir entries since the bahaviour exactly matches the shown
2295 +information. But for the directories, aufs considers all the same named
2296 +entries on the lower branches. Which means, if one of the lower entry
2297 +rejects readdir call, then aufs returns an error even if the topmost
2298 +entry allows it. This behaviour is necessary to respect the branch fs's
2299 +security, but can make users confused since the user-visible standard
2300 +attributes don't match the behaviour.
2301 +To address this issue, aufs has a mount option called dirperm1 which
2302 +checks the permission for the topmost entry only, and ignores the lower
2303 +entry's permission.
2304 +
2305 +A similar issue can happen around XATTR.
2306 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2307 +always set. Otherwise these very unpleasant situation would happen.
2308 +- listxattr(2) may return the duplicated entries.
2309 +- users may not be able to remove or reset the XATTR forever,
2310 +
2311 +
2312 +XATTR/EA support in the internal (copy,move)-(up,down)
2313 +----------------------------------------------------------------------
2314 +Generally the extended attributes of inode are categorized as these.
2315 +- "security" for LSM and capability.
2316 +- "system" for posix ACL, 'acl' mount option is required for the branch
2317 +  fs generally.
2318 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2319 +- "user" for userspace, 'user_xattr' mount option is required for the
2320 +  branch fs generally.
2321 +
2322 +Moreover there are some other categories. Aufs handles these rather
2323 +unpopular categories as the ordinary ones, ie. there is no special
2324 +condition nor exception.
2325 +
2326 +In copy-up, the support for XATTR on the dst branch may differ from the
2327 +src branch. In this case, the copy-up operation will get an error and
2328 +the original user operation which triggered the copy-up will fail. It
2329 +can happen that even all copy-up will fail.
2330 +When both of src and dst branches support XATTR and if an error occurs
2331 +during copying XATTR, then the copy-up should fail obviously. That is a
2332 +good reason and aufs should return an error to userspace. But when only
2333 +the src branch support that XATTR, aufs should not return an error.
2334 +For example, the src branch supports ACL but the dst branch doesn't
2335 +because the dst branch may natively un-support it or temporary
2336 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2337 +may NOT return an error even if the XATTR is not supported. It is
2338 +totally up to the branch fs.
2339 +
2340 +Anyway when the aufs internal copy-up gets an error from the dst branch
2341 +fs, then aufs tries removing the just copied entry and returns the error
2342 +to the userspace. The worst case of this situation will be all copy-up
2343 +will fail.
2344 +
2345 +For the copy-up operation, there two basic approaches.
2346 +- copy the specified XATTR only (by category above), and return the
2347 +  error unconditionally if it happens.
2348 +- copy all XATTR, and ignore the error on the specified category only.
2349 +
2350 +In order to support XATTR and to implement the correct behaviour, aufs
2351 +chooses the latter approach and introduces some new branch attributes,
2352 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2353 +They correspond to the XATTR namespaces (see above). Additionally, to be
2354 +convenient, "icex" is also provided which means all "icex*" attributes
2355 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2356 +
2357 +The meaning of these attributes is to ignore the error from setting
2358 +XATTR on that branch.
2359 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2360 +error from the dst branch according to the specified attributes.
2361 +
2362 +Some XATTR may have its default value. The default value may come from
2363 +the parent dir or the environment. If the default value is set at the
2364 +file creating-time, it will be overwritten by copy-up.
2365 +Some contradiction may happen I am afraid.
2366 +Do we need another attribute to stop copying XATTR? I am unsure. For
2367 +now, aufs implements the branch attributes to ignore the error.
2368 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2369 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2370 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2020-01-27 10:57:18.165538015 +0100
2371 @@ -0,0 +1,58 @@
2372 +
2373 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2374 +# 
2375 +# This program is free software; you can redistribute it and/or modify
2376 +# it under the terms of the GNU General Public License as published by
2377 +# the Free Software Foundation; either version 2 of the License, or
2378 +# (at your option) any later version.
2379 +# 
2380 +# This program is distributed in the hope that it will be useful,
2381 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2382 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2383 +# GNU General Public License for more details.
2384 +# 
2385 +# You should have received a copy of the GNU General Public License
2386 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2387 +
2388 +Export Aufs via NFS
2389 +----------------------------------------------------------------------
2390 +Here is an approach.
2391 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2392 +  generation.
2393 +- iget_locked(): initialize aufs inode generation for a new inode, and
2394 +  store it in xigen file.
2395 +- destroy_inode(): increment aufs inode generation and store it in xigen
2396 +  file. it is necessary even if it is not unlinked, because any data of
2397 +  inode may be changed by UDBA.
2398 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2399 +  build file handle by
2400 +  + branch id (4 bytes)
2401 +  + superblock generation (4 bytes)
2402 +  + inode number (4 or 8 bytes)
2403 +  + parent dir inode number (4 or 8 bytes)
2404 +  + inode generation (4 bytes))
2405 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2406 +    bytes)
2407 +  + file handle for a branch (by exportfs_encode_fh())
2408 +- fh_to_dentry():
2409 +  + find the index of a branch from its id in handle, and check it is
2410 +    still exist in aufs.
2411 +  + 1st level: get the inode number from handle and search it in cache.
2412 +  + 2nd level: if not found in cache, get the parent inode number from
2413 +    the handle and search it in cache. and then open the found parent
2414 +    dir, find the matching inode number by vfs_readdir() and get its
2415 +    name, and call lookup_one_len() for the target dentry.
2416 +  + 3rd level: if the parent dir is not cached, call
2417 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2418 +    build a pathname of it, convert it a pathname in aufs, call
2419 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2420 +    the 2nd level.
2421 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2422 +    for every branch, but not itself. to get this, (currently) aufs
2423 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2424 +    idea, but I didn't get other approach.
2425 +  + test the generation of the gotten inode.
2426 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2427 +  convert it into ESTALE for NFSD.
2428 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2429 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2430 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2431 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2432 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2020-01-27 10:57:18.165538015 +0100
2433 @@ -0,0 +1,52 @@
2434 +
2435 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2436 +# 
2437 +# This program is free software; you can redistribute it and/or modify
2438 +# it under the terms of the GNU General Public License as published by
2439 +# the Free Software Foundation; either version 2 of the License, or
2440 +# (at your option) any later version.
2441 +# 
2442 +# This program is distributed in the hope that it will be useful,
2443 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2444 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2445 +# GNU General Public License for more details.
2446 +# 
2447 +# You should have received a copy of the GNU General Public License
2448 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2449 +
2450 +Show Whiteout Mode (shwh)
2451 +----------------------------------------------------------------------
2452 +Generally aufs hides the name of whiteouts. But in some cases, to show
2453 +them is very useful for users. For instance, creating a new middle layer
2454 +(branch) by merging existing layers.
2455 +
2456 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2457 +When you have three branches,
2458 +- Bottom: 'system', squashfs (underlying base system), read-only
2459 +- Middle: 'mods', squashfs, read-only
2460 +- Top: 'overlay', ram (tmpfs), read-write
2461 +
2462 +The top layer is loaded at boot time and saved at shutdown, to preserve
2463 +the changes made to the system during the session.
2464 +When larger changes have been made, or smaller changes have accumulated,
2465 +the size of the saved top layer data grows. At this point, it would be
2466 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2467 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2468 +restoring save and load speed.
2469 +
2470 +This merging is simplified by the use of another aufs mount, of just the
2471 +two overlay branches using the 'shwh' option.
2472 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2473 +       aufs /livesys/merge_union
2474 +
2475 +A merged view of these two branches is then available at
2476 +/livesys/merge_union, and the new feature is that the whiteouts are
2477 +visible!
2478 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2479 +writing to all branches. Also the default mode for all branches is 'ro'.
2480 +It is now possible to save the combined contents of the two overlay
2481 +branches to a new squashfs, e.g.:
2482 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2483 +
2484 +This new squashfs archive can be stored on the boot device and the
2485 +initramfs will use it to replace the old one at the next boot.
2486 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2487 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2488 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2020-01-27 10:57:18.165538015 +0100
2489 @@ -0,0 +1,47 @@
2490 +
2491 +# Copyright (C) 2010-2020 Junjiro R. Okajima
2492 +#
2493 +# This program is free software; you can redistribute it and/or modify
2494 +# it under the terms of the GNU General Public License as published by
2495 +# the Free Software Foundation; either version 2 of the License, or
2496 +# (at your option) any later version.
2497 +#
2498 +# This program is distributed in the hope that it will be useful,
2499 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2500 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2501 +# GNU General Public License for more details.
2502 +#
2503 +# You should have received a copy of the GNU General Public License
2504 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2505 +
2506 +Dynamically customizable FS operations
2507 +----------------------------------------------------------------------
2508 +Generally FS operations (struct inode_operations, struct
2509 +address_space_operations, struct file_operations, etc.) are defined as
2510 +"static const", but it never means that FS have only one set of
2511 +operation. Some FS have multiple sets of them. For instance, ext2 has
2512 +three sets, one for XIP, for NOBH, and for normal.
2513 +Since aufs overrides and redirects these operations, sometimes aufs has
2514 +to change its behaviour according to the branch FS type. More importantly
2515 +VFS acts differently if a function (member in the struct) is set or
2516 +not. It means aufs should have several sets of operations and select one
2517 +among them according to the branch FS definition.
2518 +
2519 +In order to solve this problem and not to affect the behaviour of VFS,
2520 +aufs defines these operations dynamically. For instance, aufs defines
2521 +dummy direct_IO function for struct address_space_operations, but it may
2522 +not be set to the address_space_operations actually. When the branch FS
2523 +doesn't have it, aufs doesn't set it to its address_space_operations
2524 +while the function definition itself is still alive. So the behaviour
2525 +itself will not change, and it will return an error when direct_IO is
2526 +not set.
2527 +
2528 +The lifetime of these dynamically generated operation object is
2529 +maintained by aufs branch object. When the branch is removed from aufs,
2530 +the reference counter of the object is decremented. When it reaches
2531 +zero, the dynamically generated operation object will be freed.
2532 +
2533 +This approach is designed to support AIO (io_submit), Direct I/O and
2534 +XIP (DAX) mainly.
2535 +Currently this approach is applied to address_space_operations for
2536 +regular files only.
2537 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2538 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2539 +++ linux/Documentation/filesystems/aufs/README 2020-01-23 09:59:36.748660743 +0100
2540 @@ -0,0 +1,401 @@
2541 +
2542 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2543 +http://aufs.sf.net
2544 +Junjiro R. Okajima
2545 +
2546 +
2547 +0. Introduction
2548 +----------------------------------------
2549 +In the early days, aufs was entirely re-designed and re-implemented
2550 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2551 +improvements and implementations, it became totally different from
2552 +Unionfs while keeping the basic features.
2553 +Later, Unionfs Version 2.x series began taking some of the same
2554 +approaches to aufs1's.
2555 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2556 +University and his team.
2557 +
2558 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2559 +support,
2560 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2561 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2562 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2563 +  or aufs1 from CVS on SourceForge.
2564 +
2565 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2566 +      According to Christoph Hellwig, linux rejects all union-type
2567 +      filesystems but UnionMount.
2568 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2569 +
2570 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2571 +    UnionMount, and he pointed out an issue around a directory mutex
2572 +    lock and aufs addressed it. But it is still unsure whether aufs will
2573 +    be merged (or any other union solution).
2574 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2575 +
2576 +
2577 +1. Features
2578 +----------------------------------------
2579 +- unite several directories into a single virtual filesystem. The member
2580 +  directory is called as a branch.
2581 +- you can specify the permission flags to the branch, which are 'readonly',
2582 +  'readwrite' and 'whiteout-able.'
2583 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2584 +  readonly branch are modifiable logically.
2585 +- dynamic branch manipulation, add, del.
2586 +- etc...
2587 +
2588 +Also there are many enhancements in aufs, such as:
2589 +- test only the highest one for the directory permission (dirperm1)
2590 +- copyup on open (coo=)
2591 +- 'move' policy for copy-up between two writable branches, after
2592 +  checking free space.
2593 +- xattr, acl
2594 +- readdir(3) in userspace.
2595 +- keep inode number by external inode number table
2596 +- keep the timestamps of file/dir in internal copyup operation
2597 +- seekable directory, supporting NFS readdir.
2598 +- whiteout is hardlinked in order to reduce the consumption of inodes
2599 +  on branch
2600 +- do not copyup, nor create a whiteout when it is unnecessary
2601 +- revert a single systemcall when an error occurs in aufs
2602 +- remount interface instead of ioctl
2603 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2604 +- loopback mounted filesystem as a branch
2605 +- kernel thread for removing the dir who has a plenty of whiteouts
2606 +- support copyup sparse file (a file which has a 'hole' in it)
2607 +- default permission flags for branches
2608 +- selectable permission flags for ro branch, whether whiteout can
2609 +  exist or not
2610 +- export via NFS.
2611 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2612 +- support multiple writable branches, some policies to select one
2613 +  among multiple writable branches.
2614 +- a new semantics for link(2) and rename(2) to support multiple
2615 +  writable branches.
2616 +- no glibc changes are required.
2617 +- pseudo hardlink (hardlink over branches)
2618 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2619 +  including NFS or remote filesystem branch.
2620 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2621 +- and more...
2622 +
2623 +Currently these features are dropped temporary from aufs5.
2624 +See design/08plan.txt in detail.
2625 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2626 +  (robr)
2627 +- statistics of aufs thread (/sys/fs/aufs/stat)
2628 +
2629 +Features or just an idea in the future (see also design/*.txt),
2630 +- reorder the branch index without del/re-add.
2631 +- permanent xino files for NFSD
2632 +- an option for refreshing the opened files after add/del branches
2633 +- light version, without branch manipulation. (unnecessary?)
2634 +- copyup in userspace
2635 +- inotify in userspace
2636 +- readv/writev
2637 +
2638 +
2639 +2. Download
2640 +----------------------------------------
2641 +There are three GIT trees for aufs5, aufs5-linux.git,
2642 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2643 +"aufs-util.git."
2644 +While the aufs-util is always necessary, you need either of aufs5-linux
2645 +or aufs5-standalone.
2646 +
2647 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2648 +git://git.kernel.org/.../torvalds/linux.git.
2649 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2650 +build aufs5 as an external kernel module.
2651 +Several extra patches are not included in this tree. Only
2652 +aufs5-standalone tree contains them. They are described in the later
2653 +section "Configuration and Compilation."
2654 +
2655 +On the other hand, the aufs5-standalone tree has only aufs source files
2656 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2657 +But you need to apply all aufs patches manually.
2658 +
2659 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2660 +represents the linux kernel version, "linux-5.x". For instance,
2661 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2662 +"aufs5.x-rcN" branch.
2663 +
2664 +o aufs5-linux tree
2665 +$ git clone --reference /your/linux/git/tree \
2666 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2667 +- if you don't have linux GIT tree, then remove "--reference ..."
2668 +$ cd aufs5-linux.git
2669 +$ git checkout origin/aufs5.0
2670 +
2671 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2672 +leave the patch-work to GIT.
2673 +$ cd /your/linux/git/tree
2674 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2675 +$ git fetch aufs5
2676 +$ git checkout -b my5.0 v5.0
2677 +$ (add your local change...)
2678 +$ git pull aufs5 aufs5.0
2679 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2680 +- you may need to solve some conflicts between your_changes and
2681 +  aufs5.0. in this case, git-rerere is recommended so that you can
2682 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2683 +  later in the future.
2684 +
2685 +o aufs5-standalone tree
2686 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2687 +$ cd aufs5-standalone.git
2688 +$ git checkout origin/aufs5.0
2689 +
2690 +o aufs-util tree
2691 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2692 +- note that the public aufs-util.git is on SourceForge instead of
2693 +  GitHUB.
2694 +$ cd aufs-util.git
2695 +$ git checkout origin/aufs5.0
2696 +
2697 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2698 +The minor version number, 'x' in '5.x', of aufs may not always
2699 +follow the minor version number of the kernel.
2700 +Because changes in the kernel that cause the use of a new
2701 +minor version number do not always require changes to aufs-util.
2702 +
2703 +Since aufs-util has its own minor version number, you may not be
2704 +able to find a GIT branch in aufs-util for your kernel's
2705 +exact minor version number.
2706 +In this case, you should git-checkout the branch for the
2707 +nearest lower number.
2708 +
2709 +For (an unreleased) example:
2710 +If you are using "linux-5.10" and the "aufs5.10" branch
2711 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2712 +or something numerically smaller is the branch for your kernel.
2713 +
2714 +Also you can view all branches by
2715 +       $ git branch -a
2716 +
2717 +
2718 +3. Configuration and Compilation
2719 +----------------------------------------
2720 +Make sure you have git-checkout'ed the correct branch.
2721 +
2722 +For aufs5-linux tree,
2723 +- enable CONFIG_AUFS_FS.
2724 +- set other aufs configurations if necessary.
2725 +
2726 +For aufs5-standalone tree,
2727 +There are several ways to build.
2728 +
2729 +1.
2730 +- apply ./aufs5-kbuild.patch to your kernel source files.
2731 +- apply ./aufs5-base.patch too.
2732 +- apply ./aufs5-mmap.patch too.
2733 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2734 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2735 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2736 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2737 +- enable CONFIG_AUFS_FS, you can select either
2738 +  =m or =y.
2739 +- and build your kernel as usual.
2740 +- install the built kernel.
2741 +- install the header files too by "make headers_install" to the
2742 +  directory where you specify. By default, it is $PWD/usr.
2743 +  "make help" shows a brief note for headers_install.
2744 +- and reboot your system.
2745 +
2746 +2.
2747 +- module only (CONFIG_AUFS_FS=m).
2748 +- apply ./aufs5-base.patch to your kernel source files.
2749 +- apply ./aufs5-mmap.patch too.
2750 +- apply ./aufs5-standalone.patch too.
2751 +- build your kernel, don't forget "make headers_install", and reboot.
2752 +- edit ./config.mk and set other aufs configurations if necessary.
2753 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2754 +  every aufs configurations.
2755 +- build the module by simple "make".
2756 +- you can specify ${KDIR} make variable which points to your kernel
2757 +  source tree.
2758 +- install the files
2759 +  + run "make install" to install the aufs module, or copy the built
2760 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2761 +  + run "make install_headers" (instead of headers_install) to install
2762 +    the modified aufs header file (you can specify DESTDIR which is
2763 +    available in aufs standalone version's Makefile only), or copy
2764 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2765 +    you like manually. By default, the target directory is $PWD/usr.
2766 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2767 +  kernel source tree.
2768 +
2769 +Note: The header file aufs_type.h is necessary to build aufs-util
2770 +      as well as "make headers_install" in the kernel source tree.
2771 +      headers_install is subject to be forgotten, but it is essentially
2772 +      necessary, not only for building aufs-util.
2773 +      You may not meet problems without headers_install in some older
2774 +      version though.
2775 +
2776 +And then,
2777 +- read README in aufs-util, build and install it
2778 +- note that your distribution may contain an obsoleted version of
2779 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2780 +  utilities, make sure that your compiler refers the correct aufs header
2781 +  file which is built by "make headers_install."
2782 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2783 +  then run "make install_ulib" too. And refer to the aufs manual in
2784 +  detail.
2785 +
2786 +There several other patches in aufs5-standalone.git. They are all
2787 +optional. When you meet some problems, they will help you.
2788 +- aufs5-loopback.patch
2789 +  Supports a nested loopback mount in a branch-fs. This patch is
2790 +  unnecessary until aufs produces a message like "you may want to try
2791 +  another patch for loopback file".
2792 +- proc_mounts.patch
2793 +  When there are many mountpoints and many mount(2)/umount(2) are
2794 +  running, then /proc/mounts may not show the all mountpoints.  This
2795 +  patch makes /proc/mounts always show the full mountpoints list.
2796 +  If you don't want to apply this patch and meet such problem, then you
2797 +  need to increase the value of 'ProcMounts_Times' make-variable in
2798 +  aufs-util.git as a second best solution.
2799 +- vfs-ino.patch
2800 +  Modifies a system global kernel internal function get_next_ino() in
2801 +  order to stop assigning 0 for an inode-number. Not directly related to
2802 +  aufs, but recommended generally.
2803 +- tmpfs-idr.patch
2804 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2805 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2806 +  duplication of inode number, which is important for backup tools and
2807 +  other utilities. When you find aufs XINO files for tmpfs branch
2808 +  growing too much, try this patch.
2809 +- lockdep-debug.patch
2810 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2811 +  also a caller of VFS functions for branch filesystems, subclassing of
2812 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2813 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2814 +  need to apply this debug patch to expand several constant values.
2815 +  If you don't know what LOCKDEP is, then you don't have apply this
2816 +  patch.
2817 +
2818 +
2819 +4. Usage
2820 +----------------------------------------
2821 +At first, make sure aufs-util are installed, and please read the aufs
2822 +manual, aufs.5 in aufs-util.git tree.
2823 +$ man -l aufs.5
2824 +
2825 +And then,
2826 +$ mkdir /tmp/rw /tmp/aufs
2827 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2828 +
2829 +Here is another example. The result is equivalent.
2830 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2831 +  Or
2832 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2833 +# mount -o remount,append:${HOME} /tmp/aufs
2834 +
2835 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2836 +you modify a file under /tmp/aufs, the one on your home directory is
2837 +not affected, instead the same named file will be newly created under
2838 +/tmp/rw. And all of your modification to a file will be applied to
2839 +the one under /tmp/rw. This is called the file based Copy on Write
2840 +(COW) method.
2841 +Aufs mount options are described in aufs.5.
2842 +If you run chroot or something and make your aufs as a root directory,
2843 +then you need to customize the shutdown script. See the aufs manual in
2844 +detail.
2845 +
2846 +Additionally, there are some sample usages of aufs which are a
2847 +diskless system with network booting, and LiveCD over NFS.
2848 +See sample dir in CVS tree on SourceForge.
2849 +
2850 +
2851 +5. Contact
2852 +----------------------------------------
2853 +When you have any problems or strange behaviour in aufs, please let me
2854 +know with:
2855 +- /proc/mounts (instead of the output of mount(8))
2856 +- /sys/module/aufs/*
2857 +- /sys/fs/aufs/* (if you have them)
2858 +- /debug/aufs/* (if you have them)
2859 +- linux kernel version
2860 +  if your kernel is not plain, for example modified by distributor,
2861 +  the url where i can download its source is necessary too.
2862 +- aufs version which was printed at loading the module or booting the
2863 +  system, instead of the date you downloaded.
2864 +- configuration (define/undefine CONFIG_AUFS_xxx)
2865 +- kernel configuration or /proc/config.gz (if you have it)
2866 +- LSM (linux security module, if you are using)
2867 +- behaviour which you think to be incorrect
2868 +- actual operation, reproducible one is better
2869 +- mailto: aufs-users at lists.sourceforge.net
2870 +
2871 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2872 +and Feature Requests) on SourceForge. Please join and write to
2873 +aufs-users ML.
2874 +
2875 +
2876 +6. Acknowledgements
2877 +----------------------------------------
2878 +Thanks to everyone who have tried and are using aufs, whoever
2879 +have reported a bug or any feedback.
2880 +
2881 +Especially donators:
2882 +Tomas Matejicek(slax.org) made a donation (much more than once).
2883 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2884 +       scripts) is making "doubling" donations.
2885 +       Unfortunately I cannot list all of the donators, but I really
2886 +       appreciate.
2887 +       It ends Aug 2010, but the ordinary donation URL is still available.
2888 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2889 +Dai Itasaka made a donation (2007/8).
2890 +Chuck Smith made a donation (2008/4, 10 and 12).
2891 +Henk Schoneveld made a donation (2008/9).
2892 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2893 +Francois Dupoux made a donation (2008/11).
2894 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2895 +       aufs2 GIT tree (2009/2).
2896 +William Grant made a donation (2009/3).
2897 +Patrick Lane made a donation (2009/4).
2898 +The Mail Archive (mail-archive.com) made donations (2009/5).
2899 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2900 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2901 +Pavel Pronskiy made a donation (2011/2).
2902 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2903 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2904 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2905 +11).
2906 +Sam Liddicott made a donation (2011/9).
2907 +Era Scarecrow made a donation (2013/4).
2908 +Bor Ratajc made a donation (2013/4).
2909 +Alessandro Gorreta made a donation (2013/4).
2910 +POIRETTE Marc made a donation (2013/4).
2911 +Alessandro Gorreta made a donation (2013/4).
2912 +lauri kasvandik made a donation (2013/5).
2913 +"pemasu from Finland" made a donation (2013/7).
2914 +The Parted Magic Project made a donation (2013/9 and 11).
2915 +Pavel Barta made a donation (2013/10).
2916 +Nikolay Pertsev made a donation (2014/5).
2917 +James B made a donation (2014/7 and 2015/7).
2918 +Stefano Di Biase made a donation (2014/8).
2919 +Daniel Epellei made a donation (2015/1).
2920 +OmegaPhil made a donation (2016/1, 2018/4).
2921 +Tomasz Szewczyk made a donation (2016/4).
2922 +James Burry made a donation (2016/12).
2923 +Carsten Rose made a donation (2018/9).
2924 +Porteus Kiosk made a donation (2018/10).
2925 +
2926 +Thank you very much.
2927 +Donations are always, including future donations, very important and
2928 +helpful for me to keep on developing aufs.
2929 +
2930 +
2931 +7.
2932 +----------------------------------------
2933 +If you are an experienced user, no explanation is needed. Aufs is
2934 +just a linux filesystem.
2935 +
2936 +
2937 +Enjoy!
2938 +
2939 +# Local variables: ;
2940 +# mode: text;
2941 +# End: ;
2942 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2943 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2944 +++ linux/fs/aufs/aufs.h        2020-01-27 10:57:18.165538015 +0100
2945 @@ -0,0 +1,62 @@
2946 +/* SPDX-License-Identifier: GPL-2.0 */
2947 +/*
2948 + * Copyright (C) 2005-2020 Junjiro R. Okajima
2949 + *
2950 + * This program, aufs is free software; you can redistribute it and/or modify
2951 + * it under the terms of the GNU General Public License as published by
2952 + * the Free Software Foundation; either version 2 of the License, or
2953 + * (at your option) any later version.
2954 + *
2955 + * This program is distributed in the hope that it will be useful,
2956 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2957 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2958 + * GNU General Public License for more details.
2959 + *
2960 + * You should have received a copy of the GNU General Public License
2961 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2962 + */
2963 +
2964 +/*
2965 + * all header files
2966 + */
2967 +
2968 +#ifndef __AUFS_H__
2969 +#define __AUFS_H__
2970 +
2971 +#ifdef __KERNEL__
2972 +
2973 +#define AuStub(type, name, body, ...) \
2974 +       static inline type name(__VA_ARGS__) { body; }
2975 +
2976 +#define AuStubVoid(name, ...) \
2977 +       AuStub(void, name, , __VA_ARGS__)
2978 +#define AuStubInt0(name, ...) \
2979 +       AuStub(int, name, return 0, __VA_ARGS__)
2980 +
2981 +#include "debug.h"
2982 +
2983 +#include "branch.h"
2984 +#include "cpup.h"
2985 +#include "dcsub.h"
2986 +#include "dbgaufs.h"
2987 +#include "dentry.h"
2988 +#include "dir.h"
2989 +#include "dirren.h"
2990 +#include "dynop.h"
2991 +#include "file.h"
2992 +#include "fstype.h"
2993 +#include "hbl.h"
2994 +#include "inode.h"
2995 +#include "lcnt.h"
2996 +#include "loop.h"
2997 +#include "module.h"
2998 +#include "opts.h"
2999 +#include "rwsem.h"
3000 +#include "super.h"
3001 +#include "sysaufs.h"
3002 +#include "vfsub.h"
3003 +#include "whout.h"
3004 +#include "wkq.h"
3005 +
3006 +#endif /* __KERNEL__ */
3007 +#endif /* __AUFS_H__ */
3008 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3009 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
3010 +++ linux/fs/aufs/branch.c      2020-08-03 09:14:46.095748745 +0200
3011 @@ -0,0 +1,1427 @@
3012 +// SPDX-License-Identifier: GPL-2.0
3013 +/*
3014 + * Copyright (C) 2005-2020 Junjiro R. Okajima
3015 + *
3016 + * This program, aufs is free software; you can redistribute it and/or modify
3017 + * it under the terms of the GNU General Public License as published by
3018 + * the Free Software Foundation; either version 2 of the License, or
3019 + * (at your option) any later version.
3020 + *
3021 + * This program is distributed in the hope that it will be useful,
3022 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3023 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3024 + * GNU General Public License for more details.
3025 + *
3026 + * You should have received a copy of the GNU General Public License
3027 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3028 + */
3029 +
3030 +/*
3031 + * branch management
3032 + */
3033 +
3034 +#include <linux/compat.h>
3035 +#include <linux/statfs.h>
3036 +#include "aufs.h"
3037 +
3038 +/*
3039 + * free a single branch
3040 + */
3041 +static void au_br_do_free(struct au_branch *br)
3042 +{
3043 +       int i;
3044 +       struct au_wbr *wbr;
3045 +       struct au_dykey **key;
3046 +
3047 +       au_hnotify_fin_br(br);
3048 +       /* always, regardless the mount option */
3049 +       au_dr_hino_free(&br->br_dirren);
3050 +       au_xino_put(br);
3051 +
3052 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3053 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3054 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3055 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
3056 +
3057 +       wbr = br->br_wbr;
3058 +       if (wbr) {
3059 +               for (i = 0; i < AuBrWh_Last; i++)
3060 +                       dput(wbr->wbr_wh[i]);
3061 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3062 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3063 +       }
3064 +
3065 +       if (br->br_fhsm) {
3066 +               au_br_fhsm_fin(br->br_fhsm);
3067 +               au_kfree_try_rcu(br->br_fhsm);
3068 +       }
3069 +
3070 +       key = br->br_dykey;
3071 +       for (i = 0; i < AuBrDynOp; i++, key++)
3072 +               if (*key)
3073 +                       au_dy_put(*key);
3074 +               else
3075 +                       break;
3076 +
3077 +       /* recursive lock, s_umount of branch's */
3078 +       /* synchronize_rcu(); */ /* why? */
3079 +       lockdep_off();
3080 +       path_put(&br->br_path);
3081 +       lockdep_on();
3082 +       au_kfree_rcu(wbr);
3083 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3084 +       au_lcnt_wait_for_fin(&br->br_count);
3085 +       /* I don't know why, but percpu_refcount requires this */
3086 +       /* synchronize_rcu(); */
3087 +       au_kfree_rcu(br);
3088 +}
3089 +
3090 +/*
3091 + * frees all branches
3092 + */
3093 +void au_br_free(struct au_sbinfo *sbinfo)
3094 +{
3095 +       aufs_bindex_t bmax;
3096 +       struct au_branch **br;
3097 +
3098 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3099 +
3100 +       bmax = sbinfo->si_bbot + 1;
3101 +       br = sbinfo->si_branch;
3102 +       while (bmax--)
3103 +               au_br_do_free(*br++);
3104 +}
3105 +
3106 +/*
3107 + * find the index of a branch which is specified by @br_id.
3108 + */
3109 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3110 +{
3111 +       aufs_bindex_t bindex, bbot;
3112 +
3113 +       bbot = au_sbbot(sb);
3114 +       for (bindex = 0; bindex <= bbot; bindex++)
3115 +               if (au_sbr_id(sb, bindex) == br_id)
3116 +                       return bindex;
3117 +       return -1;
3118 +}
3119 +
3120 +/* ---------------------------------------------------------------------- */
3121 +
3122 +/*
3123 + * add a branch
3124 + */
3125 +
3126 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3127 +                       struct dentry *h_root)
3128 +{
3129 +       if (unlikely(h_adding == h_root
3130 +                    || au_test_loopback_overlap(sb, h_adding)))
3131 +               return 1;
3132 +       if (h_adding->d_sb != h_root->d_sb)
3133 +               return 0;
3134 +       return au_test_subdir(h_adding, h_root)
3135 +               || au_test_subdir(h_root, h_adding);
3136 +}
3137 +
3138 +/*
3139 + * returns a newly allocated branch. @new_nbranch is a number of branches
3140 + * after adding a branch.
3141 + */
3142 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3143 +                                    int perm)
3144 +{
3145 +       struct au_branch *add_branch;
3146 +       struct dentry *root;
3147 +       struct inode *inode;
3148 +       int err;
3149 +
3150 +       err = -ENOMEM;
3151 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3152 +       if (unlikely(!add_branch))
3153 +               goto out;
3154 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3155 +       if (unlikely(!add_branch->br_xino))
3156 +               goto out_br;
3157 +       err = au_hnotify_init_br(add_branch, perm);
3158 +       if (unlikely(err))
3159 +               goto out_xino;
3160 +
3161 +       if (au_br_writable(perm)) {
3162 +               /* may be freed separately at changing the branch permission */
3163 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3164 +                                            GFP_NOFS);
3165 +               if (unlikely(!add_branch->br_wbr))
3166 +                       goto out_hnotify;
3167 +       }
3168 +
3169 +       if (au_br_fhsm(perm)) {
3170 +               err = au_fhsm_br_alloc(add_branch);
3171 +               if (unlikely(err))
3172 +                       goto out_wbr;
3173 +       }
3174 +
3175 +       root = sb->s_root;
3176 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3177 +       if (!err)
3178 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3179 +       if (!err) {
3180 +               inode = d_inode(root);
3181 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3182 +                                       /*may_shrink*/0);
3183 +       }
3184 +       if (!err)
3185 +               return add_branch; /* success */
3186 +
3187 +out_wbr:
3188 +       au_kfree_rcu(add_branch->br_wbr);
3189 +out_hnotify:
3190 +       au_hnotify_fin_br(add_branch);
3191 +out_xino:
3192 +       au_xino_put(add_branch);
3193 +out_br:
3194 +       au_kfree_rcu(add_branch);
3195 +out:
3196 +       return ERR_PTR(err);
3197 +}
3198 +
3199 +/*
3200 + * test if the branch permission is legal or not.
3201 + */
3202 +static int test_br(struct inode *inode, int brperm, char *path)
3203 +{
3204 +       int err;
3205 +
3206 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3207 +       if (!err)
3208 +               goto out;
3209 +
3210 +       err = -EINVAL;
3211 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3212 +
3213 +out:
3214 +       return err;
3215 +}
3216 +
3217 +/*
3218 + * returns:
3219 + * 0: success, the caller will add it
3220 + * plus: success, it is already unified, the caller should ignore it
3221 + * minus: error
3222 + */
3223 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3224 +{
3225 +       int err;
3226 +       aufs_bindex_t bbot, bindex;
3227 +       struct dentry *root, *h_dentry;
3228 +       struct inode *inode, *h_inode;
3229 +
3230 +       root = sb->s_root;
3231 +       bbot = au_sbbot(sb);
3232 +       if (unlikely(bbot >= 0
3233 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3234 +               err = 1;
3235 +               if (!remount) {
3236 +                       err = -EINVAL;
3237 +                       pr_err("%s duplicated\n", add->pathname);
3238 +               }
3239 +               goto out;
3240 +       }
3241 +
3242 +       err = -ENOSPC; /* -E2BIG; */
3243 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3244 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3245 +               pr_err("number of branches exceeded %s\n", add->pathname);
3246 +               goto out;
3247 +       }
3248 +
3249 +       err = -EDOM;
3250 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3251 +               pr_err("bad index %d\n", add->bindex);
3252 +               goto out;
3253 +       }
3254 +
3255 +       inode = d_inode(add->path.dentry);
3256 +       err = -ENOENT;
3257 +       if (unlikely(!inode->i_nlink)) {
3258 +               pr_err("no existence %s\n", add->pathname);
3259 +               goto out;
3260 +       }
3261 +
3262 +       err = -EINVAL;
3263 +       if (unlikely(inode->i_sb == sb)) {
3264 +               pr_err("%s must be outside\n", add->pathname);
3265 +               goto out;
3266 +       }
3267 +
3268 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3269 +               pr_err("unsupported filesystem, %s (%s)\n",
3270 +                      add->pathname, au_sbtype(inode->i_sb));
3271 +               goto out;
3272 +       }
3273 +
3274 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3275 +               pr_err("already stacked, %s (%s)\n",
3276 +                      add->pathname, au_sbtype(inode->i_sb));
3277 +               goto out;
3278 +       }
3279 +
3280 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3281 +       if (unlikely(err))
3282 +               goto out;
3283 +
3284 +       if (bbot < 0)
3285 +               return 0; /* success */
3286 +
3287 +       err = -EINVAL;
3288 +       for (bindex = 0; bindex <= bbot; bindex++)
3289 +               if (unlikely(test_overlap(sb, add->path.dentry,
3290 +                                         au_h_dptr(root, bindex)))) {
3291 +                       pr_err("%s is overlapped\n", add->pathname);
3292 +                       goto out;
3293 +               }
3294 +
3295 +       err = 0;
3296 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3297 +               h_dentry = au_h_dptr(root, 0);
3298 +               h_inode = d_inode(h_dentry);
3299 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3300 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3301 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3302 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3303 +                               add->pathname,
3304 +                               i_uid_read(inode), i_gid_read(inode),
3305 +                               (inode->i_mode & S_IALLUGO),
3306 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3307 +                               (h_inode->i_mode & S_IALLUGO));
3308 +       }
3309 +
3310 +out:
3311 +       return err;
3312 +}
3313 +
3314 +/*
3315 + * initialize or clean the whiteouts for an adding branch
3316 + */
3317 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3318 +                        int new_perm)
3319 +{
3320 +       int err, old_perm;
3321 +       aufs_bindex_t bindex;
3322 +       struct inode *h_inode;
3323 +       struct au_wbr *wbr;
3324 +       struct au_hinode *hdir;
3325 +       struct dentry *h_dentry;
3326 +
3327 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3328 +       if (unlikely(err))
3329 +               goto out;
3330 +
3331 +       wbr = br->br_wbr;
3332 +       old_perm = br->br_perm;
3333 +       br->br_perm = new_perm;
3334 +       hdir = NULL;
3335 +       h_inode = NULL;
3336 +       bindex = au_br_index(sb, br->br_id);
3337 +       if (0 <= bindex) {
3338 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3339 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3340 +       } else {
3341 +               h_dentry = au_br_dentry(br);
3342 +               h_inode = d_inode(h_dentry);
3343 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3344 +       }
3345 +       if (!wbr)
3346 +               err = au_wh_init(br, sb);
3347 +       else {
3348 +               wbr_wh_write_lock(wbr);
3349 +               err = au_wh_init(br, sb);
3350 +               wbr_wh_write_unlock(wbr);
3351 +       }
3352 +       if (hdir)
3353 +               au_hn_inode_unlock(hdir);
3354 +       else
3355 +               inode_unlock(h_inode);
3356 +       vfsub_mnt_drop_write(au_br_mnt(br));
3357 +       br->br_perm = old_perm;
3358 +
3359 +       if (!err && wbr && !au_br_writable(new_perm)) {
3360 +               au_kfree_rcu(wbr);
3361 +               br->br_wbr = NULL;
3362 +       }
3363 +
3364 +out:
3365 +       return err;
3366 +}
3367 +
3368 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3369 +                      int perm)
3370 +{
3371 +       int err;
3372 +       struct kstatfs kst;
3373 +       struct au_wbr *wbr;
3374 +
3375 +       wbr = br->br_wbr;
3376 +       au_rw_init(&wbr->wbr_wh_rwsem);
3377 +       atomic_set(&wbr->wbr_wh_running, 0);
3378 +
3379 +       /*
3380 +        * a limit for rmdir/rename a dir
3381 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3382 +        */
3383 +       err = vfs_statfs(&br->br_path, &kst);
3384 +       if (unlikely(err))
3385 +               goto out;
3386 +       err = -EINVAL;
3387 +       if (kst.f_namelen >= NAME_MAX)
3388 +               err = au_br_init_wh(sb, br, perm);
3389 +       else
3390 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3391 +                      au_br_dentry(br),
3392 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3393 +
3394 +out:
3395 +       return err;
3396 +}
3397 +
3398 +/* initialize a new branch */
3399 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3400 +                     struct au_opt_add *add)
3401 +{
3402 +       int err;
3403 +       struct au_branch *brbase;
3404 +       struct file *xf;
3405 +       struct inode *h_inode;
3406 +
3407 +       err = 0;
3408 +       br->br_perm = add->perm;
3409 +       br->br_path = add->path; /* set first, path_get() later */
3410 +       spin_lock_init(&br->br_dykey_lock);
3411 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3412 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3413 +       br->br_id = au_new_br_id(sb);
3414 +       AuDebugOn(br->br_id < 0);
3415 +
3416 +       /* always, regardless the given option */
3417 +       err = au_dr_br_init(sb, br, &add->path);
3418 +       if (unlikely(err))
3419 +               goto out_err;
3420 +
3421 +       if (au_br_writable(add->perm)) {
3422 +               err = au_wbr_init(br, sb, add->perm);
3423 +               if (unlikely(err))
3424 +                       goto out_err;
3425 +       }
3426 +
3427 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3428 +               brbase = au_sbr(sb, 0);
3429 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3430 +               AuDebugOn(!xf);
3431 +               h_inode = d_inode(add->path.dentry);
3432 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3433 +               if (unlikely(err)) {
3434 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3435 +                       goto out_err;
3436 +               }
3437 +       }
3438 +
3439 +       sysaufs_br_init(br);
3440 +       path_get(&br->br_path);
3441 +       goto out; /* success */
3442 +
3443 +out_err:
3444 +       memset(&br->br_path, 0, sizeof(br->br_path));
3445 +out:
3446 +       return err;
3447 +}
3448 +
3449 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3450 +                            struct au_branch *br, aufs_bindex_t bbot,
3451 +                            aufs_bindex_t amount)
3452 +{
3453 +       struct au_branch **brp;
3454 +
3455 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3456 +
3457 +       brp = sbinfo->si_branch + bindex;
3458 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3459 +       *brp = br;
3460 +       sbinfo->si_bbot++;
3461 +       if (unlikely(bbot < 0))
3462 +               sbinfo->si_bbot = 0;
3463 +}
3464 +
3465 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3466 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3467 +{
3468 +       struct au_hdentry *hdp;
3469 +
3470 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3471 +
3472 +       hdp = au_hdentry(dinfo, bindex);
3473 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3474 +       au_h_dentry_init(hdp);
3475 +       dinfo->di_bbot++;
3476 +       if (unlikely(bbot < 0))
3477 +               dinfo->di_btop = 0;
3478 +}
3479 +
3480 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3481 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3482 +{
3483 +       struct au_hinode *hip;
3484 +
3485 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3486 +
3487 +       hip = au_hinode(iinfo, bindex);
3488 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3489 +       au_hinode_init(hip);
3490 +       iinfo->ii_bbot++;
3491 +       if (unlikely(bbot < 0))
3492 +               iinfo->ii_btop = 0;
3493 +}
3494 +
3495 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3496 +                        aufs_bindex_t bindex)
3497 +{
3498 +       struct dentry *root, *h_dentry;
3499 +       struct inode *root_inode, *h_inode;
3500 +       aufs_bindex_t bbot, amount;
3501 +
3502 +       root = sb->s_root;
3503 +       root_inode = d_inode(root);
3504 +       bbot = au_sbbot(sb);
3505 +       amount = bbot + 1 - bindex;
3506 +       h_dentry = au_br_dentry(br);
3507 +       au_sbilist_lock();
3508 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3509 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3510 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3511 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3512 +       h_inode = d_inode(h_dentry);
3513 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3514 +       au_sbilist_unlock();
3515 +}
3516 +
3517 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3518 +{
3519 +       int err;
3520 +       aufs_bindex_t bbot, add_bindex;
3521 +       struct dentry *root, *h_dentry;
3522 +       struct inode *root_inode;
3523 +       struct au_branch *add_branch;
3524 +
3525 +       root = sb->s_root;
3526 +       root_inode = d_inode(root);
3527 +       IMustLock(root_inode);
3528 +       IiMustWriteLock(root_inode);
3529 +       err = test_add(sb, add, remount);
3530 +       if (unlikely(err < 0))
3531 +               goto out;
3532 +       if (err) {
3533 +               err = 0;
3534 +               goto out; /* success */
3535 +       }
3536 +
3537 +       bbot = au_sbbot(sb);
3538 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3539 +       err = PTR_ERR(add_branch);
3540 +       if (IS_ERR(add_branch))
3541 +               goto out;
3542 +
3543 +       err = au_br_init(add_branch, sb, add);
3544 +       if (unlikely(err)) {
3545 +               au_br_do_free(add_branch);
3546 +               goto out;
3547 +       }
3548 +
3549 +       add_bindex = add->bindex;
3550 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3551 +       au_br_do_add(sb, add_branch, add_bindex);
3552 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3553 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3554 +
3555 +       h_dentry = add->path.dentry;
3556 +       if (!add_bindex) {
3557 +               au_cpup_attr_all(root_inode, /*force*/1);
3558 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3559 +       } else
3560 +               au_add_nlink(root_inode, d_inode(h_dentry));
3561 +
3562 +out:
3563 +       return err;
3564 +}
3565 +
3566 +/* ---------------------------------------------------------------------- */
3567 +
3568 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3569 +                                      unsigned long long max __maybe_unused,
3570 +                                      void *arg)
3571 +{
3572 +       unsigned long long n;
3573 +       struct file **p, *f;
3574 +       struct hlist_bl_head *files;
3575 +       struct hlist_bl_node *pos;
3576 +       struct au_finfo *finfo;
3577 +
3578 +       n = 0;
3579 +       p = a;
3580 +       files = &au_sbi(sb)->si_files;
3581 +       hlist_bl_lock(files);
3582 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3583 +               f = finfo->fi_file;
3584 +               if (file_count(f)
3585 +                   && !special_file(file_inode(f)->i_mode)) {
3586 +                       get_file(f);
3587 +                       *p++ = f;
3588 +                       n++;
3589 +                       AuDebugOn(n > max);
3590 +               }
3591 +       }
3592 +       hlist_bl_unlock(files);
3593 +
3594 +       return n;
3595 +}
3596 +
3597 +static struct file **au_farray_alloc(struct super_block *sb,
3598 +                                    unsigned long long *max)
3599 +{
3600 +       struct au_sbinfo *sbi;
3601 +
3602 +       sbi = au_sbi(sb);
3603 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3604 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3605 +}
3606 +
3607 +static void au_farray_free(struct file **a, unsigned long long max)
3608 +{
3609 +       unsigned long long ull;
3610 +
3611 +       for (ull = 0; ull < max; ull++)
3612 +               if (a[ull])
3613 +                       fput(a[ull]);
3614 +       kvfree(a);
3615 +}
3616 +
3617 +/* ---------------------------------------------------------------------- */
3618 +
3619 +/*
3620 + * delete a branch
3621 + */
3622 +
3623 +/* to show the line number, do not make it inlined function */
3624 +#define AuVerbose(do_info, fmt, ...) do { \
3625 +       if (do_info) \
3626 +               pr_info(fmt, ##__VA_ARGS__); \
3627 +} while (0)
3628 +
3629 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3630 +                        aufs_bindex_t bbot)
3631 +{
3632 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3633 +}
3634 +
3635 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3636 +                        aufs_bindex_t bbot)
3637 +{
3638 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3639 +}
3640 +
3641 +/*
3642 + * test if the branch is deletable or not.
3643 + */
3644 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3645 +                           unsigned int sigen, const unsigned int verbose)
3646 +{
3647 +       int err, i, j, ndentry;
3648 +       aufs_bindex_t btop, bbot;
3649 +       struct au_dcsub_pages dpages;
3650 +       struct au_dpage *dpage;
3651 +       struct dentry *d;
3652 +
3653 +       err = au_dpages_init(&dpages, GFP_NOFS);
3654 +       if (unlikely(err))
3655 +               goto out;
3656 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3657 +       if (unlikely(err))
3658 +               goto out_dpages;
3659 +
3660 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3661 +               dpage = dpages.dpages + i;
3662 +               ndentry = dpage->ndentry;
3663 +               for (j = 0; !err && j < ndentry; j++) {
3664 +                       d = dpage->dentries[j];
3665 +                       AuDebugOn(au_dcount(d) <= 0);
3666 +                       if (!au_digen_test(d, sigen)) {
3667 +                               di_read_lock_child(d, AuLock_IR);
3668 +                               if (unlikely(au_dbrange_test(d))) {
3669 +                                       di_read_unlock(d, AuLock_IR);
3670 +                                       continue;
3671 +                               }
3672 +                       } else {
3673 +                               di_write_lock_child(d);
3674 +                               if (unlikely(au_dbrange_test(d))) {
3675 +                                       di_write_unlock(d);
3676 +                                       continue;
3677 +                               }
3678 +                               err = au_reval_dpath(d, sigen);
3679 +                               if (!err)
3680 +                                       di_downgrade_lock(d, AuLock_IR);
3681 +                               else {
3682 +                                       di_write_unlock(d);
3683 +                                       break;
3684 +                               }
3685 +                       }
3686 +
3687 +                       /* AuDbgDentry(d); */
3688 +                       btop = au_dbtop(d);
3689 +                       bbot = au_dbbot(d);
3690 +                       if (btop <= bindex
3691 +                           && bindex <= bbot
3692 +                           && au_h_dptr(d, bindex)
3693 +                           && au_test_dbusy(d, btop, bbot)) {
3694 +                               err = -EBUSY;
3695 +                               AuVerbose(verbose, "busy %pd\n", d);
3696 +                               AuDbgDentry(d);
3697 +                       }
3698 +                       di_read_unlock(d, AuLock_IR);
3699 +               }
3700 +       }
3701 +
3702 +out_dpages:
3703 +       au_dpages_free(&dpages);
3704 +out:
3705 +       return err;
3706 +}
3707 +
3708 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3709 +                          unsigned int sigen, const unsigned int verbose)
3710 +{
3711 +       int err;
3712 +       unsigned long long max, ull;
3713 +       struct inode *i, **array;
3714 +       aufs_bindex_t btop, bbot;
3715 +
3716 +       array = au_iarray_alloc(sb, &max);
3717 +       err = PTR_ERR(array);
3718 +       if (IS_ERR(array))
3719 +               goto out;
3720 +
3721 +       err = 0;
3722 +       AuDbg("b%d\n", bindex);
3723 +       for (ull = 0; !err && ull < max; ull++) {
3724 +               i = array[ull];
3725 +               if (unlikely(!i))
3726 +                       break;
3727 +               if (i->i_ino == AUFS_ROOT_INO)
3728 +                       continue;
3729 +
3730 +               /* AuDbgInode(i); */
3731 +               if (au_iigen(i, NULL) == sigen)
3732 +                       ii_read_lock_child(i);
3733 +               else {
3734 +                       ii_write_lock_child(i);
3735 +                       err = au_refresh_hinode_self(i);
3736 +                       au_iigen_dec(i);
3737 +                       if (!err)
3738 +                               ii_downgrade_lock(i);
3739 +                       else {
3740 +                               ii_write_unlock(i);
3741 +                               break;
3742 +                       }
3743 +               }
3744 +
3745 +               btop = au_ibtop(i);
3746 +               bbot = au_ibbot(i);
3747 +               if (btop <= bindex
3748 +                   && bindex <= bbot
3749 +                   && au_h_iptr(i, bindex)
3750 +                   && au_test_ibusy(i, btop, bbot)) {
3751 +                       err = -EBUSY;
3752 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3753 +                       AuDbgInode(i);
3754 +               }
3755 +               ii_read_unlock(i);
3756 +       }
3757 +       au_iarray_free(array, max);
3758 +
3759 +out:
3760 +       return err;
3761 +}
3762 +
3763 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3764 +                             const unsigned int verbose)
3765 +{
3766 +       int err;
3767 +       unsigned int sigen;
3768 +
3769 +       sigen = au_sigen(root->d_sb);
3770 +       DiMustNoWaiters(root);
3771 +       IiMustNoWaiters(d_inode(root));
3772 +       di_write_unlock(root);
3773 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3774 +       if (!err)
3775 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3776 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3777 +
3778 +       return err;
3779 +}
3780 +
3781 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3782 +                        struct file **to_free, int *idx)
3783 +{
3784 +       int err;
3785 +       unsigned char matched, root;
3786 +       aufs_bindex_t bindex, bbot;
3787 +       struct au_fidir *fidir;
3788 +       struct au_hfile *hfile;
3789 +
3790 +       err = 0;
3791 +       root = IS_ROOT(file->f_path.dentry);
3792 +       if (root) {
3793 +               get_file(file);
3794 +               to_free[*idx] = file;
3795 +               (*idx)++;
3796 +               goto out;
3797 +       }
3798 +
3799 +       matched = 0;
3800 +       fidir = au_fi(file)->fi_hdir;
3801 +       AuDebugOn(!fidir);
3802 +       bbot = au_fbbot_dir(file);
3803 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3804 +               hfile = fidir->fd_hfile + bindex;
3805 +               if (!hfile->hf_file)
3806 +                       continue;
3807 +
3808 +               if (hfile->hf_br->br_id == br_id) {
3809 +                       matched = 1;
3810 +                       break;
3811 +               }
3812 +       }
3813 +       if (matched)
3814 +               err = -EBUSY;
3815 +
3816 +out:
3817 +       return err;
3818 +}
3819 +
3820 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3821 +                         struct file **to_free, int opened)
3822 +{
3823 +       int err, idx;
3824 +       unsigned long long ull, max;
3825 +       aufs_bindex_t btop;
3826 +       struct file *file, **array;
3827 +       struct dentry *root;
3828 +       struct au_hfile *hfile;
3829 +
3830 +       array = au_farray_alloc(sb, &max);
3831 +       err = PTR_ERR(array);
3832 +       if (IS_ERR(array))
3833 +               goto out;
3834 +
3835 +       err = 0;
3836 +       idx = 0;
3837 +       root = sb->s_root;
3838 +       di_write_unlock(root);
3839 +       for (ull = 0; ull < max; ull++) {
3840 +               file = array[ull];
3841 +               if (unlikely(!file))
3842 +                       break;
3843 +
3844 +               /* AuDbg("%pD\n", file); */
3845 +               fi_read_lock(file);
3846 +               btop = au_fbtop(file);
3847 +               if (!d_is_dir(file->f_path.dentry)) {
3848 +                       hfile = &au_fi(file)->fi_htop;
3849 +                       if (hfile->hf_br->br_id == br_id)
3850 +                               err = -EBUSY;
3851 +               } else
3852 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3853 +               fi_read_unlock(file);
3854 +               if (unlikely(err))
3855 +                       break;
3856 +       }
3857 +       di_write_lock_child(root);
3858 +       au_farray_free(array, max);
3859 +       AuDebugOn(idx > opened);
3860 +
3861 +out:
3862 +       return err;
3863 +}
3864 +
3865 +static void br_del_file(struct file **to_free, unsigned long long opened,
3866 +                       aufs_bindex_t br_id)
3867 +{
3868 +       unsigned long long ull;
3869 +       aufs_bindex_t bindex, btop, bbot, bfound;
3870 +       struct file *file;
3871 +       struct au_fidir *fidir;
3872 +       struct au_hfile *hfile;
3873 +
3874 +       for (ull = 0; ull < opened; ull++) {
3875 +               file = to_free[ull];
3876 +               if (unlikely(!file))
3877 +                       break;
3878 +
3879 +               /* AuDbg("%pD\n", file); */
3880 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3881 +               bfound = -1;
3882 +               fidir = au_fi(file)->fi_hdir;
3883 +               AuDebugOn(!fidir);
3884 +               fi_write_lock(file);
3885 +               btop = au_fbtop(file);
3886 +               bbot = au_fbbot_dir(file);
3887 +               for (bindex = btop; bindex <= bbot; bindex++) {
3888 +                       hfile = fidir->fd_hfile + bindex;
3889 +                       if (!hfile->hf_file)
3890 +                               continue;
3891 +
3892 +                       if (hfile->hf_br->br_id == br_id) {
3893 +                               bfound = bindex;
3894 +                               break;
3895 +                       }
3896 +               }
3897 +               AuDebugOn(bfound < 0);
3898 +               au_set_h_fptr(file, bfound, NULL);
3899 +               if (bfound == btop) {
3900 +                       for (btop++; btop <= bbot; btop++)
3901 +                               if (au_hf_dir(file, btop)) {
3902 +                                       au_set_fbtop(file, btop);
3903 +                                       break;
3904 +                               }
3905 +               }
3906 +               fi_write_unlock(file);
3907 +       }
3908 +}
3909 +
3910 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3911 +                            const aufs_bindex_t bindex,
3912 +                            const aufs_bindex_t bbot)
3913 +{
3914 +       struct au_branch **brp, **p;
3915 +
3916 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3917 +
3918 +       brp = sbinfo->si_branch + bindex;
3919 +       if (bindex < bbot)
3920 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3921 +       sbinfo->si_branch[0 + bbot] = NULL;
3922 +       sbinfo->si_bbot--;
3923 +
3924 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3925 +                       /*may_shrink*/1);
3926 +       if (p)
3927 +               sbinfo->si_branch = p;
3928 +       /* harmless error */
3929 +}
3930 +
3931 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3932 +                            const aufs_bindex_t bbot)
3933 +{
3934 +       struct au_hdentry *hdp, *p;
3935 +
3936 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3937 +
3938 +       hdp = au_hdentry(dinfo, bindex);
3939 +       if (bindex < bbot)
3940 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3941 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3942 +       dinfo->di_bbot--;
3943 +
3944 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3945 +                       /*may_shrink*/1);
3946 +       if (p)
3947 +               dinfo->di_hdentry = p;
3948 +       /* harmless error */
3949 +}
3950 +
3951 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3952 +                            const aufs_bindex_t bbot)
3953 +{
3954 +       struct au_hinode *hip, *p;
3955 +
3956 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3957 +
3958 +       hip = au_hinode(iinfo, bindex);
3959 +       if (bindex < bbot)
3960 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3961 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3962 +       iinfo->ii_bbot--;
3963 +
3964 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3965 +                       /*may_shrink*/1);
3966 +       if (p)
3967 +               iinfo->ii_hinode = p;
3968 +       /* harmless error */
3969 +}
3970 +
3971 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3972 +                        struct au_branch *br)
3973 +{
3974 +       aufs_bindex_t bbot;
3975 +       struct au_sbinfo *sbinfo;
3976 +       struct dentry *root, *h_root;
3977 +       struct inode *inode, *h_inode;
3978 +       struct au_hinode *hinode;
3979 +
3980 +       SiMustWriteLock(sb);
3981 +
3982 +       root = sb->s_root;
3983 +       inode = d_inode(root);
3984 +       sbinfo = au_sbi(sb);
3985 +       bbot = sbinfo->si_bbot;
3986 +
3987 +       h_root = au_h_dptr(root, bindex);
3988 +       hinode = au_hi(inode, bindex);
3989 +       h_inode = au_igrab(hinode->hi_inode);
3990 +       au_hiput(hinode);
3991 +
3992 +       au_sbilist_lock();
3993 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3994 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3995 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3996 +       au_sbilist_unlock();
3997 +
3998 +       /* ignore an error */
3999 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
4000 +
4001 +       dput(h_root);
4002 +       iput(h_inode);
4003 +       au_br_do_free(br);
4004 +}
4005 +
4006 +static unsigned long long empty_cb(struct super_block *sb, void *array,
4007 +                                  unsigned long long max, void *arg)
4008 +{
4009 +       return max;
4010 +}
4011 +
4012 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4013 +{
4014 +       int err, rerr, i;
4015 +       unsigned long long opened;
4016 +       unsigned int mnt_flags;
4017 +       aufs_bindex_t bindex, bbot, br_id;
4018 +       unsigned char do_wh, verbose;
4019 +       struct au_branch *br;
4020 +       struct au_wbr *wbr;
4021 +       struct dentry *root;
4022 +       struct file **to_free;
4023 +
4024 +       err = 0;
4025 +       opened = 0;
4026 +       to_free = NULL;
4027 +       root = sb->s_root;
4028 +       bindex = au_find_dbindex(root, del->h_path.dentry);
4029 +       if (bindex < 0) {
4030 +               if (remount)
4031 +                       goto out; /* success */
4032 +               err = -ENOENT;
4033 +               pr_err("%s no such branch\n", del->pathname);
4034 +               goto out;
4035 +       }
4036 +       AuDbg("bindex b%d\n", bindex);
4037 +
4038 +       err = -EBUSY;
4039 +       mnt_flags = au_mntflags(sb);
4040 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4041 +       bbot = au_sbbot(sb);
4042 +       if (unlikely(!bbot)) {
4043 +               AuVerbose(verbose, "no more branches left\n");
4044 +               goto out;
4045 +       }
4046 +
4047 +       br = au_sbr(sb, bindex);
4048 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
4049 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4050 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4051 +               goto out;
4052 +       }
4053 +
4054 +       br_id = br->br_id;
4055 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
4056 +       if (unlikely(opened)) {
4057 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
4058 +               err = PTR_ERR(to_free);
4059 +               if (IS_ERR(to_free))
4060 +                       goto out;
4061 +
4062 +               err = test_file_busy(sb, br_id, to_free, opened);
4063 +               if (unlikely(err)) {
4064 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4065 +                       goto out;
4066 +               }
4067 +       }
4068 +
4069 +       wbr = br->br_wbr;
4070 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4071 +       if (do_wh) {
4072 +               /* instead of WbrWhMustWriteLock(wbr) */
4073 +               SiMustWriteLock(sb);
4074 +               for (i = 0; i < AuBrWh_Last; i++) {
4075 +                       dput(wbr->wbr_wh[i]);
4076 +                       wbr->wbr_wh[i] = NULL;
4077 +               }
4078 +       }
4079 +
4080 +       err = test_children_busy(root, bindex, verbose);
4081 +       if (unlikely(err)) {
4082 +               if (do_wh)
4083 +                       goto out_wh;
4084 +               goto out;
4085 +       }
4086 +
4087 +       err = 0;
4088 +       if (to_free) {
4089 +               /*
4090 +                * now we confirmed the branch is deletable.
4091 +                * let's free the remaining opened dirs on the branch.
4092 +                */
4093 +               di_write_unlock(root);
4094 +               br_del_file(to_free, opened, br_id);
4095 +               di_write_lock_child(root);
4096 +       }
4097 +
4098 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4099 +       dbgaufs_xino_del(br);           /* remove one */
4100 +       au_br_do_del(sb, bindex, br);
4101 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4102 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4103 +
4104 +       if (!bindex) {
4105 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4106 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4107 +       } else
4108 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4109 +       if (au_opt_test(mnt_flags, PLINK))
4110 +               au_plink_half_refresh(sb, br_id);
4111 +
4112 +       goto out; /* success */
4113 +
4114 +out_wh:
4115 +       /* revert */
4116 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4117 +       if (rerr)
4118 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4119 +                       del->pathname, rerr);
4120 +out:
4121 +       if (to_free)
4122 +               au_farray_free(to_free, opened);
4123 +       return err;
4124 +}
4125 +
4126 +/* ---------------------------------------------------------------------- */
4127 +
4128 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4129 +{
4130 +       int err;
4131 +       aufs_bindex_t btop, bbot;
4132 +       struct aufs_ibusy ibusy;
4133 +       struct inode *inode, *h_inode;
4134 +
4135 +       err = -EPERM;
4136 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4137 +               goto out;
4138 +
4139 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4140 +       if (!err)
4141 +               /* VERIFY_WRITE */
4142 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4143 +       if (unlikely(err)) {
4144 +               err = -EFAULT;
4145 +               AuTraceErr(err);
4146 +               goto out;
4147 +       }
4148 +
4149 +       err = -EINVAL;
4150 +       si_read_lock(sb, AuLock_FLUSH);
4151 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4152 +               goto out_unlock;
4153 +
4154 +       err = 0;
4155 +       ibusy.h_ino = 0; /* invalid */
4156 +       inode = ilookup(sb, ibusy.ino);
4157 +       if (!inode
4158 +           || inode->i_ino == AUFS_ROOT_INO
4159 +           || au_is_bad_inode(inode))
4160 +               goto out_unlock;
4161 +
4162 +       ii_read_lock_child(inode);
4163 +       btop = au_ibtop(inode);
4164 +       bbot = au_ibbot(inode);
4165 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4166 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4167 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4168 +                       ibusy.h_ino = h_inode->i_ino;
4169 +       }
4170 +       ii_read_unlock(inode);
4171 +       iput(inode);
4172 +
4173 +out_unlock:
4174 +       si_read_unlock(sb);
4175 +       if (!err) {
4176 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4177 +               if (unlikely(err)) {
4178 +                       err = -EFAULT;
4179 +                       AuTraceErr(err);
4180 +               }
4181 +       }
4182 +out:
4183 +       return err;
4184 +}
4185 +
4186 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4187 +{
4188 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4189 +}
4190 +
4191 +#ifdef CONFIG_COMPAT
4192 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4193 +{
4194 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4195 +}
4196 +#endif
4197 +
4198 +/* ---------------------------------------------------------------------- */
4199 +
4200 +/*
4201 + * change a branch permission
4202 + */
4203 +
4204 +static void au_warn_ima(void)
4205 +{
4206 +#ifdef CONFIG_IMA
4207 +       /* since it doesn't support mark_files_ro() */
4208 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4209 +#endif
4210 +}
4211 +
4212 +static int do_need_sigen_inc(int a, int b)
4213 +{
4214 +       return au_br_whable(a) && !au_br_whable(b);
4215 +}
4216 +
4217 +static int need_sigen_inc(int old, int new)
4218 +{
4219 +       return do_need_sigen_inc(old, new)
4220 +               || do_need_sigen_inc(new, old);
4221 +}
4222 +
4223 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4224 +{
4225 +       int err, do_warn;
4226 +       unsigned int mnt_flags;
4227 +       unsigned long long ull, max;
4228 +       aufs_bindex_t br_id;
4229 +       unsigned char verbose, writer;
4230 +       struct file *file, *hf, **array;
4231 +       struct au_hfile *hfile;
4232 +       struct inode *h_inode;
4233 +
4234 +       mnt_flags = au_mntflags(sb);
4235 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4236 +
4237 +       array = au_farray_alloc(sb, &max);
4238 +       err = PTR_ERR(array);
4239 +       if (IS_ERR(array))
4240 +               goto out;
4241 +
4242 +       do_warn = 0;
4243 +       br_id = au_sbr_id(sb, bindex);
4244 +       for (ull = 0; ull < max; ull++) {
4245 +               file = array[ull];
4246 +               if (unlikely(!file))
4247 +                       break;
4248 +
4249 +               /* AuDbg("%pD\n", file); */
4250 +               fi_read_lock(file);
4251 +               if (unlikely(au_test_mmapped(file))) {
4252 +                       err = -EBUSY;
4253 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4254 +                       AuDbgFile(file);
4255 +                       FiMustNoWaiters(file);
4256 +                       fi_read_unlock(file);
4257 +                       goto out_array;
4258 +               }
4259 +
4260 +               hfile = &au_fi(file)->fi_htop;
4261 +               hf = hfile->hf_file;
4262 +               if (!d_is_reg(file->f_path.dentry)
4263 +                   || !(file->f_mode & FMODE_WRITE)
4264 +                   || hfile->hf_br->br_id != br_id
4265 +                   || !(hf->f_mode & FMODE_WRITE))
4266 +                       array[ull] = NULL;
4267 +               else {
4268 +                       do_warn = 1;
4269 +                       get_file(file);
4270 +               }
4271 +
4272 +               FiMustNoWaiters(file);
4273 +               fi_read_unlock(file);
4274 +               fput(file);
4275 +       }
4276 +
4277 +       err = 0;
4278 +       if (do_warn)
4279 +               au_warn_ima();
4280 +
4281 +       for (ull = 0; ull < max; ull++) {
4282 +               file = array[ull];
4283 +               if (!file)
4284 +                       continue;
4285 +
4286 +               /* todo: already flushed? */
4287 +               /*
4288 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4289 +                * approach which resets f_mode and calls mnt_drop_write() and
4290 +                * file_release_write() for each file, because the branch
4291 +                * attribute in aufs world is totally different from the native
4292 +                * fs rw/ro mode.
4293 +               */
4294 +               /* fi_read_lock(file); */
4295 +               hfile = &au_fi(file)->fi_htop;
4296 +               hf = hfile->hf_file;
4297 +               /* fi_read_unlock(file); */
4298 +               spin_lock(&hf->f_lock);
4299 +               writer = !!(hf->f_mode & FMODE_WRITER);
4300 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4301 +               spin_unlock(&hf->f_lock);
4302 +               if (writer) {
4303 +                       h_inode = file_inode(hf);
4304 +                       if (hf->f_mode & FMODE_READ)
4305 +                               i_readcount_inc(h_inode);
4306 +                       put_write_access(h_inode);
4307 +                       __mnt_drop_write(hf->f_path.mnt);
4308 +               }
4309 +       }
4310 +
4311 +out_array:
4312 +       au_farray_free(array, max);
4313 +out:
4314 +       AuTraceErr(err);
4315 +       return err;
4316 +}
4317 +
4318 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4319 +             int *do_refresh)
4320 +{
4321 +       int err, rerr;
4322 +       aufs_bindex_t bindex;
4323 +       struct dentry *root;
4324 +       struct au_branch *br;
4325 +       struct au_br_fhsm *bf;
4326 +
4327 +       root = sb->s_root;
4328 +       bindex = au_find_dbindex(root, mod->h_root);
4329 +       if (bindex < 0) {
4330 +               if (remount)
4331 +                       return 0; /* success */
4332 +               err = -ENOENT;
4333 +               pr_err("%s no such branch\n", mod->path);
4334 +               goto out;
4335 +       }
4336 +       AuDbg("bindex b%d\n", bindex);
4337 +
4338 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4339 +       if (unlikely(err))
4340 +               goto out;
4341 +
4342 +       br = au_sbr(sb, bindex);
4343 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4344 +       if (br->br_perm == mod->perm)
4345 +               return 0; /* success */
4346 +
4347 +       /* pre-allocate for non-fhsm --> fhsm */
4348 +       bf = NULL;
4349 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4350 +               err = au_fhsm_br_alloc(br);
4351 +               if (unlikely(err))
4352 +                       goto out;
4353 +               bf = br->br_fhsm;
4354 +               br->br_fhsm = NULL;
4355 +       }
4356 +
4357 +       if (au_br_writable(br->br_perm)) {
4358 +               /* remove whiteout base */
4359 +               err = au_br_init_wh(sb, br, mod->perm);
4360 +               if (unlikely(err))
4361 +                       goto out_bf;
4362 +
4363 +               if (!au_br_writable(mod->perm)) {
4364 +                       /* rw --> ro, file might be mmapped */
4365 +                       DiMustNoWaiters(root);
4366 +                       IiMustNoWaiters(d_inode(root));
4367 +                       di_write_unlock(root);
4368 +                       err = au_br_mod_files_ro(sb, bindex);
4369 +                       /* aufs_write_lock() calls ..._child() */
4370 +                       di_write_lock_child(root);
4371 +
4372 +                       if (unlikely(err)) {
4373 +                               rerr = -ENOMEM;
4374 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4375 +                                                    GFP_NOFS);
4376 +                               if (br->br_wbr)
4377 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4378 +                               if (unlikely(rerr)) {
4379 +                                       AuIOErr("nested error %d (%d)\n",
4380 +                                               rerr, err);
4381 +                                       br->br_perm = mod->perm;
4382 +                               }
4383 +                       }
4384 +               }
4385 +       } else if (au_br_writable(mod->perm)) {
4386 +               /* ro --> rw */
4387 +               err = -ENOMEM;
4388 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4389 +               if (br->br_wbr) {
4390 +                       err = au_wbr_init(br, sb, mod->perm);
4391 +                       if (unlikely(err)) {
4392 +                               au_kfree_rcu(br->br_wbr);
4393 +                               br->br_wbr = NULL;
4394 +                       }
4395 +               }
4396 +       }
4397 +       if (unlikely(err))
4398 +               goto out_bf;
4399 +
4400 +       if (au_br_fhsm(br->br_perm)) {
4401 +               if (!au_br_fhsm(mod->perm)) {
4402 +                       /* fhsm --> non-fhsm */
4403 +                       au_br_fhsm_fin(br->br_fhsm);
4404 +                       au_kfree_rcu(br->br_fhsm);
4405 +                       br->br_fhsm = NULL;
4406 +               }
4407 +       } else if (au_br_fhsm(mod->perm))
4408 +               /* non-fhsm --> fhsm */
4409 +               br->br_fhsm = bf;
4410 +
4411 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4412 +       br->br_perm = mod->perm;
4413 +       goto out; /* success */
4414 +
4415 +out_bf:
4416 +       au_kfree_try_rcu(bf);
4417 +out:
4418 +       AuTraceErr(err);
4419 +       return err;
4420 +}
4421 +
4422 +/* ---------------------------------------------------------------------- */
4423 +
4424 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4425 +{
4426 +       int err;
4427 +       struct kstatfs kstfs;
4428 +
4429 +       err = vfs_statfs(&br->br_path, &kstfs);
4430 +       if (!err) {
4431 +               stfs->f_blocks = kstfs.f_blocks;
4432 +               stfs->f_bavail = kstfs.f_bavail;
4433 +               stfs->f_files = kstfs.f_files;
4434 +               stfs->f_ffree = kstfs.f_ffree;
4435 +       }
4436 +
4437 +       return err;
4438 +}
4439 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4440 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4441 +++ linux/fs/aufs/branch.h      2020-01-27 10:57:18.165538015 +0100
4442 @@ -0,0 +1,366 @@
4443 +/* SPDX-License-Identifier: GPL-2.0 */
4444 +/*
4445 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4446 + *
4447 + * This program, aufs is free software; you can redistribute it and/or modify
4448 + * it under the terms of the GNU General Public License as published by
4449 + * the Free Software Foundation; either version 2 of the License, or
4450 + * (at your option) any later version.
4451 + *
4452 + * This program is distributed in the hope that it will be useful,
4453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4455 + * GNU General Public License for more details.
4456 + *
4457 + * You should have received a copy of the GNU General Public License
4458 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4459 + */
4460 +
4461 +/*
4462 + * branch filesystems and xino for them
4463 + */
4464 +
4465 +#ifndef __AUFS_BRANCH_H__
4466 +#define __AUFS_BRANCH_H__
4467 +
4468 +#ifdef __KERNEL__
4469 +
4470 +#include <linux/mount.h>
4471 +#include "dirren.h"
4472 +#include "dynop.h"
4473 +#include "lcnt.h"
4474 +#include "rwsem.h"
4475 +#include "super.h"
4476 +
4477 +/* ---------------------------------------------------------------------- */
4478 +
4479 +/* a xino file */
4480 +struct au_xino {
4481 +       struct file             **xi_file;
4482 +       unsigned int            xi_nfile;
4483 +
4484 +       struct {
4485 +               spinlock_t              spin;
4486 +               ino_t                   *array;
4487 +               int                     total;
4488 +               /* reserved for future use */
4489 +               /* unsigned long        *bitmap; */
4490 +               wait_queue_head_t       wqh;
4491 +       } xi_nondir;
4492 +
4493 +       struct mutex            xi_mtx; /* protects xi_file array */
4494 +       struct hlist_bl_head    xi_writing;
4495 +
4496 +       atomic_t                xi_truncating;
4497 +
4498 +       struct kref             xi_kref;
4499 +};
4500 +
4501 +/* File-based Hierarchical Storage Management */
4502 +struct au_br_fhsm {
4503 +#ifdef CONFIG_AUFS_FHSM
4504 +       struct mutex            bf_lock;
4505 +       unsigned long           bf_jiffy;
4506 +       struct aufs_stfs        bf_stfs;
4507 +       int                     bf_readable;
4508 +#endif
4509 +};
4510 +
4511 +/* members for writable branch only */
4512 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4513 +struct au_wbr {
4514 +       struct au_rwsem         wbr_wh_rwsem;
4515 +       struct dentry           *wbr_wh[AuBrWh_Last];
4516 +       atomic_t                wbr_wh_running;
4517 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4518 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4519 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4520 +
4521 +       /* mfs mode */
4522 +       unsigned long long      wbr_bytes;
4523 +};
4524 +
4525 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4526 +#define AuBrDynOp (AuDyLast * 4)
4527 +
4528 +#ifdef CONFIG_AUFS_HFSNOTIFY
4529 +/* support for asynchronous destruction */
4530 +struct au_br_hfsnotify {
4531 +       struct fsnotify_group   *hfsn_group;
4532 +};
4533 +#endif
4534 +
4535 +/* sysfs entries */
4536 +struct au_brsysfs {
4537 +       char                    name[16];
4538 +       struct attribute        attr;
4539 +};
4540 +
4541 +enum {
4542 +       AuBrSysfs_BR,
4543 +       AuBrSysfs_BRID,
4544 +       AuBrSysfs_Last
4545 +};
4546 +
4547 +/* protected by superblock rwsem */
4548 +struct au_branch {
4549 +       struct au_xino          *br_xino;
4550 +
4551 +       aufs_bindex_t           br_id;
4552 +
4553 +       int                     br_perm;
4554 +       struct path             br_path;
4555 +       spinlock_t              br_dykey_lock;
4556 +       struct au_dykey         *br_dykey[AuBrDynOp];
4557 +       au_lcnt_t               br_nfiles;      /* opened files */
4558 +       au_lcnt_t               br_count;       /* in-use for other */
4559 +
4560 +       struct au_wbr           *br_wbr;
4561 +       struct au_br_fhsm       *br_fhsm;
4562 +
4563 +#ifdef CONFIG_AUFS_HFSNOTIFY
4564 +       struct au_br_hfsnotify  *br_hfsn;
4565 +#endif
4566 +
4567 +#ifdef CONFIG_SYSFS
4568 +       /* entries under sysfs per mount-point */
4569 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4570 +#endif
4571 +
4572 +#ifdef CONFIG_DEBUG_FS
4573 +       struct dentry            *br_dbgaufs; /* xino */
4574 +#endif
4575 +
4576 +       struct au_dr_br         br_dirren;
4577 +};
4578 +
4579 +/* ---------------------------------------------------------------------- */
4580 +
4581 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4582 +{
4583 +       return br->br_path.mnt;
4584 +}
4585 +
4586 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4587 +{
4588 +       return br->br_path.dentry;
4589 +}
4590 +
4591 +static inline struct super_block *au_br_sb(struct au_branch *br)
4592 +{
4593 +       return au_br_mnt(br)->mnt_sb;
4594 +}
4595 +
4596 +static inline int au_br_rdonly(struct au_branch *br)
4597 +{
4598 +       return (sb_rdonly(au_br_sb(br))
4599 +               || !au_br_writable(br->br_perm))
4600 +               ? -EROFS : 0;
4601 +}
4602 +
4603 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4604 +{
4605 +#ifdef CONFIG_AUFS_HNOTIFY
4606 +       return !(brperm & AuBrPerm_RR);
4607 +#else
4608 +       return 0;
4609 +#endif
4610 +}
4611 +
4612 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4613 +{
4614 +       int err, exec_flag;
4615 +
4616 +       err = 0;
4617 +       exec_flag = oflag & __FMODE_EXEC;
4618 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4619 +               err = -EACCES;
4620 +
4621 +       return err;
4622 +}
4623 +
4624 +static inline void au_xino_get(struct au_branch *br)
4625 +{
4626 +       struct au_xino *xi;
4627 +
4628 +       xi = br->br_xino;
4629 +       if (xi)
4630 +               kref_get(&xi->xi_kref);
4631 +}
4632 +
4633 +static inline int au_xino_count(struct au_branch *br)
4634 +{
4635 +       int v;
4636 +       struct au_xino *xi;
4637 +
4638 +       v = 0;
4639 +       xi = br->br_xino;
4640 +       if (xi)
4641 +               v = kref_read(&xi->xi_kref);
4642 +
4643 +       return v;
4644 +}
4645 +
4646 +/* ---------------------------------------------------------------------- */
4647 +
4648 +/* branch.c */
4649 +struct au_sbinfo;
4650 +void au_br_free(struct au_sbinfo *sinfo);
4651 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4652 +struct au_opt_add;
4653 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4654 +struct au_opt_del;
4655 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4656 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4657 +#ifdef CONFIG_COMPAT
4658 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4659 +#endif
4660 +struct au_opt_mod;
4661 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4662 +             int *do_refresh);
4663 +struct aufs_stfs;
4664 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4665 +
4666 +/* xino.c */
4667 +static const loff_t au_loff_max = LLONG_MAX;
4668 +
4669 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4670 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4671 +                           int wbrtop);
4672 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4673 +                            struct file *copy_src);
4674 +struct au_xi_new {
4675 +       struct au_xino *xi;     /* switch between xino and xigen */
4676 +       int idx;
4677 +       struct path *base;
4678 +       struct file *copy_src;
4679 +};
4680 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4681 +
4682 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4683 +                ino_t *ino);
4684 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4685 +                 ino_t ino);
4686 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4687 +                  loff_t *pos);
4688 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4689 +                   size_t size, loff_t *pos);
4690 +
4691 +int au_xib_trunc(struct super_block *sb);
4692 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4693 +
4694 +struct au_xino *au_xino_alloc(unsigned int nfile);
4695 +int au_xino_put(struct au_branch *br);
4696 +struct file *au_xino_file1(struct au_xino *xi);
4697 +
4698 +struct au_opt_xino;
4699 +void au_xino_clr(struct super_block *sb);
4700 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4701 +struct file *au_xino_def(struct super_block *sb);
4702 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4703 +                   struct path *base);
4704 +
4705 +ino_t au_xino_new_ino(struct super_block *sb);
4706 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4707 +
4708 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4709 +                      ino_t h_ino, int idx);
4710 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4711 +                     int *idx);
4712 +
4713 +int au_xino_path(struct seq_file *seq, struct file *file);
4714 +
4715 +/* ---------------------------------------------------------------------- */
4716 +
4717 +/* @idx is signed to accept -1 meaning the first file */
4718 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4719 +{
4720 +       struct file *file;
4721 +
4722 +       file = NULL;
4723 +       if (!xi)
4724 +               goto out;
4725 +
4726 +       if (idx >= 0) {
4727 +               if (idx < xi->xi_nfile)
4728 +                       file = xi->xi_file[idx];
4729 +       } else
4730 +               file = au_xino_file1(xi);
4731 +
4732 +out:
4733 +       return file;
4734 +}
4735 +
4736 +/* ---------------------------------------------------------------------- */
4737 +
4738 +/* Superblock to branch */
4739 +static inline
4740 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4741 +{
4742 +       return au_sbr(sb, bindex)->br_id;
4743 +}
4744 +
4745 +static inline
4746 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4747 +{
4748 +       return au_br_mnt(au_sbr(sb, bindex));
4749 +}
4750 +
4751 +static inline
4752 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4753 +{
4754 +       return au_br_sb(au_sbr(sb, bindex));
4755 +}
4756 +
4757 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4758 +{
4759 +       return au_sbr(sb, bindex)->br_perm;
4760 +}
4761 +
4762 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4763 +{
4764 +       return au_br_whable(au_sbr_perm(sb, bindex));
4765 +}
4766 +
4767 +/* ---------------------------------------------------------------------- */
4768 +
4769 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4770 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4771 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4772 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4773 +/*
4774 +#define wbr_wh_read_trylock_nested(wbr) \
4775 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4776 +#define wbr_wh_write_trylock_nested(wbr) \
4777 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4778 +*/
4779 +
4780 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4781 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4782 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4783 +
4784 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4785 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4786 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4787 +
4788 +/* ---------------------------------------------------------------------- */
4789 +
4790 +#ifdef CONFIG_AUFS_FHSM
4791 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4792 +{
4793 +       mutex_init(&brfhsm->bf_lock);
4794 +       brfhsm->bf_jiffy = 0;
4795 +       brfhsm->bf_readable = 0;
4796 +}
4797 +
4798 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4799 +{
4800 +       mutex_destroy(&brfhsm->bf_lock);
4801 +}
4802 +#else
4803 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4804 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4805 +#endif
4806 +
4807 +#endif /* __KERNEL__ */
4808 +#endif /* __AUFS_BRANCH_H__ */
4809 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4810 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4811 +++ linux/fs/aufs/conf.mk       2019-07-11 15:42:14.462237786 +0200
4812 @@ -0,0 +1,40 @@
4813 +# SPDX-License-Identifier: GPL-2.0
4814 +
4815 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4816 +
4817 +define AuConf
4818 +ifdef ${1}
4819 +AuConfStr += ${1}=${${1}}
4820 +endif
4821 +endef
4822 +
4823 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4824 +       SBILIST \
4825 +       HNOTIFY HFSNOTIFY \
4826 +       EXPORT INO_T_64 \
4827 +       XATTR \
4828 +       FHSM \
4829 +       RDU \
4830 +       DIRREN \
4831 +       SHWH \
4832 +       BR_RAMFS \
4833 +       BR_FUSE POLL \
4834 +       BR_HFSPLUS \
4835 +       BDEV_LOOP \
4836 +       DEBUG MAGIC_SYSRQ
4837 +$(foreach i, ${AuConfAll}, \
4838 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4839 +
4840 +AuConfName = ${obj}/conf.str
4841 +${AuConfName}.tmp: FORCE
4842 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4843 +${AuConfName}: ${AuConfName}.tmp
4844 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4845 +       echo '  GEN    ' $@; \
4846 +       cp -p $< $@; \
4847 +       }
4848 +FORCE:
4849 +clean-files += ${AuConfName} ${AuConfName}.tmp
4850 +${obj}/sysfs.o: ${AuConfName}
4851 +
4852 +-include ${srctree}/${src}/conf_priv.mk
4853 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4854 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4855 +++ linux/fs/aufs/cpup.c        2020-01-27 10:57:18.165538015 +0100
4856 @@ -0,0 +1,1458 @@
4857 +// SPDX-License-Identifier: GPL-2.0
4858 +/*
4859 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4860 + *
4861 + * This program, aufs is free software; you can redistribute it and/or modify
4862 + * it under the terms of the GNU General Public License as published by
4863 + * the Free Software Foundation; either version 2 of the License, or
4864 + * (at your option) any later version.
4865 + *
4866 + * This program is distributed in the hope that it will be useful,
4867 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4868 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4869 + * GNU General Public License for more details.
4870 + *
4871 + * You should have received a copy of the GNU General Public License
4872 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4873 + */
4874 +
4875 +/*
4876 + * copy-up functions, see wbr_policy.c for copy-down
4877 + */
4878 +
4879 +#include <linux/fs_stack.h>
4880 +#include <linux/mm.h>
4881 +#include <linux/task_work.h>
4882 +#include "aufs.h"
4883 +
4884 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4885 +{
4886 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4887 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4888 +
4889 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4890 +
4891 +       dst->i_flags |= iflags & ~mask;
4892 +       if (au_test_fs_notime(dst->i_sb))
4893 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4894 +}
4895 +
4896 +void au_cpup_attr_timesizes(struct inode *inode)
4897 +{
4898 +       struct inode *h_inode;
4899 +
4900 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4901 +       fsstack_copy_attr_times(inode, h_inode);
4902 +       fsstack_copy_inode_size(inode, h_inode);
4903 +}
4904 +
4905 +void au_cpup_attr_nlink(struct inode *inode, int force)
4906 +{
4907 +       struct inode *h_inode;
4908 +       struct super_block *sb;
4909 +       aufs_bindex_t bindex, bbot;
4910 +
4911 +       sb = inode->i_sb;
4912 +       bindex = au_ibtop(inode);
4913 +       h_inode = au_h_iptr(inode, bindex);
4914 +       if (!force
4915 +           && !S_ISDIR(h_inode->i_mode)
4916 +           && au_opt_test(au_mntflags(sb), PLINK)
4917 +           && au_plink_test(inode))
4918 +               return;
4919 +
4920 +       /*
4921 +        * 0 can happen in revalidating.
4922 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4923 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4924 +        * case.
4925 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4926 +        *       the incorrect link count.
4927 +        */
4928 +       set_nlink(inode, h_inode->i_nlink);
4929 +
4930 +       /*
4931 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4932 +        * it may includes whplink directory.
4933 +        */
4934 +       if (S_ISDIR(h_inode->i_mode)) {
4935 +               bbot = au_ibbot(inode);
4936 +               for (bindex++; bindex <= bbot; bindex++) {
4937 +                       h_inode = au_h_iptr(inode, bindex);
4938 +                       if (h_inode)
4939 +                               au_add_nlink(inode, h_inode);
4940 +               }
4941 +       }
4942 +}
4943 +
4944 +void au_cpup_attr_changeable(struct inode *inode)
4945 +{
4946 +       struct inode *h_inode;
4947 +
4948 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4949 +       inode->i_mode = h_inode->i_mode;
4950 +       inode->i_uid = h_inode->i_uid;
4951 +       inode->i_gid = h_inode->i_gid;
4952 +       au_cpup_attr_timesizes(inode);
4953 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4954 +}
4955 +
4956 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4957 +{
4958 +       struct au_iinfo *iinfo = au_ii(inode);
4959 +
4960 +       IiMustWriteLock(inode);
4961 +
4962 +       iinfo->ii_higen = h_inode->i_generation;
4963 +       iinfo->ii_hsb1 = h_inode->i_sb;
4964 +}
4965 +
4966 +void au_cpup_attr_all(struct inode *inode, int force)
4967 +{
4968 +       struct inode *h_inode;
4969 +
4970 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4971 +       au_cpup_attr_changeable(inode);
4972 +       if (inode->i_nlink > 0)
4973 +               au_cpup_attr_nlink(inode, force);
4974 +       inode->i_rdev = h_inode->i_rdev;
4975 +       inode->i_blkbits = h_inode->i_blkbits;
4976 +       au_cpup_igen(inode, h_inode);
4977 +}
4978 +
4979 +/* ---------------------------------------------------------------------- */
4980 +
4981 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4982 +
4983 +/* keep the timestamps of the parent dir when cpup */
4984 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4985 +                   struct path *h_path)
4986 +{
4987 +       struct inode *h_inode;
4988 +
4989 +       dt->dt_dentry = dentry;
4990 +       dt->dt_h_path = *h_path;
4991 +       h_inode = d_inode(h_path->dentry);
4992 +       dt->dt_atime = h_inode->i_atime;
4993 +       dt->dt_mtime = h_inode->i_mtime;
4994 +       /* smp_mb(); */
4995 +}
4996 +
4997 +void au_dtime_revert(struct au_dtime *dt)
4998 +{
4999 +       struct iattr attr;
5000 +       int err;
5001 +
5002 +       attr.ia_atime = dt->dt_atime;
5003 +       attr.ia_mtime = dt->dt_mtime;
5004 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5005 +               | ATTR_ATIME | ATTR_ATIME_SET;
5006 +
5007 +       /* no delegation since this is a directory */
5008 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
5009 +       if (unlikely(err))
5010 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
5011 +}
5012 +
5013 +/* ---------------------------------------------------------------------- */
5014 +
5015 +/* internal use only */
5016 +struct au_cpup_reg_attr {
5017 +       int             valid;
5018 +       struct kstat    st;
5019 +       unsigned int    iflags; /* inode->i_flags */
5020 +};
5021 +
5022 +static noinline_for_stack
5023 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5024 +              struct au_cpup_reg_attr *h_src_attr)
5025 +{
5026 +       int err, sbits, icex;
5027 +       unsigned int mnt_flags;
5028 +       unsigned char verbose;
5029 +       struct iattr ia;
5030 +       struct path h_path;
5031 +       struct inode *h_isrc, *h_idst;
5032 +       struct kstat *h_st;
5033 +       struct au_branch *br;
5034 +
5035 +       h_path.dentry = au_h_dptr(dst, bindex);
5036 +       h_idst = d_inode(h_path.dentry);
5037 +       br = au_sbr(dst->d_sb, bindex);
5038 +       h_path.mnt = au_br_mnt(br);
5039 +       h_isrc = d_inode(h_src);
5040 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
5041 +               | ATTR_ATIME | ATTR_MTIME
5042 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
5043 +       if (h_src_attr && h_src_attr->valid) {
5044 +               h_st = &h_src_attr->st;
5045 +               ia.ia_uid = h_st->uid;
5046 +               ia.ia_gid = h_st->gid;
5047 +               ia.ia_atime = h_st->atime;
5048 +               ia.ia_mtime = h_st->mtime;
5049 +               if (h_idst->i_mode != h_st->mode
5050 +                   && !S_ISLNK(h_idst->i_mode)) {
5051 +                       ia.ia_valid |= ATTR_MODE;
5052 +                       ia.ia_mode = h_st->mode;
5053 +               }
5054 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5055 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5056 +       } else {
5057 +               ia.ia_uid = h_isrc->i_uid;
5058 +               ia.ia_gid = h_isrc->i_gid;
5059 +               ia.ia_atime = h_isrc->i_atime;
5060 +               ia.ia_mtime = h_isrc->i_mtime;
5061 +               if (h_idst->i_mode != h_isrc->i_mode
5062 +                   && !S_ISLNK(h_idst->i_mode)) {
5063 +                       ia.ia_valid |= ATTR_MODE;
5064 +                       ia.ia_mode = h_isrc->i_mode;
5065 +               }
5066 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5067 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5068 +       }
5069 +       /* no delegation since it is just created */
5070 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5071 +
5072 +       /* is this nfs only? */
5073 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5074 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5075 +               ia.ia_mode = h_isrc->i_mode;
5076 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5077 +       }
5078 +
5079 +       icex = br->br_perm & AuBrAttr_ICEX;
5080 +       if (!err) {
5081 +               mnt_flags = au_mntflags(dst->d_sb);
5082 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5083 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5084 +       }
5085 +
5086 +       return err;
5087 +}
5088 +
5089 +/* ---------------------------------------------------------------------- */
5090 +
5091 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5092 +                          char *buf, unsigned long blksize)
5093 +{
5094 +       int err;
5095 +       size_t sz, rbytes, wbytes;
5096 +       unsigned char all_zero;
5097 +       char *p, *zp;
5098 +       struct inode *h_inode;
5099 +       /* reduce stack usage */
5100 +       struct iattr *ia;
5101 +
5102 +       zp = page_address(ZERO_PAGE(0));
5103 +       if (unlikely(!zp))
5104 +               return -ENOMEM; /* possible? */
5105 +
5106 +       err = 0;
5107 +       all_zero = 0;
5108 +       while (len) {
5109 +               AuDbg("len %lld\n", len);
5110 +               sz = blksize;
5111 +               if (len < blksize)
5112 +                       sz = len;
5113 +
5114 +               rbytes = 0;
5115 +               /* todo: signal_pending? */
5116 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5117 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5118 +                       err = rbytes;
5119 +               }
5120 +               if (unlikely(err < 0))
5121 +                       break;
5122 +
5123 +               all_zero = 0;
5124 +               if (len >= rbytes && rbytes == blksize)
5125 +                       all_zero = !memcmp(buf, zp, rbytes);
5126 +               if (!all_zero) {
5127 +                       wbytes = rbytes;
5128 +                       p = buf;
5129 +                       while (wbytes) {
5130 +                               size_t b;
5131 +
5132 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5133 +                               err = b;
5134 +                               /* todo: signal_pending? */
5135 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5136 +                                       continue;
5137 +                               if (unlikely(err < 0))
5138 +                                       break;
5139 +                               wbytes -= b;
5140 +                               p += b;
5141 +                       }
5142 +                       if (unlikely(err < 0))
5143 +                               break;
5144 +               } else {
5145 +                       loff_t res;
5146 +
5147 +                       AuLabel(hole);
5148 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5149 +                       err = res;
5150 +                       if (unlikely(res < 0))
5151 +                               break;
5152 +               }
5153 +               len -= rbytes;
5154 +               err = 0;
5155 +       }
5156 +
5157 +       /* the last block may be a hole */
5158 +       if (!err && all_zero) {
5159 +               AuLabel(last hole);
5160 +
5161 +               err = 1;
5162 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5163 +                       /* nfs requires this step to make last hole */
5164 +                       /* is this only nfs? */
5165 +                       do {
5166 +                               /* todo: signal_pending? */
5167 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5168 +                       } while (err == -EAGAIN || err == -EINTR);
5169 +                       if (err == 1)
5170 +                               dst->f_pos--;
5171 +               }
5172 +
5173 +               if (err == 1) {
5174 +                       ia = (void *)buf;
5175 +                       ia->ia_size = dst->f_pos;
5176 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5177 +                       ia->ia_file = dst;
5178 +                       h_inode = file_inode(dst);
5179 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5180 +                       /* no delegation since it is just created */
5181 +                       err = vfsub_notify_change(&dst->f_path, ia,
5182 +                                                 /*delegated*/NULL);
5183 +                       inode_unlock(h_inode);
5184 +               }
5185 +       }
5186 +
5187 +       return err;
5188 +}
5189 +
5190 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5191 +{
5192 +       int err;
5193 +       unsigned long blksize;
5194 +       unsigned char do_kfree;
5195 +       char *buf;
5196 +       struct super_block *h_sb;
5197 +
5198 +       err = -ENOMEM;
5199 +       h_sb = file_inode(dst)->i_sb;
5200 +       blksize = h_sb->s_blocksize;
5201 +       if (!blksize || PAGE_SIZE < blksize)
5202 +               blksize = PAGE_SIZE;
5203 +       AuDbg("blksize %lu\n", blksize);
5204 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5205 +       if (do_kfree)
5206 +               buf = kmalloc(blksize, GFP_NOFS);
5207 +       else
5208 +               buf = (void *)__get_free_page(GFP_NOFS);
5209 +       if (unlikely(!buf))
5210 +               goto out;
5211 +
5212 +       if (len > (1 << 22))
5213 +               AuDbg("copying a large file %lld\n", (long long)len);
5214 +
5215 +       src->f_pos = 0;
5216 +       dst->f_pos = 0;
5217 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5218 +       if (do_kfree) {
5219 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5220 +               au_kfree_do_rcu(buf);
5221 +       } else
5222 +               free_page((unsigned long)buf);
5223 +
5224 +out:
5225 +       return err;
5226 +}
5227 +
5228 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5229 +{
5230 +       int err;
5231 +       struct super_block *h_src_sb;
5232 +       struct inode *h_src_inode;
5233 +
5234 +       h_src_inode = file_inode(src);
5235 +       h_src_sb = h_src_inode->i_sb;
5236 +
5237 +       /* XFS acquires inode_lock */
5238 +       if (!au_test_xfs(h_src_sb))
5239 +               err = au_copy_file(dst, src, len);
5240 +       else {
5241 +               inode_unlock_shared(h_src_inode);
5242 +               err = au_copy_file(dst, src, len);
5243 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5244 +       }
5245 +
5246 +       return err;
5247 +}
5248 +
5249 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5250 +{
5251 +       int err;
5252 +       loff_t lo;
5253 +       struct super_block *h_src_sb;
5254 +       struct inode *h_src_inode;
5255 +
5256 +       h_src_inode = file_inode(src);
5257 +       h_src_sb = h_src_inode->i_sb;
5258 +       if (h_src_sb != file_inode(dst)->i_sb
5259 +           || !dst->f_op->remap_file_range) {
5260 +               err = au_do_copy(dst, src, len);
5261 +               goto out;
5262 +       }
5263 +
5264 +       if (!au_test_nfs(h_src_sb)) {
5265 +               inode_unlock_shared(h_src_inode);
5266 +               lo = vfsub_clone_file_range(src, dst, len);
5267 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5268 +       } else
5269 +               lo = vfsub_clone_file_range(src, dst, len);
5270 +       if (lo == len) {
5271 +               err = 0;
5272 +               goto out; /* success */
5273 +       } else if (lo >= 0)
5274 +               /* todo: possible? */
5275 +               /* paritially succeeded */
5276 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5277 +       else if (lo != -EOPNOTSUPP) {
5278 +               /* older XFS has a condition in cloning */
5279 +               err = lo;
5280 +               goto out;
5281 +       }
5282 +
5283 +       /* the backend fs on NFS may not support cloning */
5284 +       err = au_do_copy(dst, src, len);
5285 +
5286 +out:
5287 +       AuTraceErr(err);
5288 +       return err;
5289 +}
5290 +
5291 +/*
5292 + * to support a sparse file which is opened with O_APPEND,
5293 + * we need to close the file.
5294 + */
5295 +static int au_cp_regular(struct au_cp_generic *cpg)
5296 +{
5297 +       int err, i;
5298 +       enum { SRC, DST };
5299 +       struct {
5300 +               aufs_bindex_t bindex;
5301 +               unsigned int flags;
5302 +               struct dentry *dentry;
5303 +               int force_wr;
5304 +               struct file *file;
5305 +       } *f, file[] = {
5306 +               {
5307 +                       .bindex = cpg->bsrc,
5308 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5309 +               },
5310 +               {
5311 +                       .bindex = cpg->bdst,
5312 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5313 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5314 +               }
5315 +       };
5316 +       struct au_branch *br;
5317 +       struct super_block *sb, *h_src_sb;
5318 +       struct inode *h_src_inode;
5319 +       struct task_struct *tsk = current;
5320 +
5321 +       /* bsrc branch can be ro/rw. */
5322 +       sb = cpg->dentry->d_sb;
5323 +       f = file;
5324 +       for (i = 0; i < 2; i++, f++) {
5325 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5326 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5327 +                                   /*file*/NULL, f->force_wr);
5328 +               if (IS_ERR(f->file)) {
5329 +                       err = PTR_ERR(f->file);
5330 +                       if (i == SRC)
5331 +                               goto out;
5332 +                       else
5333 +                               goto out_src;
5334 +               }
5335 +       }
5336 +
5337 +       /* try stopping to update while we copyup */
5338 +       h_src_inode = d_inode(file[SRC].dentry);
5339 +       h_src_sb = h_src_inode->i_sb;
5340 +       if (!au_test_nfs(h_src_sb))
5341 +               IMustLock(h_src_inode);
5342 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5343 +
5344 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5345 +       if (tsk->flags & PF_KTHREAD)
5346 +               __fput_sync(file[DST].file);
5347 +       else {
5348 +               /* it happened actually */
5349 +               fput(file[DST].file);
5350 +               /*
5351 +                * too bad.
5352 +                * we have to call both since we don't know which place the file
5353 +                * was added to.
5354 +                */
5355 +               task_work_run();
5356 +               flush_delayed_fput();
5357 +       }
5358 +       br = au_sbr(sb, file[DST].bindex);
5359 +       au_lcnt_dec(&br->br_nfiles);
5360 +
5361 +out_src:
5362 +       fput(file[SRC].file);
5363 +       br = au_sbr(sb, file[SRC].bindex);
5364 +       au_lcnt_dec(&br->br_nfiles);
5365 +out:
5366 +       return err;
5367 +}
5368 +
5369 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5370 +                             struct au_cpup_reg_attr *h_src_attr)
5371 +{
5372 +       int err, rerr;
5373 +       loff_t l;
5374 +       struct path h_path;
5375 +       struct inode *h_src_inode, *h_dst_inode;
5376 +
5377 +       err = 0;
5378 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5379 +       l = i_size_read(h_src_inode);
5380 +       if (cpg->len == -1 || l < cpg->len)
5381 +               cpg->len = l;
5382 +       if (cpg->len) {
5383 +               /* try stopping to update while we are referencing */
5384 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5385 +               au_pin_hdir_unlock(cpg->pin);
5386 +
5387 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5388 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5389 +               h_src_attr->iflags = h_src_inode->i_flags;
5390 +               if (!au_test_nfs(h_src_inode->i_sb))
5391 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5392 +               else {
5393 +                       inode_unlock_shared(h_src_inode);
5394 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5395 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5396 +               }
5397 +               if (unlikely(err)) {
5398 +                       inode_unlock_shared(h_src_inode);
5399 +                       goto out;
5400 +               }
5401 +               h_src_attr->valid = 1;
5402 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5403 +                       err = au_cp_regular(cpg);
5404 +                       inode_unlock_shared(h_src_inode);
5405 +               } else {
5406 +                       inode_unlock_shared(h_src_inode);
5407 +                       err = au_cp_regular(cpg);
5408 +               }
5409 +               rerr = au_pin_hdir_relock(cpg->pin);
5410 +               if (!err && rerr)
5411 +                       err = rerr;
5412 +       }
5413 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5414 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5415 +               h_dst_inode = d_inode(h_path.dentry);
5416 +               spin_lock(&h_dst_inode->i_lock);
5417 +               h_dst_inode->i_state |= I_LINKABLE;
5418 +               spin_unlock(&h_dst_inode->i_lock);
5419 +       }
5420 +
5421 +out:
5422 +       return err;
5423 +}
5424 +
5425 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5426 +                             struct inode *h_dir)
5427 +{
5428 +       int err, symlen;
5429 +       mm_segment_t old_fs;
5430 +       union {
5431 +               char *k;
5432 +               char __user *u;
5433 +       } sym;
5434 +
5435 +       err = -ENOMEM;
5436 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5437 +       if (unlikely(!sym.k))
5438 +               goto out;
5439 +
5440 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5441 +       old_fs = get_fs();
5442 +       set_fs(KERNEL_DS);
5443 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5444 +       err = symlen;
5445 +       set_fs(old_fs);
5446 +
5447 +       if (symlen > 0) {
5448 +               sym.k[symlen] = 0;
5449 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5450 +       }
5451 +       free_page((unsigned long)sym.k);
5452 +
5453 +out:
5454 +       return err;
5455 +}
5456 +
5457 +/*
5458 + * regardless 'acl' option, reset all ACL.
5459 + * All ACL will be copied up later from the original entry on the lower branch.
5460 + */
5461 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5462 +{
5463 +       int err;
5464 +       struct dentry *h_dentry;
5465 +       struct inode *h_inode;
5466 +
5467 +       h_dentry = h_path->dentry;
5468 +       h_inode = d_inode(h_dentry);
5469 +       /* forget_all_cached_acls(h_inode)); */
5470 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5471 +       AuTraceErr(err);
5472 +       if (err == -EOPNOTSUPP)
5473 +               err = 0;
5474 +       if (!err)
5475 +               err = vfsub_acl_chmod(h_inode, mode);
5476 +
5477 +       AuTraceErr(err);
5478 +       return err;
5479 +}
5480 +
5481 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5482 +                         struct inode *h_dir, struct path *h_path)
5483 +{
5484 +       int err;
5485 +       struct inode *dir, *inode;
5486 +
5487 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5488 +       AuTraceErr(err);
5489 +       if (err == -EOPNOTSUPP)
5490 +               err = 0;
5491 +       if (unlikely(err))
5492 +               goto out;
5493 +
5494 +       /*
5495 +        * strange behaviour from the users view,
5496 +        * particularly setattr case
5497 +        */
5498 +       dir = d_inode(dst_parent);
5499 +       if (au_ibtop(dir) == cpg->bdst)
5500 +               au_cpup_attr_nlink(dir, /*force*/1);
5501 +       inode = d_inode(cpg->dentry);
5502 +       au_cpup_attr_nlink(inode, /*force*/1);
5503 +
5504 +out:
5505 +       return err;
5506 +}
5507 +
5508 +static noinline_for_stack
5509 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5510 +              struct au_cpup_reg_attr *h_src_attr)
5511 +{
5512 +       int err;
5513 +       umode_t mode;
5514 +       unsigned int mnt_flags;
5515 +       unsigned char isdir, isreg, force;
5516 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5517 +       struct au_dtime dt;
5518 +       struct path h_path;
5519 +       struct dentry *h_src, *h_dst, *h_parent;
5520 +       struct inode *h_inode, *h_dir;
5521 +       struct super_block *sb;
5522 +
5523 +       /* bsrc branch can be ro/rw. */
5524 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5525 +       h_inode = d_inode(h_src);
5526 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5527 +
5528 +       /* try stopping to be referenced while we are creating */
5529 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5530 +       if (au_ftest_cpup(cpg->flags, RENAME))
5531 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5532 +                                 AUFS_WH_PFX_LEN));
5533 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5534 +       h_dir = d_inode(h_parent);
5535 +       IMustLock(h_dir);
5536 +       AuDebugOn(h_parent != h_dst->d_parent);
5537 +
5538 +       sb = cpg->dentry->d_sb;
5539 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5540 +       if (do_dt) {
5541 +               h_path.dentry = h_parent;
5542 +               au_dtime_store(&dt, dst_parent, &h_path);
5543 +       }
5544 +       h_path.dentry = h_dst;
5545 +
5546 +       isreg = 0;
5547 +       isdir = 0;
5548 +       mode = h_inode->i_mode;
5549 +       switch (mode & S_IFMT) {
5550 +       case S_IFREG:
5551 +               isreg = 1;
5552 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5553 +               if (!err)
5554 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5555 +               break;
5556 +       case S_IFDIR:
5557 +               isdir = 1;
5558 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5559 +               if (!err)
5560 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5561 +               break;
5562 +       case S_IFLNK:
5563 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5564 +               break;
5565 +       case S_IFCHR:
5566 +       case S_IFBLK:
5567 +               AuDebugOn(!capable(CAP_MKNOD));
5568 +               /*FALLTHROUGH*/
5569 +       case S_IFIFO:
5570 +       case S_IFSOCK:
5571 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5572 +               break;
5573 +       default:
5574 +               AuIOErr("Unknown inode type 0%o\n", mode);
5575 +               err = -EIO;
5576 +       }
5577 +       if (!err)
5578 +               err = au_reset_acl(h_dir, &h_path, mode);
5579 +
5580 +       mnt_flags = au_mntflags(sb);
5581 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5582 +           && !isdir
5583 +           && au_opt_test(mnt_flags, XINO)
5584 +           && (h_inode->i_nlink == 1
5585 +               || (h_inode->i_state & I_LINKABLE))
5586 +           /* todo: unnecessary? */
5587 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5588 +           && cpg->bdst < cpg->bsrc
5589 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5590 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5591 +               /* ignore this error */
5592 +
5593 +       if (!err) {
5594 +               force = 0;
5595 +               if (isreg) {
5596 +                       force = !!cpg->len;
5597 +                       if (cpg->len == -1)
5598 +                               force = !!i_size_read(h_inode);
5599 +               }
5600 +               au_fhsm_wrote(sb, cpg->bdst, force);
5601 +       }
5602 +
5603 +       if (do_dt)
5604 +               au_dtime_revert(&dt);
5605 +       return err;
5606 +}
5607 +
5608 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5609 +{
5610 +       int err;
5611 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5612 +       struct inode *h_dir;
5613 +       aufs_bindex_t bdst;
5614 +
5615 +       dentry = cpg->dentry;
5616 +       bdst = cpg->bdst;
5617 +       h_dentry = au_h_dptr(dentry, bdst);
5618 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5619 +               dget(h_dentry);
5620 +               au_set_h_dptr(dentry, bdst, NULL);
5621 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5622 +               if (!err)
5623 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5624 +               au_set_h_dptr(dentry, bdst, h_dentry);
5625 +       } else {
5626 +               err = 0;
5627 +               parent = dget_parent(dentry);
5628 +               h_parent = au_h_dptr(parent, bdst);
5629 +               dput(parent);
5630 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5631 +               if (IS_ERR(h_path->dentry))
5632 +                       err = PTR_ERR(h_path->dentry);
5633 +       }
5634 +       if (unlikely(err))
5635 +               goto out;
5636 +
5637 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5638 +       h_dir = d_inode(h_parent);
5639 +       IMustLock(h_dir);
5640 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5641 +       /* no delegation since it is just created */
5642 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5643 +                          /*flags*/0);
5644 +       dput(h_path->dentry);
5645 +
5646 +out:
5647 +       return err;
5648 +}
5649 +
5650 +/*
5651 + * copyup the @dentry from @bsrc to @bdst.
5652 + * the caller must set the both of lower dentries.
5653 + * @len is for truncating when it is -1 copyup the entire file.
5654 + * in link/rename cases, @dst_parent may be different from the real one.
5655 + * basic->bsrc can be larger than basic->bdst.
5656 + * aufs doesn't touch the credential so
5657 + * security_inode_copy_up{,_xattr}() are unnecessary.
5658 + */
5659 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5660 +{
5661 +       int err, rerr;
5662 +       aufs_bindex_t old_ibtop;
5663 +       unsigned char isdir, plink;
5664 +       struct dentry *h_src, *h_dst, *h_parent;
5665 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5666 +       struct super_block *sb;
5667 +       struct au_branch *br;
5668 +       /* to reduce stack size */
5669 +       struct {
5670 +               struct au_dtime dt;
5671 +               struct path h_path;
5672 +               struct au_cpup_reg_attr h_src_attr;
5673 +       } *a;
5674 +
5675 +       err = -ENOMEM;
5676 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5677 +       if (unlikely(!a))
5678 +               goto out;
5679 +       a->h_src_attr.valid = 0;
5680 +
5681 +       sb = cpg->dentry->d_sb;
5682 +       br = au_sbr(sb, cpg->bdst);
5683 +       a->h_path.mnt = au_br_mnt(br);
5684 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5685 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5686 +       h_dir = d_inode(h_parent);
5687 +       IMustLock(h_dir);
5688 +
5689 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5690 +       inode = d_inode(cpg->dentry);
5691 +
5692 +       if (!dst_parent)
5693 +               dst_parent = dget_parent(cpg->dentry);
5694 +       else
5695 +               dget(dst_parent);
5696 +
5697 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5698 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5699 +       if (dst_inode) {
5700 +               if (unlikely(!plink)) {
5701 +                       err = -EIO;
5702 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5703 +                               "but plink is disabled\n",
5704 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5705 +                       goto out_parent;
5706 +               }
5707 +
5708 +               if (dst_inode->i_nlink) {
5709 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5710 +
5711 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5712 +                       err = PTR_ERR(h_src);
5713 +                       if (IS_ERR(h_src))
5714 +                               goto out_parent;
5715 +                       if (unlikely(d_is_negative(h_src))) {
5716 +                               err = -EIO;
5717 +                               AuIOErr("i%lu exists on b%d "
5718 +                                       "but not pseudo-linked\n",
5719 +                                       inode->i_ino, cpg->bdst);
5720 +                               dput(h_src);
5721 +                               goto out_parent;
5722 +                       }
5723 +
5724 +                       if (do_dt) {
5725 +                               a->h_path.dentry = h_parent;
5726 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5727 +                       }
5728 +
5729 +                       a->h_path.dentry = h_dst;
5730 +                       delegated = NULL;
5731 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5732 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5733 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5734 +                       if (do_dt)
5735 +                               au_dtime_revert(&a->dt);
5736 +                       if (unlikely(err == -EWOULDBLOCK)) {
5737 +                               pr_warn("cannot retry for NFSv4 delegation"
5738 +                                       " for an internal link\n");
5739 +                               iput(delegated);
5740 +                       }
5741 +                       dput(h_src);
5742 +                       goto out_parent;
5743 +               } else
5744 +                       /* todo: cpup_wh_file? */
5745 +                       /* udba work */
5746 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5747 +       }
5748 +
5749 +       isdir = S_ISDIR(inode->i_mode);
5750 +       old_ibtop = au_ibtop(inode);
5751 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5752 +       if (unlikely(err))
5753 +               goto out_rev;
5754 +       dst_inode = d_inode(h_dst);
5755 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5756 +       /* todo: necessary? */
5757 +       /* au_pin_hdir_unlock(cpg->pin); */
5758 +
5759 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5760 +       if (unlikely(err)) {
5761 +               /* todo: necessary? */
5762 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5763 +               inode_unlock(dst_inode);
5764 +               goto out_rev;
5765 +       }
5766 +
5767 +       if (cpg->bdst < old_ibtop) {
5768 +               if (S_ISREG(inode->i_mode)) {
5769 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5770 +                       if (unlikely(err)) {
5771 +                               /* ignore an error */
5772 +                               /* au_pin_hdir_relock(cpg->pin); */
5773 +                               inode_unlock(dst_inode);
5774 +                               goto out_rev;
5775 +                       }
5776 +               }
5777 +               au_set_ibtop(inode, cpg->bdst);
5778 +       } else
5779 +               au_set_ibbot(inode, cpg->bdst);
5780 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5781 +                     au_hi_flags(inode, isdir));
5782 +
5783 +       /* todo: necessary? */
5784 +       /* err = au_pin_hdir_relock(cpg->pin); */
5785 +       inode_unlock(dst_inode);
5786 +       if (unlikely(err))
5787 +               goto out_rev;
5788 +
5789 +       src_inode = d_inode(h_src);
5790 +       if (!isdir
5791 +           && (src_inode->i_nlink > 1
5792 +               || src_inode->i_state & I_LINKABLE)
5793 +           && plink)
5794 +               au_plink_append(inode, cpg->bdst, h_dst);
5795 +
5796 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5797 +               a->h_path.dentry = h_dst;
5798 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5799 +       }
5800 +       if (!err)
5801 +               goto out_parent; /* success */
5802 +
5803 +       /* revert */
5804 +out_rev:
5805 +       a->h_path.dentry = h_parent;
5806 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5807 +       a->h_path.dentry = h_dst;
5808 +       rerr = 0;
5809 +       if (d_is_positive(h_dst)) {
5810 +               if (!isdir) {
5811 +                       /* no delegation since it is just created */
5812 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5813 +                                           /*delegated*/NULL, /*force*/0);
5814 +               } else
5815 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5816 +       }
5817 +       au_dtime_revert(&a->dt);
5818 +       if (rerr) {
5819 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5820 +               err = -EIO;
5821 +       }
5822 +out_parent:
5823 +       dput(dst_parent);
5824 +       au_kfree_rcu(a);
5825 +out:
5826 +       return err;
5827 +}
5828 +
5829 +#if 0 /* reserved */
5830 +struct au_cpup_single_args {
5831 +       int *errp;
5832 +       struct au_cp_generic *cpg;
5833 +       struct dentry *dst_parent;
5834 +};
5835 +
5836 +static void au_call_cpup_single(void *args)
5837 +{
5838 +       struct au_cpup_single_args *a = args;
5839 +
5840 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5841 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5842 +       au_pin_hdir_release(a->cpg->pin);
5843 +}
5844 +#endif
5845 +
5846 +/*
5847 + * prevent SIGXFSZ in copy-up.
5848 + * testing CAP_MKNOD is for generic fs,
5849 + * but CAP_FSETID is for xfs only, currently.
5850 + */
5851 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5852 +{
5853 +       int do_sio;
5854 +       struct super_block *sb;
5855 +       struct inode *h_dir;
5856 +
5857 +       do_sio = 0;
5858 +       sb = au_pinned_parent(pin)->d_sb;
5859 +       if (!au_wkq_test()
5860 +           && (!au_sbi(sb)->si_plink_maint_pid
5861 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5862 +               switch (mode & S_IFMT) {
5863 +               case S_IFREG:
5864 +                       /* no condition about RLIMIT_FSIZE and the file size */
5865 +                       do_sio = 1;
5866 +                       break;
5867 +               case S_IFCHR:
5868 +               case S_IFBLK:
5869 +                       do_sio = !capable(CAP_MKNOD);
5870 +                       break;
5871 +               }
5872 +               if (!do_sio)
5873 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5874 +                                 && !capable(CAP_FSETID));
5875 +               /* this workaround may be removed in the future */
5876 +               if (!do_sio) {
5877 +                       h_dir = au_pinned_h_dir(pin);
5878 +                       do_sio = h_dir->i_mode & S_ISVTX;
5879 +               }
5880 +       }
5881 +
5882 +       return do_sio;
5883 +}
5884 +
5885 +#if 0 /* reserved */
5886 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5887 +{
5888 +       int err, wkq_err;
5889 +       struct dentry *h_dentry;
5890 +
5891 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5892 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5893 +               err = au_cpup_single(cpg, dst_parent);
5894 +       else {
5895 +               struct au_cpup_single_args args = {
5896 +                       .errp           = &err,
5897 +                       .cpg            = cpg,
5898 +                       .dst_parent     = dst_parent
5899 +               };
5900 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5901 +               if (unlikely(wkq_err))
5902 +                       err = wkq_err;
5903 +       }
5904 +
5905 +       return err;
5906 +}
5907 +#endif
5908 +
5909 +/*
5910 + * copyup the @dentry from the first active lower branch to @bdst,
5911 + * using au_cpup_single().
5912 + */
5913 +static int au_cpup_simple(struct au_cp_generic *cpg)
5914 +{
5915 +       int err;
5916 +       unsigned int flags_orig;
5917 +       struct dentry *dentry;
5918 +
5919 +       AuDebugOn(cpg->bsrc < 0);
5920 +
5921 +       dentry = cpg->dentry;
5922 +       DiMustWriteLock(dentry);
5923 +
5924 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5925 +       if (!err) {
5926 +               flags_orig = cpg->flags;
5927 +               au_fset_cpup(cpg->flags, RENAME);
5928 +               err = au_cpup_single(cpg, NULL);
5929 +               cpg->flags = flags_orig;
5930 +               if (!err)
5931 +                       return 0; /* success */
5932 +
5933 +               /* revert */
5934 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5935 +               au_set_dbtop(dentry, cpg->bsrc);
5936 +       }
5937 +
5938 +       return err;
5939 +}
5940 +
5941 +struct au_cpup_simple_args {
5942 +       int *errp;
5943 +       struct au_cp_generic *cpg;
5944 +};
5945 +
5946 +static void au_call_cpup_simple(void *args)
5947 +{
5948 +       struct au_cpup_simple_args *a = args;
5949 +
5950 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5951 +       *a->errp = au_cpup_simple(a->cpg);
5952 +       au_pin_hdir_release(a->cpg->pin);
5953 +}
5954 +
5955 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5956 +{
5957 +       int err, wkq_err;
5958 +       struct dentry *dentry, *parent;
5959 +       struct file *h_file;
5960 +       struct inode *h_dir;
5961 +
5962 +       dentry = cpg->dentry;
5963 +       h_file = NULL;
5964 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5965 +               AuDebugOn(cpg->bsrc < 0);
5966 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5967 +               err = PTR_ERR(h_file);
5968 +               if (IS_ERR(h_file))
5969 +                       goto out;
5970 +       }
5971 +
5972 +       parent = dget_parent(dentry);
5973 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5974 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5975 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5976 +               err = au_cpup_simple(cpg);
5977 +       else {
5978 +               struct au_cpup_simple_args args = {
5979 +                       .errp           = &err,
5980 +                       .cpg            = cpg
5981 +               };
5982 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5983 +               if (unlikely(wkq_err))
5984 +                       err = wkq_err;
5985 +       }
5986 +
5987 +       dput(parent);
5988 +       if (h_file)
5989 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5990 +
5991 +out:
5992 +       return err;
5993 +}
5994 +
5995 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5996 +{
5997 +       aufs_bindex_t bsrc, bbot;
5998 +       struct dentry *dentry, *h_dentry;
5999 +
6000 +       if (cpg->bsrc < 0) {
6001 +               dentry = cpg->dentry;
6002 +               bbot = au_dbbot(dentry);
6003 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
6004 +                       h_dentry = au_h_dptr(dentry, bsrc);
6005 +                       if (h_dentry) {
6006 +                               AuDebugOn(d_is_negative(h_dentry));
6007 +                               break;
6008 +                       }
6009 +               }
6010 +               AuDebugOn(bsrc > bbot);
6011 +               cpg->bsrc = bsrc;
6012 +       }
6013 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
6014 +       return au_do_sio_cpup_simple(cpg);
6015 +}
6016 +
6017 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6018 +{
6019 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
6020 +       return au_do_sio_cpup_simple(cpg);
6021 +}
6022 +
6023 +/* ---------------------------------------------------------------------- */
6024 +
6025 +/*
6026 + * copyup the deleted file for writing.
6027 + */
6028 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6029 +                        struct file *file)
6030 +{
6031 +       int err;
6032 +       unsigned int flags_orig;
6033 +       aufs_bindex_t bsrc_orig;
6034 +       struct au_dinfo *dinfo;
6035 +       struct {
6036 +               struct au_hdentry *hd;
6037 +               struct dentry *h_dentry;
6038 +       } hdst, hsrc;
6039 +
6040 +       dinfo = au_di(cpg->dentry);
6041 +       AuRwMustWriteLock(&dinfo->di_rwsem);
6042 +
6043 +       bsrc_orig = cpg->bsrc;
6044 +       cpg->bsrc = dinfo->di_btop;
6045 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
6046 +       hdst.h_dentry = hdst.hd->hd_dentry;
6047 +       hdst.hd->hd_dentry = wh_dentry;
6048 +       dinfo->di_btop = cpg->bdst;
6049 +
6050 +       hsrc.h_dentry = NULL;
6051 +       if (file) {
6052 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6053 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
6054 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
6055 +       }
6056 +       flags_orig = cpg->flags;
6057 +       cpg->flags = !AuCpup_DTIME;
6058 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
6059 +       cpg->flags = flags_orig;
6060 +       if (file) {
6061 +               if (!err)
6062 +                       err = au_reopen_nondir(file);
6063 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
6064 +       }
6065 +       hdst.hd->hd_dentry = hdst.h_dentry;
6066 +       dinfo->di_btop = cpg->bsrc;
6067 +       cpg->bsrc = bsrc_orig;
6068 +
6069 +       return err;
6070 +}
6071 +
6072 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6073 +{
6074 +       int err;
6075 +       aufs_bindex_t bdst;
6076 +       struct au_dtime dt;
6077 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6078 +       struct au_branch *br;
6079 +       struct path h_path;
6080 +
6081 +       dentry = cpg->dentry;
6082 +       bdst = cpg->bdst;
6083 +       br = au_sbr(dentry->d_sb, bdst);
6084 +       parent = dget_parent(dentry);
6085 +       h_parent = au_h_dptr(parent, bdst);
6086 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6087 +       err = PTR_ERR(wh_dentry);
6088 +       if (IS_ERR(wh_dentry))
6089 +               goto out;
6090 +
6091 +       h_path.dentry = h_parent;
6092 +       h_path.mnt = au_br_mnt(br);
6093 +       au_dtime_store(&dt, parent, &h_path);
6094 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6095 +       if (unlikely(err))
6096 +               goto out_wh;
6097 +
6098 +       dget(wh_dentry);
6099 +       h_path.dentry = wh_dentry;
6100 +       if (!d_is_dir(wh_dentry)) {
6101 +               /* no delegation since it is just created */
6102 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6103 +                                  /*delegated*/NULL, /*force*/0);
6104 +       } else
6105 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6106 +       if (unlikely(err)) {
6107 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6108 +                       wh_dentry, err);
6109 +               err = -EIO;
6110 +       }
6111 +       au_dtime_revert(&dt);
6112 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6113 +
6114 +out_wh:
6115 +       dput(wh_dentry);
6116 +out:
6117 +       dput(parent);
6118 +       return err;
6119 +}
6120 +
6121 +struct au_cpup_wh_args {
6122 +       int *errp;
6123 +       struct au_cp_generic *cpg;
6124 +       struct file *file;
6125 +};
6126 +
6127 +static void au_call_cpup_wh(void *args)
6128 +{
6129 +       struct au_cpup_wh_args *a = args;
6130 +
6131 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6132 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6133 +       au_pin_hdir_release(a->cpg->pin);
6134 +}
6135 +
6136 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6137 +{
6138 +       int err, wkq_err;
6139 +       aufs_bindex_t bdst;
6140 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6141 +       struct inode *dir, *h_dir, *h_tmpdir;
6142 +       struct au_wbr *wbr;
6143 +       struct au_pin wh_pin, *pin_orig;
6144 +
6145 +       dentry = cpg->dentry;
6146 +       bdst = cpg->bdst;
6147 +       parent = dget_parent(dentry);
6148 +       dir = d_inode(parent);
6149 +       h_orph = NULL;
6150 +       h_parent = NULL;
6151 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6152 +       h_tmpdir = h_dir;
6153 +       pin_orig = NULL;
6154 +       if (!h_dir->i_nlink) {
6155 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6156 +               h_orph = wbr->wbr_orph;
6157 +
6158 +               h_parent = dget(au_h_dptr(parent, bdst));
6159 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6160 +               h_tmpdir = d_inode(h_orph);
6161 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6162 +
6163 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6164 +               /* todo: au_h_open_pre()? */
6165 +
6166 +               pin_orig = cpg->pin;
6167 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6168 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6169 +               cpg->pin = &wh_pin;
6170 +       }
6171 +
6172 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6173 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6174 +               err = au_cpup_wh(cpg, file);
6175 +       else {
6176 +               struct au_cpup_wh_args args = {
6177 +                       .errp   = &err,
6178 +                       .cpg    = cpg,
6179 +                       .file   = file
6180 +               };
6181 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6182 +               if (unlikely(wkq_err))
6183 +                       err = wkq_err;
6184 +       }
6185 +
6186 +       if (h_orph) {
6187 +               inode_unlock(h_tmpdir);
6188 +               /* todo: au_h_open_post()? */
6189 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6190 +               au_set_h_dptr(parent, bdst, h_parent);
6191 +               AuDebugOn(!pin_orig);
6192 +               cpg->pin = pin_orig;
6193 +       }
6194 +       iput(h_dir);
6195 +       dput(parent);
6196 +
6197 +       return err;
6198 +}
6199 +
6200 +/* ---------------------------------------------------------------------- */
6201 +
6202 +/*
6203 + * generic routine for both of copy-up and copy-down.
6204 + */
6205 +/* cf. revalidate function in file.c */
6206 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6207 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6208 +                        struct au_pin *pin,
6209 +                        struct dentry *h_parent, void *arg),
6210 +              void *arg)
6211 +{
6212 +       int err;
6213 +       struct au_pin pin;
6214 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6215 +
6216 +       err = 0;
6217 +       parent = dget_parent(dentry);
6218 +       if (IS_ROOT(parent))
6219 +               goto out;
6220 +
6221 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6222 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6223 +
6224 +       /* do not use au_dpage */
6225 +       real_parent = parent;
6226 +       while (1) {
6227 +               dput(parent);
6228 +               parent = dget_parent(dentry);
6229 +               h_parent = au_h_dptr(parent, bdst);
6230 +               if (h_parent)
6231 +                       goto out; /* success */
6232 +
6233 +               /* find top dir which is necessary to cpup */
6234 +               do {
6235 +                       d = parent;
6236 +                       dput(parent);
6237 +                       parent = dget_parent(d);
6238 +                       di_read_lock_parent3(parent, !AuLock_IR);
6239 +                       h_parent = au_h_dptr(parent, bdst);
6240 +                       di_read_unlock(parent, !AuLock_IR);
6241 +               } while (!h_parent);
6242 +
6243 +               if (d != real_parent)
6244 +                       di_write_lock_child3(d);
6245 +
6246 +               /* somebody else might create while we were sleeping */
6247 +               h_dentry = au_h_dptr(d, bdst);
6248 +               if (!h_dentry || d_is_negative(h_dentry)) {
6249 +                       if (h_dentry)
6250 +                               au_update_dbtop(d);
6251 +
6252 +                       au_pin_set_dentry(&pin, d);
6253 +                       err = au_do_pin(&pin);
6254 +                       if (!err) {
6255 +                               err = cp(d, bdst, &pin, h_parent, arg);
6256 +                               au_unpin(&pin);
6257 +                       }
6258 +               }
6259 +
6260 +               if (d != real_parent)
6261 +                       di_write_unlock(d);
6262 +               if (unlikely(err))
6263 +                       break;
6264 +       }
6265 +
6266 +out:
6267 +       dput(parent);
6268 +       return err;
6269 +}
6270 +
6271 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6272 +                      struct au_pin *pin,
6273 +                      struct dentry *h_parent __maybe_unused,
6274 +                      void *arg __maybe_unused)
6275 +{
6276 +       struct au_cp_generic cpg = {
6277 +               .dentry = dentry,
6278 +               .bdst   = bdst,
6279 +               .bsrc   = -1,
6280 +               .len    = 0,
6281 +               .pin    = pin,
6282 +               .flags  = AuCpup_DTIME
6283 +       };
6284 +       return au_sio_cpup_simple(&cpg);
6285 +}
6286 +
6287 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6288 +{
6289 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6290 +}
6291 +
6292 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6293 +{
6294 +       int err;
6295 +       struct dentry *parent;
6296 +       struct inode *dir;
6297 +
6298 +       parent = dget_parent(dentry);
6299 +       dir = d_inode(parent);
6300 +       err = 0;
6301 +       if (au_h_iptr(dir, bdst))
6302 +               goto out;
6303 +
6304 +       di_read_unlock(parent, AuLock_IR);
6305 +       di_write_lock_parent(parent);
6306 +       /* someone else might change our inode while we were sleeping */
6307 +       if (!au_h_iptr(dir, bdst))
6308 +               err = au_cpup_dirs(dentry, bdst);
6309 +       di_downgrade_lock(parent, AuLock_IR);
6310 +
6311 +out:
6312 +       dput(parent);
6313 +       return err;
6314 +}
6315 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6316 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6317 +++ linux/fs/aufs/cpup.h        2020-01-27 10:57:18.168871450 +0100
6318 @@ -0,0 +1,100 @@
6319 +/* SPDX-License-Identifier: GPL-2.0 */
6320 +/*
6321 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6322 + *
6323 + * This program, aufs is free software; you can redistribute it and/or modify
6324 + * it under the terms of the GNU General Public License as published by
6325 + * the Free Software Foundation; either version 2 of the License, or
6326 + * (at your option) any later version.
6327 + *
6328 + * This program is distributed in the hope that it will be useful,
6329 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6330 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6331 + * GNU General Public License for more details.
6332 + *
6333 + * You should have received a copy of the GNU General Public License
6334 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6335 + */
6336 +
6337 +/*
6338 + * copy-up/down functions
6339 + */
6340 +
6341 +#ifndef __AUFS_CPUP_H__
6342 +#define __AUFS_CPUP_H__
6343 +
6344 +#ifdef __KERNEL__
6345 +
6346 +#include <linux/path.h>
6347 +
6348 +struct inode;
6349 +struct file;
6350 +struct au_pin;
6351 +
6352 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6353 +void au_cpup_attr_timesizes(struct inode *inode);
6354 +void au_cpup_attr_nlink(struct inode *inode, int force);
6355 +void au_cpup_attr_changeable(struct inode *inode);
6356 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6357 +void au_cpup_attr_all(struct inode *inode, int force);
6358 +
6359 +/* ---------------------------------------------------------------------- */
6360 +
6361 +struct au_cp_generic {
6362 +       struct dentry   *dentry;
6363 +       aufs_bindex_t   bdst, bsrc;
6364 +       loff_t          len;
6365 +       struct au_pin   *pin;
6366 +       unsigned int    flags;
6367 +};
6368 +
6369 +/* cpup flags */
6370 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6371 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6372 +                                                  for link(2) */
6373 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6374 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6375 +                                                  cpup */
6376 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6377 +                                                  existing entry */
6378 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6379 +                                                  the branch is marked as RO */
6380 +
6381 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6382 +#undef AuCpup_HOPEN
6383 +#define AuCpup_HOPEN           0
6384 +#endif
6385 +
6386 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6387 +#define au_fset_cpup(flags, name) \
6388 +       do { (flags) |= AuCpup_##name; } while (0)
6389 +#define au_fclr_cpup(flags, name) \
6390 +       do { (flags) &= ~AuCpup_##name; } while (0)
6391 +
6392 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6393 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6394 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6395 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6396 +
6397 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6398 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6399 +                        struct au_pin *pin,
6400 +                        struct dentry *h_parent, void *arg),
6401 +              void *arg);
6402 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6403 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6404 +
6405 +/* ---------------------------------------------------------------------- */
6406 +
6407 +/* keep timestamps when copyup */
6408 +struct au_dtime {
6409 +       struct dentry *dt_dentry;
6410 +       struct path dt_h_path;
6411 +       struct timespec64 dt_atime, dt_mtime;
6412 +};
6413 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6414 +                   struct path *h_path);
6415 +void au_dtime_revert(struct au_dtime *dt);
6416 +
6417 +#endif /* __KERNEL__ */
6418 +#endif /* __AUFS_CPUP_H__ */
6419 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6420 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6421 +++ linux/fs/aufs/dbgaufs.c     2020-01-27 10:57:18.168871450 +0100
6422 @@ -0,0 +1,526 @@
6423 +// SPDX-License-Identifier: GPL-2.0
6424 +/*
6425 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6426 + *
6427 + * This program, aufs is free software; you can redistribute it and/or modify
6428 + * it under the terms of the GNU General Public License as published by
6429 + * the Free Software Foundation; either version 2 of the License, or
6430 + * (at your option) any later version.
6431 + *
6432 + * This program is distributed in the hope that it will be useful,
6433 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6434 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6435 + * GNU General Public License for more details.
6436 + *
6437 + * You should have received a copy of the GNU General Public License
6438 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6439 + */
6440 +
6441 +/*
6442 + * debugfs interface
6443 + */
6444 +
6445 +#include <linux/debugfs.h>
6446 +#include "aufs.h"
6447 +
6448 +#ifndef CONFIG_SYSFS
6449 +#error DEBUG_FS depends upon SYSFS
6450 +#endif
6451 +
6452 +static struct dentry *dbgaufs;
6453 +static const mode_t dbgaufs_mode = 0444;
6454 +
6455 +/* 20 is max digits length of ulong 64 */
6456 +struct dbgaufs_arg {
6457 +       int n;
6458 +       char a[20 * 4];
6459 +};
6460 +
6461 +/*
6462 + * common function for all XINO files
6463 + */
6464 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6465 +                             struct file *file)
6466 +{
6467 +       void *p;
6468 +
6469 +       p = file->private_data;
6470 +       if (p) {
6471 +               /* this is struct dbgaufs_arg */
6472 +               AuDebugOn(!au_kfree_sz_test(p));
6473 +               au_kfree_do_rcu(p);
6474 +       }
6475 +       return 0;
6476 +}
6477 +
6478 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6479 +                          int cnt)
6480 +{
6481 +       int err;
6482 +       struct kstat st;
6483 +       struct dbgaufs_arg *p;
6484 +
6485 +       err = -ENOMEM;
6486 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6487 +       if (unlikely(!p))
6488 +               goto out;
6489 +
6490 +       err = 0;
6491 +       p->n = 0;
6492 +       file->private_data = p;
6493 +       if (!xf)
6494 +               goto out;
6495 +
6496 +       err = vfsub_getattr(&xf->f_path, &st);
6497 +       if (!err) {
6498 +               if (do_fcnt)
6499 +                       p->n = snprintf
6500 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6501 +                                cnt, st.blocks, st.blksize,
6502 +                                (long long)st.size);
6503 +               else
6504 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6505 +                                       st.blocks, st.blksize,
6506 +                                       (long long)st.size);
6507 +               AuDebugOn(p->n >= sizeof(p->a));
6508 +       } else {
6509 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6510 +               err = 0;
6511 +       }
6512 +
6513 +out:
6514 +       return err;
6515 +}
6516 +
6517 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6518 +                              size_t count, loff_t *ppos)
6519 +{
6520 +       struct dbgaufs_arg *p;
6521 +
6522 +       p = file->private_data;
6523 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6524 +}
6525 +
6526 +/* ---------------------------------------------------------------------- */
6527 +
6528 +struct dbgaufs_plink_arg {
6529 +       int n;
6530 +       char a[];
6531 +};
6532 +
6533 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6534 +                                struct file *file)
6535 +{
6536 +       free_page((unsigned long)file->private_data);
6537 +       return 0;
6538 +}
6539 +
6540 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6541 +{
6542 +       int err, i, limit;
6543 +       unsigned long n, sum;
6544 +       struct dbgaufs_plink_arg *p;
6545 +       struct au_sbinfo *sbinfo;
6546 +       struct super_block *sb;
6547 +       struct hlist_bl_head *hbl;
6548 +
6549 +       err = -ENOMEM;
6550 +       p = (void *)get_zeroed_page(GFP_NOFS);
6551 +       if (unlikely(!p))
6552 +               goto out;
6553 +
6554 +       err = -EFBIG;
6555 +       sbinfo = inode->i_private;
6556 +       sb = sbinfo->si_sb;
6557 +       si_noflush_read_lock(sb);
6558 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6559 +               limit = PAGE_SIZE - sizeof(p->n);
6560 +
6561 +               /* the number of buckets */
6562 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6563 +               p->n += n;
6564 +               limit -= n;
6565 +
6566 +               sum = 0;
6567 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6568 +                    i++, hbl++) {
6569 +                       n = au_hbl_count(hbl);
6570 +                       sum += n;
6571 +
6572 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6573 +                       p->n += n;
6574 +                       limit -= n;
6575 +                       if (unlikely(limit <= 0))
6576 +                               goto out_free;
6577 +               }
6578 +               p->a[p->n - 1] = '\n';
6579 +
6580 +               /* the sum of plinks */
6581 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6582 +               p->n += n;
6583 +               limit -= n;
6584 +               if (unlikely(limit <= 0))
6585 +                       goto out_free;
6586 +       } else {
6587 +#define str "1\n0\n0\n"
6588 +               p->n = sizeof(str) - 1;
6589 +               strcpy(p->a, str);
6590 +#undef str
6591 +       }
6592 +       si_read_unlock(sb);
6593 +
6594 +       err = 0;
6595 +       file->private_data = p;
6596 +       goto out; /* success */
6597 +
6598 +out_free:
6599 +       free_page((unsigned long)p);
6600 +out:
6601 +       return err;
6602 +}
6603 +
6604 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6605 +                                 size_t count, loff_t *ppos)
6606 +{
6607 +       struct dbgaufs_plink_arg *p;
6608 +
6609 +       p = file->private_data;
6610 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6611 +}
6612 +
6613 +static const struct file_operations dbgaufs_plink_fop = {
6614 +       .owner          = THIS_MODULE,
6615 +       .open           = dbgaufs_plink_open,
6616 +       .release        = dbgaufs_plink_release,
6617 +       .read           = dbgaufs_plink_read
6618 +};
6619 +
6620 +/* ---------------------------------------------------------------------- */
6621 +
6622 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6623 +{
6624 +       int err;
6625 +       struct au_sbinfo *sbinfo;
6626 +       struct super_block *sb;
6627 +
6628 +       sbinfo = inode->i_private;
6629 +       sb = sbinfo->si_sb;
6630 +       si_noflush_read_lock(sb);
6631 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6632 +       si_read_unlock(sb);
6633 +       return err;
6634 +}
6635 +
6636 +static const struct file_operations dbgaufs_xib_fop = {
6637 +       .owner          = THIS_MODULE,
6638 +       .open           = dbgaufs_xib_open,
6639 +       .release        = dbgaufs_xi_release,
6640 +       .read           = dbgaufs_xi_read
6641 +};
6642 +
6643 +/* ---------------------------------------------------------------------- */
6644 +
6645 +#define DbgaufsXi_PREFIX "xi"
6646 +
6647 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6648 +{
6649 +       int err, idx;
6650 +       long l;
6651 +       aufs_bindex_t bindex;
6652 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6653 +       struct au_sbinfo *sbinfo;
6654 +       struct super_block *sb;
6655 +       struct au_xino *xi;
6656 +       struct file *xf;
6657 +       struct qstr *name;
6658 +       struct au_branch *br;
6659 +
6660 +       err = -ENOENT;
6661 +       name = &file->f_path.dentry->d_name;
6662 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6663 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6664 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6665 +               goto out;
6666 +
6667 +       AuDebugOn(name->len >= sizeof(a));
6668 +       memcpy(a, name->name, name->len);
6669 +       a[name->len] = '\0';
6670 +       p = strchr(a, '-');
6671 +       if (p)
6672 +               *p = '\0';
6673 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6674 +       if (unlikely(err))
6675 +               goto out;
6676 +       bindex = l;
6677 +       idx = 0;
6678 +       if (p) {
6679 +               err = kstrtol(p + 1, 10, &l);
6680 +               if (unlikely(err))
6681 +                       goto out;
6682 +               idx = l;
6683 +       }
6684 +
6685 +       err = -ENOENT;
6686 +       sbinfo = inode->i_private;
6687 +       sb = sbinfo->si_sb;
6688 +       si_noflush_read_lock(sb);
6689 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6690 +               goto out_si;
6691 +       br = au_sbr(sb, bindex);
6692 +       xi = br->br_xino;
6693 +       if (unlikely(idx >= xi->xi_nfile))
6694 +               goto out_si;
6695 +       xf = au_xino_file(xi, idx);
6696 +       if (xf)
6697 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6698 +                                     au_xino_count(br));
6699 +
6700 +out_si:
6701 +       si_read_unlock(sb);
6702 +out:
6703 +       AuTraceErr(err);
6704 +       return err;
6705 +}
6706 +
6707 +static const struct file_operations dbgaufs_xino_fop = {
6708 +       .owner          = THIS_MODULE,
6709 +       .open           = dbgaufs_xino_open,
6710 +       .release        = dbgaufs_xi_release,
6711 +       .read           = dbgaufs_xi_read
6712 +};
6713 +
6714 +void dbgaufs_xino_del(struct au_branch *br)
6715 +{
6716 +       struct dentry *dbgaufs;
6717 +
6718 +       dbgaufs = br->br_dbgaufs;
6719 +       if (!dbgaufs)
6720 +               return;
6721 +
6722 +       br->br_dbgaufs = NULL;
6723 +       /* debugfs acquires the parent i_mutex */
6724 +       lockdep_off();
6725 +       debugfs_remove(dbgaufs);
6726 +       lockdep_on();
6727 +}
6728 +
6729 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6730 +{
6731 +       aufs_bindex_t bbot;
6732 +       struct au_branch *br;
6733 +
6734 +       if (!au_sbi(sb)->si_dbgaufs)
6735 +               return;
6736 +
6737 +       bbot = au_sbbot(sb);
6738 +       for (; bindex <= bbot; bindex++) {
6739 +               br = au_sbr(sb, bindex);
6740 +               dbgaufs_xino_del(br);
6741 +       }
6742 +}
6743 +
6744 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6745 +                             unsigned int idx, struct dentry *parent,
6746 +                             struct au_sbinfo *sbinfo)
6747 +{
6748 +       struct au_branch *br;
6749 +       struct dentry *d;
6750 +       /* "xi" bindex(5) "-" idx(2) NULL */
6751 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6752 +
6753 +       if (!idx)
6754 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6755 +       else
6756 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6757 +                        bindex, idx);
6758 +       br = au_sbr(sb, bindex);
6759 +       if (br->br_dbgaufs) {
6760 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6761 +
6762 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6763 +                       /* debugfs acquires the parent i_mutex */
6764 +                       lockdep_off();
6765 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6766 +                                          name);
6767 +                       lockdep_on();
6768 +                       if (unlikely(!d))
6769 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6770 +                                       parent, name);
6771 +               }
6772 +       } else {
6773 +               lockdep_off();
6774 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6775 +                                                    sbinfo, &dbgaufs_xino_fop);
6776 +               lockdep_on();
6777 +               if (unlikely(!br->br_dbgaufs))
6778 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6779 +                               parent, name);
6780 +       }
6781 +}
6782 +
6783 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6784 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6785 +{
6786 +       struct au_branch *br;
6787 +       struct au_xino *xi;
6788 +       unsigned int u;
6789 +
6790 +       br = au_sbr(sb, bindex);
6791 +       xi = br->br_xino;
6792 +       for (u = 0; u < xi->xi_nfile; u++)
6793 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6794 +}
6795 +
6796 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6797 +{
6798 +       struct au_sbinfo *sbinfo;
6799 +       struct dentry *parent;
6800 +       aufs_bindex_t bbot;
6801 +
6802 +       if (!au_opt_test(au_mntflags(sb), XINO))
6803 +               return;
6804 +
6805 +       sbinfo = au_sbi(sb);
6806 +       parent = sbinfo->si_dbgaufs;
6807 +       if (!parent)
6808 +               return;
6809 +
6810 +       bbot = au_sbbot(sb);
6811 +       if (topdown)
6812 +               for (; bindex <= bbot; bindex++)
6813 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6814 +       else
6815 +               for (; bbot >= bindex; bbot--)
6816 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6817 +}
6818 +
6819 +/* ---------------------------------------------------------------------- */
6820 +
6821 +#ifdef CONFIG_AUFS_EXPORT
6822 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6823 +{
6824 +       int err;
6825 +       struct au_sbinfo *sbinfo;
6826 +       struct super_block *sb;
6827 +
6828 +       sbinfo = inode->i_private;
6829 +       sb = sbinfo->si_sb;
6830 +       si_noflush_read_lock(sb);
6831 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6832 +       si_read_unlock(sb);
6833 +       return err;
6834 +}
6835 +
6836 +static const struct file_operations dbgaufs_xigen_fop = {
6837 +       .owner          = THIS_MODULE,
6838 +       .open           = dbgaufs_xigen_open,
6839 +       .release        = dbgaufs_xi_release,
6840 +       .read           = dbgaufs_xi_read
6841 +};
6842 +
6843 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6844 +{
6845 +       int err;
6846 +
6847 +       /*
6848 +        * This function is a dynamic '__init' function actually,
6849 +        * so the tiny check for si_rwsem is unnecessary.
6850 +        */
6851 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6852 +
6853 +       err = -EIO;
6854 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6855 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6856 +                &dbgaufs_xigen_fop);
6857 +       if (sbinfo->si_dbgaufs_xigen)
6858 +               err = 0;
6859 +
6860 +       return err;
6861 +}
6862 +#else
6863 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6864 +{
6865 +       return 0;
6866 +}
6867 +#endif /* CONFIG_AUFS_EXPORT */
6868 +
6869 +/* ---------------------------------------------------------------------- */
6870 +
6871 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6872 +{
6873 +       /*
6874 +        * This function is a dynamic '__fin' function actually,
6875 +        * so the tiny check for si_rwsem is unnecessary.
6876 +        */
6877 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6878 +
6879 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6880 +       sbinfo->si_dbgaufs = NULL;
6881 +}
6882 +
6883 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6884 +{
6885 +       int err;
6886 +       char name[SysaufsSiNameLen];
6887 +
6888 +       /*
6889 +        * This function is a dynamic '__init' function actually,
6890 +        * so the tiny check for si_rwsem is unnecessary.
6891 +        */
6892 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6893 +
6894 +       err = -ENOENT;
6895 +       if (!dbgaufs) {
6896 +               AuErr1("/debug/aufs is uninitialized\n");
6897 +               goto out;
6898 +       }
6899 +
6900 +       err = -EIO;
6901 +       sysaufs_name(sbinfo, name);
6902 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6903 +       if (unlikely(!sbinfo->si_dbgaufs))
6904 +               goto out;
6905 +
6906 +       /* regardless plink/noplink option */
6907 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6908 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6909 +                &dbgaufs_plink_fop);
6910 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6911 +               goto out_dir;
6912 +
6913 +       /* regardless xino/noxino option */
6914 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6915 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6916 +                &dbgaufs_xib_fop);
6917 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6918 +               goto out_dir;
6919 +
6920 +       err = dbgaufs_xigen_init(sbinfo);
6921 +       if (!err)
6922 +               goto out; /* success */
6923 +
6924 +out_dir:
6925 +       dbgaufs_si_fin(sbinfo);
6926 +out:
6927 +       if (unlikely(err))
6928 +               pr_err("debugfs/aufs failed\n");
6929 +       return err;
6930 +}
6931 +
6932 +/* ---------------------------------------------------------------------- */
6933 +
6934 +void dbgaufs_fin(void)
6935 +{
6936 +       debugfs_remove(dbgaufs);
6937 +}
6938 +
6939 +int __init dbgaufs_init(void)
6940 +{
6941 +       int err;
6942 +
6943 +       err = -EIO;
6944 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6945 +       if (dbgaufs)
6946 +               err = 0;
6947 +       return err;
6948 +}
6949 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6950 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6951 +++ linux/fs/aufs/dbgaufs.h     2020-01-27 10:57:18.168871450 +0100
6952 @@ -0,0 +1,53 @@
6953 +/* SPDX-License-Identifier: GPL-2.0 */
6954 +/*
6955 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6956 + *
6957 + * This program, aufs is free software; you can redistribute it and/or modify
6958 + * it under the terms of the GNU General Public License as published by
6959 + * the Free Software Foundation; either version 2 of the License, or
6960 + * (at your option) any later version.
6961 + *
6962 + * This program is distributed in the hope that it will be useful,
6963 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6964 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6965 + * GNU General Public License for more details.
6966 + *
6967 + * You should have received a copy of the GNU General Public License
6968 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6969 + */
6970 +
6971 +/*
6972 + * debugfs interface
6973 + */
6974 +
6975 +#ifndef __DBGAUFS_H__
6976 +#define __DBGAUFS_H__
6977 +
6978 +#ifdef __KERNEL__
6979 +
6980 +struct super_block;
6981 +struct au_sbinfo;
6982 +struct au_branch;
6983 +
6984 +#ifdef CONFIG_DEBUG_FS
6985 +/* dbgaufs.c */
6986 +void dbgaufs_xino_del(struct au_branch *br);
6987 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6988 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6989 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6990 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6991 +void dbgaufs_fin(void);
6992 +int __init dbgaufs_init(void);
6993 +#else
6994 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6995 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6996 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6997 +          int topdown)
6998 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6999 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7000 +AuStubVoid(dbgaufs_fin, void)
7001 +AuStubInt0(__init dbgaufs_init, void)
7002 +#endif /* CONFIG_DEBUG_FS */
7003 +
7004 +#endif /* __KERNEL__ */
7005 +#endif /* __DBGAUFS_H__ */
7006 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7007 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
7008 +++ linux/fs/aufs/dcsub.c       2020-01-27 10:57:18.168871450 +0100
7009 @@ -0,0 +1,225 @@
7010 +// SPDX-License-Identifier: GPL-2.0
7011 +/*
7012 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7013 + *
7014 + * This program, aufs is free software; you can redistribute it and/or modify
7015 + * it under the terms of the GNU General Public License as published by
7016 + * the Free Software Foundation; either version 2 of the License, or
7017 + * (at your option) any later version.
7018 + *
7019 + * This program is distributed in the hope that it will be useful,
7020 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7021 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7022 + * GNU General Public License for more details.
7023 + *
7024 + * You should have received a copy of the GNU General Public License
7025 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7026 + */
7027 +
7028 +/*
7029 + * sub-routines for dentry cache
7030 + */
7031 +
7032 +#include "aufs.h"
7033 +
7034 +static void au_dpage_free(struct au_dpage *dpage)
7035 +{
7036 +       int i;
7037 +       struct dentry **p;
7038 +
7039 +       p = dpage->dentries;
7040 +       for (i = 0; i < dpage->ndentry; i++)
7041 +               dput(*p++);
7042 +       free_page((unsigned long)dpage->dentries);
7043 +}
7044 +
7045 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7046 +{
7047 +       int err;
7048 +       void *p;
7049 +
7050 +       err = -ENOMEM;
7051 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7052 +       if (unlikely(!dpages->dpages))
7053 +               goto out;
7054 +
7055 +       p = (void *)__get_free_page(gfp);
7056 +       if (unlikely(!p))
7057 +               goto out_dpages;
7058 +
7059 +       dpages->dpages[0].ndentry = 0;
7060 +       dpages->dpages[0].dentries = p;
7061 +       dpages->ndpage = 1;
7062 +       return 0; /* success */
7063 +
7064 +out_dpages:
7065 +       au_kfree_try_rcu(dpages->dpages);
7066 +out:
7067 +       return err;
7068 +}
7069 +
7070 +void au_dpages_free(struct au_dcsub_pages *dpages)
7071 +{
7072 +       int i;
7073 +       struct au_dpage *p;
7074 +
7075 +       p = dpages->dpages;
7076 +       for (i = 0; i < dpages->ndpage; i++)
7077 +               au_dpage_free(p++);
7078 +       au_kfree_try_rcu(dpages->dpages);
7079 +}
7080 +
7081 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7082 +                           struct dentry *dentry, gfp_t gfp)
7083 +{
7084 +       int err, sz;
7085 +       struct au_dpage *dpage;
7086 +       void *p;
7087 +
7088 +       dpage = dpages->dpages + dpages->ndpage - 1;
7089 +       sz = PAGE_SIZE / sizeof(dentry);
7090 +       if (unlikely(dpage->ndentry >= sz)) {
7091 +               AuLabel(new dpage);
7092 +               err = -ENOMEM;
7093 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7094 +               p = au_kzrealloc(dpages->dpages, sz,
7095 +                                sz + sizeof(*dpages->dpages), gfp,
7096 +                                /*may_shrink*/0);
7097 +               if (unlikely(!p))
7098 +                       goto out;
7099 +
7100 +               dpages->dpages = p;
7101 +               dpage = dpages->dpages + dpages->ndpage;
7102 +               p = (void *)__get_free_page(gfp);
7103 +               if (unlikely(!p))
7104 +                       goto out;
7105 +
7106 +               dpage->ndentry = 0;
7107 +               dpage->dentries = p;
7108 +               dpages->ndpage++;
7109 +       }
7110 +
7111 +       AuDebugOn(au_dcount(dentry) <= 0);
7112 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7113 +       return 0; /* success */
7114 +
7115 +out:
7116 +       return err;
7117 +}
7118 +
7119 +/* todo: BAD approach */
7120 +/* copied from linux/fs/dcache.c */
7121 +enum d_walk_ret {
7122 +       D_WALK_CONTINUE,
7123 +       D_WALK_QUIT,
7124 +       D_WALK_NORETRY,
7125 +       D_WALK_SKIP,
7126 +};
7127 +
7128 +extern void d_walk(struct dentry *parent, void *data,
7129 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7130 +
7131 +struct ac_dpages_arg {
7132 +       int err;
7133 +       struct au_dcsub_pages *dpages;
7134 +       struct super_block *sb;
7135 +       au_dpages_test test;
7136 +       void *arg;
7137 +};
7138 +
7139 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7140 +{
7141 +       enum d_walk_ret ret;
7142 +       struct ac_dpages_arg *arg = _arg;
7143 +
7144 +       ret = D_WALK_CONTINUE;
7145 +       if (dentry->d_sb == arg->sb
7146 +           && !IS_ROOT(dentry)
7147 +           && au_dcount(dentry) > 0
7148 +           && au_di(dentry)
7149 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7150 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7151 +               if (unlikely(arg->err))
7152 +                       ret = D_WALK_QUIT;
7153 +       }
7154 +
7155 +       return ret;
7156 +}
7157 +
7158 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7159 +                  au_dpages_test test, void *arg)
7160 +{
7161 +       struct ac_dpages_arg args = {
7162 +               .err    = 0,
7163 +               .dpages = dpages,
7164 +               .sb     = root->d_sb,
7165 +               .test   = test,
7166 +               .arg    = arg
7167 +       };
7168 +
7169 +       d_walk(root, &args, au_call_dpages_append);
7170 +
7171 +       return args.err;
7172 +}
7173 +
7174 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7175 +                      int do_include, au_dpages_test test, void *arg)
7176 +{
7177 +       int err;
7178 +
7179 +       err = 0;
7180 +       write_seqlock(&rename_lock);
7181 +       spin_lock(&dentry->d_lock);
7182 +       if (do_include
7183 +           && au_dcount(dentry) > 0
7184 +           && (!test || test(dentry, arg)))
7185 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7186 +       spin_unlock(&dentry->d_lock);
7187 +       if (unlikely(err))
7188 +               goto out;
7189 +
7190 +       /*
7191 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7192 +        * mount
7193 +        */
7194 +       while (!IS_ROOT(dentry)) {
7195 +               dentry = dentry->d_parent; /* rename_lock is locked */
7196 +               spin_lock(&dentry->d_lock);
7197 +               if (au_dcount(dentry) > 0
7198 +                   && (!test || test(dentry, arg)))
7199 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7200 +               spin_unlock(&dentry->d_lock);
7201 +               if (unlikely(err))
7202 +                       break;
7203 +       }
7204 +
7205 +out:
7206 +       write_sequnlock(&rename_lock);
7207 +       return err;
7208 +}
7209 +
7210 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7211 +{
7212 +       return au_di(dentry) && dentry->d_sb == arg;
7213 +}
7214 +
7215 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7216 +                           struct dentry *dentry, int do_include)
7217 +{
7218 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7219 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7220 +}
7221 +
7222 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7223 +{
7224 +       struct path path[2] = {
7225 +               {
7226 +                       .dentry = d1
7227 +               },
7228 +               {
7229 +                       .dentry = d2
7230 +               }
7231 +       };
7232 +
7233 +       return path_is_under(path + 0, path + 1);
7234 +}
7235 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7236 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7237 +++ linux/fs/aufs/dcsub.h       2020-01-27 10:57:18.168871450 +0100
7238 @@ -0,0 +1,137 @@
7239 +/* SPDX-License-Identifier: GPL-2.0 */
7240 +/*
7241 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7242 + *
7243 + * This program, aufs is free software; you can redistribute it and/or modify
7244 + * it under the terms of the GNU General Public License as published by
7245 + * the Free Software Foundation; either version 2 of the License, or
7246 + * (at your option) any later version.
7247 + *
7248 + * This program is distributed in the hope that it will be useful,
7249 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7250 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7251 + * GNU General Public License for more details.
7252 + *
7253 + * You should have received a copy of the GNU General Public License
7254 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7255 + */
7256 +
7257 +/*
7258 + * sub-routines for dentry cache
7259 + */
7260 +
7261 +#ifndef __AUFS_DCSUB_H__
7262 +#define __AUFS_DCSUB_H__
7263 +
7264 +#ifdef __KERNEL__
7265 +
7266 +#include <linux/dcache.h>
7267 +#include <linux/fs.h>
7268 +
7269 +struct au_dpage {
7270 +       int ndentry;
7271 +       struct dentry **dentries;
7272 +};
7273 +
7274 +struct au_dcsub_pages {
7275 +       int ndpage;
7276 +       struct au_dpage *dpages;
7277 +};
7278 +
7279 +/* ---------------------------------------------------------------------- */
7280 +
7281 +/* dcsub.c */
7282 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7283 +void au_dpages_free(struct au_dcsub_pages *dpages);
7284 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7285 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7286 +                  au_dpages_test test, void *arg);
7287 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7288 +                      int do_include, au_dpages_test test, void *arg);
7289 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7290 +                           struct dentry *dentry, int do_include);
7291 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7292 +
7293 +/* ---------------------------------------------------------------------- */
7294 +
7295 +/*
7296 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7297 + * include/linux/dcache.h. Try them (in the future).
7298 + */
7299 +
7300 +static inline int au_d_hashed_positive(struct dentry *d)
7301 +{
7302 +       int err;
7303 +       struct inode *inode = d_inode(d);
7304 +
7305 +       err = 0;
7306 +       if (unlikely(d_unhashed(d)
7307 +                    || d_is_negative(d)
7308 +                    || !inode->i_nlink))
7309 +               err = -ENOENT;
7310 +       return err;
7311 +}
7312 +
7313 +static inline int au_d_linkable(struct dentry *d)
7314 +{
7315 +       int err;
7316 +       struct inode *inode = d_inode(d);
7317 +
7318 +       err = au_d_hashed_positive(d);
7319 +       if (err
7320 +           && d_is_positive(d)
7321 +           && (inode->i_state & I_LINKABLE))
7322 +               err = 0;
7323 +       return err;
7324 +}
7325 +
7326 +static inline int au_d_alive(struct dentry *d)
7327 +{
7328 +       int err;
7329 +       struct inode *inode;
7330 +
7331 +       err = 0;
7332 +       if (!IS_ROOT(d))
7333 +               err = au_d_hashed_positive(d);
7334 +       else {
7335 +               inode = d_inode(d);
7336 +               if (unlikely(d_unlinked(d)
7337 +                            || d_is_negative(d)
7338 +                            || !inode->i_nlink))
7339 +                       err = -ENOENT;
7340 +       }
7341 +       return err;
7342 +}
7343 +
7344 +static inline int au_alive_dir(struct dentry *d)
7345 +{
7346 +       int err;
7347 +
7348 +       err = au_d_alive(d);
7349 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7350 +               err = -ENOENT;
7351 +       return err;
7352 +}
7353 +
7354 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7355 +{
7356 +       return a->len == b->len
7357 +               && !memcmp(a->name, b->name, a->len);
7358 +}
7359 +
7360 +/*
7361 + * by the commit
7362 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7363 + *                     taking d_lock
7364 + * the type of d_lockref.count became int, but the inlined function d_count()
7365 + * still returns unsigned int.
7366 + * I don't know why. Maybe it is for every d_count() users?
7367 + * Anyway au_dcount() lives on.
7368 + */
7369 +static inline int au_dcount(struct dentry *d)
7370 +{
7371 +       return (int)d_count(d);
7372 +}
7373 +
7374 +#endif /* __KERNEL__ */
7375 +#endif /* __AUFS_DCSUB_H__ */
7376 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7377 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7378 +++ linux/fs/aufs/debug.c       2020-01-27 10:57:18.168871450 +0100
7379 @@ -0,0 +1,441 @@
7380 +// SPDX-License-Identifier: GPL-2.0
7381 +/*
7382 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7383 + *
7384 + * This program, aufs is free software; you can redistribute it and/or modify
7385 + * it under the terms of the GNU General Public License as published by
7386 + * the Free Software Foundation; either version 2 of the License, or
7387 + * (at your option) any later version.
7388 + *
7389 + * This program is distributed in the hope that it will be useful,
7390 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7391 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7392 + * GNU General Public License for more details.
7393 + *
7394 + * You should have received a copy of the GNU General Public License
7395 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7396 + */
7397 +
7398 +/*
7399 + * debug print functions
7400 + */
7401 +
7402 +#include <linux/iversion.h>
7403 +#include "aufs.h"
7404 +
7405 +/* Returns 0, or -errno.  arg is in kp->arg. */
7406 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7407 +{
7408 +       int err, n;
7409 +
7410 +       err = kstrtoint(val, 0, &n);
7411 +       if (!err) {
7412 +               if (n > 0)
7413 +                       au_debug_on();
7414 +               else
7415 +                       au_debug_off();
7416 +       }
7417 +       return err;
7418 +}
7419 +
7420 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7421 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7422 +{
7423 +       atomic_t *a;
7424 +
7425 +       a = kp->arg;
7426 +       return sprintf(buffer, "%d", atomic_read(a));
7427 +}
7428 +
7429 +static struct kernel_param_ops param_ops_atomic_t = {
7430 +       .set = param_atomic_t_set,
7431 +       .get = param_atomic_t_get
7432 +       /* void (*free)(void *arg) */
7433 +};
7434 +
7435 +atomic_t aufs_debug = ATOMIC_INIT(0);
7436 +MODULE_PARM_DESC(debug, "debug print");
7437 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7438 +
7439 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7440 +char *au_plevel = KERN_DEBUG;
7441 +#define dpri(fmt, ...) do {                                    \
7442 +       if ((au_plevel                                          \
7443 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7444 +           || au_debug_test())                                 \
7445 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7446 +} while (0)
7447 +
7448 +/* ---------------------------------------------------------------------- */
7449 +
7450 +void au_dpri_whlist(struct au_nhash *whlist)
7451 +{
7452 +       unsigned long ul, n;
7453 +       struct hlist_head *head;
7454 +       struct au_vdir_wh *pos;
7455 +
7456 +       n = whlist->nh_num;
7457 +       head = whlist->nh_head;
7458 +       for (ul = 0; ul < n; ul++) {
7459 +               hlist_for_each_entry(pos, head, wh_hash)
7460 +                       dpri("b%d, %.*s, %d\n",
7461 +                            pos->wh_bindex,
7462 +                            pos->wh_str.len, pos->wh_str.name,
7463 +                            pos->wh_str.len);
7464 +               head++;
7465 +       }
7466 +}
7467 +
7468 +void au_dpri_vdir(struct au_vdir *vdir)
7469 +{
7470 +       unsigned long ul;
7471 +       union au_vdir_deblk_p p;
7472 +       unsigned char *o;
7473 +
7474 +       if (!vdir || IS_ERR(vdir)) {
7475 +               dpri("err %ld\n", PTR_ERR(vdir));
7476 +               return;
7477 +       }
7478 +
7479 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7480 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7481 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7482 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7483 +               p.deblk = vdir->vd_deblk[ul];
7484 +               o = p.deblk;
7485 +               dpri("[%lu]: %p\n", ul, o);
7486 +       }
7487 +}
7488 +
7489 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7490 +                       struct dentry *wh)
7491 +{
7492 +       char *n = NULL;
7493 +       int l = 0;
7494 +
7495 +       if (!inode || IS_ERR(inode)) {
7496 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7497 +               return -1;
7498 +       }
7499 +
7500 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7501 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7502 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7503 +       if (wh) {
7504 +               n = (void *)wh->d_name.name;
7505 +               l = wh->d_name.len;
7506 +       }
7507 +
7508 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7509 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7510 +            bindex, inode,
7511 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7512 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7513 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7514 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7515 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7516 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7517 +            inode->i_generation,
7518 +            l ? ", wh " : "", l, n);
7519 +       return 0;
7520 +}
7521 +
7522 +void au_dpri_inode(struct inode *inode)
7523 +{
7524 +       struct au_iinfo *iinfo;
7525 +       struct au_hinode *hi;
7526 +       aufs_bindex_t bindex;
7527 +       int err, hn;
7528 +
7529 +       err = do_pri_inode(-1, inode, -1, NULL);
7530 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7531 +               return;
7532 +
7533 +       iinfo = au_ii(inode);
7534 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7535 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7536 +       if (iinfo->ii_btop < 0)
7537 +               return;
7538 +       hn = 0;
7539 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7540 +               hi = au_hinode(iinfo, bindex);
7541 +               hn = !!au_hn(hi);
7542 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7543 +       }
7544 +}
7545 +
7546 +void au_dpri_dalias(struct inode *inode)
7547 +{
7548 +       struct dentry *d;
7549 +
7550 +       spin_lock(&inode->i_lock);
7551 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7552 +               au_dpri_dentry(d);
7553 +       spin_unlock(&inode->i_lock);
7554 +}
7555 +
7556 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7557 +{
7558 +       struct dentry *wh = NULL;
7559 +       int hn;
7560 +       struct inode *inode;
7561 +       struct au_iinfo *iinfo;
7562 +       struct au_hinode *hi;
7563 +
7564 +       if (!dentry || IS_ERR(dentry)) {
7565 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7566 +               return -1;
7567 +       }
7568 +       /* do not call dget_parent() here */
7569 +       /* note: access d_xxx without d_lock */
7570 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7571 +            bindex, dentry, dentry,
7572 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7573 +            au_dcount(dentry), dentry->d_flags,
7574 +            d_unhashed(dentry) ? "un" : "");
7575 +       hn = -1;
7576 +       inode = NULL;
7577 +       if (d_is_positive(dentry))
7578 +               inode = d_inode(dentry);
7579 +       if (inode
7580 +           && au_test_aufs(dentry->d_sb)
7581 +           && bindex >= 0
7582 +           && !au_is_bad_inode(inode)) {
7583 +               iinfo = au_ii(inode);
7584 +               hi = au_hinode(iinfo, bindex);
7585 +               hn = !!au_hn(hi);
7586 +               wh = hi->hi_whdentry;
7587 +       }
7588 +       do_pri_inode(bindex, inode, hn, wh);
7589 +       return 0;
7590 +}
7591 +
7592 +void au_dpri_dentry(struct dentry *dentry)
7593 +{
7594 +       struct au_dinfo *dinfo;
7595 +       aufs_bindex_t bindex;
7596 +       int err;
7597 +
7598 +       err = do_pri_dentry(-1, dentry);
7599 +       if (err || !au_test_aufs(dentry->d_sb))
7600 +               return;
7601 +
7602 +       dinfo = au_di(dentry);
7603 +       if (!dinfo)
7604 +               return;
7605 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7606 +            dinfo->di_btop, dinfo->di_bbot,
7607 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7608 +            dinfo->di_tmpfile);
7609 +       if (dinfo->di_btop < 0)
7610 +               return;
7611 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7612 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7613 +}
7614 +
7615 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7616 +{
7617 +       char a[32];
7618 +
7619 +       if (!file || IS_ERR(file)) {
7620 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7621 +               return -1;
7622 +       }
7623 +       a[0] = 0;
7624 +       if (bindex < 0
7625 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7626 +           && au_test_aufs(file->f_path.dentry->d_sb)
7627 +           && au_fi(file))
7628 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7629 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7630 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7631 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7632 +            file->f_version, file->f_pos, a);
7633 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7634 +               do_pri_dentry(bindex, file->f_path.dentry);
7635 +       return 0;
7636 +}
7637 +
7638 +void au_dpri_file(struct file *file)
7639 +{
7640 +       struct au_finfo *finfo;
7641 +       struct au_fidir *fidir;
7642 +       struct au_hfile *hfile;
7643 +       aufs_bindex_t bindex;
7644 +       int err;
7645 +
7646 +       err = do_pri_file(-1, file);
7647 +       if (err
7648 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7649 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7650 +               return;
7651 +
7652 +       finfo = au_fi(file);
7653 +       if (!finfo)
7654 +               return;
7655 +       if (finfo->fi_btop < 0)
7656 +               return;
7657 +       fidir = finfo->fi_hdir;
7658 +       if (!fidir)
7659 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7660 +       else
7661 +               for (bindex = finfo->fi_btop;
7662 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7663 +                    bindex++) {
7664 +                       hfile = fidir->fd_hfile + bindex;
7665 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7666 +               }
7667 +}
7668 +
7669 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7670 +{
7671 +       struct vfsmount *mnt;
7672 +       struct super_block *sb;
7673 +
7674 +       if (!br || IS_ERR(br))
7675 +               goto out;
7676 +       mnt = au_br_mnt(br);
7677 +       if (!mnt || IS_ERR(mnt))
7678 +               goto out;
7679 +       sb = mnt->mnt_sb;
7680 +       if (!sb || IS_ERR(sb))
7681 +               goto out;
7682 +
7683 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7684 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7685 +            "xino %d\n",
7686 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7687 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7688 +            sb->s_flags, sb->s_count,
7689 +            atomic_read(&sb->s_active),
7690 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7691 +       return 0;
7692 +
7693 +out:
7694 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7695 +       return -1;
7696 +}
7697 +
7698 +void au_dpri_sb(struct super_block *sb)
7699 +{
7700 +       struct au_sbinfo *sbinfo;
7701 +       aufs_bindex_t bindex;
7702 +       int err;
7703 +       /* to reduce stack size */
7704 +       struct {
7705 +               struct vfsmount mnt;
7706 +               struct au_branch fake;
7707 +       } *a;
7708 +
7709 +       /* this function can be called from magic sysrq */
7710 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7711 +       if (unlikely(!a)) {
7712 +               dpri("no memory\n");
7713 +               return;
7714 +       }
7715 +
7716 +       a->mnt.mnt_sb = sb;
7717 +       a->fake.br_path.mnt = &a->mnt;
7718 +       err = do_pri_br(-1, &a->fake);
7719 +       au_kfree_rcu(a);
7720 +       dpri("dev 0x%x\n", sb->s_dev);
7721 +       if (err || !au_test_aufs(sb))
7722 +               return;
7723 +
7724 +       sbinfo = au_sbi(sb);
7725 +       if (!sbinfo)
7726 +               return;
7727 +       dpri("nw %d, gen %u, kobj %d\n",
7728 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7729 +            kref_read(&sbinfo->si_kobj.kref));
7730 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7731 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7732 +}
7733 +
7734 +/* ---------------------------------------------------------------------- */
7735 +
7736 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7737 +{
7738 +       struct inode *h_inode, *inode = d_inode(dentry);
7739 +       struct dentry *h_dentry;
7740 +       aufs_bindex_t bindex, bbot, bi;
7741 +
7742 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7743 +               return;
7744 +
7745 +       bbot = au_dbbot(dentry);
7746 +       bi = au_ibbot(inode);
7747 +       if (bi < bbot)
7748 +               bbot = bi;
7749 +       bindex = au_dbtop(dentry);
7750 +       bi = au_ibtop(inode);
7751 +       if (bi > bindex)
7752 +               bindex = bi;
7753 +
7754 +       for (; bindex <= bbot; bindex++) {
7755 +               h_dentry = au_h_dptr(dentry, bindex);
7756 +               if (!h_dentry)
7757 +                       continue;
7758 +               h_inode = au_h_iptr(inode, bindex);
7759 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7760 +                       au_debug_on();
7761 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7762 +                       AuDbgDentry(dentry);
7763 +                       AuDbgInode(inode);
7764 +                       au_debug_off();
7765 +                       BUG();
7766 +               }
7767 +       }
7768 +}
7769 +
7770 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7771 +{
7772 +       int err, i, j;
7773 +       struct au_dcsub_pages dpages;
7774 +       struct au_dpage *dpage;
7775 +       struct dentry **dentries;
7776 +
7777 +       err = au_dpages_init(&dpages, GFP_NOFS);
7778 +       AuDebugOn(err);
7779 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7780 +       AuDebugOn(err);
7781 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7782 +               dpage = dpages.dpages + i;
7783 +               dentries = dpage->dentries;
7784 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7785 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7786 +       }
7787 +       au_dpages_free(&dpages);
7788 +}
7789 +
7790 +void au_dbg_verify_kthread(void)
7791 +{
7792 +       if (au_wkq_test()) {
7793 +               au_dbg_blocked();
7794 +               /*
7795 +                * It may be recursive, but udba=notify between two aufs mounts,
7796 +                * where a single ro branch is shared, is not a problem.
7797 +                */
7798 +               /* WARN_ON(1); */
7799 +       }
7800 +}
7801 +
7802 +/* ---------------------------------------------------------------------- */
7803 +
7804 +int __init au_debug_init(void)
7805 +{
7806 +       aufs_bindex_t bindex;
7807 +       struct au_vdir_destr destr;
7808 +
7809 +       bindex = -1;
7810 +       AuDebugOn(bindex >= 0);
7811 +
7812 +       destr.len = -1;
7813 +       AuDebugOn(destr.len < NAME_MAX);
7814 +
7815 +#ifdef CONFIG_4KSTACKS
7816 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7817 +#endif
7818 +
7819 +       return 0;
7820 +}
7821 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7822 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7823 +++ linux/fs/aufs/debug.h       2020-01-27 10:57:18.168871450 +0100
7824 @@ -0,0 +1,226 @@
7825 +/* SPDX-License-Identifier: GPL-2.0 */
7826 +/*
7827 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7828 + *
7829 + * This program, aufs is free software; you can redistribute it and/or modify
7830 + * it under the terms of the GNU General Public License as published by
7831 + * the Free Software Foundation; either version 2 of the License, or
7832 + * (at your option) any later version.
7833 + *
7834 + * This program is distributed in the hope that it will be useful,
7835 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7836 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7837 + * GNU General Public License for more details.
7838 + *
7839 + * You should have received a copy of the GNU General Public License
7840 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7841 + */
7842 +
7843 +/*
7844 + * debug print functions
7845 + */
7846 +
7847 +#ifndef __AUFS_DEBUG_H__
7848 +#define __AUFS_DEBUG_H__
7849 +
7850 +#ifdef __KERNEL__
7851 +
7852 +#include <linux/atomic.h>
7853 +#include <linux/module.h>
7854 +#include <linux/kallsyms.h>
7855 +#include <linux/sysrq.h>
7856 +
7857 +#ifdef CONFIG_AUFS_DEBUG
7858 +#define AuDebugOn(a)           BUG_ON(a)
7859 +
7860 +/* module parameter */
7861 +extern atomic_t aufs_debug;
7862 +static inline void au_debug_on(void)
7863 +{
7864 +       atomic_inc(&aufs_debug);
7865 +}
7866 +static inline void au_debug_off(void)
7867 +{
7868 +       atomic_dec_if_positive(&aufs_debug);
7869 +}
7870 +
7871 +static inline int au_debug_test(void)
7872 +{
7873 +       return atomic_read(&aufs_debug) > 0;
7874 +}
7875 +#else
7876 +#define AuDebugOn(a)           do {} while (0)
7877 +AuStubVoid(au_debug_on, void)
7878 +AuStubVoid(au_debug_off, void)
7879 +AuStubInt0(au_debug_test, void)
7880 +#endif /* CONFIG_AUFS_DEBUG */
7881 +
7882 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7883 +
7884 +/* ---------------------------------------------------------------------- */
7885 +
7886 +/* debug print */
7887 +
7888 +#define AuDbg(fmt, ...) do { \
7889 +       if (au_debug_test()) \
7890 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7891 +} while (0)
7892 +#define AuLabel(l)             AuDbg(#l "\n")
7893 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7894 +#define AuWarn1(fmt, ...) do { \
7895 +       static unsigned char _c; \
7896 +       if (!_c++) \
7897 +               pr_warn(fmt, ##__VA_ARGS__); \
7898 +} while (0)
7899 +
7900 +#define AuErr1(fmt, ...) do { \
7901 +       static unsigned char _c; \
7902 +       if (!_c++) \
7903 +               pr_err(fmt, ##__VA_ARGS__); \
7904 +} while (0)
7905 +
7906 +#define AuIOErr1(fmt, ...) do { \
7907 +       static unsigned char _c; \
7908 +       if (!_c++) \
7909 +               AuIOErr(fmt, ##__VA_ARGS__); \
7910 +} while (0)
7911 +
7912 +#define AuUnsupportMsg "This operation is not supported." \
7913 +                       " Please report this application to aufs-users ML."
7914 +#define AuUnsupport(fmt, ...) do { \
7915 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7916 +       dump_stack(); \
7917 +} while (0)
7918 +
7919 +#define AuTraceErr(e) do { \
7920 +       if (unlikely((e) < 0)) \
7921 +               AuDbg("err %d\n", (int)(e)); \
7922 +} while (0)
7923 +
7924 +#define AuTraceErrPtr(p) do { \
7925 +       if (IS_ERR(p)) \
7926 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7927 +} while (0)
7928 +
7929 +/* dirty macros for debug print, use with "%.*s" and caution */
7930 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7931 +
7932 +/* ---------------------------------------------------------------------- */
7933 +
7934 +struct dentry;
7935 +#ifdef CONFIG_AUFS_DEBUG
7936 +extern struct mutex au_dbg_mtx;
7937 +extern char *au_plevel;
7938 +struct au_nhash;
7939 +void au_dpri_whlist(struct au_nhash *whlist);
7940 +struct au_vdir;
7941 +void au_dpri_vdir(struct au_vdir *vdir);
7942 +struct inode;
7943 +void au_dpri_inode(struct inode *inode);
7944 +void au_dpri_dalias(struct inode *inode);
7945 +void au_dpri_dentry(struct dentry *dentry);
7946 +struct file;
7947 +void au_dpri_file(struct file *filp);
7948 +struct super_block;
7949 +void au_dpri_sb(struct super_block *sb);
7950 +
7951 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7952 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7953 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7954 +void au_dbg_verify_kthread(void);
7955 +
7956 +int __init au_debug_init(void);
7957 +
7958 +#define AuDbgWhlist(w) do { \
7959 +       mutex_lock(&au_dbg_mtx); \
7960 +       AuDbg(#w "\n"); \
7961 +       au_dpri_whlist(w); \
7962 +       mutex_unlock(&au_dbg_mtx); \
7963 +} while (0)
7964 +
7965 +#define AuDbgVdir(v) do { \
7966 +       mutex_lock(&au_dbg_mtx); \
7967 +       AuDbg(#v "\n"); \
7968 +       au_dpri_vdir(v); \
7969 +       mutex_unlock(&au_dbg_mtx); \
7970 +} while (0)
7971 +
7972 +#define AuDbgInode(i) do { \
7973 +       mutex_lock(&au_dbg_mtx); \
7974 +       AuDbg(#i "\n"); \
7975 +       au_dpri_inode(i); \
7976 +       mutex_unlock(&au_dbg_mtx); \
7977 +} while (0)
7978 +
7979 +#define AuDbgDAlias(i) do { \
7980 +       mutex_lock(&au_dbg_mtx); \
7981 +       AuDbg(#i "\n"); \
7982 +       au_dpri_dalias(i); \
7983 +       mutex_unlock(&au_dbg_mtx); \
7984 +} while (0)
7985 +
7986 +#define AuDbgDentry(d) do { \
7987 +       mutex_lock(&au_dbg_mtx); \
7988 +       AuDbg(#d "\n"); \
7989 +       au_dpri_dentry(d); \
7990 +       mutex_unlock(&au_dbg_mtx); \
7991 +} while (0)
7992 +
7993 +#define AuDbgFile(f) do { \
7994 +       mutex_lock(&au_dbg_mtx); \
7995 +       AuDbg(#f "\n"); \
7996 +       au_dpri_file(f); \
7997 +       mutex_unlock(&au_dbg_mtx); \
7998 +} while (0)
7999 +
8000 +#define AuDbgSb(sb) do { \
8001 +       mutex_lock(&au_dbg_mtx); \
8002 +       AuDbg(#sb "\n"); \
8003 +       au_dpri_sb(sb); \
8004 +       mutex_unlock(&au_dbg_mtx); \
8005 +} while (0)
8006 +
8007 +#define AuDbgSym(addr) do {                            \
8008 +       char sym[KSYM_SYMBOL_LEN];                      \
8009 +       sprint_symbol(sym, (unsigned long)addr);        \
8010 +       AuDbg("%s\n", sym);                             \
8011 +} while (0)
8012 +#else
8013 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
8014 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8015 +AuStubVoid(au_dbg_verify_kthread, void)
8016 +AuStubInt0(__init au_debug_init, void)
8017 +
8018 +#define AuDbgWhlist(w)         do {} while (0)
8019 +#define AuDbgVdir(v)           do {} while (0)
8020 +#define AuDbgInode(i)          do {} while (0)
8021 +#define AuDbgDAlias(i)         do {} while (0)
8022 +#define AuDbgDentry(d)         do {} while (0)
8023 +#define AuDbgFile(f)           do {} while (0)
8024 +#define AuDbgSb(sb)            do {} while (0)
8025 +#define AuDbgSym(addr)         do {} while (0)
8026 +#endif /* CONFIG_AUFS_DEBUG */
8027 +
8028 +/* ---------------------------------------------------------------------- */
8029 +
8030 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8031 +int __init au_sysrq_init(void);
8032 +void au_sysrq_fin(void);
8033 +
8034 +#ifdef CONFIG_HW_CONSOLE
8035 +#define au_dbg_blocked() do { \
8036 +       WARN_ON(1); \
8037 +       handle_sysrq('w'); \
8038 +} while (0)
8039 +#else
8040 +AuStubVoid(au_dbg_blocked, void)
8041 +#endif
8042 +
8043 +#else
8044 +AuStubInt0(__init au_sysrq_init, void)
8045 +AuStubVoid(au_sysrq_fin, void)
8046 +AuStubVoid(au_dbg_blocked, void)
8047 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8048 +
8049 +#endif /* __KERNEL__ */
8050 +#endif /* __AUFS_DEBUG_H__ */
8051 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8052 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
8053 +++ linux/fs/aufs/dentry.c      2020-01-27 10:57:18.168871450 +0100
8054 @@ -0,0 +1,1154 @@
8055 +// SPDX-License-Identifier: GPL-2.0
8056 +/*
8057 + * Copyright (C) 2005-2020 Junjiro R. Okajima
8058 + *
8059 + * This program, aufs is free software; you can redistribute it and/or modify
8060 + * it under the terms of the GNU General Public License as published by
8061 + * the Free Software Foundation; either version 2 of the License, or
8062 + * (at your option) any later version.
8063 + *
8064 + * This program is distributed in the hope that it will be useful,
8065 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8066 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8067 + * GNU General Public License for more details.
8068 + *
8069 + * You should have received a copy of the GNU General Public License
8070 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8071 + */
8072 +
8073 +/*
8074 + * lookup and dentry operations
8075 + */
8076 +
8077 +#include <linux/iversion.h>
8078 +#include <linux/namei.h>
8079 +#include "aufs.h"
8080 +
8081 +/*
8082 + * returns positive/negative dentry, NULL or an error.
8083 + * NULL means whiteout-ed or not-found.
8084 + */
8085 +static struct dentry*
8086 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8087 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8088 +{
8089 +       struct dentry *h_dentry;
8090 +       struct inode *h_inode;
8091 +       struct au_branch *br;
8092 +       int wh_found, opq;
8093 +       unsigned char wh_able;
8094 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8095 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8096 +                                                         IGNORE_PERM);
8097 +
8098 +       wh_found = 0;
8099 +       br = au_sbr(dentry->d_sb, bindex);
8100 +       wh_able = !!au_br_whable(br->br_perm);
8101 +       if (wh_able)
8102 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8103 +       h_dentry = ERR_PTR(wh_found);
8104 +       if (!wh_found)
8105 +               goto real_lookup;
8106 +       if (unlikely(wh_found < 0))
8107 +               goto out;
8108 +
8109 +       /* We found a whiteout */
8110 +       /* au_set_dbbot(dentry, bindex); */
8111 +       au_set_dbwh(dentry, bindex);
8112 +       if (!allow_neg)
8113 +               return NULL; /* success */
8114 +
8115 +real_lookup:
8116 +       if (!ignore_perm)
8117 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8118 +       else
8119 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8120 +       if (IS_ERR(h_dentry)) {
8121 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8122 +                   && !allow_neg)
8123 +                       h_dentry = NULL;
8124 +               goto out;
8125 +       }
8126 +
8127 +       h_inode = d_inode(h_dentry);
8128 +       if (d_is_negative(h_dentry)) {
8129 +               if (!allow_neg)
8130 +                       goto out_neg;
8131 +       } else if (wh_found
8132 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8133 +               goto out_neg;
8134 +       else if (au_ftest_lkup(args->flags, DIRREN)
8135 +                /* && h_inode */
8136 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8137 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8138 +                     (unsigned long long)h_inode->i_ino);
8139 +               goto out_neg;
8140 +       }
8141 +
8142 +       if (au_dbbot(dentry) <= bindex)
8143 +               au_set_dbbot(dentry, bindex);
8144 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8145 +               au_set_dbtop(dentry, bindex);
8146 +       au_set_h_dptr(dentry, bindex, h_dentry);
8147 +
8148 +       if (!d_is_dir(h_dentry)
8149 +           || !wh_able
8150 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8151 +               goto out; /* success */
8152 +
8153 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8154 +       opq = au_diropq_test(h_dentry);
8155 +       inode_unlock_shared(h_inode);
8156 +       if (opq > 0)
8157 +               au_set_dbdiropq(dentry, bindex);
8158 +       else if (unlikely(opq < 0)) {
8159 +               au_set_h_dptr(dentry, bindex, NULL);
8160 +               h_dentry = ERR_PTR(opq);
8161 +       }
8162 +       goto out;
8163 +
8164 +out_neg:
8165 +       dput(h_dentry);
8166 +       h_dentry = NULL;
8167 +out:
8168 +       return h_dentry;
8169 +}
8170 +
8171 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8172 +{
8173 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8174 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8175 +               return -EPERM;
8176 +       return 0;
8177 +}
8178 +
8179 +/*
8180 + * returns the number of lower positive dentries,
8181 + * otherwise an error.
8182 + * can be called at unlinking with @type is zero.
8183 + */
8184 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8185 +                  unsigned int flags)
8186 +{
8187 +       int npositive, err;
8188 +       aufs_bindex_t bindex, btail, bdiropq;
8189 +       unsigned char isdir, dirperm1, dirren;
8190 +       struct au_do_lookup_args args = {
8191 +               .flags          = flags,
8192 +               .name           = &dentry->d_name
8193 +       };
8194 +       struct dentry *parent;
8195 +       struct super_block *sb;
8196 +
8197 +       sb = dentry->d_sb;
8198 +       err = au_test_shwh(sb, args.name);
8199 +       if (unlikely(err))
8200 +               goto out;
8201 +
8202 +       err = au_wh_name_alloc(&args.whname, args.name);
8203 +       if (unlikely(err))
8204 +               goto out;
8205 +
8206 +       isdir = !!d_is_dir(dentry);
8207 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8208 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8209 +       if (dirren)
8210 +               au_fset_lkup(args.flags, DIRREN);
8211 +
8212 +       npositive = 0;
8213 +       parent = dget_parent(dentry);
8214 +       btail = au_dbtaildir(parent);
8215 +       for (bindex = btop; bindex <= btail; bindex++) {
8216 +               struct dentry *h_parent, *h_dentry;
8217 +               struct inode *h_inode, *h_dir;
8218 +               struct au_branch *br;
8219 +
8220 +               h_dentry = au_h_dptr(dentry, bindex);
8221 +               if (h_dentry) {
8222 +                       if (d_is_positive(h_dentry))
8223 +                               npositive++;
8224 +                       break;
8225 +               }
8226 +               h_parent = au_h_dptr(parent, bindex);
8227 +               if (!h_parent || !d_is_dir(h_parent))
8228 +                       continue;
8229 +
8230 +               if (dirren) {
8231 +                       /* if the inum matches, then use the prepared name */
8232 +                       err = au_dr_lkup_name(&args, bindex);
8233 +                       if (unlikely(err))
8234 +                               goto out_parent;
8235 +               }
8236 +
8237 +               h_dir = d_inode(h_parent);
8238 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8239 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8240 +               inode_unlock_shared(h_dir);
8241 +               err = PTR_ERR(h_dentry);
8242 +               if (IS_ERR(h_dentry))
8243 +                       goto out_parent;
8244 +               if (h_dentry)
8245 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8246 +               if (dirperm1)
8247 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8248 +
8249 +               if (au_dbwh(dentry) == bindex)
8250 +                       break;
8251 +               if (!h_dentry)
8252 +                       continue;
8253 +               if (d_is_negative(h_dentry))
8254 +                       continue;
8255 +               h_inode = d_inode(h_dentry);
8256 +               npositive++;
8257 +               if (!args.type)
8258 +                       args.type = h_inode->i_mode & S_IFMT;
8259 +               if (args.type != S_IFDIR)
8260 +                       break;
8261 +               else if (isdir) {
8262 +                       /* the type of lower may be different */
8263 +                       bdiropq = au_dbdiropq(dentry);
8264 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8265 +                               break;
8266 +               }
8267 +               br = au_sbr(sb, bindex);
8268 +               if (dirren
8269 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8270 +                                          /*add_ent*/NULL)) {
8271 +                       /* prepare next name to lookup */
8272 +                       err = au_dr_lkup(&args, dentry, bindex);
8273 +                       if (unlikely(err))
8274 +                               goto out_parent;
8275 +               }
8276 +       }
8277 +
8278 +       if (npositive) {
8279 +               AuLabel(positive);
8280 +               au_update_dbtop(dentry);
8281 +       }
8282 +       err = npositive;
8283 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8284 +                    && au_dbtop(dentry) < 0)) {
8285 +               err = -EIO;
8286 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8287 +                       dentry, err);
8288 +       }
8289 +
8290 +out_parent:
8291 +       dput(parent);
8292 +       au_kfree_try_rcu(args.whname.name);
8293 +       if (dirren)
8294 +               au_dr_lkup_fin(&args);
8295 +out:
8296 +       return err;
8297 +}
8298 +
8299 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8300 +{
8301 +       struct dentry *dentry;
8302 +       int wkq_err;
8303 +
8304 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8305 +               dentry = vfsub_lkup_one(name, parent);
8306 +       else {
8307 +               struct vfsub_lkup_one_args args = {
8308 +                       .errp   = &dentry,
8309 +                       .name   = name,
8310 +                       .parent = parent
8311 +               };
8312 +
8313 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8314 +               if (unlikely(wkq_err))
8315 +                       dentry = ERR_PTR(wkq_err);
8316 +       }
8317 +
8318 +       return dentry;
8319 +}
8320 +
8321 +/*
8322 + * lookup @dentry on @bindex which should be negative.
8323 + */
8324 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8325 +{
8326 +       int err;
8327 +       struct dentry *parent, *h_parent, *h_dentry;
8328 +       struct au_branch *br;
8329 +
8330 +       parent = dget_parent(dentry);
8331 +       h_parent = au_h_dptr(parent, bindex);
8332 +       br = au_sbr(dentry->d_sb, bindex);
8333 +       if (wh)
8334 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8335 +       else
8336 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8337 +       err = PTR_ERR(h_dentry);
8338 +       if (IS_ERR(h_dentry))
8339 +               goto out;
8340 +       if (unlikely(d_is_positive(h_dentry))) {
8341 +               err = -EIO;
8342 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8343 +               dput(h_dentry);
8344 +               goto out;
8345 +       }
8346 +
8347 +       err = 0;
8348 +       if (bindex < au_dbtop(dentry))
8349 +               au_set_dbtop(dentry, bindex);
8350 +       if (au_dbbot(dentry) < bindex)
8351 +               au_set_dbbot(dentry, bindex);
8352 +       au_set_h_dptr(dentry, bindex, h_dentry);
8353 +
8354 +out:
8355 +       dput(parent);
8356 +       return err;
8357 +}
8358 +
8359 +/* ---------------------------------------------------------------------- */
8360 +
8361 +/* subset of struct inode */
8362 +struct au_iattr {
8363 +       unsigned long           i_ino;
8364 +       /* unsigned int         i_nlink; */
8365 +       kuid_t                  i_uid;
8366 +       kgid_t                  i_gid;
8367 +       u64                     i_version;
8368 +/*
8369 +       loff_t                  i_size;
8370 +       blkcnt_t                i_blocks;
8371 +*/
8372 +       umode_t                 i_mode;
8373 +};
8374 +
8375 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8376 +{
8377 +       ia->i_ino = h_inode->i_ino;
8378 +       /* ia->i_nlink = h_inode->i_nlink; */
8379 +       ia->i_uid = h_inode->i_uid;
8380 +       ia->i_gid = h_inode->i_gid;
8381 +       ia->i_version = inode_query_iversion(h_inode);
8382 +/*
8383 +       ia->i_size = h_inode->i_size;
8384 +       ia->i_blocks = h_inode->i_blocks;
8385 +*/
8386 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8387 +}
8388 +
8389 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8390 +{
8391 +       return ia->i_ino != h_inode->i_ino
8392 +               /* || ia->i_nlink != h_inode->i_nlink */
8393 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8394 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8395 +               || !inode_eq_iversion(h_inode, ia->i_version)
8396 +/*
8397 +               || ia->i_size != h_inode->i_size
8398 +               || ia->i_blocks != h_inode->i_blocks
8399 +*/
8400 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8401 +}
8402 +
8403 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8404 +                             struct au_branch *br)
8405 +{
8406 +       int err;
8407 +       struct au_iattr ia;
8408 +       struct inode *h_inode;
8409 +       struct dentry *h_d;
8410 +       struct super_block *h_sb;
8411 +
8412 +       err = 0;
8413 +       memset(&ia, -1, sizeof(ia));
8414 +       h_sb = h_dentry->d_sb;
8415 +       h_inode = NULL;
8416 +       if (d_is_positive(h_dentry)) {
8417 +               h_inode = d_inode(h_dentry);
8418 +               au_iattr_save(&ia, h_inode);
8419 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8420 +               /* nfs d_revalidate may return 0 for negative dentry */
8421 +               /* fuse d_revalidate always return 0 for negative dentry */
8422 +               goto out;
8423 +
8424 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8425 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8426 +       err = PTR_ERR(h_d);
8427 +       if (IS_ERR(h_d))
8428 +               goto out;
8429 +
8430 +       err = 0;
8431 +       if (unlikely(h_d != h_dentry
8432 +                    || d_inode(h_d) != h_inode
8433 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8434 +               err = au_busy_or_stale();
8435 +       dput(h_d);
8436 +
8437 +out:
8438 +       AuTraceErr(err);
8439 +       return err;
8440 +}
8441 +
8442 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8443 +               struct dentry *h_parent, struct au_branch *br)
8444 +{
8445 +       int err;
8446 +
8447 +       err = 0;
8448 +       if (udba == AuOpt_UDBA_REVAL
8449 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8450 +               IMustLock(h_dir);
8451 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8452 +       } else if (udba != AuOpt_UDBA_NONE)
8453 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8454 +
8455 +       return err;
8456 +}
8457 +
8458 +/* ---------------------------------------------------------------------- */
8459 +
8460 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8461 +{
8462 +       int err;
8463 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8464 +       struct au_hdentry tmp, *p, *q;
8465 +       struct au_dinfo *dinfo;
8466 +       struct super_block *sb;
8467 +
8468 +       DiMustWriteLock(dentry);
8469 +
8470 +       sb = dentry->d_sb;
8471 +       dinfo = au_di(dentry);
8472 +       bbot = dinfo->di_bbot;
8473 +       bwh = dinfo->di_bwh;
8474 +       bdiropq = dinfo->di_bdiropq;
8475 +       bindex = dinfo->di_btop;
8476 +       p = au_hdentry(dinfo, bindex);
8477 +       for (; bindex <= bbot; bindex++, p++) {
8478 +               if (!p->hd_dentry)
8479 +                       continue;
8480 +
8481 +               new_bindex = au_br_index(sb, p->hd_id);
8482 +               if (new_bindex == bindex)
8483 +                       continue;
8484 +
8485 +               if (dinfo->di_bwh == bindex)
8486 +                       bwh = new_bindex;
8487 +               if (dinfo->di_bdiropq == bindex)
8488 +                       bdiropq = new_bindex;
8489 +               if (new_bindex < 0) {
8490 +                       au_hdput(p);
8491 +                       p->hd_dentry = NULL;
8492 +                       continue;
8493 +               }
8494 +
8495 +               /* swap two lower dentries, and loop again */
8496 +               q = au_hdentry(dinfo, new_bindex);
8497 +               tmp = *q;
8498 +               *q = *p;
8499 +               *p = tmp;
8500 +               if (tmp.hd_dentry) {
8501 +                       bindex--;
8502 +                       p--;
8503 +               }
8504 +       }
8505 +
8506 +       dinfo->di_bwh = -1;
8507 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8508 +               dinfo->di_bwh = bwh;
8509 +
8510 +       dinfo->di_bdiropq = -1;
8511 +       if (bdiropq >= 0
8512 +           && bdiropq <= au_sbbot(sb)
8513 +           && au_sbr_whable(sb, bdiropq))
8514 +               dinfo->di_bdiropq = bdiropq;
8515 +
8516 +       err = -EIO;
8517 +       dinfo->di_btop = -1;
8518 +       dinfo->di_bbot = -1;
8519 +       bbot = au_dbbot(parent);
8520 +       bindex = 0;
8521 +       p = au_hdentry(dinfo, bindex);
8522 +       for (; bindex <= bbot; bindex++, p++)
8523 +               if (p->hd_dentry) {
8524 +                       dinfo->di_btop = bindex;
8525 +                       break;
8526 +               }
8527 +
8528 +       if (dinfo->di_btop >= 0) {
8529 +               bindex = bbot;
8530 +               p = au_hdentry(dinfo, bindex);
8531 +               for (; bindex >= 0; bindex--, p--)
8532 +                       if (p->hd_dentry) {
8533 +                               dinfo->di_bbot = bindex;
8534 +                               err = 0;
8535 +                               break;
8536 +                       }
8537 +       }
8538 +
8539 +       return err;
8540 +}
8541 +
8542 +static void au_do_hide(struct dentry *dentry)
8543 +{
8544 +       struct inode *inode;
8545 +
8546 +       if (d_really_is_positive(dentry)) {
8547 +               inode = d_inode(dentry);
8548 +               if (!d_is_dir(dentry)) {
8549 +                       if (inode->i_nlink && !d_unhashed(dentry))
8550 +                               drop_nlink(inode);
8551 +               } else {
8552 +                       clear_nlink(inode);
8553 +                       /* stop next lookup */
8554 +                       inode->i_flags |= S_DEAD;
8555 +               }
8556 +               smp_mb(); /* necessary? */
8557 +       }
8558 +       d_drop(dentry);
8559 +}
8560 +
8561 +static int au_hide_children(struct dentry *parent)
8562 +{
8563 +       int err, i, j, ndentry;
8564 +       struct au_dcsub_pages dpages;
8565 +       struct au_dpage *dpage;
8566 +       struct dentry *dentry;
8567 +
8568 +       err = au_dpages_init(&dpages, GFP_NOFS);
8569 +       if (unlikely(err))
8570 +               goto out;
8571 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8572 +       if (unlikely(err))
8573 +               goto out_dpages;
8574 +
8575 +       /* in reverse order */
8576 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8577 +               dpage = dpages.dpages + i;
8578 +               ndentry = dpage->ndentry;
8579 +               for (j = ndentry - 1; j >= 0; j--) {
8580 +                       dentry = dpage->dentries[j];
8581 +                       if (dentry != parent)
8582 +                               au_do_hide(dentry);
8583 +               }
8584 +       }
8585 +
8586 +out_dpages:
8587 +       au_dpages_free(&dpages);
8588 +out:
8589 +       return err;
8590 +}
8591 +
8592 +static void au_hide(struct dentry *dentry)
8593 +{
8594 +       int err;
8595 +
8596 +       AuDbgDentry(dentry);
8597 +       if (d_is_dir(dentry)) {
8598 +               /* shrink_dcache_parent(dentry); */
8599 +               err = au_hide_children(dentry);
8600 +               if (unlikely(err))
8601 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8602 +                               dentry, err);
8603 +       }
8604 +       au_do_hide(dentry);
8605 +}
8606 +
8607 +/*
8608 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8609 + *
8610 + * a dirty branch is added
8611 + * - on the top of layers
8612 + * - in the middle of layers
8613 + * - to the bottom of layers
8614 + *
8615 + * on the added branch there exists
8616 + * - a whiteout
8617 + * - a diropq
8618 + * - a same named entry
8619 + *   + exist
8620 + *     * negative --> positive
8621 + *     * positive --> positive
8622 + *      - type is unchanged
8623 + *      - type is changed
8624 + *   + doesn't exist
8625 + *     * negative --> negative
8626 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8627 + * - none
8628 + */
8629 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8630 +                              struct au_dinfo *tmp)
8631 +{
8632 +       int err;
8633 +       aufs_bindex_t bindex, bbot;
8634 +       struct {
8635 +               struct dentry *dentry;
8636 +               struct inode *inode;
8637 +               mode_t mode;
8638 +       } orig_h, tmp_h = {
8639 +               .dentry = NULL
8640 +       };
8641 +       struct au_hdentry *hd;
8642 +       struct inode *inode, *h_inode;
8643 +       struct dentry *h_dentry;
8644 +
8645 +       err = 0;
8646 +       AuDebugOn(dinfo->di_btop < 0);
8647 +       orig_h.mode = 0;
8648 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8649 +       orig_h.inode = NULL;
8650 +       if (d_is_positive(orig_h.dentry)) {
8651 +               orig_h.inode = d_inode(orig_h.dentry);
8652 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8653 +       }
8654 +       if (tmp->di_btop >= 0) {
8655 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8656 +               if (d_is_positive(tmp_h.dentry)) {
8657 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8658 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8659 +               }
8660 +       }
8661 +
8662 +       inode = NULL;
8663 +       if (d_really_is_positive(dentry))
8664 +               inode = d_inode(dentry);
8665 +       if (!orig_h.inode) {
8666 +               AuDbg("negative originally\n");
8667 +               if (inode) {
8668 +                       au_hide(dentry);
8669 +                       goto out;
8670 +               }
8671 +               AuDebugOn(inode);
8672 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8673 +               AuDebugOn(dinfo->di_bdiropq != -1);
8674 +
8675 +               if (!tmp_h.inode) {
8676 +                       AuDbg("negative --> negative\n");
8677 +                       /* should have only one negative lower */
8678 +                       if (tmp->di_btop >= 0
8679 +                           && tmp->di_btop < dinfo->di_btop) {
8680 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8681 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8682 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8683 +                               au_di_cp(dinfo, tmp);
8684 +                               hd = au_hdentry(tmp, tmp->di_btop);
8685 +                               au_set_h_dptr(dentry, tmp->di_btop,
8686 +                                             dget(hd->hd_dentry));
8687 +                       }
8688 +                       au_dbg_verify_dinode(dentry);
8689 +               } else {
8690 +                       AuDbg("negative --> positive\n");
8691 +                       /*
8692 +                        * similar to the behaviour of creating with bypassing
8693 +                        * aufs.
8694 +                        * unhash it in order to force an error in the
8695 +                        * succeeding create operation.
8696 +                        * we should not set S_DEAD here.
8697 +                        */
8698 +                       d_drop(dentry);
8699 +                       /* au_di_swap(tmp, dinfo); */
8700 +                       au_dbg_verify_dinode(dentry);
8701 +               }
8702 +       } else {
8703 +               AuDbg("positive originally\n");
8704 +               /* inode may be NULL */
8705 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8706 +               if (!tmp_h.inode) {
8707 +                       AuDbg("positive --> negative\n");
8708 +                       /* or bypassing aufs */
8709 +                       au_hide(dentry);
8710 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8711 +                               dinfo->di_bwh = tmp->di_bwh;
8712 +                       if (inode)
8713 +                               err = au_refresh_hinode_self(inode);
8714 +                       au_dbg_verify_dinode(dentry);
8715 +               } else if (orig_h.mode == tmp_h.mode) {
8716 +                       AuDbg("positive --> positive, same type\n");
8717 +                       if (!S_ISDIR(orig_h.mode)
8718 +                           && dinfo->di_btop > tmp->di_btop) {
8719 +                               /*
8720 +                                * similar to the behaviour of removing and
8721 +                                * creating.
8722 +                                */
8723 +                               au_hide(dentry);
8724 +                               if (inode)
8725 +                                       err = au_refresh_hinode_self(inode);
8726 +                               au_dbg_verify_dinode(dentry);
8727 +                       } else {
8728 +                               /* fill empty slots */
8729 +                               if (dinfo->di_btop > tmp->di_btop)
8730 +                                       dinfo->di_btop = tmp->di_btop;
8731 +                               if (dinfo->di_bbot < tmp->di_bbot)
8732 +                                       dinfo->di_bbot = tmp->di_bbot;
8733 +                               dinfo->di_bwh = tmp->di_bwh;
8734 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8735 +                               bbot = dinfo->di_bbot;
8736 +                               bindex = tmp->di_btop;
8737 +                               hd = au_hdentry(tmp, bindex);
8738 +                               for (; bindex <= bbot; bindex++, hd++) {
8739 +                                       if (au_h_dptr(dentry, bindex))
8740 +                                               continue;
8741 +                                       h_dentry = hd->hd_dentry;
8742 +                                       if (!h_dentry)
8743 +                                               continue;
8744 +                                       AuDebugOn(d_is_negative(h_dentry));
8745 +                                       h_inode = d_inode(h_dentry);
8746 +                                       AuDebugOn(orig_h.mode
8747 +                                                 != (h_inode->i_mode
8748 +                                                     & S_IFMT));
8749 +                                       au_set_h_dptr(dentry, bindex,
8750 +                                                     dget(h_dentry));
8751 +                               }
8752 +                               if (inode)
8753 +                                       err = au_refresh_hinode(inode, dentry);
8754 +                               au_dbg_verify_dinode(dentry);
8755 +                       }
8756 +               } else {
8757 +                       AuDbg("positive --> positive, different type\n");
8758 +                       /* similar to the behaviour of removing and creating */
8759 +                       au_hide(dentry);
8760 +                       if (inode)
8761 +                               err = au_refresh_hinode_self(inode);
8762 +                       au_dbg_verify_dinode(dentry);
8763 +               }
8764 +       }
8765 +
8766 +out:
8767 +       return err;
8768 +}
8769 +
8770 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8771 +{
8772 +       const struct dentry_operations *dop
8773 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8774 +       static const unsigned int mask
8775 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8776 +
8777 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8778 +
8779 +       if (dentry->d_op == dop)
8780 +               return;
8781 +
8782 +       AuDbg("%pd\n", dentry);
8783 +       spin_lock(&dentry->d_lock);
8784 +       if (dop == &aufs_dop)
8785 +               dentry->d_flags |= mask;
8786 +       else
8787 +               dentry->d_flags &= ~mask;
8788 +       dentry->d_op = dop;
8789 +       spin_unlock(&dentry->d_lock);
8790 +}
8791 +
8792 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8793 +{
8794 +       int err, ebrange, nbr;
8795 +       unsigned int sigen;
8796 +       struct au_dinfo *dinfo, *tmp;
8797 +       struct super_block *sb;
8798 +       struct inode *inode;
8799 +
8800 +       DiMustWriteLock(dentry);
8801 +       AuDebugOn(IS_ROOT(dentry));
8802 +       AuDebugOn(d_really_is_negative(parent));
8803 +
8804 +       sb = dentry->d_sb;
8805 +       sigen = au_sigen(sb);
8806 +       err = au_digen_test(parent, sigen);
8807 +       if (unlikely(err))
8808 +               goto out;
8809 +
8810 +       nbr = au_sbbot(sb) + 1;
8811 +       dinfo = au_di(dentry);
8812 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8813 +       if (unlikely(err))
8814 +               goto out;
8815 +       ebrange = au_dbrange_test(dentry);
8816 +       if (!ebrange)
8817 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8818 +
8819 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8820 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8821 +               if (d_really_is_positive(dentry)) {
8822 +                       inode = d_inode(dentry);
8823 +                       err = au_refresh_hinode_self(inode);
8824 +               }
8825 +               au_dbg_verify_dinode(dentry);
8826 +               if (!err)
8827 +                       goto out_dgen; /* success */
8828 +               goto out;
8829 +       }
8830 +
8831 +       /* temporary dinfo */
8832 +       AuDbgDentry(dentry);
8833 +       err = -ENOMEM;
8834 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8835 +       if (unlikely(!tmp))
8836 +               goto out;
8837 +       au_di_swap(tmp, dinfo);
8838 +       /* returns the number of positive dentries */
8839 +       /*
8840 +        * if current working dir is removed, it returns an error.
8841 +        * but the dentry is legal.
8842 +        */
8843 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8844 +       AuDbgDentry(dentry);
8845 +       au_di_swap(tmp, dinfo);
8846 +       if (err == -ENOENT)
8847 +               err = 0;
8848 +       if (err >= 0) {
8849 +               /* compare/refresh by dinfo */
8850 +               AuDbgDentry(dentry);
8851 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8852 +               au_dbg_verify_dinode(dentry);
8853 +               AuTraceErr(err);
8854 +       }
8855 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8856 +       au_rw_write_unlock(&tmp->di_rwsem);
8857 +       au_di_free(tmp);
8858 +       if (unlikely(err))
8859 +               goto out;
8860 +
8861 +out_dgen:
8862 +       au_update_digen(dentry);
8863 +out:
8864 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8865 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8866 +               AuDbgDentry(dentry);
8867 +       }
8868 +       AuTraceErr(err);
8869 +       return err;
8870 +}
8871 +
8872 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8873 +                          struct dentry *dentry, aufs_bindex_t bindex)
8874 +{
8875 +       int err, valid;
8876 +
8877 +       err = 0;
8878 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8879 +               goto out;
8880 +
8881 +       AuDbg("b%d\n", bindex);
8882 +       /*
8883 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8884 +        * due to whiteout and branch permission.
8885 +        */
8886 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8887 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8888 +       /* it may return tri-state */
8889 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8890 +
8891 +       if (unlikely(valid < 0))
8892 +               err = valid;
8893 +       else if (!valid)
8894 +               err = -EINVAL;
8895 +
8896 +out:
8897 +       AuTraceErr(err);
8898 +       return err;
8899 +}
8900 +
8901 +/* todo: remove this */
8902 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8903 +                         unsigned int flags, int do_udba, int dirren)
8904 +{
8905 +       int err;
8906 +       umode_t mode, h_mode;
8907 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8908 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8909 +       struct inode *h_inode, *h_cached_inode;
8910 +       struct dentry *h_dentry;
8911 +       struct qstr *name, *h_name;
8912 +
8913 +       err = 0;
8914 +       plus = 0;
8915 +       mode = 0;
8916 +       ibs = -1;
8917 +       ibe = -1;
8918 +       unhashed = !!d_unhashed(dentry);
8919 +       is_root = !!IS_ROOT(dentry);
8920 +       name = &dentry->d_name;
8921 +       tmpfile = au_di(dentry)->di_tmpfile;
8922 +
8923 +       /*
8924 +        * Theoretically, REVAL test should be unnecessary in case of
8925 +        * {FS,I}NOTIFY.
8926 +        * But {fs,i}notify doesn't fire some necessary events,
8927 +        *      IN_ATTRIB for atime/nlink/pageio
8928 +        * Let's do REVAL test too.
8929 +        */
8930 +       if (do_udba && inode) {
8931 +               mode = (inode->i_mode & S_IFMT);
8932 +               plus = (inode->i_nlink > 0);
8933 +               ibs = au_ibtop(inode);
8934 +               ibe = au_ibbot(inode);
8935 +       }
8936 +
8937 +       btop = au_dbtop(dentry);
8938 +       btail = btop;
8939 +       if (inode && S_ISDIR(inode->i_mode))
8940 +               btail = au_dbtaildir(dentry);
8941 +       for (bindex = btop; bindex <= btail; bindex++) {
8942 +               h_dentry = au_h_dptr(dentry, bindex);
8943 +               if (!h_dentry)
8944 +                       continue;
8945 +
8946 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8947 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8948 +               spin_lock(&h_dentry->d_lock);
8949 +               h_name = &h_dentry->d_name;
8950 +               if (unlikely(do_udba
8951 +                            && !is_root
8952 +                            && ((!h_nfs
8953 +                                 && (unhashed != !!d_unhashed(h_dentry)
8954 +                                     || (!tmpfile && !dirren
8955 +                                         && !au_qstreq(name, h_name))
8956 +                                         ))
8957 +                                || (h_nfs
8958 +                                    && !(flags & LOOKUP_OPEN)
8959 +                                    && (h_dentry->d_flags
8960 +                                        & DCACHE_NFSFS_RENAMED)))
8961 +                           )) {
8962 +                       int h_unhashed;
8963 +
8964 +                       h_unhashed = d_unhashed(h_dentry);
8965 +                       spin_unlock(&h_dentry->d_lock);
8966 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8967 +                             unhashed, h_unhashed, dentry, h_dentry);
8968 +                       goto err;
8969 +               }
8970 +               spin_unlock(&h_dentry->d_lock);
8971 +
8972 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8973 +               if (unlikely(err))
8974 +                       /* do not goto err, to keep the errno */
8975 +                       break;
8976 +
8977 +               /* todo: plink too? */
8978 +               if (!do_udba)
8979 +                       continue;
8980 +
8981 +               /* UDBA tests */
8982 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8983 +                       goto err;
8984 +
8985 +               h_inode = NULL;
8986 +               if (d_is_positive(h_dentry))
8987 +                       h_inode = d_inode(h_dentry);
8988 +               h_plus = plus;
8989 +               h_mode = mode;
8990 +               h_cached_inode = h_inode;
8991 +               if (h_inode) {
8992 +                       h_mode = (h_inode->i_mode & S_IFMT);
8993 +                       h_plus = (h_inode->i_nlink > 0);
8994 +               }
8995 +               if (inode && ibs <= bindex && bindex <= ibe)
8996 +                       h_cached_inode = au_h_iptr(inode, bindex);
8997 +
8998 +               if (!h_nfs) {
8999 +                       if (unlikely(plus != h_plus && !tmpfile))
9000 +                               goto err;
9001 +               } else {
9002 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9003 +                                    && !is_root
9004 +                                    && !IS_ROOT(h_dentry)
9005 +                                    && unhashed != d_unhashed(h_dentry)))
9006 +                               goto err;
9007 +               }
9008 +               if (unlikely(mode != h_mode
9009 +                            || h_cached_inode != h_inode))
9010 +                       goto err;
9011 +               continue;
9012 +
9013 +err:
9014 +               err = -EINVAL;
9015 +               break;
9016 +       }
9017 +
9018 +       AuTraceErr(err);
9019 +       return err;
9020 +}
9021 +
9022 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
9023 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9024 +{
9025 +       int err;
9026 +       struct dentry *parent;
9027 +
9028 +       if (!au_digen_test(dentry, sigen))
9029 +               return 0;
9030 +
9031 +       parent = dget_parent(dentry);
9032 +       di_read_lock_parent(parent, AuLock_IR);
9033 +       AuDebugOn(au_digen_test(parent, sigen));
9034 +       au_dbg_verify_gen(parent, sigen);
9035 +       err = au_refresh_dentry(dentry, parent);
9036 +       di_read_unlock(parent, AuLock_IR);
9037 +       dput(parent);
9038 +       AuTraceErr(err);
9039 +       return err;
9040 +}
9041 +
9042 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9043 +{
9044 +       int err;
9045 +       struct dentry *d, *parent;
9046 +
9047 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9048 +               return simple_reval_dpath(dentry, sigen);
9049 +
9050 +       /* slow loop, keep it simple and stupid */
9051 +       /* cf: au_cpup_dirs() */
9052 +       err = 0;
9053 +       parent = NULL;
9054 +       while (au_digen_test(dentry, sigen)) {
9055 +               d = dentry;
9056 +               while (1) {
9057 +                       dput(parent);
9058 +                       parent = dget_parent(d);
9059 +                       if (!au_digen_test(parent, sigen))
9060 +                               break;
9061 +                       d = parent;
9062 +               }
9063 +
9064 +               if (d != dentry)
9065 +                       di_write_lock_child2(d);
9066 +
9067 +               /* someone might update our dentry while we were sleeping */
9068 +               if (au_digen_test(d, sigen)) {
9069 +                       /*
9070 +                        * todo: consolidate with simple_reval_dpath(),
9071 +                        * do_refresh() and au_reval_for_attr().
9072 +                        */
9073 +                       di_read_lock_parent(parent, AuLock_IR);
9074 +                       err = au_refresh_dentry(d, parent);
9075 +                       di_read_unlock(parent, AuLock_IR);
9076 +               }
9077 +
9078 +               if (d != dentry)
9079 +                       di_write_unlock(d);
9080 +               dput(parent);
9081 +               if (unlikely(err))
9082 +                       break;
9083 +       }
9084 +
9085 +       return err;
9086 +}
9087 +
9088 +/*
9089 + * if valid returns 1, otherwise 0.
9090 + */
9091 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9092 +{
9093 +       int valid, err;
9094 +       unsigned int sigen;
9095 +       unsigned char do_udba, dirren;
9096 +       struct super_block *sb;
9097 +       struct inode *inode;
9098 +
9099 +       /* todo: support rcu-walk? */
9100 +       if (flags & LOOKUP_RCU)
9101 +               return -ECHILD;
9102 +
9103 +       valid = 0;
9104 +       if (unlikely(!au_di(dentry)))
9105 +               goto out;
9106 +
9107 +       valid = 1;
9108 +       sb = dentry->d_sb;
9109 +       /*
9110 +        * todo: very ugly
9111 +        * i_mutex of parent dir may be held,
9112 +        * but we should not return 'invalid' due to busy.
9113 +        */
9114 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9115 +       if (unlikely(err)) {
9116 +               valid = err;
9117 +               AuTraceErr(err);
9118 +               goto out;
9119 +       }
9120 +       inode = NULL;
9121 +       if (d_really_is_positive(dentry))
9122 +               inode = d_inode(dentry);
9123 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9124 +               err = -EINVAL;
9125 +               AuTraceErr(err);
9126 +               goto out_dgrade;
9127 +       }
9128 +       if (unlikely(au_dbrange_test(dentry))) {
9129 +               err = -EINVAL;
9130 +               AuTraceErr(err);
9131 +               goto out_dgrade;
9132 +       }
9133 +
9134 +       sigen = au_sigen(sb);
9135 +       if (au_digen_test(dentry, sigen)) {
9136 +               AuDebugOn(IS_ROOT(dentry));
9137 +               err = au_reval_dpath(dentry, sigen);
9138 +               if (unlikely(err)) {
9139 +                       AuTraceErr(err);
9140 +                       goto out_dgrade;
9141 +               }
9142 +       }
9143 +       di_downgrade_lock(dentry, AuLock_IR);
9144 +
9145 +       err = -EINVAL;
9146 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9147 +           && inode
9148 +           && !(inode->i_state && I_LINKABLE)
9149 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9150 +               AuTraceErr(err);
9151 +               goto out_inval;
9152 +       }
9153 +
9154 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9155 +       if (do_udba && inode) {
9156 +               aufs_bindex_t btop = au_ibtop(inode);
9157 +               struct inode *h_inode;
9158 +
9159 +               if (btop >= 0) {
9160 +                       h_inode = au_h_iptr(inode, btop);
9161 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9162 +                               AuTraceErr(err);
9163 +                               goto out_inval;
9164 +                       }
9165 +               }
9166 +       }
9167 +
9168 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9169 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9170 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9171 +               err = -EIO;
9172 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9173 +                     dentry, err);
9174 +       }
9175 +       goto out_inval;
9176 +
9177 +out_dgrade:
9178 +       di_downgrade_lock(dentry, AuLock_IR);
9179 +out_inval:
9180 +       aufs_read_unlock(dentry, AuLock_IR);
9181 +       AuTraceErr(err);
9182 +       valid = !err;
9183 +out:
9184 +       if (!valid) {
9185 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9186 +               d_drop(dentry);
9187 +       }
9188 +       return valid;
9189 +}
9190 +
9191 +static void aufs_d_release(struct dentry *dentry)
9192 +{
9193 +       if (au_di(dentry)) {
9194 +               au_di_fin(dentry);
9195 +               au_hn_di_reinit(dentry);
9196 +       }
9197 +}
9198 +
9199 +const struct dentry_operations aufs_dop = {
9200 +       .d_revalidate           = aufs_d_revalidate,
9201 +       .d_weak_revalidate      = aufs_d_revalidate,
9202 +       .d_release              = aufs_d_release
9203 +};
9204 +
9205 +/* aufs_dop without d_revalidate */
9206 +const struct dentry_operations aufs_dop_noreval = {
9207 +       .d_release              = aufs_d_release
9208 +};
9209 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9210 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9211 +++ linux/fs/aufs/dentry.h      2020-01-27 10:57:18.168871450 +0100
9212 @@ -0,0 +1,268 @@
9213 +/* SPDX-License-Identifier: GPL-2.0 */
9214 +/*
9215 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9216 + *
9217 + * This program, aufs is free software; you can redistribute it and/or modify
9218 + * it under the terms of the GNU General Public License as published by
9219 + * the Free Software Foundation; either version 2 of the License, or
9220 + * (at your option) any later version.
9221 + *
9222 + * This program is distributed in the hope that it will be useful,
9223 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9224 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9225 + * GNU General Public License for more details.
9226 + *
9227 + * You should have received a copy of the GNU General Public License
9228 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9229 + */
9230 +
9231 +/*
9232 + * lookup and dentry operations
9233 + */
9234 +
9235 +#ifndef __AUFS_DENTRY_H__
9236 +#define __AUFS_DENTRY_H__
9237 +
9238 +#ifdef __KERNEL__
9239 +
9240 +#include <linux/dcache.h>
9241 +#include "dirren.h"
9242 +#include "rwsem.h"
9243 +
9244 +struct au_hdentry {
9245 +       struct dentry           *hd_dentry;
9246 +       aufs_bindex_t           hd_id;
9247 +};
9248 +
9249 +struct au_dinfo {
9250 +       atomic_t                di_generation;
9251 +
9252 +       struct au_rwsem         di_rwsem;
9253 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9254 +       unsigned char           di_tmpfile; /* to allow the different name */
9255 +       struct au_hdentry       *di_hdentry;
9256 +       struct rcu_head         rcu;
9257 +} ____cacheline_aligned_in_smp;
9258 +
9259 +/* ---------------------------------------------------------------------- */
9260 +
9261 +/* flags for au_lkup_dentry() */
9262 +#define AuLkup_ALLOW_NEG       1
9263 +#define AuLkup_IGNORE_PERM     (1 << 1)
9264 +#define AuLkup_DIRREN          (1 << 2)
9265 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9266 +#define au_fset_lkup(flags, name) \
9267 +       do { (flags) |= AuLkup_##name; } while (0)
9268 +#define au_fclr_lkup(flags, name) \
9269 +       do { (flags) &= ~AuLkup_##name; } while (0)
9270 +
9271 +#ifndef CONFIG_AUFS_DIRREN
9272 +#undef AuLkup_DIRREN
9273 +#define AuLkup_DIRREN 0
9274 +#endif
9275 +
9276 +struct au_do_lookup_args {
9277 +       unsigned int            flags;
9278 +       mode_t                  type;
9279 +       struct qstr             whname, *name;
9280 +       struct au_dr_lookup     dirren;
9281 +};
9282 +
9283 +/* ---------------------------------------------------------------------- */
9284 +
9285 +/* dentry.c */
9286 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9287 +struct au_branch;
9288 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9289 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9290 +               struct dentry *h_parent, struct au_branch *br);
9291 +
9292 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9293 +                  unsigned int flags);
9294 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9295 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9296 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9297 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9298 +
9299 +/* dinfo.c */
9300 +void au_di_init_once(void *_di);
9301 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9302 +void au_di_free(struct au_dinfo *dinfo);
9303 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9304 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9305 +int au_di_init(struct dentry *dentry);
9306 +void au_di_fin(struct dentry *dentry);
9307 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9308 +
9309 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9310 +void di_read_unlock(struct dentry *d, int flags);
9311 +void di_downgrade_lock(struct dentry *d, int flags);
9312 +void di_write_lock(struct dentry *d, unsigned int lsc);
9313 +void di_write_unlock(struct dentry *d);
9314 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9315 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9316 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9317 +
9318 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9319 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9320 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9321 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9322 +
9323 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9324 +                  struct dentry *h_dentry);
9325 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9326 +int au_dbrange_test(struct dentry *dentry);
9327 +void au_update_digen(struct dentry *dentry);
9328 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9329 +void au_update_dbtop(struct dentry *dentry);
9330 +void au_update_dbbot(struct dentry *dentry);
9331 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9332 +
9333 +/* ---------------------------------------------------------------------- */
9334 +
9335 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9336 +{
9337 +       return dentry->d_fsdata;
9338 +}
9339 +
9340 +/* ---------------------------------------------------------------------- */
9341 +
9342 +/* lock subclass for dinfo */
9343 +enum {
9344 +       AuLsc_DI_CHILD,         /* child first */
9345 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9346 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9347 +       AuLsc_DI_PARENT,
9348 +       AuLsc_DI_PARENT2,
9349 +       AuLsc_DI_PARENT3,
9350 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9351 +};
9352 +
9353 +/*
9354 + * di_read_lock_child, di_write_lock_child,
9355 + * di_read_lock_child2, di_write_lock_child2,
9356 + * di_read_lock_child3, di_write_lock_child3,
9357 + * di_read_lock_parent, di_write_lock_parent,
9358 + * di_read_lock_parent2, di_write_lock_parent2,
9359 + * di_read_lock_parent3, di_write_lock_parent3,
9360 + */
9361 +#define AuReadLockFunc(name, lsc) \
9362 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9363 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9364 +
9365 +#define AuWriteLockFunc(name, lsc) \
9366 +static inline void di_write_lock_##name(struct dentry *d) \
9367 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9368 +
9369 +#define AuRWLockFuncs(name, lsc) \
9370 +       AuReadLockFunc(name, lsc) \
9371 +       AuWriteLockFunc(name, lsc)
9372 +
9373 +AuRWLockFuncs(child, CHILD);
9374 +AuRWLockFuncs(child2, CHILD2);
9375 +AuRWLockFuncs(child3, CHILD3);
9376 +AuRWLockFuncs(parent, PARENT);
9377 +AuRWLockFuncs(parent2, PARENT2);
9378 +AuRWLockFuncs(parent3, PARENT3);
9379 +
9380 +#undef AuReadLockFunc
9381 +#undef AuWriteLockFunc
9382 +#undef AuRWLockFuncs
9383 +
9384 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9385 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9386 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9387 +
9388 +/* ---------------------------------------------------------------------- */
9389 +
9390 +/* todo: memory barrier? */
9391 +static inline unsigned int au_digen(struct dentry *d)
9392 +{
9393 +       return atomic_read(&au_di(d)->di_generation);
9394 +}
9395 +
9396 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9397 +{
9398 +       hdentry->hd_dentry = NULL;
9399 +}
9400 +
9401 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9402 +                                           aufs_bindex_t bindex)
9403 +{
9404 +       return di->di_hdentry + bindex;
9405 +}
9406 +
9407 +static inline void au_hdput(struct au_hdentry *hd)
9408 +{
9409 +       if (hd)
9410 +               dput(hd->hd_dentry);
9411 +}
9412 +
9413 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9414 +{
9415 +       DiMustAnyLock(dentry);
9416 +       return au_di(dentry)->di_btop;
9417 +}
9418 +
9419 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9420 +{
9421 +       DiMustAnyLock(dentry);
9422 +       return au_di(dentry)->di_bbot;
9423 +}
9424 +
9425 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9426 +{
9427 +       DiMustAnyLock(dentry);
9428 +       return au_di(dentry)->di_bwh;
9429 +}
9430 +
9431 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9432 +{
9433 +       DiMustAnyLock(dentry);
9434 +       return au_di(dentry)->di_bdiropq;
9435 +}
9436 +
9437 +/* todo: hard/soft set? */
9438 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9439 +{
9440 +       DiMustWriteLock(dentry);
9441 +       au_di(dentry)->di_btop = bindex;
9442 +}
9443 +
9444 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9445 +{
9446 +       DiMustWriteLock(dentry);
9447 +       au_di(dentry)->di_bbot = bindex;
9448 +}
9449 +
9450 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9451 +{
9452 +       DiMustWriteLock(dentry);
9453 +       /* dbwh can be outside of btop - bbot range */
9454 +       au_di(dentry)->di_bwh = bindex;
9455 +}
9456 +
9457 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9458 +{
9459 +       DiMustWriteLock(dentry);
9460 +       au_di(dentry)->di_bdiropq = bindex;
9461 +}
9462 +
9463 +/* ---------------------------------------------------------------------- */
9464 +
9465 +#ifdef CONFIG_AUFS_HNOTIFY
9466 +static inline void au_digen_dec(struct dentry *d)
9467 +{
9468 +       atomic_dec(&au_di(d)->di_generation);
9469 +}
9470 +
9471 +static inline void au_hn_di_reinit(struct dentry *dentry)
9472 +{
9473 +       dentry->d_fsdata = NULL;
9474 +}
9475 +#else
9476 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9477 +#endif /* CONFIG_AUFS_HNOTIFY */
9478 +
9479 +#endif /* __KERNEL__ */
9480 +#endif /* __AUFS_DENTRY_H__ */
9481 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9482 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9483 +++ linux/fs/aufs/dinfo.c       2020-01-27 10:57:18.168871450 +0100
9484 @@ -0,0 +1,554 @@
9485 +// SPDX-License-Identifier: GPL-2.0
9486 +/*
9487 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9488 + *
9489 + * This program, aufs is free software; you can redistribute it and/or modify
9490 + * it under the terms of the GNU General Public License as published by
9491 + * the Free Software Foundation; either version 2 of the License, or
9492 + * (at your option) any later version.
9493 + *
9494 + * This program is distributed in the hope that it will be useful,
9495 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9496 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9497 + * GNU General Public License for more details.
9498 + *
9499 + * You should have received a copy of the GNU General Public License
9500 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9501 + */
9502 +
9503 +/*
9504 + * dentry private data
9505 + */
9506 +
9507 +#include "aufs.h"
9508 +
9509 +void au_di_init_once(void *_dinfo)
9510 +{
9511 +       struct au_dinfo *dinfo = _dinfo;
9512 +
9513 +       au_rw_init(&dinfo->di_rwsem);
9514 +}
9515 +
9516 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9517 +{
9518 +       struct au_dinfo *dinfo;
9519 +       int nbr, i;
9520 +
9521 +       dinfo = au_cache_alloc_dinfo();
9522 +       if (unlikely(!dinfo))
9523 +               goto out;
9524 +
9525 +       nbr = au_sbbot(sb) + 1;
9526 +       if (nbr <= 0)
9527 +               nbr = 1;
9528 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9529 +       if (dinfo->di_hdentry) {
9530 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9531 +               dinfo->di_btop = -1;
9532 +               dinfo->di_bbot = -1;
9533 +               dinfo->di_bwh = -1;
9534 +               dinfo->di_bdiropq = -1;
9535 +               dinfo->di_tmpfile = 0;
9536 +               for (i = 0; i < nbr; i++)
9537 +                       dinfo->di_hdentry[i].hd_id = -1;
9538 +               goto out;
9539 +       }
9540 +
9541 +       au_cache_free_dinfo(dinfo);
9542 +       dinfo = NULL;
9543 +
9544 +out:
9545 +       return dinfo;
9546 +}
9547 +
9548 +void au_di_free(struct au_dinfo *dinfo)
9549 +{
9550 +       struct au_hdentry *p;
9551 +       aufs_bindex_t bbot, bindex;
9552 +
9553 +       /* dentry may not be revalidated */
9554 +       bindex = dinfo->di_btop;
9555 +       if (bindex >= 0) {
9556 +               bbot = dinfo->di_bbot;
9557 +               p = au_hdentry(dinfo, bindex);
9558 +               while (bindex++ <= bbot)
9559 +                       au_hdput(p++);
9560 +       }
9561 +       au_kfree_try_rcu(dinfo->di_hdentry);
9562 +       au_cache_free_dinfo(dinfo);
9563 +}
9564 +
9565 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9566 +{
9567 +       struct au_hdentry *p;
9568 +       aufs_bindex_t bi;
9569 +
9570 +       AuRwMustWriteLock(&a->di_rwsem);
9571 +       AuRwMustWriteLock(&b->di_rwsem);
9572 +
9573 +#define DiSwap(v, name)                                \
9574 +       do {                                    \
9575 +               v = a->di_##name;               \
9576 +               a->di_##name = b->di_##name;    \
9577 +               b->di_##name = v;               \
9578 +       } while (0)
9579 +
9580 +       DiSwap(p, hdentry);
9581 +       DiSwap(bi, btop);
9582 +       DiSwap(bi, bbot);
9583 +       DiSwap(bi, bwh);
9584 +       DiSwap(bi, bdiropq);
9585 +       /* smp_mb(); */
9586 +
9587 +#undef DiSwap
9588 +}
9589 +
9590 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9591 +{
9592 +       AuRwMustWriteLock(&dst->di_rwsem);
9593 +       AuRwMustWriteLock(&src->di_rwsem);
9594 +
9595 +       dst->di_btop = src->di_btop;
9596 +       dst->di_bbot = src->di_bbot;
9597 +       dst->di_bwh = src->di_bwh;
9598 +       dst->di_bdiropq = src->di_bdiropq;
9599 +       /* smp_mb(); */
9600 +}
9601 +
9602 +int au_di_init(struct dentry *dentry)
9603 +{
9604 +       int err;
9605 +       struct super_block *sb;
9606 +       struct au_dinfo *dinfo;
9607 +
9608 +       err = 0;
9609 +       sb = dentry->d_sb;
9610 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9611 +       if (dinfo) {
9612 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9613 +               /* smp_mb(); */ /* atomic_set */
9614 +               dentry->d_fsdata = dinfo;
9615 +       } else
9616 +               err = -ENOMEM;
9617 +
9618 +       return err;
9619 +}
9620 +
9621 +void au_di_fin(struct dentry *dentry)
9622 +{
9623 +       struct au_dinfo *dinfo;
9624 +
9625 +       dinfo = au_di(dentry);
9626 +       AuRwDestroy(&dinfo->di_rwsem);
9627 +       au_di_free(dinfo);
9628 +}
9629 +
9630 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9631 +{
9632 +       int err, sz;
9633 +       struct au_hdentry *hdp;
9634 +
9635 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9636 +
9637 +       err = -ENOMEM;
9638 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9639 +       if (!sz)
9640 +               sz = sizeof(*hdp);
9641 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9642 +                          may_shrink);
9643 +       if (hdp) {
9644 +               dinfo->di_hdentry = hdp;
9645 +               err = 0;
9646 +       }
9647 +
9648 +       return err;
9649 +}
9650 +
9651 +/* ---------------------------------------------------------------------- */
9652 +
9653 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9654 +{
9655 +       switch (lsc) {
9656 +       case AuLsc_DI_CHILD:
9657 +               ii_write_lock_child(inode);
9658 +               break;
9659 +       case AuLsc_DI_CHILD2:
9660 +               ii_write_lock_child2(inode);
9661 +               break;
9662 +       case AuLsc_DI_CHILD3:
9663 +               ii_write_lock_child3(inode);
9664 +               break;
9665 +       case AuLsc_DI_PARENT:
9666 +               ii_write_lock_parent(inode);
9667 +               break;
9668 +       case AuLsc_DI_PARENT2:
9669 +               ii_write_lock_parent2(inode);
9670 +               break;
9671 +       case AuLsc_DI_PARENT3:
9672 +               ii_write_lock_parent3(inode);
9673 +               break;
9674 +       default:
9675 +               BUG();
9676 +       }
9677 +}
9678 +
9679 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9680 +{
9681 +       switch (lsc) {
9682 +       case AuLsc_DI_CHILD:
9683 +               ii_read_lock_child(inode);
9684 +               break;
9685 +       case AuLsc_DI_CHILD2:
9686 +               ii_read_lock_child2(inode);
9687 +               break;
9688 +       case AuLsc_DI_CHILD3:
9689 +               ii_read_lock_child3(inode);
9690 +               break;
9691 +       case AuLsc_DI_PARENT:
9692 +               ii_read_lock_parent(inode);
9693 +               break;
9694 +       case AuLsc_DI_PARENT2:
9695 +               ii_read_lock_parent2(inode);
9696 +               break;
9697 +       case AuLsc_DI_PARENT3:
9698 +               ii_read_lock_parent3(inode);
9699 +               break;
9700 +       default:
9701 +               BUG();
9702 +       }
9703 +}
9704 +
9705 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9706 +{
9707 +       struct inode *inode;
9708 +
9709 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9710 +       if (d_really_is_positive(d)) {
9711 +               inode = d_inode(d);
9712 +               if (au_ftest_lock(flags, IW))
9713 +                       do_ii_write_lock(inode, lsc);
9714 +               else if (au_ftest_lock(flags, IR))
9715 +                       do_ii_read_lock(inode, lsc);
9716 +       }
9717 +}
9718 +
9719 +void di_read_unlock(struct dentry *d, int flags)
9720 +{
9721 +       struct inode *inode;
9722 +
9723 +       if (d_really_is_positive(d)) {
9724 +               inode = d_inode(d);
9725 +               if (au_ftest_lock(flags, IW)) {
9726 +                       au_dbg_verify_dinode(d);
9727 +                       ii_write_unlock(inode);
9728 +               } else if (au_ftest_lock(flags, IR)) {
9729 +                       au_dbg_verify_dinode(d);
9730 +                       ii_read_unlock(inode);
9731 +               }
9732 +       }
9733 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9734 +}
9735 +
9736 +void di_downgrade_lock(struct dentry *d, int flags)
9737 +{
9738 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9739 +               ii_downgrade_lock(d_inode(d));
9740 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9741 +}
9742 +
9743 +void di_write_lock(struct dentry *d, unsigned int lsc)
9744 +{
9745 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9746 +       if (d_really_is_positive(d))
9747 +               do_ii_write_lock(d_inode(d), lsc);
9748 +}
9749 +
9750 +void di_write_unlock(struct dentry *d)
9751 +{
9752 +       au_dbg_verify_dinode(d);
9753 +       if (d_really_is_positive(d))
9754 +               ii_write_unlock(d_inode(d));
9755 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9756 +}
9757 +
9758 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9759 +{
9760 +       AuDebugOn(d1 == d2
9761 +                 || d_inode(d1) == d_inode(d2)
9762 +                 || d1->d_sb != d2->d_sb);
9763 +
9764 +       if ((isdir && au_test_subdir(d1, d2))
9765 +           || d1 < d2) {
9766 +               di_write_lock_child(d1);
9767 +               di_write_lock_child2(d2);
9768 +       } else {
9769 +               di_write_lock_child(d2);
9770 +               di_write_lock_child2(d1);
9771 +       }
9772 +}
9773 +
9774 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9775 +{
9776 +       AuDebugOn(d1 == d2
9777 +                 || d_inode(d1) == d_inode(d2)
9778 +                 || d1->d_sb != d2->d_sb);
9779 +
9780 +       if ((isdir && au_test_subdir(d1, d2))
9781 +           || d1 < d2) {
9782 +               di_write_lock_parent(d1);
9783 +               di_write_lock_parent2(d2);
9784 +       } else {
9785 +               di_write_lock_parent(d2);
9786 +               di_write_lock_parent2(d1);
9787 +       }
9788 +}
9789 +
9790 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9791 +{
9792 +       di_write_unlock(d1);
9793 +       if (d_inode(d1) == d_inode(d2))
9794 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9795 +       else
9796 +               di_write_unlock(d2);
9797 +}
9798 +
9799 +/* ---------------------------------------------------------------------- */
9800 +
9801 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9802 +{
9803 +       struct dentry *d;
9804 +
9805 +       DiMustAnyLock(dentry);
9806 +
9807 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9808 +               return NULL;
9809 +       AuDebugOn(bindex < 0);
9810 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9811 +       AuDebugOn(d && au_dcount(d) <= 0);
9812 +       return d;
9813 +}
9814 +
9815 +/*
9816 + * extended version of au_h_dptr().
9817 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9818 + * error.
9819 + */
9820 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9821 +{
9822 +       struct dentry *h_dentry;
9823 +       struct inode *inode, *h_inode;
9824 +
9825 +       AuDebugOn(d_really_is_negative(dentry));
9826 +
9827 +       h_dentry = NULL;
9828 +       if (au_dbtop(dentry) <= bindex
9829 +           && bindex <= au_dbbot(dentry))
9830 +               h_dentry = au_h_dptr(dentry, bindex);
9831 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9832 +               dget(h_dentry);
9833 +               goto out; /* success */
9834 +       }
9835 +
9836 +       inode = d_inode(dentry);
9837 +       AuDebugOn(bindex < au_ibtop(inode));
9838 +       AuDebugOn(au_ibbot(inode) < bindex);
9839 +       h_inode = au_h_iptr(inode, bindex);
9840 +       h_dentry = d_find_alias(h_inode);
9841 +       if (h_dentry) {
9842 +               if (!IS_ERR(h_dentry)) {
9843 +                       if (!au_d_linkable(h_dentry))
9844 +                               goto out; /* success */
9845 +                       dput(h_dentry);
9846 +               } else
9847 +                       goto out;
9848 +       }
9849 +
9850 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9851 +               h_dentry = au_plink_lkup(inode, bindex);
9852 +               AuDebugOn(!h_dentry);
9853 +               if (!IS_ERR(h_dentry)) {
9854 +                       if (!au_d_hashed_positive(h_dentry))
9855 +                               goto out; /* success */
9856 +                       dput(h_dentry);
9857 +                       h_dentry = NULL;
9858 +               }
9859 +       }
9860 +
9861 +out:
9862 +       AuDbgDentry(h_dentry);
9863 +       return h_dentry;
9864 +}
9865 +
9866 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9867 +{
9868 +       aufs_bindex_t bbot, bwh;
9869 +
9870 +       bbot = au_dbbot(dentry);
9871 +       if (0 <= bbot) {
9872 +               bwh = au_dbwh(dentry);
9873 +               if (!bwh)
9874 +                       return bwh;
9875 +               if (0 < bwh && bwh < bbot)
9876 +                       return bwh - 1;
9877 +       }
9878 +       return bbot;
9879 +}
9880 +
9881 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9882 +{
9883 +       aufs_bindex_t bbot, bopq;
9884 +
9885 +       bbot = au_dbtail(dentry);
9886 +       if (0 <= bbot) {
9887 +               bopq = au_dbdiropq(dentry);
9888 +               if (0 <= bopq && bopq < bbot)
9889 +                       bbot = bopq;
9890 +       }
9891 +       return bbot;
9892 +}
9893 +
9894 +/* ---------------------------------------------------------------------- */
9895 +
9896 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9897 +                  struct dentry *h_dentry)
9898 +{
9899 +       struct au_dinfo *dinfo;
9900 +       struct au_hdentry *hd;
9901 +       struct au_branch *br;
9902 +
9903 +       DiMustWriteLock(dentry);
9904 +
9905 +       dinfo = au_di(dentry);
9906 +       hd = au_hdentry(dinfo, bindex);
9907 +       au_hdput(hd);
9908 +       hd->hd_dentry = h_dentry;
9909 +       if (h_dentry) {
9910 +               br = au_sbr(dentry->d_sb, bindex);
9911 +               hd->hd_id = br->br_id;
9912 +       }
9913 +}
9914 +
9915 +int au_dbrange_test(struct dentry *dentry)
9916 +{
9917 +       int err;
9918 +       aufs_bindex_t btop, bbot;
9919 +
9920 +       err = 0;
9921 +       btop = au_dbtop(dentry);
9922 +       bbot = au_dbbot(dentry);
9923 +       if (btop >= 0)
9924 +               AuDebugOn(bbot < 0 && btop > bbot);
9925 +       else {
9926 +               err = -EIO;
9927 +               AuDebugOn(bbot >= 0);
9928 +       }
9929 +
9930 +       return err;
9931 +}
9932 +
9933 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9934 +{
9935 +       int err;
9936 +
9937 +       err = 0;
9938 +       if (unlikely(au_digen(dentry) != sigen
9939 +                    || au_iigen_test(d_inode(dentry), sigen)))
9940 +               err = -EIO;
9941 +
9942 +       return err;
9943 +}
9944 +
9945 +void au_update_digen(struct dentry *dentry)
9946 +{
9947 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9948 +       /* smp_mb(); */ /* atomic_set */
9949 +}
9950 +
9951 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9952 +{
9953 +       struct au_dinfo *dinfo;
9954 +       struct dentry *h_d;
9955 +       struct au_hdentry *hdp;
9956 +       aufs_bindex_t bindex, bbot;
9957 +
9958 +       DiMustWriteLock(dentry);
9959 +
9960 +       dinfo = au_di(dentry);
9961 +       if (!dinfo || dinfo->di_btop < 0)
9962 +               return;
9963 +
9964 +       if (do_put_zero) {
9965 +               bbot = dinfo->di_bbot;
9966 +               bindex = dinfo->di_btop;
9967 +               hdp = au_hdentry(dinfo, bindex);
9968 +               for (; bindex <= bbot; bindex++, hdp++) {
9969 +                       h_d = hdp->hd_dentry;
9970 +                       if (h_d && d_is_negative(h_d))
9971 +                               au_set_h_dptr(dentry, bindex, NULL);
9972 +               }
9973 +       }
9974 +
9975 +       dinfo->di_btop = 0;
9976 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9977 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9978 +               if (hdp->hd_dentry)
9979 +                       break;
9980 +       if (dinfo->di_btop > dinfo->di_bbot) {
9981 +               dinfo->di_btop = -1;
9982 +               dinfo->di_bbot = -1;
9983 +               return;
9984 +       }
9985 +
9986 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9987 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9988 +               if (hdp->hd_dentry)
9989 +                       break;
9990 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9991 +}
9992 +
9993 +void au_update_dbtop(struct dentry *dentry)
9994 +{
9995 +       aufs_bindex_t bindex, bbot;
9996 +       struct dentry *h_dentry;
9997 +
9998 +       bbot = au_dbbot(dentry);
9999 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
10000 +               h_dentry = au_h_dptr(dentry, bindex);
10001 +               if (!h_dentry)
10002 +                       continue;
10003 +               if (d_is_positive(h_dentry)) {
10004 +                       au_set_dbtop(dentry, bindex);
10005 +                       return;
10006 +               }
10007 +               au_set_h_dptr(dentry, bindex, NULL);
10008 +       }
10009 +}
10010 +
10011 +void au_update_dbbot(struct dentry *dentry)
10012 +{
10013 +       aufs_bindex_t bindex, btop;
10014 +       struct dentry *h_dentry;
10015 +
10016 +       btop = au_dbtop(dentry);
10017 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
10018 +               h_dentry = au_h_dptr(dentry, bindex);
10019 +               if (!h_dentry)
10020 +                       continue;
10021 +               if (d_is_positive(h_dentry)) {
10022 +                       au_set_dbbot(dentry, bindex);
10023 +                       return;
10024 +               }
10025 +               au_set_h_dptr(dentry, bindex, NULL);
10026 +       }
10027 +}
10028 +
10029 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10030 +{
10031 +       aufs_bindex_t bindex, bbot;
10032 +
10033 +       bbot = au_dbbot(dentry);
10034 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
10035 +               if (au_h_dptr(dentry, bindex) == h_dentry)
10036 +                       return bindex;
10037 +       return -1;
10038 +}
10039 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10040 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
10041 +++ linux/fs/aufs/dir.c 2020-01-27 10:57:18.168871450 +0100
10042 @@ -0,0 +1,763 @@
10043 +// SPDX-License-Identifier: GPL-2.0
10044 +/*
10045 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10046 + *
10047 + * This program, aufs is free software; you can redistribute it and/or modify
10048 + * it under the terms of the GNU General Public License as published by
10049 + * the Free Software Foundation; either version 2 of the License, or
10050 + * (at your option) any later version.
10051 + *
10052 + * This program is distributed in the hope that it will be useful,
10053 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10054 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10055 + * GNU General Public License for more details.
10056 + *
10057 + * You should have received a copy of the GNU General Public License
10058 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10059 + */
10060 +
10061 +/*
10062 + * directory operations
10063 + */
10064 +
10065 +#include <linux/fs_stack.h>
10066 +#include <linux/iversion.h>
10067 +#include "aufs.h"
10068 +
10069 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10070 +{
10071 +       unsigned int nlink;
10072 +
10073 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10074 +
10075 +       nlink = dir->i_nlink;
10076 +       nlink += h_dir->i_nlink - 2;
10077 +       if (h_dir->i_nlink < 2)
10078 +               nlink += 2;
10079 +       smp_mb(); /* for i_nlink */
10080 +       /* 0 can happen in revaliding */
10081 +       set_nlink(dir, nlink);
10082 +}
10083 +
10084 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10085 +{
10086 +       unsigned int nlink;
10087 +
10088 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10089 +
10090 +       nlink = dir->i_nlink;
10091 +       nlink -= h_dir->i_nlink - 2;
10092 +       if (h_dir->i_nlink < 2)
10093 +               nlink -= 2;
10094 +       smp_mb(); /* for i_nlink */
10095 +       /* nlink == 0 means the branch-fs is broken */
10096 +       set_nlink(dir, nlink);
10097 +}
10098 +
10099 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10100 +{
10101 +       loff_t sz;
10102 +       aufs_bindex_t bindex, bbot;
10103 +       struct file *h_file;
10104 +       struct dentry *h_dentry;
10105 +
10106 +       sz = 0;
10107 +       if (file) {
10108 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10109 +
10110 +               bbot = au_fbbot_dir(file);
10111 +               for (bindex = au_fbtop(file);
10112 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10113 +                    bindex++) {
10114 +                       h_file = au_hf_dir(file, bindex);
10115 +                       if (h_file && file_inode(h_file))
10116 +                               sz += vfsub_f_size_read(h_file);
10117 +               }
10118 +       } else {
10119 +               AuDebugOn(!dentry);
10120 +               AuDebugOn(!d_is_dir(dentry));
10121 +
10122 +               bbot = au_dbtaildir(dentry);
10123 +               for (bindex = au_dbtop(dentry);
10124 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10125 +                    bindex++) {
10126 +                       h_dentry = au_h_dptr(dentry, bindex);
10127 +                       if (h_dentry && d_is_positive(h_dentry))
10128 +                               sz += i_size_read(d_inode(h_dentry));
10129 +               }
10130 +       }
10131 +       if (sz < KMALLOC_MAX_SIZE)
10132 +               sz = roundup_pow_of_two(sz);
10133 +       if (sz > KMALLOC_MAX_SIZE)
10134 +               sz = KMALLOC_MAX_SIZE;
10135 +       else if (sz < NAME_MAX) {
10136 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10137 +               sz = AUFS_RDBLK_DEF;
10138 +       }
10139 +       return sz;
10140 +}
10141 +
10142 +struct au_dir_ts_arg {
10143 +       struct dentry *dentry;
10144 +       aufs_bindex_t brid;
10145 +};
10146 +
10147 +static void au_do_dir_ts(void *arg)
10148 +{
10149 +       struct au_dir_ts_arg *a = arg;
10150 +       struct au_dtime dt;
10151 +       struct path h_path;
10152 +       struct inode *dir, *h_dir;
10153 +       struct super_block *sb;
10154 +       struct au_branch *br;
10155 +       struct au_hinode *hdir;
10156 +       int err;
10157 +       aufs_bindex_t btop, bindex;
10158 +
10159 +       sb = a->dentry->d_sb;
10160 +       if (d_really_is_negative(a->dentry))
10161 +               goto out;
10162 +       /* no dir->i_mutex lock */
10163 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10164 +
10165 +       dir = d_inode(a->dentry);
10166 +       btop = au_ibtop(dir);
10167 +       bindex = au_br_index(sb, a->brid);
10168 +       if (bindex < btop)
10169 +               goto out_unlock;
10170 +
10171 +       br = au_sbr(sb, bindex);
10172 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10173 +       if (!h_path.dentry)
10174 +               goto out_unlock;
10175 +       h_path.mnt = au_br_mnt(br);
10176 +       au_dtime_store(&dt, a->dentry, &h_path);
10177 +
10178 +       br = au_sbr(sb, btop);
10179 +       if (!au_br_writable(br->br_perm))
10180 +               goto out_unlock;
10181 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10182 +       h_path.mnt = au_br_mnt(br);
10183 +       err = vfsub_mnt_want_write(h_path.mnt);
10184 +       if (err)
10185 +               goto out_unlock;
10186 +       hdir = au_hi(dir, btop);
10187 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10188 +       h_dir = au_h_iptr(dir, btop);
10189 +       if (h_dir->i_nlink
10190 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10191 +               dt.dt_h_path = h_path;
10192 +               au_dtime_revert(&dt);
10193 +       }
10194 +       au_hn_inode_unlock(hdir);
10195 +       vfsub_mnt_drop_write(h_path.mnt);
10196 +       au_cpup_attr_timesizes(dir);
10197 +
10198 +out_unlock:
10199 +       aufs_read_unlock(a->dentry, AuLock_DW);
10200 +out:
10201 +       dput(a->dentry);
10202 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10203 +       au_kfree_try_rcu(arg);
10204 +}
10205 +
10206 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10207 +{
10208 +       int perm, wkq_err;
10209 +       aufs_bindex_t btop;
10210 +       struct au_dir_ts_arg *arg;
10211 +       struct dentry *dentry;
10212 +       struct super_block *sb;
10213 +
10214 +       IMustLock(dir);
10215 +
10216 +       dentry = d_find_any_alias(dir);
10217 +       AuDebugOn(!dentry);
10218 +       sb = dentry->d_sb;
10219 +       btop = au_ibtop(dir);
10220 +       if (btop == bindex) {
10221 +               au_cpup_attr_timesizes(dir);
10222 +               goto out;
10223 +       }
10224 +
10225 +       perm = au_sbr_perm(sb, btop);
10226 +       if (!au_br_writable(perm))
10227 +               goto out;
10228 +
10229 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10230 +       if (!arg)
10231 +               goto out;
10232 +
10233 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10234 +       arg->brid = au_sbr_id(sb, bindex);
10235 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10236 +       if (unlikely(wkq_err)) {
10237 +               pr_err("wkq %d\n", wkq_err);
10238 +               dput(dentry);
10239 +               au_kfree_try_rcu(arg);
10240 +       }
10241 +
10242 +out:
10243 +       dput(dentry);
10244 +}
10245 +
10246 +/* ---------------------------------------------------------------------- */
10247 +
10248 +static int reopen_dir(struct file *file)
10249 +{
10250 +       int err;
10251 +       unsigned int flags;
10252 +       aufs_bindex_t bindex, btail, btop;
10253 +       struct dentry *dentry, *h_dentry;
10254 +       struct file *h_file;
10255 +
10256 +       /* open all lower dirs */
10257 +       dentry = file->f_path.dentry;
10258 +       btop = au_dbtop(dentry);
10259 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10260 +               au_set_h_fptr(file, bindex, NULL);
10261 +       au_set_fbtop(file, btop);
10262 +
10263 +       btail = au_dbtaildir(dentry);
10264 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10265 +               au_set_h_fptr(file, bindex, NULL);
10266 +       au_set_fbbot_dir(file, btail);
10267 +
10268 +       flags = vfsub_file_flags(file);
10269 +       for (bindex = btop; bindex <= btail; bindex++) {
10270 +               h_dentry = au_h_dptr(dentry, bindex);
10271 +               if (!h_dentry)
10272 +                       continue;
10273 +               h_file = au_hf_dir(file, bindex);
10274 +               if (h_file)
10275 +                       continue;
10276 +
10277 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10278 +               err = PTR_ERR(h_file);
10279 +               if (IS_ERR(h_file))
10280 +                       goto out; /* close all? */
10281 +               au_set_h_fptr(file, bindex, h_file);
10282 +       }
10283 +       au_update_figen(file);
10284 +       /* todo: necessary? */
10285 +       /* file->f_ra = h_file->f_ra; */
10286 +       err = 0;
10287 +
10288 +out:
10289 +       return err;
10290 +}
10291 +
10292 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10293 +{
10294 +       int err;
10295 +       aufs_bindex_t bindex, btail;
10296 +       struct dentry *dentry, *h_dentry;
10297 +       struct vfsmount *mnt;
10298 +
10299 +       FiMustWriteLock(file);
10300 +       AuDebugOn(h_file);
10301 +
10302 +       err = 0;
10303 +       mnt = file->f_path.mnt;
10304 +       dentry = file->f_path.dentry;
10305 +       file->f_version = inode_query_iversion(d_inode(dentry));
10306 +       bindex = au_dbtop(dentry);
10307 +       au_set_fbtop(file, bindex);
10308 +       btail = au_dbtaildir(dentry);
10309 +       au_set_fbbot_dir(file, btail);
10310 +       for (; !err && bindex <= btail; bindex++) {
10311 +               h_dentry = au_h_dptr(dentry, bindex);
10312 +               if (!h_dentry)
10313 +                       continue;
10314 +
10315 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10316 +               if (unlikely(err))
10317 +                       break;
10318 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10319 +               if (IS_ERR(h_file)) {
10320 +                       err = PTR_ERR(h_file);
10321 +                       break;
10322 +               }
10323 +               au_set_h_fptr(file, bindex, h_file);
10324 +       }
10325 +       au_update_figen(file);
10326 +       /* todo: necessary? */
10327 +       /* file->f_ra = h_file->f_ra; */
10328 +       if (!err)
10329 +               return 0; /* success */
10330 +
10331 +       /* close all */
10332 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10333 +               au_set_h_fptr(file, bindex, NULL);
10334 +       au_set_fbtop(file, -1);
10335 +       au_set_fbbot_dir(file, -1);
10336 +
10337 +       return err;
10338 +}
10339 +
10340 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10341 +                        struct file *file)
10342 +{
10343 +       int err;
10344 +       struct super_block *sb;
10345 +       struct au_fidir *fidir;
10346 +
10347 +       err = -ENOMEM;
10348 +       sb = file->f_path.dentry->d_sb;
10349 +       si_read_lock(sb, AuLock_FLUSH);
10350 +       fidir = au_fidir_alloc(sb);
10351 +       if (fidir) {
10352 +               struct au_do_open_args args = {
10353 +                       .open   = do_open_dir,
10354 +                       .fidir  = fidir
10355 +               };
10356 +               err = au_do_open(file, &args);
10357 +               if (unlikely(err))
10358 +                       au_kfree_rcu(fidir);
10359 +       }
10360 +       si_read_unlock(sb);
10361 +       return err;
10362 +}
10363 +
10364 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10365 +                           struct file *file)
10366 +{
10367 +       struct au_vdir *vdir_cache;
10368 +       struct au_finfo *finfo;
10369 +       struct au_fidir *fidir;
10370 +       struct au_hfile *hf;
10371 +       aufs_bindex_t bindex, bbot;
10372 +
10373 +       finfo = au_fi(file);
10374 +       fidir = finfo->fi_hdir;
10375 +       if (fidir) {
10376 +               au_hbl_del(&finfo->fi_hlist,
10377 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10378 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10379 +               if (vdir_cache)
10380 +                       au_vdir_free(vdir_cache);
10381 +
10382 +               bindex = finfo->fi_btop;
10383 +               if (bindex >= 0) {
10384 +                       hf = fidir->fd_hfile + bindex;
10385 +                       /*
10386 +                        * calls fput() instead of filp_close(),
10387 +                        * since no dnotify or lock for the lower file.
10388 +                        */
10389 +                       bbot = fidir->fd_bbot;
10390 +                       for (; bindex <= bbot; bindex++, hf++)
10391 +                               if (hf->hf_file)
10392 +                                       au_hfput(hf, /*execed*/0);
10393 +               }
10394 +               au_kfree_rcu(fidir);
10395 +               finfo->fi_hdir = NULL;
10396 +       }
10397 +       au_finfo_fin(file);
10398 +       return 0;
10399 +}
10400 +
10401 +/* ---------------------------------------------------------------------- */
10402 +
10403 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10404 +{
10405 +       int err;
10406 +       aufs_bindex_t bindex, bbot;
10407 +       struct file *h_file;
10408 +
10409 +       err = 0;
10410 +       bbot = au_fbbot_dir(file);
10411 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10412 +               h_file = au_hf_dir(file, bindex);
10413 +               if (h_file)
10414 +                       err = vfsub_flush(h_file, id);
10415 +       }
10416 +       return err;
10417 +}
10418 +
10419 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10420 +{
10421 +       return au_do_flush(file, id, au_do_flush_dir);
10422 +}
10423 +
10424 +/* ---------------------------------------------------------------------- */
10425 +
10426 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10427 +{
10428 +       int err;
10429 +       aufs_bindex_t bbot, bindex;
10430 +       struct inode *inode;
10431 +       struct super_block *sb;
10432 +
10433 +       err = 0;
10434 +       sb = dentry->d_sb;
10435 +       inode = d_inode(dentry);
10436 +       IMustLock(inode);
10437 +       bbot = au_dbbot(dentry);
10438 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10439 +               struct path h_path;
10440 +
10441 +               if (au_test_ro(sb, bindex, inode))
10442 +                       continue;
10443 +               h_path.dentry = au_h_dptr(dentry, bindex);
10444 +               if (!h_path.dentry)
10445 +                       continue;
10446 +
10447 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10448 +               err = vfsub_fsync(NULL, &h_path, datasync);
10449 +       }
10450 +
10451 +       return err;
10452 +}
10453 +
10454 +static int au_do_fsync_dir(struct file *file, int datasync)
10455 +{
10456 +       int err;
10457 +       aufs_bindex_t bbot, bindex;
10458 +       struct file *h_file;
10459 +       struct super_block *sb;
10460 +       struct inode *inode;
10461 +
10462 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10463 +       if (unlikely(err))
10464 +               goto out;
10465 +
10466 +       inode = file_inode(file);
10467 +       sb = inode->i_sb;
10468 +       bbot = au_fbbot_dir(file);
10469 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10470 +               h_file = au_hf_dir(file, bindex);
10471 +               if (!h_file || au_test_ro(sb, bindex, inode))
10472 +                       continue;
10473 +
10474 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10475 +       }
10476 +
10477 +out:
10478 +       return err;
10479 +}
10480 +
10481 +/*
10482 + * @file may be NULL
10483 + */
10484 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10485 +                         int datasync)
10486 +{
10487 +       int err;
10488 +       struct dentry *dentry;
10489 +       struct inode *inode;
10490 +       struct super_block *sb;
10491 +
10492 +       err = 0;
10493 +       dentry = file->f_path.dentry;
10494 +       inode = d_inode(dentry);
10495 +       inode_lock(inode);
10496 +       sb = dentry->d_sb;
10497 +       si_noflush_read_lock(sb);
10498 +       if (file)
10499 +               err = au_do_fsync_dir(file, datasync);
10500 +       else {
10501 +               di_write_lock_child(dentry);
10502 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10503 +       }
10504 +       au_cpup_attr_timesizes(inode);
10505 +       di_write_unlock(dentry);
10506 +       if (file)
10507 +               fi_write_unlock(file);
10508 +
10509 +       si_read_unlock(sb);
10510 +       inode_unlock(inode);
10511 +       return err;
10512 +}
10513 +
10514 +/* ---------------------------------------------------------------------- */
10515 +
10516 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10517 +{
10518 +       int err;
10519 +       struct dentry *dentry;
10520 +       struct inode *inode, *h_inode;
10521 +       struct super_block *sb;
10522 +
10523 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10524 +
10525 +       dentry = file->f_path.dentry;
10526 +       inode = d_inode(dentry);
10527 +       IMustLock(inode);
10528 +
10529 +       sb = dentry->d_sb;
10530 +       si_read_lock(sb, AuLock_FLUSH);
10531 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10532 +       if (unlikely(err))
10533 +               goto out;
10534 +       err = au_alive_dir(dentry);
10535 +       if (!err)
10536 +               err = au_vdir_init(file);
10537 +       di_downgrade_lock(dentry, AuLock_IR);
10538 +       if (unlikely(err))
10539 +               goto out_unlock;
10540 +
10541 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10542 +       if (!au_test_nfsd()) {
10543 +               err = au_vdir_fill_de(file, ctx);
10544 +               fsstack_copy_attr_atime(inode, h_inode);
10545 +       } else {
10546 +               /*
10547 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10548 +                * encode_fh() and others.
10549 +                */
10550 +               atomic_inc(&h_inode->i_count);
10551 +               di_read_unlock(dentry, AuLock_IR);
10552 +               si_read_unlock(sb);
10553 +               err = au_vdir_fill_de(file, ctx);
10554 +               fsstack_copy_attr_atime(inode, h_inode);
10555 +               fi_write_unlock(file);
10556 +               iput(h_inode);
10557 +
10558 +               AuTraceErr(err);
10559 +               return err;
10560 +       }
10561 +
10562 +out_unlock:
10563 +       di_read_unlock(dentry, AuLock_IR);
10564 +       fi_write_unlock(file);
10565 +out:
10566 +       si_read_unlock(sb);
10567 +       return err;
10568 +}
10569 +
10570 +/* ---------------------------------------------------------------------- */
10571 +
10572 +#define AuTestEmpty_WHONLY     1
10573 +#define AuTestEmpty_CALLED     (1 << 1)
10574 +#define AuTestEmpty_SHWH       (1 << 2)
10575 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10576 +#define au_fset_testempty(flags, name) \
10577 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10578 +#define au_fclr_testempty(flags, name) \
10579 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10580 +
10581 +#ifndef CONFIG_AUFS_SHWH
10582 +#undef AuTestEmpty_SHWH
10583 +#define AuTestEmpty_SHWH       0
10584 +#endif
10585 +
10586 +struct test_empty_arg {
10587 +       struct dir_context ctx;
10588 +       struct au_nhash *whlist;
10589 +       unsigned int flags;
10590 +       int err;
10591 +       aufs_bindex_t bindex;
10592 +};
10593 +
10594 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10595 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10596 +                        unsigned int d_type)
10597 +{
10598 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10599 +                                                 ctx);
10600 +       char *name = (void *)__name;
10601 +
10602 +       arg->err = 0;
10603 +       au_fset_testempty(arg->flags, CALLED);
10604 +       /* smp_mb(); */
10605 +       if (name[0] == '.'
10606 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10607 +               goto out; /* success */
10608 +
10609 +       if (namelen <= AUFS_WH_PFX_LEN
10610 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10611 +               if (au_ftest_testempty(arg->flags, WHONLY)
10612 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10613 +                       arg->err = -ENOTEMPTY;
10614 +               goto out;
10615 +       }
10616 +
10617 +       name += AUFS_WH_PFX_LEN;
10618 +       namelen -= AUFS_WH_PFX_LEN;
10619 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10620 +               arg->err = au_nhash_append_wh
10621 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10622 +                        au_ftest_testempty(arg->flags, SHWH));
10623 +
10624 +out:
10625 +       /* smp_mb(); */
10626 +       AuTraceErr(arg->err);
10627 +       return arg->err;
10628 +}
10629 +
10630 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10631 +{
10632 +       int err;
10633 +       struct file *h_file;
10634 +       struct au_branch *br;
10635 +
10636 +       h_file = au_h_open(dentry, arg->bindex,
10637 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10638 +                          /*file*/NULL, /*force_wr*/0);
10639 +       err = PTR_ERR(h_file);
10640 +       if (IS_ERR(h_file))
10641 +               goto out;
10642 +
10643 +       err = 0;
10644 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10645 +           && !file_inode(h_file)->i_nlink)
10646 +               goto out_put;
10647 +
10648 +       do {
10649 +               arg->err = 0;
10650 +               au_fclr_testempty(arg->flags, CALLED);
10651 +               /* smp_mb(); */
10652 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10653 +               if (err >= 0)
10654 +                       err = arg->err;
10655 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10656 +
10657 +out_put:
10658 +       fput(h_file);
10659 +       br = au_sbr(dentry->d_sb, arg->bindex);
10660 +       au_lcnt_dec(&br->br_nfiles);
10661 +out:
10662 +       return err;
10663 +}
10664 +
10665 +struct do_test_empty_args {
10666 +       int *errp;
10667 +       struct dentry *dentry;
10668 +       struct test_empty_arg *arg;
10669 +};
10670 +
10671 +static void call_do_test_empty(void *args)
10672 +{
10673 +       struct do_test_empty_args *a = args;
10674 +       *a->errp = do_test_empty(a->dentry, a->arg);
10675 +}
10676 +
10677 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10678 +{
10679 +       int err, wkq_err;
10680 +       struct dentry *h_dentry;
10681 +       struct inode *h_inode;
10682 +
10683 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10684 +       h_inode = d_inode(h_dentry);
10685 +       /* todo: i_mode changes anytime? */
10686 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10687 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10688 +       inode_unlock_shared(h_inode);
10689 +       if (!err)
10690 +               err = do_test_empty(dentry, arg);
10691 +       else {
10692 +               struct do_test_empty_args args = {
10693 +                       .errp   = &err,
10694 +                       .dentry = dentry,
10695 +                       .arg    = arg
10696 +               };
10697 +               unsigned int flags = arg->flags;
10698 +
10699 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10700 +               if (unlikely(wkq_err))
10701 +                       err = wkq_err;
10702 +               arg->flags = flags;
10703 +       }
10704 +
10705 +       return err;
10706 +}
10707 +
10708 +int au_test_empty_lower(struct dentry *dentry)
10709 +{
10710 +       int err;
10711 +       unsigned int rdhash;
10712 +       aufs_bindex_t bindex, btop, btail;
10713 +       struct au_nhash whlist;
10714 +       struct test_empty_arg arg = {
10715 +               .ctx = {
10716 +                       .actor = test_empty_cb
10717 +               }
10718 +       };
10719 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10720 +
10721 +       SiMustAnyLock(dentry->d_sb);
10722 +
10723 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10724 +       if (!rdhash)
10725 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10726 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10727 +       if (unlikely(err))
10728 +               goto out;
10729 +
10730 +       arg.flags = 0;
10731 +       arg.whlist = &whlist;
10732 +       btop = au_dbtop(dentry);
10733 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10734 +               au_fset_testempty(arg.flags, SHWH);
10735 +       test_empty = do_test_empty;
10736 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10737 +               test_empty = sio_test_empty;
10738 +       arg.bindex = btop;
10739 +       err = test_empty(dentry, &arg);
10740 +       if (unlikely(err))
10741 +               goto out_whlist;
10742 +
10743 +       au_fset_testempty(arg.flags, WHONLY);
10744 +       btail = au_dbtaildir(dentry);
10745 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10746 +               struct dentry *h_dentry;
10747 +
10748 +               h_dentry = au_h_dptr(dentry, bindex);
10749 +               if (h_dentry && d_is_positive(h_dentry)) {
10750 +                       arg.bindex = bindex;
10751 +                       err = test_empty(dentry, &arg);
10752 +               }
10753 +       }
10754 +
10755 +out_whlist:
10756 +       au_nhash_wh_free(&whlist);
10757 +out:
10758 +       return err;
10759 +}
10760 +
10761 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10762 +{
10763 +       int err;
10764 +       struct test_empty_arg arg = {
10765 +               .ctx = {
10766 +                       .actor = test_empty_cb
10767 +               }
10768 +       };
10769 +       aufs_bindex_t bindex, btail;
10770 +
10771 +       err = 0;
10772 +       arg.whlist = whlist;
10773 +       arg.flags = AuTestEmpty_WHONLY;
10774 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10775 +               au_fset_testempty(arg.flags, SHWH);
10776 +       btail = au_dbtaildir(dentry);
10777 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10778 +               struct dentry *h_dentry;
10779 +
10780 +               h_dentry = au_h_dptr(dentry, bindex);
10781 +               if (h_dentry && d_is_positive(h_dentry)) {
10782 +                       arg.bindex = bindex;
10783 +                       err = sio_test_empty(dentry, &arg);
10784 +               }
10785 +       }
10786 +
10787 +       return err;
10788 +}
10789 +
10790 +/* ---------------------------------------------------------------------- */
10791 +
10792 +const struct file_operations aufs_dir_fop = {
10793 +       .owner          = THIS_MODULE,
10794 +       .llseek         = default_llseek,
10795 +       .read           = generic_read_dir,
10796 +       .iterate_shared = aufs_iterate_shared,
10797 +       .unlocked_ioctl = aufs_ioctl_dir,
10798 +#ifdef CONFIG_COMPAT
10799 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10800 +#endif
10801 +       .open           = aufs_open_dir,
10802 +       .release        = aufs_release_dir,
10803 +       .flush          = aufs_flush_dir,
10804 +       .fsync          = aufs_fsync_dir
10805 +};
10806 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10807 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10808 +++ linux/fs/aufs/dir.h 2020-08-03 09:14:46.095748745 +0200
10809 @@ -0,0 +1,134 @@
10810 +/* SPDX-License-Identifier: GPL-2.0 */
10811 +/*
10812 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10813 + *
10814 + * This program, aufs is free software; you can redistribute it and/or modify
10815 + * it under the terms of the GNU General Public License as published by
10816 + * the Free Software Foundation; either version 2 of the License, or
10817 + * (at your option) any later version.
10818 + *
10819 + * This program is distributed in the hope that it will be useful,
10820 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10821 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10822 + * GNU General Public License for more details.
10823 + *
10824 + * You should have received a copy of the GNU General Public License
10825 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10826 + */
10827 +
10828 +/*
10829 + * directory operations
10830 + */
10831 +
10832 +#ifndef __AUFS_DIR_H__
10833 +#define __AUFS_DIR_H__
10834 +
10835 +#ifdef __KERNEL__
10836 +
10837 +#include <linux/fs.h>
10838 +
10839 +/* ---------------------------------------------------------------------- */
10840 +
10841 +/* need to be faster and smaller */
10842 +
10843 +struct au_nhash {
10844 +       unsigned int            nh_num;
10845 +       struct hlist_head       *nh_head;
10846 +};
10847 +
10848 +struct au_vdir_destr {
10849 +       unsigned char   len;
10850 +       unsigned char   name[];
10851 +} __packed;
10852 +
10853 +struct au_vdir_dehstr {
10854 +       struct hlist_node       hash;
10855 +       struct au_vdir_destr    *str;
10856 +       struct rcu_head         rcu;
10857 +} ____cacheline_aligned_in_smp;
10858 +
10859 +struct au_vdir_de {
10860 +       ino_t                   de_ino;
10861 +       unsigned char           de_type;
10862 +       /* caution: packed */
10863 +       struct au_vdir_destr    de_str;
10864 +} __packed;
10865 +
10866 +struct au_vdir_wh {
10867 +       struct hlist_node       wh_hash;
10868 +#ifdef CONFIG_AUFS_SHWH
10869 +       ino_t                   wh_ino;
10870 +       aufs_bindex_t           wh_bindex;
10871 +       unsigned char           wh_type;
10872 +#else
10873 +       aufs_bindex_t           wh_bindex;
10874 +#endif
10875 +       /* caution: packed */
10876 +       struct au_vdir_destr    wh_str;
10877 +} __packed;
10878 +
10879 +union au_vdir_deblk_p {
10880 +       unsigned char           *deblk;
10881 +       struct au_vdir_de       *de;
10882 +};
10883 +
10884 +struct au_vdir {
10885 +       unsigned char   **vd_deblk;
10886 +       unsigned long   vd_nblk;
10887 +       struct {
10888 +               unsigned long           ul;
10889 +               union au_vdir_deblk_p   p;
10890 +       } vd_last;
10891 +
10892 +       u64             vd_version;
10893 +       unsigned int    vd_deblk_sz;
10894 +       unsigned long   vd_jiffy;
10895 +       struct rcu_head rcu;
10896 +} ____cacheline_aligned_in_smp;
10897 +
10898 +/* ---------------------------------------------------------------------- */
10899 +
10900 +/* dir.c */
10901 +extern const struct file_operations aufs_dir_fop;
10902 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10903 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10904 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10905 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10906 +int au_test_empty_lower(struct dentry *dentry);
10907 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10908 +
10909 +/* vdir.c */
10910 +unsigned int au_rdhash_est(loff_t sz);
10911 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10912 +void au_nhash_wh_free(struct au_nhash *whlist);
10913 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10914 +                           int limit);
10915 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10916 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10917 +                      unsigned int d_type, aufs_bindex_t bindex,
10918 +                      unsigned char shwh);
10919 +void au_vdir_free(struct au_vdir *vdir);
10920 +int au_vdir_init(struct file *file);
10921 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10922 +
10923 +/* ioctl.c */
10924 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10925 +
10926 +#ifdef CONFIG_AUFS_RDU
10927 +/* rdu.c */
10928 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10929 +#ifdef CONFIG_COMPAT
10930 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10931 +                        unsigned long arg);
10932 +#endif
10933 +#else
10934 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10935 +       unsigned int cmd, unsigned long arg)
10936 +#ifdef CONFIG_COMPAT
10937 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10938 +       unsigned int cmd, unsigned long arg)
10939 +#endif
10940 +#endif
10941 +
10942 +#endif /* __KERNEL__ */
10943 +#endif /* __AUFS_DIR_H__ */
10944 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10945 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10946 +++ linux/fs/aufs/dirren.c      2020-08-03 09:14:46.095748745 +0200
10947 @@ -0,0 +1,1316 @@
10948 +// SPDX-License-Identifier: GPL-2.0
10949 +/*
10950 + * Copyright (C) 2017-2020 Junjiro R. Okajima
10951 + *
10952 + * This program, aufs is free software; you can redistribute it and/or modify
10953 + * it under the terms of the GNU General Public License as published by
10954 + * the Free Software Foundation; either version 2 of the License, or
10955 + * (at your option) any later version.
10956 + *
10957 + * This program is distributed in the hope that it will be useful,
10958 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10959 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10960 + * GNU General Public License for more details.
10961 + *
10962 + * You should have received a copy of the GNU General Public License
10963 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10964 + */
10965 +
10966 +/*
10967 + * special handling in renaming a directory
10968 + * in order to support looking-up the before-renamed name on the lower readonly
10969 + * branches
10970 + */
10971 +
10972 +#include <linux/byteorder/generic.h>
10973 +#include "aufs.h"
10974 +
10975 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10976 +{
10977 +       int idx;
10978 +
10979 +       idx = au_dr_ihash(ent->dr_h_ino);
10980 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10981 +}
10982 +
10983 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10984 +{
10985 +       int ret, i;
10986 +       struct hlist_bl_head *hbl;
10987 +
10988 +       ret = 1;
10989 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10990 +               hbl = dr->dr_h_ino + i;
10991 +               hlist_bl_lock(hbl);
10992 +               ret &= hlist_bl_empty(hbl);
10993 +               hlist_bl_unlock(hbl);
10994 +       }
10995 +
10996 +       return ret;
10997 +}
10998 +
10999 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11000 +{
11001 +       struct au_dr_hino *found, *ent;
11002 +       struct hlist_bl_head *hbl;
11003 +       struct hlist_bl_node *pos;
11004 +       int idx;
11005 +
11006 +       found = NULL;
11007 +       idx = au_dr_ihash(ino);
11008 +       hbl = dr->dr_h_ino + idx;
11009 +       hlist_bl_lock(hbl);
11010 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11011 +               if (ent->dr_h_ino == ino) {
11012 +                       found = ent;
11013 +                       break;
11014 +               }
11015 +       hlist_bl_unlock(hbl);
11016 +
11017 +       return found;
11018 +}
11019 +
11020 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11021 +                       struct au_dr_hino *add_ent)
11022 +{
11023 +       int found, idx;
11024 +       struct hlist_bl_head *hbl;
11025 +       struct hlist_bl_node *pos;
11026 +       struct au_dr_hino *ent;
11027 +
11028 +       found = 0;
11029 +       idx = au_dr_ihash(ino);
11030 +       hbl = dr->dr_h_ino + idx;
11031 +#if 0 /* debug print */
11032 +       {
11033 +               struct hlist_bl_node *tmp;
11034 +
11035 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11036 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11037 +       }
11038 +#endif
11039 +       hlist_bl_lock(hbl);
11040 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11041 +               if (ent->dr_h_ino == ino) {
11042 +                       found = 1;
11043 +                       break;
11044 +               }
11045 +       if (!found && add_ent)
11046 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11047 +       hlist_bl_unlock(hbl);
11048 +
11049 +       if (!found && add_ent)
11050 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11051 +
11052 +       return found;
11053 +}
11054 +
11055 +void au_dr_hino_free(struct au_dr_br *dr)
11056 +{
11057 +       int i;
11058 +       struct hlist_bl_head *hbl;
11059 +       struct hlist_bl_node *pos, *tmp;
11060 +       struct au_dr_hino *ent;
11061 +
11062 +       /* SiMustWriteLock(sb); */
11063 +
11064 +       for (i = 0; i < AuDirren_NHASH; i++) {
11065 +               hbl = dr->dr_h_ino + i;
11066 +               /* no spinlock since sbinfo must be write-locked */
11067 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11068 +                       au_kfree_rcu(ent);
11069 +               INIT_HLIST_BL_HEAD(hbl);
11070 +       }
11071 +}
11072 +
11073 +/* returns the number of inodes or an error */
11074 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11075 +                           struct file *hinofile)
11076 +{
11077 +       int err, i;
11078 +       ssize_t ssz;
11079 +       loff_t pos, oldsize;
11080 +       __be64 u64;
11081 +       struct inode *hinoinode;
11082 +       struct hlist_bl_head *hbl;
11083 +       struct hlist_bl_node *n1, *n2;
11084 +       struct au_dr_hino *ent;
11085 +
11086 +       SiMustWriteLock(sb);
11087 +       AuDebugOn(!au_br_writable(br->br_perm));
11088 +
11089 +       hinoinode = file_inode(hinofile);
11090 +       oldsize = i_size_read(hinoinode);
11091 +
11092 +       err = 0;
11093 +       pos = 0;
11094 +       hbl = br->br_dirren.dr_h_ino;
11095 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11096 +               /* no bit-lock since sbinfo must be write-locked */
11097 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11098 +                       AuDbg("hi%llu, %pD2\n",
11099 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11100 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11101 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11102 +                       if (ssz == sizeof(u64))
11103 +                               continue;
11104 +
11105 +                       /* write error */
11106 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11107 +                       err = -ENOSPC;
11108 +                       if (ssz < 0)
11109 +                               err = ssz;
11110 +                       break;
11111 +               }
11112 +       }
11113 +       /* regardless the error */
11114 +       if (pos < oldsize) {
11115 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11116 +               AuTraceErr(err);
11117 +       }
11118 +
11119 +       AuTraceErr(err);
11120 +       return err;
11121 +}
11122 +
11123 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11124 +{
11125 +       int err, hidx;
11126 +       ssize_t ssz;
11127 +       size_t sz, n;
11128 +       loff_t pos;
11129 +       uint64_t u64;
11130 +       struct au_dr_hino *ent;
11131 +       struct inode *hinoinode;
11132 +       struct hlist_bl_head *hbl;
11133 +
11134 +       err = 0;
11135 +       pos = 0;
11136 +       hbl = dr->dr_h_ino;
11137 +       hinoinode = file_inode(hinofile);
11138 +       sz = i_size_read(hinoinode);
11139 +       AuDebugOn(sz % sizeof(u64));
11140 +       n = sz / sizeof(u64);
11141 +       while (n--) {
11142 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11143 +               if (unlikely(ssz != sizeof(u64))) {
11144 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11145 +                       err = -EINVAL;
11146 +                       if (ssz < 0)
11147 +                               err = ssz;
11148 +                       goto out_free;
11149 +               }
11150 +
11151 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11152 +               if (!ent) {
11153 +                       err = -ENOMEM;
11154 +                       AuTraceErr(err);
11155 +                       goto out_free;
11156 +               }
11157 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11158 +               AuDbg("hi%llu, %pD2\n",
11159 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11160 +               hidx = au_dr_ihash(ent->dr_h_ino);
11161 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11162 +       }
11163 +       goto out; /* success */
11164 +
11165 +out_free:
11166 +       au_dr_hino_free(dr);
11167 +out:
11168 +       AuTraceErr(err);
11169 +       return err;
11170 +}
11171 +
11172 +/*
11173 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11174 + * @path is a switch to distinguish load and store.
11175 + */
11176 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11177 +                     struct au_branch *br, const struct path *path)
11178 +{
11179 +       int err, flags;
11180 +       unsigned char load, suspend;
11181 +       struct file *hinofile;
11182 +       struct au_hinode *hdir;
11183 +       struct inode *dir, *delegated;
11184 +       struct path hinopath;
11185 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11186 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11187 +
11188 +       AuDebugOn(bindex < 0 && !br);
11189 +       AuDebugOn(bindex >= 0 && br);
11190 +
11191 +       err = -EINVAL;
11192 +       suspend = !br;
11193 +       if (suspend)
11194 +               br = au_sbr(sb, bindex);
11195 +       load = !!path;
11196 +       if (!load) {
11197 +               path = &br->br_path;
11198 +               AuDebugOn(!au_br_writable(br->br_perm));
11199 +               if (unlikely(!au_br_writable(br->br_perm)))
11200 +                       goto out;
11201 +       }
11202 +
11203 +       hdir = NULL;
11204 +       if (suspend) {
11205 +               dir = d_inode(sb->s_root);
11206 +               hdir = au_hinode(au_ii(dir), bindex);
11207 +               dir = hdir->hi_inode;
11208 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11209 +       } else {
11210 +               dir = d_inode(path->dentry);
11211 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11212 +       }
11213 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11214 +       err = PTR_ERR(hinopath.dentry);
11215 +       if (IS_ERR(hinopath.dentry))
11216 +               goto out_unlock;
11217 +
11218 +       err = 0;
11219 +       flags = O_RDONLY;
11220 +       if (load) {
11221 +               if (d_is_negative(hinopath.dentry))
11222 +                       goto out_dput; /* success */
11223 +       } else {
11224 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11225 +                       if (d_is_positive(hinopath.dentry)) {
11226 +                               delegated = NULL;
11227 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11228 +                                                  /*force*/0);
11229 +                               AuTraceErr(err);
11230 +                               if (unlikely(err))
11231 +                                       pr_err("ignored err %d, %pd2\n",
11232 +                                              err, hinopath.dentry);
11233 +                               if (unlikely(err == -EWOULDBLOCK))
11234 +                                       iput(delegated);
11235 +                               err = 0;
11236 +                       }
11237 +                       goto out_dput;
11238 +               } else if (!d_is_positive(hinopath.dentry)) {
11239 +                       err = vfsub_create(dir, &hinopath, 0600,
11240 +                                          /*want_excl*/false);
11241 +                       AuTraceErr(err);
11242 +                       if (unlikely(err))
11243 +                               goto out_dput;
11244 +               }
11245 +               flags = O_WRONLY;
11246 +       }
11247 +       hinopath.mnt = path->mnt;
11248 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11249 +       if (suspend)
11250 +               au_hn_inode_unlock(hdir);
11251 +       else
11252 +               inode_unlock(dir);
11253 +       dput(hinopath.dentry);
11254 +       AuTraceErrPtr(hinofile);
11255 +       if (IS_ERR(hinofile)) {
11256 +               err = PTR_ERR(hinofile);
11257 +               goto out;
11258 +       }
11259 +
11260 +       if (load)
11261 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11262 +       else
11263 +               err = au_dr_hino_store(sb, br, hinofile);
11264 +       fput(hinofile);
11265 +       goto out;
11266 +
11267 +out_dput:
11268 +       dput(hinopath.dentry);
11269 +out_unlock:
11270 +       if (suspend)
11271 +               au_hn_inode_unlock(hdir);
11272 +       else
11273 +               inode_unlock(dir);
11274 +out:
11275 +       AuTraceErr(err);
11276 +       return err;
11277 +}
11278 +
11279 +/* ---------------------------------------------------------------------- */
11280 +
11281 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11282 +{
11283 +       int err;
11284 +       struct kstatfs kstfs;
11285 +       dev_t dev;
11286 +       struct dentry *dentry;
11287 +       struct super_block *sb;
11288 +
11289 +       err = vfs_statfs((void *)path, &kstfs);
11290 +       AuTraceErr(err);
11291 +       if (unlikely(err))
11292 +               goto out;
11293 +
11294 +       /* todo: support for UUID */
11295 +
11296 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11297 +               brid->type = AuBrid_FSID;
11298 +               brid->fsid = kstfs.f_fsid;
11299 +       } else {
11300 +               dentry = path->dentry;
11301 +               sb = dentry->d_sb;
11302 +               dev = sb->s_dev;
11303 +               if (dev) {
11304 +                       brid->type = AuBrid_DEV;
11305 +                       brid->dev = dev;
11306 +               }
11307 +       }
11308 +
11309 +out:
11310 +       return err;
11311 +}
11312 +
11313 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11314 +                 const struct path *path)
11315 +{
11316 +       int err, i;
11317 +       struct au_dr_br *dr;
11318 +       struct hlist_bl_head *hbl;
11319 +
11320 +       dr = &br->br_dirren;
11321 +       hbl = dr->dr_h_ino;
11322 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11323 +               INIT_HLIST_BL_HEAD(hbl);
11324 +
11325 +       err = au_dr_brid_init(&dr->dr_brid, path);
11326 +       if (unlikely(err))
11327 +               goto out;
11328 +
11329 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11330 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11331 +
11332 +out:
11333 +       AuTraceErr(err);
11334 +       return err;
11335 +}
11336 +
11337 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11338 +{
11339 +       int err;
11340 +
11341 +       err = 0;
11342 +       if (au_br_writable(br->br_perm))
11343 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11344 +       if (!err)
11345 +               au_dr_hino_free(&br->br_dirren);
11346 +
11347 +       return err;
11348 +}
11349 +
11350 +/* ---------------------------------------------------------------------- */
11351 +
11352 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11353 +                      char *buf, size_t sz)
11354 +{
11355 +       int err;
11356 +       unsigned int major, minor;
11357 +       char *p;
11358 +
11359 +       p = buf;
11360 +       err = snprintf(p, sz, "%d_", brid->type);
11361 +       AuDebugOn(err > sz);
11362 +       p += err;
11363 +       sz -= err;
11364 +       switch (brid->type) {
11365 +       case AuBrid_Unset:
11366 +               return -EINVAL;
11367 +       case AuBrid_UUID:
11368 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11369 +               break;
11370 +       case AuBrid_FSID:
11371 +               err = snprintf(p, sz, "%08x-%08x",
11372 +                              brid->fsid.val[0], brid->fsid.val[1]);
11373 +               break;
11374 +       case AuBrid_DEV:
11375 +               major = MAJOR(brid->dev);
11376 +               minor = MINOR(brid->dev);
11377 +               if (major <= 0xff && minor <= 0xff)
11378 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11379 +               else
11380 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11381 +               break;
11382 +       }
11383 +       AuDebugOn(err > sz);
11384 +       p += err;
11385 +       sz -= err;
11386 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11387 +       AuDebugOn(err > sz);
11388 +       p += err;
11389 +       sz -= err;
11390 +
11391 +       return p - buf;
11392 +}
11393 +
11394 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11395 +{
11396 +       int rlen;
11397 +       struct dentry *br_dentry;
11398 +       struct inode *br_inode;
11399 +
11400 +       br_dentry = au_br_dentry(br);
11401 +       br_inode = d_inode(br_dentry);
11402 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11403 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11404 +       AuDebugOn(rlen > len);
11405 +
11406 +       return rlen;
11407 +}
11408 +
11409 +/* ---------------------------------------------------------------------- */
11410 +
11411 +/*
11412 + * from the given @h_dentry, construct drinfo at @*fdata.
11413 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11414 + * @allocated.
11415 + */
11416 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11417 +                              struct dentry *h_dentry,
11418 +                              unsigned char *allocated)
11419 +{
11420 +       int err, v;
11421 +       struct au_drinfo_fdata *f, *p;
11422 +       struct au_drinfo *drinfo;
11423 +       struct inode *h_inode;
11424 +       struct qstr *qname;
11425 +
11426 +       err = 0;
11427 +       f = *fdata;
11428 +       h_inode = d_inode(h_dentry);
11429 +       qname = &h_dentry->d_name;
11430 +       drinfo = &f->drinfo;
11431 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11432 +       drinfo->oldnamelen = qname->len;
11433 +       if (*allocated < sizeof(*f) + qname->len) {
11434 +               v = roundup_pow_of_two(*allocated + qname->len);
11435 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11436 +               if (unlikely(!p)) {
11437 +                       err = -ENOMEM;
11438 +                       AuTraceErr(err);
11439 +                       goto out;
11440 +               }
11441 +               f = p;
11442 +               *fdata = f;
11443 +               *allocated = v;
11444 +               drinfo = &f->drinfo;
11445 +       }
11446 +       memcpy(drinfo->oldname, qname->name, qname->len);
11447 +       AuDbg("i%llu, %.*s\n",
11448 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11449 +             drinfo->oldname);
11450 +
11451 +out:
11452 +       AuTraceErr(err);
11453 +       return err;
11454 +}
11455 +
11456 +/* callers have to free the return value */
11457 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11458 +{
11459 +       struct au_drinfo *ret, *drinfo;
11460 +       struct au_drinfo_fdata fdata;
11461 +       int len;
11462 +       loff_t pos;
11463 +       ssize_t ssz;
11464 +
11465 +       ret = ERR_PTR(-EIO);
11466 +       pos = 0;
11467 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11468 +       if (unlikely(ssz != sizeof(fdata))) {
11469 +               AuIOErr("ssz %zd, %u, %pD2\n",
11470 +                       ssz, (unsigned int)sizeof(fdata), file);
11471 +               goto out;
11472 +       }
11473 +
11474 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11475 +       switch (fdata.magic) {
11476 +       case AUFS_DRINFO_MAGIC_V1:
11477 +               break;
11478 +       default:
11479 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11480 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11481 +               goto out;
11482 +       }
11483 +
11484 +       drinfo = &fdata.drinfo;
11485 +       len = drinfo->oldnamelen;
11486 +       if (!len) {
11487 +               AuIOErr("broken drinfo %pD2\n", file);
11488 +               goto out;
11489 +       }
11490 +
11491 +       ret = NULL;
11492 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11493 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11494 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11495 +                     (unsigned long long)drinfo->ino,
11496 +                     (unsigned long long)h_ino, file);
11497 +               goto out; /* success */
11498 +       }
11499 +
11500 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11501 +       if (unlikely(!ret)) {
11502 +               ret = ERR_PTR(-ENOMEM);
11503 +               AuTraceErrPtr(ret);
11504 +               goto out;
11505 +       }
11506 +
11507 +       *ret = *drinfo;
11508 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11509 +       if (unlikely(ssz != len)) {
11510 +               au_kfree_rcu(ret);
11511 +               ret = ERR_PTR(-EIO);
11512 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11513 +               goto out;
11514 +       }
11515 +
11516 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11517 +
11518 +out:
11519 +       return ret;
11520 +}
11521 +
11522 +/* ---------------------------------------------------------------------- */
11523 +
11524 +/* in order to be revertible */
11525 +struct au_drinfo_rev_elm {
11526 +       int                     created;
11527 +       struct dentry           *info_dentry;
11528 +       struct au_drinfo        *info_last;
11529 +};
11530 +
11531 +struct au_drinfo_rev {
11532 +       unsigned char                   already;
11533 +       aufs_bindex_t                   nelm;
11534 +       struct au_drinfo_rev_elm        elm[];
11535 +};
11536 +
11537 +/* todo: isn't it too large? */
11538 +struct au_drinfo_store {
11539 +       struct path h_ppath;
11540 +       struct dentry *h_dentry;
11541 +       struct au_drinfo_fdata *fdata;
11542 +       char *infoname;                 /* inside of whname, just after PFX */
11543 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11544 +       aufs_bindex_t btgt, btail;
11545 +       unsigned char no_sio,
11546 +               allocated,              /* current size of *fdata */
11547 +               infonamelen,            /* room size for p */
11548 +               whnamelen,              /* length of the generated name */
11549 +               renameback;             /* renamed back */
11550 +};
11551 +
11552 +/* on rename(2) error, the caller should revert it using @elm */
11553 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11554 +                             struct au_drinfo_rev_elm *elm)
11555 +{
11556 +       int err, len;
11557 +       ssize_t ssz;
11558 +       loff_t pos;
11559 +       struct path infopath = {
11560 +               .mnt = w->h_ppath.mnt
11561 +       };
11562 +       struct inode *h_dir, *h_inode, *delegated;
11563 +       struct file *infofile;
11564 +       struct qstr *qname;
11565 +
11566 +       AuDebugOn(elm
11567 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11568 +
11569 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11570 +                                              w->whnamelen);
11571 +       AuTraceErrPtr(infopath.dentry);
11572 +       if (IS_ERR(infopath.dentry)) {
11573 +               err = PTR_ERR(infopath.dentry);
11574 +               goto out;
11575 +       }
11576 +
11577 +       err = 0;
11578 +       h_dir = d_inode(w->h_ppath.dentry);
11579 +       if (elm && d_is_negative(infopath.dentry)) {
11580 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11581 +               AuTraceErr(err);
11582 +               if (unlikely(err))
11583 +                       goto out_dput;
11584 +               elm->created = 1;
11585 +               elm->info_dentry = dget(infopath.dentry);
11586 +       }
11587 +
11588 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11589 +       AuTraceErrPtr(infofile);
11590 +       if (IS_ERR(infofile)) {
11591 +               err = PTR_ERR(infofile);
11592 +               goto out_dput;
11593 +       }
11594 +
11595 +       h_inode = d_inode(infopath.dentry);
11596 +       if (elm && i_size_read(h_inode)) {
11597 +               h_inode = d_inode(w->h_dentry);
11598 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11599 +               AuTraceErrPtr(elm->info_last);
11600 +               if (IS_ERR(elm->info_last)) {
11601 +                       err = PTR_ERR(elm->info_last);
11602 +                       elm->info_last = NULL;
11603 +                       AuDebugOn(elm->info_dentry);
11604 +                       goto out_fput;
11605 +               }
11606 +       }
11607 +
11608 +       if (elm && w->renameback) {
11609 +               delegated = NULL;
11610 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11611 +               AuTraceErr(err);
11612 +               if (unlikely(err == -EWOULDBLOCK))
11613 +                       iput(delegated);
11614 +               goto out_fput;
11615 +       }
11616 +
11617 +       pos = 0;
11618 +       qname = &w->h_dentry->d_name;
11619 +       len = sizeof(*w->fdata) + qname->len;
11620 +       if (!elm)
11621 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11622 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11623 +       if (ssz == len) {
11624 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11625 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11626 +               goto out_fput; /* success */
11627 +       } else {
11628 +               err = -EIO;
11629 +               if (ssz < 0)
11630 +                       err = ssz;
11631 +               /* the caller should revert it using @elm */
11632 +       }
11633 +
11634 +out_fput:
11635 +       fput(infofile);
11636 +out_dput:
11637 +       dput(infopath.dentry);
11638 +out:
11639 +       AuTraceErr(err);
11640 +       return err;
11641 +}
11642 +
11643 +struct au_call_drinfo_do_store_args {
11644 +       int *errp;
11645 +       struct au_drinfo_store *w;
11646 +       struct au_drinfo_rev_elm *elm;
11647 +};
11648 +
11649 +static void au_call_drinfo_do_store(void *args)
11650 +{
11651 +       struct au_call_drinfo_do_store_args *a = args;
11652 +
11653 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11654 +}
11655 +
11656 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11657 +                              struct au_drinfo_rev_elm *elm)
11658 +{
11659 +       int err, wkq_err;
11660 +
11661 +       if (w->no_sio)
11662 +               err = au_drinfo_do_store(w, elm);
11663 +       else {
11664 +               struct au_call_drinfo_do_store_args a = {
11665 +                       .errp   = &err,
11666 +                       .w      = w,
11667 +                       .elm    = elm
11668 +               };
11669 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11670 +               if (unlikely(wkq_err))
11671 +                       err = wkq_err;
11672 +       }
11673 +       AuTraceErr(err);
11674 +
11675 +       return err;
11676 +}
11677 +
11678 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11679 +                                    aufs_bindex_t btgt)
11680 +{
11681 +       int err;
11682 +
11683 +       memset(w, 0, sizeof(*w));
11684 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11685 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11686 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11687 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11688 +       w->btgt = btgt;
11689 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11690 +
11691 +       err = -ENOMEM;
11692 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11693 +       if (unlikely(!w->fdata)) {
11694 +               AuTraceErr(err);
11695 +               goto out;
11696 +       }
11697 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11698 +       err = 0;
11699 +
11700 +out:
11701 +       return err;
11702 +}
11703 +
11704 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11705 +{
11706 +       au_kfree_rcu(w->fdata);
11707 +}
11708 +
11709 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11710 +                               struct au_drinfo_store *w)
11711 +{
11712 +       struct au_drinfo_rev_elm *elm;
11713 +       struct inode *h_dir, *delegated;
11714 +       int err, nelm;
11715 +       struct path infopath = {
11716 +               .mnt = w->h_ppath.mnt
11717 +       };
11718 +
11719 +       h_dir = d_inode(w->h_ppath.dentry);
11720 +       IMustLock(h_dir);
11721 +
11722 +       err = 0;
11723 +       elm = rev->elm;
11724 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11725 +               AuDebugOn(elm->created && elm->info_last);
11726 +               if (elm->created) {
11727 +                       AuDbg("here\n");
11728 +                       delegated = NULL;
11729 +                       infopath.dentry = elm->info_dentry;
11730 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11731 +                                          !w->no_sio);
11732 +                       AuTraceErr(err);
11733 +                       if (unlikely(err == -EWOULDBLOCK))
11734 +                               iput(delegated);
11735 +                       dput(elm->info_dentry);
11736 +               } else if (elm->info_last) {
11737 +                       AuDbg("here\n");
11738 +                       w->fdata->drinfo = *elm->info_last;
11739 +                       memcpy(w->fdata->drinfo.oldname,
11740 +                              elm->info_last->oldname,
11741 +                              elm->info_last->oldnamelen);
11742 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11743 +                       au_kfree_rcu(elm->info_last);
11744 +               }
11745 +               if (unlikely(err))
11746 +                       AuIOErr("%d, %s\n", err, w->whname);
11747 +               /* go on even if err */
11748 +       }
11749 +}
11750 +
11751 +/* caller has to call au_dr_rename_fin() later */
11752 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11753 +                          struct qstr *dst_name, void *_rev)
11754 +{
11755 +       int err, sz, nelm;
11756 +       aufs_bindex_t bindex, btail;
11757 +       struct au_drinfo_store work;
11758 +       struct au_drinfo_rev *rev, **p;
11759 +       struct au_drinfo_rev_elm *elm;
11760 +       struct super_block *sb;
11761 +       struct au_branch *br;
11762 +       struct au_hinode *hdir;
11763 +
11764 +       err = au_drinfo_store_work_init(&work, btgt);
11765 +       AuTraceErr(err);
11766 +       if (unlikely(err))
11767 +               goto out;
11768 +
11769 +       err = -ENOMEM;
11770 +       btail = au_dbtaildir(dentry);
11771 +       nelm = btail - btgt;
11772 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11773 +       rev = kcalloc(1, sz, GFP_NOFS);
11774 +       if (unlikely(!rev)) {
11775 +               AuTraceErr(err);
11776 +               goto out_args;
11777 +       }
11778 +       rev->nelm = nelm;
11779 +       elm = rev->elm;
11780 +       p = _rev;
11781 +       *p = rev;
11782 +
11783 +       err = 0;
11784 +       sb = dentry->d_sb;
11785 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11786 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11787 +       hdir = au_hi(d_inode(dentry), btgt);
11788 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11789 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11790 +               work.h_dentry = au_h_dptr(dentry, bindex);
11791 +               if (!work.h_dentry)
11792 +                       continue;
11793 +
11794 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11795 +                                         &work.allocated);
11796 +               AuTraceErr(err);
11797 +               if (unlikely(err))
11798 +                       break;
11799 +
11800 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11801 +               br = au_sbr(sb, bindex);
11802 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11803 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11804 +                                                work.infonamelen);
11805 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11806 +                     work.whnamelen, work.whname,
11807 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11808 +                     work.fdata->drinfo.oldnamelen,
11809 +                     work.fdata->drinfo.oldname);
11810 +
11811 +               err = au_drinfo_store_sio(&work, elm);
11812 +               AuTraceErr(err);
11813 +               if (unlikely(err))
11814 +                       break;
11815 +       }
11816 +       if (unlikely(err)) {
11817 +               /* revert all drinfo */
11818 +               au_drinfo_store_rev(rev, &work);
11819 +               au_kfree_try_rcu(rev);
11820 +               *p = NULL;
11821 +       }
11822 +       au_hn_inode_unlock(hdir);
11823 +
11824 +out_args:
11825 +       au_drinfo_store_work_fin(&work);
11826 +out:
11827 +       return err;
11828 +}
11829 +
11830 +/* ---------------------------------------------------------------------- */
11831 +
11832 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11833 +                struct qstr *dst_name, void *_rev)
11834 +{
11835 +       int err, already;
11836 +       ino_t ino;
11837 +       struct super_block *sb;
11838 +       struct au_branch *br;
11839 +       struct au_dr_br *dr;
11840 +       struct dentry *h_dentry;
11841 +       struct inode *h_inode;
11842 +       struct au_dr_hino *ent;
11843 +       struct au_drinfo_rev *rev, **p;
11844 +
11845 +       AuDbg("bindex %d\n", bindex);
11846 +
11847 +       err = -ENOMEM;
11848 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11849 +       if (unlikely(!ent))
11850 +               goto out;
11851 +
11852 +       sb = src->d_sb;
11853 +       br = au_sbr(sb, bindex);
11854 +       dr = &br->br_dirren;
11855 +       h_dentry = au_h_dptr(src, bindex);
11856 +       h_inode = d_inode(h_dentry);
11857 +       ino = h_inode->i_ino;
11858 +       ent->dr_h_ino = ino;
11859 +       already = au_dr_hino_test_add(dr, ino, ent);
11860 +       AuDbg("b%d, hi%llu, already %d\n",
11861 +             bindex, (unsigned long long)ino, already);
11862 +
11863 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11864 +       AuTraceErr(err);
11865 +       if (!err) {
11866 +               p = _rev;
11867 +               rev = *p;
11868 +               rev->already = already;
11869 +               goto out; /* success */
11870 +       }
11871 +
11872 +       /* revert */
11873 +       if (!already)
11874 +               au_dr_hino_del(dr, ent);
11875 +       au_kfree_rcu(ent);
11876 +
11877 +out:
11878 +       AuTraceErr(err);
11879 +       return err;
11880 +}
11881 +
11882 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11883 +{
11884 +       struct au_drinfo_rev *rev;
11885 +       struct au_drinfo_rev_elm *elm;
11886 +       int nelm;
11887 +
11888 +       rev = _rev;
11889 +       elm = rev->elm;
11890 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11891 +               dput(elm->info_dentry);
11892 +               au_kfree_rcu(elm->info_last);
11893 +       }
11894 +       au_kfree_try_rcu(rev);
11895 +}
11896 +
11897 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11898 +{
11899 +       int err;
11900 +       struct au_drinfo_store work;
11901 +       struct au_drinfo_rev *rev = _rev;
11902 +       struct super_block *sb;
11903 +       struct au_branch *br;
11904 +       struct inode *h_inode;
11905 +       struct au_dr_br *dr;
11906 +       struct au_dr_hino *ent;
11907 +
11908 +       err = au_drinfo_store_work_init(&work, btgt);
11909 +       if (unlikely(err))
11910 +               goto out;
11911 +
11912 +       sb = src->d_sb;
11913 +       br = au_sbr(sb, btgt);
11914 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11915 +       work.h_ppath.mnt = au_br_mnt(br);
11916 +       au_drinfo_store_rev(rev, &work);
11917 +       au_drinfo_store_work_fin(&work);
11918 +       if (rev->already)
11919 +               goto out;
11920 +
11921 +       dr = &br->br_dirren;
11922 +       h_inode = d_inode(work.h_ppath.dentry);
11923 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11924 +       BUG_ON(!ent);
11925 +       au_dr_hino_del(dr, ent);
11926 +       au_kfree_rcu(ent);
11927 +
11928 +out:
11929 +       au_kfree_try_rcu(rev);
11930 +       if (unlikely(err))
11931 +               pr_err("failed to remove dirren info\n");
11932 +}
11933 +
11934 +/* ---------------------------------------------------------------------- */
11935 +
11936 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11937 +                                          char *whname, int whnamelen,
11938 +                                          struct dentry **info_dentry)
11939 +{
11940 +       struct au_drinfo *drinfo;
11941 +       struct file *f;
11942 +       struct inode *h_dir;
11943 +       struct path infopath;
11944 +       int unlocked;
11945 +
11946 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11947 +
11948 +       *info_dentry = NULL;
11949 +       drinfo = NULL;
11950 +       unlocked = 0;
11951 +       h_dir = d_inode(h_ppath->dentry);
11952 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11953 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11954 +                                              whnamelen);
11955 +       if (IS_ERR(infopath.dentry)) {
11956 +               drinfo = (void *)infopath.dentry;
11957 +               goto out;
11958 +       }
11959 +
11960 +       if (d_is_negative(infopath.dentry))
11961 +               goto out_dput; /* success */
11962 +
11963 +       infopath.mnt = h_ppath->mnt;
11964 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11965 +       inode_unlock_shared(h_dir);
11966 +       unlocked = 1;
11967 +       if (IS_ERR(f)) {
11968 +               drinfo = (void *)f;
11969 +               goto out_dput;
11970 +       }
11971 +
11972 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11973 +       if (IS_ERR_OR_NULL(drinfo))
11974 +               goto out_fput;
11975 +
11976 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11977 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11978 +
11979 +out_fput:
11980 +       fput(f);
11981 +out_dput:
11982 +       dput(infopath.dentry);
11983 +out:
11984 +       if (!unlocked)
11985 +               inode_unlock_shared(h_dir);
11986 +       AuTraceErrPtr(drinfo);
11987 +       return drinfo;
11988 +}
11989 +
11990 +struct au_drinfo_do_load_args {
11991 +       struct au_drinfo **drinfop;
11992 +       struct path *h_ppath;
11993 +       char *whname;
11994 +       int whnamelen;
11995 +       struct dentry **info_dentry;
11996 +};
11997 +
11998 +static void au_call_drinfo_do_load(void *args)
11999 +{
12000 +       struct au_drinfo_do_load_args *a = args;
12001 +
12002 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12003 +                                       a->info_dentry);
12004 +}
12005 +
12006 +struct au_drinfo_load {
12007 +       struct path h_ppath;
12008 +       struct qstr *qname;
12009 +       unsigned char no_sio;
12010 +
12011 +       aufs_bindex_t ninfo;
12012 +       struct au_drinfo **drinfo;
12013 +};
12014 +
12015 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12016 +                         struct au_branch *br)
12017 +{
12018 +       int err, wkq_err, whnamelen, e;
12019 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12020 +               = AUFS_WH_DR_INFO_PFX;
12021 +       struct au_drinfo *drinfo;
12022 +       struct qstr oldname;
12023 +       struct inode *h_dir, *delegated;
12024 +       struct dentry *info_dentry;
12025 +       struct path infopath;
12026 +
12027 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12028 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
12029 +                                   sizeof(whname) - whnamelen);
12030 +       if (w->no_sio)
12031 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12032 +                                          &info_dentry);
12033 +       else {
12034 +               struct au_drinfo_do_load_args args = {
12035 +                       .drinfop        = &drinfo,
12036 +                       .h_ppath        = &w->h_ppath,
12037 +                       .whname         = whname,
12038 +                       .whnamelen      = whnamelen,
12039 +                       .info_dentry    = &info_dentry
12040 +               };
12041 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12042 +               if (unlikely(wkq_err))
12043 +                       drinfo = ERR_PTR(wkq_err);
12044 +       }
12045 +       err = PTR_ERR(drinfo);
12046 +       if (IS_ERR_OR_NULL(drinfo))
12047 +               goto out;
12048 +
12049 +       err = 0;
12050 +       oldname.len = drinfo->oldnamelen;
12051 +       oldname.name = drinfo->oldname;
12052 +       if (au_qstreq(w->qname, &oldname)) {
12053 +               /* the name is renamed back */
12054 +               au_kfree_rcu(drinfo);
12055 +               drinfo = NULL;
12056 +
12057 +               infopath.dentry = info_dentry;
12058 +               infopath.mnt = w->h_ppath.mnt;
12059 +               h_dir = d_inode(w->h_ppath.dentry);
12060 +               delegated = NULL;
12061 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12062 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12063 +               inode_unlock(h_dir);
12064 +               if (unlikely(e))
12065 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12066 +               if (unlikely(e == -EWOULDBLOCK))
12067 +                       iput(delegated);
12068 +       }
12069 +       au_kfree_rcu(w->drinfo[bindex]);
12070 +       w->drinfo[bindex] = drinfo;
12071 +       dput(info_dentry);
12072 +
12073 +out:
12074 +       AuTraceErr(err);
12075 +       return err;
12076 +}
12077 +
12078 +/* ---------------------------------------------------------------------- */
12079 +
12080 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12081 +{
12082 +       struct au_drinfo **p = drinfo;
12083 +
12084 +       while (n-- > 0)
12085 +               au_kfree_rcu(*drinfo++);
12086 +       au_kfree_try_rcu(p);
12087 +}
12088 +
12089 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12090 +              aufs_bindex_t btgt)
12091 +{
12092 +       int err, ninfo;
12093 +       struct au_drinfo_load w;
12094 +       aufs_bindex_t bindex, bbot;
12095 +       struct au_branch *br;
12096 +       struct inode *h_dir;
12097 +       struct au_dr_hino *ent;
12098 +       struct super_block *sb;
12099 +
12100 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12101 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12102 +             AuLNPair(&lkup->whname), btgt);
12103 +
12104 +       sb = dentry->d_sb;
12105 +       bbot = au_sbbot(sb);
12106 +       w.ninfo = bbot + 1;
12107 +       if (!lkup->dirren.drinfo) {
12108 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12109 +                                             sizeof(*lkup->dirren.drinfo),
12110 +                                             GFP_NOFS);
12111 +               if (unlikely(!lkup->dirren.drinfo)) {
12112 +                       err = -ENOMEM;
12113 +                       goto out;
12114 +               }
12115 +               lkup->dirren.ninfo = w.ninfo;
12116 +       }
12117 +       w.drinfo = lkup->dirren.drinfo;
12118 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12119 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12120 +       AuDebugOn(!w.h_ppath.dentry);
12121 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12122 +       w.qname = &dentry->d_name;
12123 +
12124 +       ninfo = 0;
12125 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12126 +               br = au_sbr(sb, bindex);
12127 +               err = au_drinfo_load(&w, bindex, br);
12128 +               if (unlikely(err))
12129 +                       goto out_free;
12130 +               if (w.drinfo[bindex])
12131 +                       ninfo++;
12132 +       }
12133 +       if (!ninfo) {
12134 +               br = au_sbr(sb, btgt);
12135 +               h_dir = d_inode(w.h_ppath.dentry);
12136 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12137 +               AuDebugOn(!ent);
12138 +               au_dr_hino_del(&br->br_dirren, ent);
12139 +               au_kfree_rcu(ent);
12140 +       }
12141 +       goto out; /* success */
12142 +
12143 +out_free:
12144 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12145 +       lkup->dirren.ninfo = 0;
12146 +       lkup->dirren.drinfo = NULL;
12147 +out:
12148 +       AuTraceErr(err);
12149 +       return err;
12150 +}
12151 +
12152 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12153 +{
12154 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12155 +}
12156 +
12157 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12158 +{
12159 +       int err;
12160 +       struct au_drinfo *drinfo;
12161 +
12162 +       err = 0;
12163 +       if (!lkup->dirren.drinfo)
12164 +               goto out;
12165 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12166 +       drinfo = lkup->dirren.drinfo[btgt];
12167 +       if (!drinfo)
12168 +               goto out;
12169 +
12170 +       au_kfree_try_rcu(lkup->whname.name);
12171 +       lkup->whname.name = NULL;
12172 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12173 +       lkup->dirren.dr_name.name = drinfo->oldname;
12174 +       lkup->name = &lkup->dirren.dr_name;
12175 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12176 +       if (!err)
12177 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12178 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12179 +                     btgt);
12180 +
12181 +out:
12182 +       AuTraceErr(err);
12183 +       return err;
12184 +}
12185 +
12186 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12187 +                    ino_t h_ino)
12188 +{
12189 +       int match;
12190 +       struct au_drinfo *drinfo;
12191 +
12192 +       match = 1;
12193 +       if (!lkup->dirren.drinfo)
12194 +               goto out;
12195 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12196 +       drinfo = lkup->dirren.drinfo[bindex];
12197 +       if (!drinfo)
12198 +               goto out;
12199 +
12200 +       match = (drinfo->ino == h_ino);
12201 +       AuDbg("match %d\n", match);
12202 +
12203 +out:
12204 +       return match;
12205 +}
12206 +
12207 +/* ---------------------------------------------------------------------- */
12208 +
12209 +int au_dr_opt_set(struct super_block *sb)
12210 +{
12211 +       int err;
12212 +       aufs_bindex_t bindex, bbot;
12213 +       struct au_branch *br;
12214 +
12215 +       err = 0;
12216 +       bbot = au_sbbot(sb);
12217 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12218 +               br = au_sbr(sb, bindex);
12219 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12220 +       }
12221 +
12222 +       return err;
12223 +}
12224 +
12225 +int au_dr_opt_flush(struct super_block *sb)
12226 +{
12227 +       int err;
12228 +       aufs_bindex_t bindex, bbot;
12229 +       struct au_branch *br;
12230 +
12231 +       err = 0;
12232 +       bbot = au_sbbot(sb);
12233 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12234 +               br = au_sbr(sb, bindex);
12235 +               if (au_br_writable(br->br_perm))
12236 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12237 +       }
12238 +
12239 +       return err;
12240 +}
12241 +
12242 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12243 +{
12244 +       int err;
12245 +       aufs_bindex_t bindex, bbot;
12246 +       struct au_branch *br;
12247 +
12248 +       err = 0;
12249 +       if (!no_flush) {
12250 +               err = au_dr_opt_flush(sb);
12251 +               if (unlikely(err))
12252 +                       goto out;
12253 +       }
12254 +
12255 +       bbot = au_sbbot(sb);
12256 +       for (bindex = 0; bindex <= bbot; bindex++) {
12257 +               br = au_sbr(sb, bindex);
12258 +               au_dr_hino_free(&br->br_dirren);
12259 +       }
12260 +
12261 +out:
12262 +       return err;
12263 +}
12264 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12265 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12266 +++ linux/fs/aufs/dirren.h      2020-01-27 10:57:18.168871450 +0100
12267 @@ -0,0 +1,140 @@
12268 +/* SPDX-License-Identifier: GPL-2.0 */
12269 +/*
12270 + * Copyright (C) 2017-2020 Junjiro R. Okajima
12271 + *
12272 + * This program, aufs is free software; you can redistribute it and/or modify
12273 + * it under the terms of the GNU General Public License as published by
12274 + * the Free Software Foundation; either version 2 of the License, or
12275 + * (at your option) any later version.
12276 + *
12277 + * This program is distributed in the hope that it will be useful,
12278 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12279 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12280 + * GNU General Public License for more details.
12281 + *
12282 + * You should have received a copy of the GNU General Public License
12283 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12284 + */
12285 +
12286 +/*
12287 + * renamed dir info
12288 + */
12289 +
12290 +#ifndef __AUFS_DIRREN_H__
12291 +#define __AUFS_DIRREN_H__
12292 +
12293 +#ifdef __KERNEL__
12294 +
12295 +#include <linux/dcache.h>
12296 +#include <linux/statfs.h>
12297 +#include <linux/uuid.h>
12298 +#include "hbl.h"
12299 +
12300 +#define AuDirren_NHASH 100
12301 +
12302 +#ifdef CONFIG_AUFS_DIRREN
12303 +enum au_brid_type {
12304 +       AuBrid_Unset,
12305 +       AuBrid_UUID,
12306 +       AuBrid_FSID,
12307 +       AuBrid_DEV
12308 +};
12309 +
12310 +struct au_dr_brid {
12311 +       enum au_brid_type       type;
12312 +       union {
12313 +               uuid_t  uuid;   /* unimplemented yet */
12314 +               fsid_t  fsid;
12315 +               dev_t   dev;
12316 +       };
12317 +};
12318 +
12319 +/* 20 is the max digits length of ulong 64 */
12320 +/* brid-type "_" uuid "_" inum */
12321 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12322 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12323 +
12324 +struct au_dr_hino {
12325 +       struct hlist_bl_node    dr_hnode;
12326 +       ino_t                   dr_h_ino;
12327 +};
12328 +
12329 +struct au_dr_br {
12330 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12331 +       struct au_dr_brid       dr_brid;
12332 +};
12333 +
12334 +struct au_dr_lookup {
12335 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12336 +       struct qstr             dr_name; /* subset of dr_info */
12337 +       aufs_bindex_t           ninfo;
12338 +       struct au_drinfo        **drinfo;
12339 +};
12340 +#else
12341 +struct au_dr_hino;
12342 +/* empty */
12343 +struct au_dr_br { };
12344 +struct au_dr_lookup { };
12345 +#endif
12346 +
12347 +/* ---------------------------------------------------------------------- */
12348 +
12349 +struct au_branch;
12350 +struct au_do_lookup_args;
12351 +struct au_hinode;
12352 +#ifdef CONFIG_AUFS_DIRREN
12353 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12354 +                       struct au_dr_hino *add_ent);
12355 +void au_dr_hino_free(struct au_dr_br *dr);
12356 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12357 +                 const struct path *path);
12358 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12359 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12360 +                struct qstr *dst_name, void *_rev);
12361 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12362 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12363 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12364 +              aufs_bindex_t bindex);
12365 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12366 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12367 +                    ino_t h_ino);
12368 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12369 +int au_dr_opt_set(struct super_block *sb);
12370 +int au_dr_opt_flush(struct super_block *sb);
12371 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12372 +#else
12373 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12374 +          struct au_dr_hino *add_ent);
12375 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12376 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12377 +          const struct path *path);
12378 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12379 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12380 +          struct qstr *dst_name, void *_rev);
12381 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12382 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12383 +          void *rev);
12384 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12385 +          aufs_bindex_t bindex);
12386 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12387 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12388 +          aufs_bindex_t bindex, ino_t h_ino);
12389 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12390 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12391 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12392 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12393 +#endif
12394 +
12395 +/* ---------------------------------------------------------------------- */
12396 +
12397 +#ifdef CONFIG_AUFS_DIRREN
12398 +static inline int au_dr_ihash(ino_t h_ino)
12399 +{
12400 +       return h_ino % AuDirren_NHASH;
12401 +}
12402 +#else
12403 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12404 +#endif
12405 +
12406 +#endif /* __KERNEL__ */
12407 +#endif /* __AUFS_DIRREN_H__ */
12408 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12409 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12410 +++ linux/fs/aufs/dynop.c       2020-01-27 10:57:18.168871450 +0100
12411 @@ -0,0 +1,367 @@
12412 +// SPDX-License-Identifier: GPL-2.0
12413 +/*
12414 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12415 + *
12416 + * This program, aufs is free software; you can redistribute it and/or modify
12417 + * it under the terms of the GNU General Public License as published by
12418 + * the Free Software Foundation; either version 2 of the License, or
12419 + * (at your option) any later version.
12420 + *
12421 + * This program is distributed in the hope that it will be useful,
12422 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12423 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12424 + * GNU General Public License for more details.
12425 + *
12426 + * You should have received a copy of the GNU General Public License
12427 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12428 + */
12429 +
12430 +/*
12431 + * dynamically customizable operations for regular files
12432 + */
12433 +
12434 +#include "aufs.h"
12435 +
12436 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12437 +
12438 +/*
12439 + * How large will these lists be?
12440 + * Usually just a few elements, 20-30 at most for each, I guess.
12441 + */
12442 +static struct hlist_bl_head dynop[AuDyLast];
12443 +
12444 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12445 +                                    const void *h_op)
12446 +{
12447 +       struct au_dykey *key, *tmp;
12448 +       struct hlist_bl_node *pos;
12449 +
12450 +       key = NULL;
12451 +       hlist_bl_lock(hbl);
12452 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12453 +               if (tmp->dk_op.dy_hop == h_op) {
12454 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12455 +                               key = tmp;
12456 +                       break;
12457 +               }
12458 +       hlist_bl_unlock(hbl);
12459 +
12460 +       return key;
12461 +}
12462 +
12463 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12464 +{
12465 +       struct au_dykey **k, *found;
12466 +       const void *h_op = key->dk_op.dy_hop;
12467 +       int i;
12468 +
12469 +       found = NULL;
12470 +       k = br->br_dykey;
12471 +       for (i = 0; i < AuBrDynOp; i++)
12472 +               if (k[i]) {
12473 +                       if (k[i]->dk_op.dy_hop == h_op) {
12474 +                               found = k[i];
12475 +                               break;
12476 +                       }
12477 +               } else
12478 +                       break;
12479 +       if (!found) {
12480 +               spin_lock(&br->br_dykey_lock);
12481 +               for (; i < AuBrDynOp; i++)
12482 +                       if (k[i]) {
12483 +                               if (k[i]->dk_op.dy_hop == h_op) {
12484 +                                       found = k[i];
12485 +                                       break;
12486 +                               }
12487 +                       } else {
12488 +                               k[i] = key;
12489 +                               break;
12490 +                       }
12491 +               spin_unlock(&br->br_dykey_lock);
12492 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12493 +       }
12494 +
12495 +       return found;
12496 +}
12497 +
12498 +/* kref_get() if @key is already added */
12499 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12500 +{
12501 +       struct au_dykey *tmp, *found;
12502 +       struct hlist_bl_node *pos;
12503 +       const void *h_op = key->dk_op.dy_hop;
12504 +
12505 +       found = NULL;
12506 +       hlist_bl_lock(hbl);
12507 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12508 +               if (tmp->dk_op.dy_hop == h_op) {
12509 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12510 +                               found = tmp;
12511 +                       break;
12512 +               }
12513 +       if (!found)
12514 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12515 +       hlist_bl_unlock(hbl);
12516 +
12517 +       if (!found)
12518 +               DyPrSym(key);
12519 +       return found;
12520 +}
12521 +
12522 +static void dy_free_rcu(struct rcu_head *rcu)
12523 +{
12524 +       struct au_dykey *key;
12525 +
12526 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12527 +       DyPrSym(key);
12528 +       kfree(key);
12529 +}
12530 +
12531 +static void dy_free(struct kref *kref)
12532 +{
12533 +       struct au_dykey *key;
12534 +       struct hlist_bl_head *hbl;
12535 +
12536 +       key = container_of(kref, struct au_dykey, dk_kref);
12537 +       hbl = dynop + key->dk_op.dy_type;
12538 +       au_hbl_del(&key->dk_hnode, hbl);
12539 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12540 +}
12541 +
12542 +void au_dy_put(struct au_dykey *key)
12543 +{
12544 +       kref_put(&key->dk_kref, dy_free);
12545 +}
12546 +
12547 +/* ---------------------------------------------------------------------- */
12548 +
12549 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12550 +
12551 +#ifdef CONFIG_AUFS_DEBUG
12552 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12553 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12554 +#else
12555 +#define DyDbgDeclare(cnt)      do {} while (0)
12556 +#define DyDbgInc(cnt)          do {} while (0)
12557 +#endif
12558 +
12559 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12560 +       DyDbgInc(cnt);                                                  \
12561 +       if (h_op->func) {                                               \
12562 +               if (src.func)                                           \
12563 +                       dst.func = src.func;                            \
12564 +               else                                                    \
12565 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12566 +       }                                                               \
12567 +} while (0)
12568 +
12569 +#define DySetForce(func, dst, src) do {                \
12570 +       AuDebugOn(!src.func);                   \
12571 +       DyDbgInc(cnt);                          \
12572 +       dst.func = src.func;                    \
12573 +} while (0)
12574 +
12575 +#define DySetAop(func) \
12576 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12577 +#define DySetAopForce(func) \
12578 +       DySetForce(func, dyaop->da_op, aufs_aop)
12579 +
12580 +static void dy_aop(struct au_dykey *key, const void *h_op,
12581 +                  struct super_block *h_sb __maybe_unused)
12582 +{
12583 +       struct au_dyaop *dyaop = (void *)key;
12584 +       const struct address_space_operations *h_aop = h_op;
12585 +       DyDbgDeclare(cnt);
12586 +
12587 +       AuDbg("%s\n", au_sbtype(h_sb));
12588 +
12589 +       DySetAop(writepage);
12590 +       DySetAopForce(readpage);        /* force */
12591 +       DySetAop(writepages);
12592 +       DySetAop(set_page_dirty);
12593 +       DySetAop(readpages);
12594 +       DySetAop(write_begin);
12595 +       DySetAop(write_end);
12596 +       DySetAop(bmap);
12597 +       DySetAop(invalidatepage);
12598 +       DySetAop(releasepage);
12599 +       DySetAop(freepage);
12600 +       /* this one will be changed according to an aufs mount option */
12601 +       DySetAop(direct_IO);
12602 +       DySetAop(migratepage);
12603 +       DySetAop(isolate_page);
12604 +       DySetAop(putback_page);
12605 +       DySetAop(launder_page);
12606 +       DySetAop(is_partially_uptodate);
12607 +       DySetAop(is_dirty_writeback);
12608 +       DySetAop(error_remove_page);
12609 +       DySetAop(swap_activate);
12610 +       DySetAop(swap_deactivate);
12611 +
12612 +       DyDbgSize(cnt, *h_aop);
12613 +}
12614 +
12615 +/* ---------------------------------------------------------------------- */
12616 +
12617 +static void dy_bug(struct kref *kref)
12618 +{
12619 +       BUG();
12620 +}
12621 +
12622 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12623 +{
12624 +       struct au_dykey *key, *old;
12625 +       struct hlist_bl_head *hbl;
12626 +       struct op {
12627 +               unsigned int sz;
12628 +               void (*set)(struct au_dykey *key, const void *h_op,
12629 +                           struct super_block *h_sb __maybe_unused);
12630 +       };
12631 +       static const struct op a[] = {
12632 +               [AuDy_AOP] = {
12633 +                       .sz     = sizeof(struct au_dyaop),
12634 +                       .set    = dy_aop
12635 +               }
12636 +       };
12637 +       const struct op *p;
12638 +
12639 +       hbl = dynop + op->dy_type;
12640 +       key = dy_gfind_get(hbl, op->dy_hop);
12641 +       if (key)
12642 +               goto out_add; /* success */
12643 +
12644 +       p = a + op->dy_type;
12645 +       key = kzalloc(p->sz, GFP_NOFS);
12646 +       if (unlikely(!key)) {
12647 +               key = ERR_PTR(-ENOMEM);
12648 +               goto out;
12649 +       }
12650 +
12651 +       key->dk_op.dy_hop = op->dy_hop;
12652 +       kref_init(&key->dk_kref);
12653 +       p->set(key, op->dy_hop, au_br_sb(br));
12654 +       old = dy_gadd(hbl, key);
12655 +       if (old) {
12656 +               au_kfree_rcu(key);
12657 +               key = old;
12658 +       }
12659 +
12660 +out_add:
12661 +       old = dy_bradd(br, key);
12662 +       if (old)
12663 +               /* its ref-count should never be zero here */
12664 +               kref_put(&key->dk_kref, dy_bug);
12665 +out:
12666 +       return key;
12667 +}
12668 +
12669 +/* ---------------------------------------------------------------------- */
12670 +/*
12671 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12672 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12673 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12674 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12675 + * See the aufs manual in detail.
12676 + */
12677 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12678 +{
12679 +       if (!do_dx)
12680 +               dyaop->da_op.direct_IO = NULL;
12681 +       else
12682 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12683 +}
12684 +
12685 +static struct au_dyaop *dy_aget(struct au_branch *br,
12686 +                               const struct address_space_operations *h_aop,
12687 +                               int do_dx)
12688 +{
12689 +       struct au_dyaop *dyaop;
12690 +       struct au_dynop op;
12691 +
12692 +       op.dy_type = AuDy_AOP;
12693 +       op.dy_haop = h_aop;
12694 +       dyaop = (void *)dy_get(&op, br);
12695 +       if (IS_ERR(dyaop))
12696 +               goto out;
12697 +       dy_adx(dyaop, do_dx);
12698 +
12699 +out:
12700 +       return dyaop;
12701 +}
12702 +
12703 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12704 +               struct inode *h_inode)
12705 +{
12706 +       int err, do_dx;
12707 +       struct super_block *sb;
12708 +       struct au_branch *br;
12709 +       struct au_dyaop *dyaop;
12710 +
12711 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12712 +       IiMustWriteLock(inode);
12713 +
12714 +       sb = inode->i_sb;
12715 +       br = au_sbr(sb, bindex);
12716 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12717 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12718 +       err = PTR_ERR(dyaop);
12719 +       if (IS_ERR(dyaop))
12720 +               /* unnecessary to call dy_fput() */
12721 +               goto out;
12722 +
12723 +       err = 0;
12724 +       inode->i_mapping->a_ops = &dyaop->da_op;
12725 +
12726 +out:
12727 +       return err;
12728 +}
12729 +
12730 +/*
12731 + * Is it safe to replace a_ops during the inode/file is in operation?
12732 + * Yes, I hope so.
12733 + */
12734 +int au_dy_irefresh(struct inode *inode)
12735 +{
12736 +       int err;
12737 +       aufs_bindex_t btop;
12738 +       struct inode *h_inode;
12739 +
12740 +       err = 0;
12741 +       if (S_ISREG(inode->i_mode)) {
12742 +               btop = au_ibtop(inode);
12743 +               h_inode = au_h_iptr(inode, btop);
12744 +               err = au_dy_iaop(inode, btop, h_inode);
12745 +       }
12746 +       return err;
12747 +}
12748 +
12749 +void au_dy_arefresh(int do_dx)
12750 +{
12751 +       struct hlist_bl_head *hbl;
12752 +       struct hlist_bl_node *pos;
12753 +       struct au_dykey *key;
12754 +
12755 +       hbl = dynop + AuDy_AOP;
12756 +       hlist_bl_lock(hbl);
12757 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12758 +               dy_adx((void *)key, do_dx);
12759 +       hlist_bl_unlock(hbl);
12760 +}
12761 +
12762 +/* ---------------------------------------------------------------------- */
12763 +
12764 +void __init au_dy_init(void)
12765 +{
12766 +       int i;
12767 +
12768 +       for (i = 0; i < AuDyLast; i++)
12769 +               INIT_HLIST_BL_HEAD(dynop + i);
12770 +}
12771 +
12772 +void au_dy_fin(void)
12773 +{
12774 +       int i;
12775 +
12776 +       for (i = 0; i < AuDyLast; i++)
12777 +               WARN_ON(!hlist_bl_empty(dynop + i));
12778 +}
12779 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12780 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12781 +++ linux/fs/aufs/dynop.h       2020-01-27 10:57:18.168871450 +0100
12782 @@ -0,0 +1,77 @@
12783 +/* SPDX-License-Identifier: GPL-2.0 */
12784 +/*
12785 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12786 + *
12787 + * This program, aufs is free software; you can redistribute it and/or modify
12788 + * it under the terms of the GNU General Public License as published by
12789 + * the Free Software Foundation; either version 2 of the License, or
12790 + * (at your option) any later version.
12791 + *
12792 + * This program is distributed in the hope that it will be useful,
12793 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12794 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12795 + * GNU General Public License for more details.
12796 + *
12797 + * You should have received a copy of the GNU General Public License
12798 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12799 + */
12800 +
12801 +/*
12802 + * dynamically customizable operations (for regular files only)
12803 + */
12804 +
12805 +#ifndef __AUFS_DYNOP_H__
12806 +#define __AUFS_DYNOP_H__
12807 +
12808 +#ifdef __KERNEL__
12809 +
12810 +#include <linux/fs.h>
12811 +#include <linux/kref.h>
12812 +
12813 +enum {AuDy_AOP, AuDyLast};
12814 +
12815 +struct au_dynop {
12816 +       int                                             dy_type;
12817 +       union {
12818 +               const void                              *dy_hop;
12819 +               const struct address_space_operations   *dy_haop;
12820 +       };
12821 +};
12822 +
12823 +struct au_dykey {
12824 +       union {
12825 +               struct hlist_bl_node    dk_hnode;
12826 +               struct rcu_head         dk_rcu;
12827 +       };
12828 +       struct au_dynop         dk_op;
12829 +
12830 +       /*
12831 +        * during I am in the branch local array, kref is gotten. when the
12832 +        * branch is removed, kref is put.
12833 +        */
12834 +       struct kref             dk_kref;
12835 +};
12836 +
12837 +/* stop unioning since their sizes are very different from each other */
12838 +struct au_dyaop {
12839 +       struct au_dykey                 da_key;
12840 +       struct address_space_operations da_op; /* not const */
12841 +};
12842 +/* make sure that 'struct au_dykey *' can be any type */
12843 +static_assert(!offsetof(struct au_dyaop, da_key));
12844 +
12845 +/* ---------------------------------------------------------------------- */
12846 +
12847 +/* dynop.c */
12848 +struct au_branch;
12849 +void au_dy_put(struct au_dykey *key);
12850 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12851 +               struct inode *h_inode);
12852 +int au_dy_irefresh(struct inode *inode);
12853 +void au_dy_arefresh(int do_dio);
12854 +
12855 +void __init au_dy_init(void);
12856 +void au_dy_fin(void);
12857 +
12858 +#endif /* __KERNEL__ */
12859 +#endif /* __AUFS_DYNOP_H__ */
12860 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12861 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12862 +++ linux/fs/aufs/export.c      2020-01-27 10:57:18.168871450 +0100
12863 @@ -0,0 +1,838 @@
12864 +// SPDX-License-Identifier: GPL-2.0
12865 +/*
12866 + * Copyright (C) 2005-2020 Junjiro R. Okajima
12867 + *
12868 + * This program, aufs is free software; you can redistribute it and/or modify
12869 + * it under the terms of the GNU General Public License as published by
12870 + * the Free Software Foundation; either version 2 of the License, or
12871 + * (at your option) any later version.
12872 + *
12873 + * This program is distributed in the hope that it will be useful,
12874 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12875 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12876 + * GNU General Public License for more details.
12877 + *
12878 + * You should have received a copy of the GNU General Public License
12879 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12880 + */
12881 +
12882 +/*
12883 + * export via nfs
12884 + */
12885 +
12886 +#include <linux/exportfs.h>
12887 +#include <linux/fs_struct.h>
12888 +#include <linux/namei.h>
12889 +#include <linux/nsproxy.h>
12890 +#include <linux/random.h>
12891 +#include <linux/writeback.h>
12892 +#include "aufs.h"
12893 +
12894 +union conv {
12895 +#ifdef CONFIG_AUFS_INO_T_64
12896 +       __u32 a[2];
12897 +#else
12898 +       __u32 a[1];
12899 +#endif
12900 +       ino_t ino;
12901 +};
12902 +
12903 +static ino_t decode_ino(__u32 *a)
12904 +{
12905 +       union conv u;
12906 +
12907 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12908 +       u.a[0] = a[0];
12909 +#ifdef CONFIG_AUFS_INO_T_64
12910 +       u.a[1] = a[1];
12911 +#endif
12912 +       return u.ino;
12913 +}
12914 +
12915 +static void encode_ino(__u32 *a, ino_t ino)
12916 +{
12917 +       union conv u;
12918 +
12919 +       u.ino = ino;
12920 +       a[0] = u.a[0];
12921 +#ifdef CONFIG_AUFS_INO_T_64
12922 +       a[1] = u.a[1];
12923 +#endif
12924 +}
12925 +
12926 +/* NFS file handle */
12927 +enum {
12928 +       Fh_br_id,
12929 +       Fh_sigen,
12930 +#ifdef CONFIG_AUFS_INO_T_64
12931 +       /* support 64bit inode number */
12932 +       Fh_ino1,
12933 +       Fh_ino2,
12934 +       Fh_dir_ino1,
12935 +       Fh_dir_ino2,
12936 +#else
12937 +       Fh_ino1,
12938 +       Fh_dir_ino1,
12939 +#endif
12940 +       Fh_igen,
12941 +       Fh_h_type,
12942 +       Fh_tail,
12943 +
12944 +       Fh_ino = Fh_ino1,
12945 +       Fh_dir_ino = Fh_dir_ino1
12946 +};
12947 +
12948 +static int au_test_anon(struct dentry *dentry)
12949 +{
12950 +       /* note: read d_flags without d_lock */
12951 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12952 +}
12953 +
12954 +int au_test_nfsd(void)
12955 +{
12956 +       int ret;
12957 +       struct task_struct *tsk = current;
12958 +       char comm[sizeof(tsk->comm)];
12959 +
12960 +       ret = 0;
12961 +       if (tsk->flags & PF_KTHREAD) {
12962 +               get_task_comm(comm, tsk);
12963 +               ret = !strcmp(comm, "nfsd");
12964 +       }
12965 +
12966 +       return ret;
12967 +}
12968 +
12969 +/* ---------------------------------------------------------------------- */
12970 +/* inode generation external table */
12971 +
12972 +void au_xigen_inc(struct inode *inode)
12973 +{
12974 +       loff_t pos;
12975 +       ssize_t sz;
12976 +       __u32 igen;
12977 +       struct super_block *sb;
12978 +       struct au_sbinfo *sbinfo;
12979 +
12980 +       sb = inode->i_sb;
12981 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12982 +
12983 +       sbinfo = au_sbi(sb);
12984 +       pos = inode->i_ino;
12985 +       pos *= sizeof(igen);
12986 +       igen = inode->i_generation + 1;
12987 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12988 +                        sizeof(igen), &pos);
12989 +       if (sz == sizeof(igen))
12990 +               return; /* success */
12991 +
12992 +       if (unlikely(sz >= 0))
12993 +               AuIOErr("xigen error (%zd)\n", sz);
12994 +}
12995 +
12996 +int au_xigen_new(struct inode *inode)
12997 +{
12998 +       int err;
12999 +       loff_t pos;
13000 +       ssize_t sz;
13001 +       struct super_block *sb;
13002 +       struct au_sbinfo *sbinfo;
13003 +       struct file *file;
13004 +
13005 +       err = 0;
13006 +       /* todo: dirty, at mount time */
13007 +       if (inode->i_ino == AUFS_ROOT_INO)
13008 +               goto out;
13009 +       sb = inode->i_sb;
13010 +       SiMustAnyLock(sb);
13011 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13012 +               goto out;
13013 +
13014 +       err = -EFBIG;
13015 +       pos = inode->i_ino;
13016 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13017 +               AuIOErr1("too large i%lld\n", pos);
13018 +               goto out;
13019 +       }
13020 +       pos *= sizeof(inode->i_generation);
13021 +
13022 +       err = 0;
13023 +       sbinfo = au_sbi(sb);
13024 +       file = sbinfo->si_xigen;
13025 +       BUG_ON(!file);
13026 +
13027 +       if (vfsub_f_size_read(file)
13028 +           < pos + sizeof(inode->i_generation)) {
13029 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13030 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13031 +                                sizeof(inode->i_generation), &pos);
13032 +       } else
13033 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13034 +                               sizeof(inode->i_generation), &pos);
13035 +       if (sz == sizeof(inode->i_generation))
13036 +               goto out; /* success */
13037 +
13038 +       err = sz;
13039 +       if (unlikely(sz >= 0)) {
13040 +               err = -EIO;
13041 +               AuIOErr("xigen error (%zd)\n", sz);
13042 +       }
13043 +
13044 +out:
13045 +       return err;
13046 +}
13047 +
13048 +int au_xigen_set(struct super_block *sb, struct path *path)
13049 +{
13050 +       int err;
13051 +       struct au_sbinfo *sbinfo;
13052 +       struct file *file;
13053 +
13054 +       SiMustWriteLock(sb);
13055 +
13056 +       sbinfo = au_sbi(sb);
13057 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13058 +       err = PTR_ERR(file);
13059 +       if (IS_ERR(file))
13060 +               goto out;
13061 +       err = 0;
13062 +       if (sbinfo->si_xigen)
13063 +               fput(sbinfo->si_xigen);
13064 +       sbinfo->si_xigen = file;
13065 +
13066 +out:
13067 +       AuTraceErr(err);
13068 +       return err;
13069 +}
13070 +
13071 +void au_xigen_clr(struct super_block *sb)
13072 +{
13073 +       struct au_sbinfo *sbinfo;
13074 +
13075 +       SiMustWriteLock(sb);
13076 +
13077 +       sbinfo = au_sbi(sb);
13078 +       if (sbinfo->si_xigen) {
13079 +               fput(sbinfo->si_xigen);
13080 +               sbinfo->si_xigen = NULL;
13081 +       }
13082 +}
13083 +
13084 +/* ---------------------------------------------------------------------- */
13085 +
13086 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13087 +                                   ino_t dir_ino)
13088 +{
13089 +       struct dentry *dentry, *d;
13090 +       struct inode *inode;
13091 +       unsigned int sigen;
13092 +
13093 +       dentry = NULL;
13094 +       inode = ilookup(sb, ino);
13095 +       if (!inode)
13096 +               goto out;
13097 +
13098 +       dentry = ERR_PTR(-ESTALE);
13099 +       sigen = au_sigen(sb);
13100 +       if (unlikely(au_is_bad_inode(inode)
13101 +                    || IS_DEADDIR(inode)
13102 +                    || sigen != au_iigen(inode, NULL)))
13103 +               goto out_iput;
13104 +
13105 +       dentry = NULL;
13106 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13107 +               dentry = d_find_alias(inode);
13108 +       else {
13109 +               spin_lock(&inode->i_lock);
13110 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13111 +                       spin_lock(&d->d_lock);
13112 +                       if (!au_test_anon(d)
13113 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13114 +                               dentry = dget_dlock(d);
13115 +                               spin_unlock(&d->d_lock);
13116 +                               break;
13117 +                       }
13118 +                       spin_unlock(&d->d_lock);
13119 +               }
13120 +               spin_unlock(&inode->i_lock);
13121 +       }
13122 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13123 +               /* need to refresh */
13124 +               dput(dentry);
13125 +               dentry = NULL;
13126 +       }
13127 +
13128 +out_iput:
13129 +       iput(inode);
13130 +out:
13131 +       AuTraceErrPtr(dentry);
13132 +       return dentry;
13133 +}
13134 +
13135 +/* ---------------------------------------------------------------------- */
13136 +
13137 +/* todo: dirty? */
13138 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13139 +
13140 +struct au_compare_mnt_args {
13141 +       /* input */
13142 +       struct super_block *sb;
13143 +
13144 +       /* output */
13145 +       struct vfsmount *mnt;
13146 +};
13147 +
13148 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13149 +{
13150 +       struct au_compare_mnt_args *a = arg;
13151 +
13152 +       if (mnt->mnt_sb != a->sb)
13153 +               return 0;
13154 +       a->mnt = mntget(mnt);
13155 +       return 1;
13156 +}
13157 +
13158 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13159 +{
13160 +       int err;
13161 +       struct path root;
13162 +       struct au_compare_mnt_args args = {
13163 +               .sb = sb
13164 +       };
13165 +
13166 +       get_fs_root(current->fs, &root);
13167 +       rcu_read_lock();
13168 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13169 +       rcu_read_unlock();
13170 +       path_put(&root);
13171 +       AuDebugOn(!err);
13172 +       AuDebugOn(!args.mnt);
13173 +       return args.mnt;
13174 +}
13175 +
13176 +struct au_nfsd_si_lock {
13177 +       unsigned int sigen;
13178 +       aufs_bindex_t bindex, br_id;
13179 +       unsigned char force_lock;
13180 +};
13181 +
13182 +static int si_nfsd_read_lock(struct super_block *sb,
13183 +                            struct au_nfsd_si_lock *nsi_lock)
13184 +{
13185 +       int err;
13186 +       aufs_bindex_t bindex;
13187 +
13188 +       si_read_lock(sb, AuLock_FLUSH);
13189 +
13190 +       /* branch id may be wrapped around */
13191 +       err = 0;
13192 +       bindex = au_br_index(sb, nsi_lock->br_id);
13193 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13194 +               goto out; /* success */
13195 +
13196 +       err = -ESTALE;
13197 +       bindex = -1;
13198 +       if (!nsi_lock->force_lock)
13199 +               si_read_unlock(sb);
13200 +
13201 +out:
13202 +       nsi_lock->bindex = bindex;
13203 +       return err;
13204 +}
13205 +
13206 +struct find_name_by_ino {
13207 +       struct dir_context ctx;
13208 +       int called, found;
13209 +       ino_t ino;
13210 +       char *name;
13211 +       int namelen;
13212 +};
13213 +
13214 +static int
13215 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13216 +                loff_t offset, u64 ino, unsigned int d_type)
13217 +{
13218 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13219 +                                                 ctx);
13220 +
13221 +       a->called++;
13222 +       if (a->ino != ino)
13223 +               return 0;
13224 +
13225 +       memcpy(a->name, name, namelen);
13226 +       a->namelen = namelen;
13227 +       a->found = 1;
13228 +       return 1;
13229 +}
13230 +
13231 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13232 +                                    struct au_nfsd_si_lock *nsi_lock)
13233 +{
13234 +       struct dentry *dentry, *parent;
13235 +       struct file *file;
13236 +       struct inode *dir;
13237 +       struct find_name_by_ino arg = {
13238 +               .ctx = {
13239 +                       .actor = find_name_by_ino
13240 +               }
13241 +       };
13242 +       int err;
13243 +
13244 +       parent = path->dentry;
13245 +       if (nsi_lock)
13246 +               si_read_unlock(parent->d_sb);
13247 +       file = vfsub_dentry_open(path, au_dir_roflags);
13248 +       dentry = (void *)file;
13249 +       if (IS_ERR(file))
13250 +               goto out;
13251 +
13252 +       dentry = ERR_PTR(-ENOMEM);
13253 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13254 +       if (unlikely(!arg.name))
13255 +               goto out_file;
13256 +       arg.ino = ino;
13257 +       arg.found = 0;
13258 +       do {
13259 +               arg.called = 0;
13260 +               /* smp_mb(); */
13261 +               err = vfsub_iterate_dir(file, &arg.ctx);
13262 +       } while (!err && !arg.found && arg.called);
13263 +       dentry = ERR_PTR(err);
13264 +       if (unlikely(err))
13265 +               goto out_name;
13266 +       /* instead of ENOENT */
13267 +       dentry = ERR_PTR(-ESTALE);
13268 +       if (!arg.found)
13269 +               goto out_name;
13270 +
13271 +       /* do not call vfsub_lkup_one() */
13272 +       dir = d_inode(parent);
13273 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13274 +       AuTraceErrPtr(dentry);
13275 +       if (IS_ERR(dentry))
13276 +               goto out_name;
13277 +       AuDebugOn(au_test_anon(dentry));
13278 +       if (unlikely(d_really_is_negative(dentry))) {
13279 +               dput(dentry);
13280 +               dentry = ERR_PTR(-ENOENT);
13281 +       }
13282 +
13283 +out_name:
13284 +       free_page((unsigned long)arg.name);
13285 +out_file:
13286 +       fput(file);
13287 +out:
13288 +       if (unlikely(nsi_lock
13289 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13290 +               if (!IS_ERR(dentry)) {
13291 +                       dput(dentry);
13292 +                       dentry = ERR_PTR(-ESTALE);
13293 +               }
13294 +       AuTraceErrPtr(dentry);
13295 +       return dentry;
13296 +}
13297 +
13298 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13299 +                                       ino_t dir_ino,
13300 +                                       struct au_nfsd_si_lock *nsi_lock)
13301 +{
13302 +       struct dentry *dentry;
13303 +       struct path path;
13304 +
13305 +       if (dir_ino != AUFS_ROOT_INO) {
13306 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13307 +               dentry = path.dentry;
13308 +               if (!path.dentry || IS_ERR(path.dentry))
13309 +                       goto out;
13310 +               AuDebugOn(au_test_anon(path.dentry));
13311 +       } else
13312 +               path.dentry = dget(sb->s_root);
13313 +
13314 +       path.mnt = au_mnt_get(sb);
13315 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13316 +       path_put(&path);
13317 +
13318 +out:
13319 +       AuTraceErrPtr(dentry);
13320 +       return dentry;
13321 +}
13322 +
13323 +/* ---------------------------------------------------------------------- */
13324 +
13325 +static int h_acceptable(void *expv, struct dentry *dentry)
13326 +{
13327 +       return 1;
13328 +}
13329 +
13330 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13331 +                          char *buf, int len, struct super_block *sb)
13332 +{
13333 +       char *p;
13334 +       int n;
13335 +       struct path path;
13336 +
13337 +       p = d_path(h_rootpath, buf, len);
13338 +       if (IS_ERR(p))
13339 +               goto out;
13340 +       n = strlen(p);
13341 +
13342 +       path.mnt = h_rootpath->mnt;
13343 +       path.dentry = h_parent;
13344 +       p = d_path(&path, buf, len);
13345 +       if (IS_ERR(p))
13346 +               goto out;
13347 +       if (n != 1)
13348 +               p += n;
13349 +
13350 +       path.mnt = au_mnt_get(sb);
13351 +       path.dentry = sb->s_root;
13352 +       p = d_path(&path, buf, len - strlen(p));
13353 +       mntput(path.mnt);
13354 +       if (IS_ERR(p))
13355 +               goto out;
13356 +       if (n != 1)
13357 +               p[strlen(p)] = '/';
13358 +
13359 +out:
13360 +       AuTraceErrPtr(p);
13361 +       return p;
13362 +}
13363 +
13364 +static
13365 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13366 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13367 +{
13368 +       struct dentry *dentry, *h_parent, *root;
13369 +       struct super_block *h_sb;
13370 +       char *pathname, *p;
13371 +       struct vfsmount *h_mnt;
13372 +       struct au_branch *br;
13373 +       int err;
13374 +       struct path path;
13375 +
13376 +       br = au_sbr(sb, nsi_lock->bindex);
13377 +       h_mnt = au_br_mnt(br);
13378 +       h_sb = h_mnt->mnt_sb;
13379 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13380 +       lockdep_off();
13381 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13382 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13383 +                                     h_acceptable, /*context*/NULL);
13384 +       lockdep_on();
13385 +       dentry = h_parent;
13386 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13387 +               AuWarn1("%s decode_fh failed, %ld\n",
13388 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13389 +               goto out;
13390 +       }
13391 +       dentry = NULL;
13392 +       if (unlikely(au_test_anon(h_parent))) {
13393 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13394 +                       au_sbtype(h_sb));
13395 +               goto out_h_parent;
13396 +       }
13397 +
13398 +       dentry = ERR_PTR(-ENOMEM);
13399 +       pathname = (void *)__get_free_page(GFP_NOFS);
13400 +       if (unlikely(!pathname))
13401 +               goto out_h_parent;
13402 +
13403 +       root = sb->s_root;
13404 +       path.mnt = h_mnt;
13405 +       di_read_lock_parent(root, !AuLock_IR);
13406 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13407 +       di_read_unlock(root, !AuLock_IR);
13408 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13409 +       dentry = (void *)p;
13410 +       if (IS_ERR(p))
13411 +               goto out_pathname;
13412 +
13413 +       si_read_unlock(sb);
13414 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13415 +       dentry = ERR_PTR(err);
13416 +       if (unlikely(err))
13417 +               goto out_relock;
13418 +
13419 +       dentry = ERR_PTR(-ENOENT);
13420 +       AuDebugOn(au_test_anon(path.dentry));
13421 +       if (unlikely(d_really_is_negative(path.dentry)))
13422 +               goto out_path;
13423 +
13424 +       if (ino != d_inode(path.dentry)->i_ino)
13425 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13426 +       else
13427 +               dentry = dget(path.dentry);
13428 +
13429 +out_path:
13430 +       path_put(&path);
13431 +out_relock:
13432 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13433 +               if (!IS_ERR(dentry)) {
13434 +                       dput(dentry);
13435 +                       dentry = ERR_PTR(-ESTALE);
13436 +               }
13437 +out_pathname:
13438 +       free_page((unsigned long)pathname);
13439 +out_h_parent:
13440 +       dput(h_parent);
13441 +out:
13442 +       AuTraceErrPtr(dentry);
13443 +       return dentry;
13444 +}
13445 +
13446 +/* ---------------------------------------------------------------------- */
13447 +
13448 +static struct dentry *
13449 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13450 +                 int fh_type)
13451 +{
13452 +       struct dentry *dentry;
13453 +       __u32 *fh = fid->raw;
13454 +       struct au_branch *br;
13455 +       ino_t ino, dir_ino;
13456 +       struct au_nfsd_si_lock nsi_lock = {
13457 +               .force_lock     = 0
13458 +       };
13459 +
13460 +       dentry = ERR_PTR(-ESTALE);
13461 +       /* it should never happen, but the file handle is unreliable */
13462 +       if (unlikely(fh_len < Fh_tail))
13463 +               goto out;
13464 +       nsi_lock.sigen = fh[Fh_sigen];
13465 +       nsi_lock.br_id = fh[Fh_br_id];
13466 +
13467 +       /* branch id may be wrapped around */
13468 +       br = NULL;
13469 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13470 +               goto out;
13471 +       nsi_lock.force_lock = 1;
13472 +
13473 +       /* is this inode still cached? */
13474 +       ino = decode_ino(fh + Fh_ino);
13475 +       /* it should never happen */
13476 +       if (unlikely(ino == AUFS_ROOT_INO))
13477 +               goto out_unlock;
13478 +
13479 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13480 +       dentry = decode_by_ino(sb, ino, dir_ino);
13481 +       if (IS_ERR(dentry))
13482 +               goto out_unlock;
13483 +       if (dentry)
13484 +               goto accept;
13485 +
13486 +       /* is the parent dir cached? */
13487 +       br = au_sbr(sb, nsi_lock.bindex);
13488 +       au_lcnt_inc(&br->br_nfiles);
13489 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13490 +       if (IS_ERR(dentry))
13491 +               goto out_unlock;
13492 +       if (dentry)
13493 +               goto accept;
13494 +
13495 +       /* lookup path */
13496 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13497 +       if (IS_ERR(dentry))
13498 +               goto out_unlock;
13499 +       if (unlikely(!dentry))
13500 +               /* todo?: make it ESTALE */
13501 +               goto out_unlock;
13502 +
13503 +accept:
13504 +       if (!au_digen_test(dentry, au_sigen(sb))
13505 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13506 +               goto out_unlock; /* success */
13507 +
13508 +       dput(dentry);
13509 +       dentry = ERR_PTR(-ESTALE);
13510 +out_unlock:
13511 +       if (br)
13512 +               au_lcnt_dec(&br->br_nfiles);
13513 +       si_read_unlock(sb);
13514 +out:
13515 +       AuTraceErrPtr(dentry);
13516 +       return dentry;
13517 +}
13518 +
13519 +#if 0 /* reserved for future use */
13520 +/* support subtreecheck option */
13521 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13522 +                                       int fh_len, int fh_type)
13523 +{
13524 +       struct dentry *parent;
13525 +       __u32 *fh = fid->raw;
13526 +       ino_t dir_ino;
13527 +
13528 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13529 +       parent = decode_by_ino(sb, dir_ino, 0);
13530 +       if (IS_ERR(parent))
13531 +               goto out;
13532 +       if (!parent)
13533 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13534 +                                       dir_ino, fh, fh_len);
13535 +
13536 +out:
13537 +       AuTraceErrPtr(parent);
13538 +       return parent;
13539 +}
13540 +#endif
13541 +
13542 +/* ---------------------------------------------------------------------- */
13543 +
13544 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13545 +                         struct inode *dir)
13546 +{
13547 +       int err;
13548 +       aufs_bindex_t bindex;
13549 +       struct super_block *sb, *h_sb;
13550 +       struct dentry *dentry, *parent, *h_parent;
13551 +       struct inode *h_dir;
13552 +       struct au_branch *br;
13553 +
13554 +       err = -ENOSPC;
13555 +       if (unlikely(*max_len <= Fh_tail)) {
13556 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13557 +               goto out;
13558 +       }
13559 +
13560 +       err = FILEID_ROOT;
13561 +       if (inode->i_ino == AUFS_ROOT_INO) {
13562 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13563 +               goto out;
13564 +       }
13565 +
13566 +       h_parent = NULL;
13567 +       sb = inode->i_sb;
13568 +       err = si_read_lock(sb, AuLock_FLUSH);
13569 +       if (unlikely(err))
13570 +               goto out;
13571 +
13572 +#ifdef CONFIG_AUFS_DEBUG
13573 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13574 +               AuWarn1("NFS-exporting requires xino\n");
13575 +#endif
13576 +       err = -EIO;
13577 +       parent = NULL;
13578 +       ii_read_lock_child(inode);
13579 +       bindex = au_ibtop(inode);
13580 +       if (!dir) {
13581 +               dentry = d_find_any_alias(inode);
13582 +               if (unlikely(!dentry))
13583 +                       goto out_unlock;
13584 +               AuDebugOn(au_test_anon(dentry));
13585 +               parent = dget_parent(dentry);
13586 +               dput(dentry);
13587 +               if (unlikely(!parent))
13588 +                       goto out_unlock;
13589 +               if (d_really_is_positive(parent))
13590 +                       dir = d_inode(parent);
13591 +       }
13592 +
13593 +       ii_read_lock_parent(dir);
13594 +       h_dir = au_h_iptr(dir, bindex);
13595 +       ii_read_unlock(dir);
13596 +       if (unlikely(!h_dir))
13597 +               goto out_parent;
13598 +       h_parent = d_find_any_alias(h_dir);
13599 +       if (unlikely(!h_parent))
13600 +               goto out_hparent;
13601 +
13602 +       err = -EPERM;
13603 +       br = au_sbr(sb, bindex);
13604 +       h_sb = au_br_sb(br);
13605 +       if (unlikely(!h_sb->s_export_op)) {
13606 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13607 +               goto out_hparent;
13608 +       }
13609 +
13610 +       fh[Fh_br_id] = br->br_id;
13611 +       fh[Fh_sigen] = au_sigen(sb);
13612 +       encode_ino(fh + Fh_ino, inode->i_ino);
13613 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13614 +       fh[Fh_igen] = inode->i_generation;
13615 +
13616 +       *max_len -= Fh_tail;
13617 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13618 +                                          max_len,
13619 +                                          /*connectable or subtreecheck*/0);
13620 +       err = fh[Fh_h_type];
13621 +       *max_len += Fh_tail;
13622 +       /* todo: macros? */
13623 +       if (err != FILEID_INVALID)
13624 +               err = 99;
13625 +       else
13626 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13627 +
13628 +out_hparent:
13629 +       dput(h_parent);
13630 +out_parent:
13631 +       dput(parent);
13632 +out_unlock:
13633 +       ii_read_unlock(inode);
13634 +       si_read_unlock(sb);
13635 +out:
13636 +       if (unlikely(err < 0))
13637 +               err = FILEID_INVALID;
13638 +       return err;
13639 +}
13640 +
13641 +/* ---------------------------------------------------------------------- */
13642 +
13643 +static int aufs_commit_metadata(struct inode *inode)
13644 +{
13645 +       int err;
13646 +       aufs_bindex_t bindex;
13647 +       struct super_block *sb;
13648 +       struct inode *h_inode;
13649 +       int (*f)(struct inode *inode);
13650 +
13651 +       sb = inode->i_sb;
13652 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13653 +       ii_write_lock_child(inode);
13654 +       bindex = au_ibtop(inode);
13655 +       AuDebugOn(bindex < 0);
13656 +       h_inode = au_h_iptr(inode, bindex);
13657 +
13658 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13659 +       if (f)
13660 +               err = f(h_inode);
13661 +       else {
13662 +               struct writeback_control wbc = {
13663 +                       .sync_mode      = WB_SYNC_ALL,
13664 +                       .nr_to_write    = 0 /* metadata only */
13665 +               };
13666 +
13667 +               err = sync_inode(h_inode, &wbc);
13668 +       }
13669 +
13670 +       au_cpup_attr_timesizes(inode);
13671 +       ii_write_unlock(inode);
13672 +       si_read_unlock(sb);
13673 +       return err;
13674 +}
13675 +
13676 +/* ---------------------------------------------------------------------- */
13677 +
13678 +static struct export_operations aufs_export_op = {
13679 +       .fh_to_dentry           = aufs_fh_to_dentry,
13680 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13681 +       .encode_fh              = aufs_encode_fh,
13682 +       .commit_metadata        = aufs_commit_metadata
13683 +};
13684 +
13685 +void au_export_init(struct super_block *sb)
13686 +{
13687 +       struct au_sbinfo *sbinfo;
13688 +       __u32 u;
13689 +
13690 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13691 +                        && IS_MODULE(CONFIG_EXPORTFS),
13692 +                        AUFS_NAME ": unsupported configuration "
13693 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13694 +
13695 +       sb->s_export_op = &aufs_export_op;
13696 +       sbinfo = au_sbi(sb);
13697 +       sbinfo->si_xigen = NULL;
13698 +       get_random_bytes(&u, sizeof(u));
13699 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13700 +       atomic_set(&sbinfo->si_xigen_next, u);
13701 +}
13702 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13703 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13704 +++ linux/fs/aufs/fhsm.c        2020-01-27 10:57:18.172204883 +0100
13705 @@ -0,0 +1,427 @@
13706 +// SPDX-License-Identifier: GPL-2.0
13707 +/*
13708 + * Copyright (C) 2011-2020 Junjiro R. Okajima
13709 + *
13710 + * This program, aufs is free software; you can redistribute it and/or modify
13711 + * it under the terms of the GNU General Public License as published by
13712 + * the Free Software Foundation; either version 2 of the License, or
13713 + * (at your option) any later version.
13714 + *
13715 + * This program is distributed in the hope that it will be useful,
13716 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13717 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13718 + * GNU General Public License for more details.
13719 + *
13720 + * You should have received a copy of the GNU General Public License
13721 + * along with this program; if not, write to the Free Software
13722 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13723 + */
13724 +
13725 +/*
13726 + * File-based Hierarchy Storage Management
13727 + */
13728 +
13729 +#include <linux/anon_inodes.h>
13730 +#include <linux/poll.h>
13731 +#include <linux/seq_file.h>
13732 +#include <linux/statfs.h>
13733 +#include "aufs.h"
13734 +
13735 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13736 +{
13737 +       struct au_sbinfo *sbinfo;
13738 +       struct au_fhsm *fhsm;
13739 +
13740 +       SiMustAnyLock(sb);
13741 +
13742 +       sbinfo = au_sbi(sb);
13743 +       fhsm = &sbinfo->si_fhsm;
13744 +       AuDebugOn(!fhsm);
13745 +       return fhsm->fhsm_bottom;
13746 +}
13747 +
13748 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13749 +{
13750 +       struct au_sbinfo *sbinfo;
13751 +       struct au_fhsm *fhsm;
13752 +
13753 +       SiMustWriteLock(sb);
13754 +
13755 +       sbinfo = au_sbi(sb);
13756 +       fhsm = &sbinfo->si_fhsm;
13757 +       AuDebugOn(!fhsm);
13758 +       fhsm->fhsm_bottom = bindex;
13759 +}
13760 +
13761 +/* ---------------------------------------------------------------------- */
13762 +
13763 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13764 +{
13765 +       struct au_br_fhsm *bf;
13766 +
13767 +       bf = br->br_fhsm;
13768 +       MtxMustLock(&bf->bf_lock);
13769 +
13770 +       return !bf->bf_readable
13771 +               || time_after(jiffies,
13772 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13773 +}
13774 +
13775 +/* ---------------------------------------------------------------------- */
13776 +
13777 +static void au_fhsm_notify(struct super_block *sb, int val)
13778 +{
13779 +       struct au_sbinfo *sbinfo;
13780 +       struct au_fhsm *fhsm;
13781 +
13782 +       SiMustAnyLock(sb);
13783 +
13784 +       sbinfo = au_sbi(sb);
13785 +       fhsm = &sbinfo->si_fhsm;
13786 +       if (au_fhsm_pid(fhsm)
13787 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13788 +               atomic_set(&fhsm->fhsm_readable, val);
13789 +               if (val)
13790 +                       wake_up(&fhsm->fhsm_wqh);
13791 +       }
13792 +}
13793 +
13794 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13795 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13796 +{
13797 +       int err;
13798 +       struct au_branch *br;
13799 +       struct au_br_fhsm *bf;
13800 +
13801 +       br = au_sbr(sb, bindex);
13802 +       AuDebugOn(au_br_rdonly(br));
13803 +       bf = br->br_fhsm;
13804 +       AuDebugOn(!bf);
13805 +
13806 +       if (do_lock)
13807 +               mutex_lock(&bf->bf_lock);
13808 +       else
13809 +               MtxMustLock(&bf->bf_lock);
13810 +
13811 +       /* sb->s_root for NFS is unreliable */
13812 +       err = au_br_stfs(br, &bf->bf_stfs);
13813 +       if (unlikely(err)) {
13814 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13815 +               goto out;
13816 +       }
13817 +
13818 +       bf->bf_jiffy = jiffies;
13819 +       bf->bf_readable = 1;
13820 +       if (do_notify)
13821 +               au_fhsm_notify(sb, /*val*/1);
13822 +       if (rstfs)
13823 +               *rstfs = bf->bf_stfs;
13824 +
13825 +out:
13826 +       if (do_lock)
13827 +               mutex_unlock(&bf->bf_lock);
13828 +       au_fhsm_notify(sb, /*val*/1);
13829 +
13830 +       return err;
13831 +}
13832 +
13833 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13834 +{
13835 +       int err;
13836 +       struct au_sbinfo *sbinfo;
13837 +       struct au_fhsm *fhsm;
13838 +       struct au_branch *br;
13839 +       struct au_br_fhsm *bf;
13840 +
13841 +       AuDbg("b%d, force %d\n", bindex, force);
13842 +       SiMustAnyLock(sb);
13843 +
13844 +       sbinfo = au_sbi(sb);
13845 +       fhsm = &sbinfo->si_fhsm;
13846 +       if (!au_ftest_si(sbinfo, FHSM)
13847 +           || fhsm->fhsm_bottom == bindex)
13848 +               return;
13849 +
13850 +       br = au_sbr(sb, bindex);
13851 +       bf = br->br_fhsm;
13852 +       AuDebugOn(!bf);
13853 +       mutex_lock(&bf->bf_lock);
13854 +       if (force
13855 +           || au_fhsm_pid(fhsm)
13856 +           || au_fhsm_test_jiffy(sbinfo, br))
13857 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13858 +                                 /*do_notify*/1);
13859 +       mutex_unlock(&bf->bf_lock);
13860 +}
13861 +
13862 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13863 +{
13864 +       aufs_bindex_t bindex, bbot;
13865 +       struct au_branch *br;
13866 +
13867 +       /* exclude the bottom */
13868 +       bbot = au_fhsm_bottom(sb);
13869 +       for (bindex = 0; bindex < bbot; bindex++) {
13870 +               br = au_sbr(sb, bindex);
13871 +               if (au_br_fhsm(br->br_perm))
13872 +                       au_fhsm_wrote(sb, bindex, force);
13873 +       }
13874 +}
13875 +
13876 +/* ---------------------------------------------------------------------- */
13877 +
13878 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13879 +{
13880 +       __poll_t mask;
13881 +       struct au_sbinfo *sbinfo;
13882 +       struct au_fhsm *fhsm;
13883 +
13884 +       mask = 0;
13885 +       sbinfo = file->private_data;
13886 +       fhsm = &sbinfo->si_fhsm;
13887 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13888 +       if (atomic_read(&fhsm->fhsm_readable))
13889 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13890 +
13891 +       if (!mask)
13892 +               AuDbg("mask 0x%x\n", mask);
13893 +       return mask;
13894 +}
13895 +
13896 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13897 +                             struct aufs_stfs *stfs, __s16 brid)
13898 +{
13899 +       int err;
13900 +
13901 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13902 +       if (!err)
13903 +               err = __put_user(brid, &stbr->brid);
13904 +       if (unlikely(err))
13905 +               err = -EFAULT;
13906 +
13907 +       return err;
13908 +}
13909 +
13910 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13911 +                              struct aufs_stbr __user *stbr, size_t count)
13912 +{
13913 +       ssize_t err;
13914 +       int nstbr;
13915 +       aufs_bindex_t bindex, bbot;
13916 +       struct au_branch *br;
13917 +       struct au_br_fhsm *bf;
13918 +
13919 +       /* except the bottom branch */
13920 +       err = 0;
13921 +       nstbr = 0;
13922 +       bbot = au_fhsm_bottom(sb);
13923 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13924 +               br = au_sbr(sb, bindex);
13925 +               if (!au_br_fhsm(br->br_perm))
13926 +                       continue;
13927 +
13928 +               bf = br->br_fhsm;
13929 +               mutex_lock(&bf->bf_lock);
13930 +               if (bf->bf_readable) {
13931 +                       err = -EFAULT;
13932 +                       if (count >= sizeof(*stbr))
13933 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13934 +                                                         br->br_id);
13935 +                       if (!err) {
13936 +                               bf->bf_readable = 0;
13937 +                               count -= sizeof(*stbr);
13938 +                               nstbr++;
13939 +                       }
13940 +               }
13941 +               mutex_unlock(&bf->bf_lock);
13942 +       }
13943 +       if (!err)
13944 +               err = sizeof(*stbr) * nstbr;
13945 +
13946 +       return err;
13947 +}
13948 +
13949 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13950 +                          loff_t *pos)
13951 +{
13952 +       ssize_t err;
13953 +       int readable;
13954 +       aufs_bindex_t nfhsm, bindex, bbot;
13955 +       struct au_sbinfo *sbinfo;
13956 +       struct au_fhsm *fhsm;
13957 +       struct au_branch *br;
13958 +       struct super_block *sb;
13959 +
13960 +       err = 0;
13961 +       sbinfo = file->private_data;
13962 +       fhsm = &sbinfo->si_fhsm;
13963 +need_data:
13964 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13965 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13966 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13967 +                       err = -EAGAIN;
13968 +               else
13969 +                       err = wait_event_interruptible_locked_irq
13970 +                               (fhsm->fhsm_wqh,
13971 +                                atomic_read(&fhsm->fhsm_readable));
13972 +       }
13973 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13974 +       if (unlikely(err))
13975 +               goto out;
13976 +
13977 +       /* sb may already be dead */
13978 +       au_rw_read_lock(&sbinfo->si_rwsem);
13979 +       readable = atomic_read(&fhsm->fhsm_readable);
13980 +       if (readable > 0) {
13981 +               sb = sbinfo->si_sb;
13982 +               AuDebugOn(!sb);
13983 +               /* exclude the bottom branch */
13984 +               nfhsm = 0;
13985 +               bbot = au_fhsm_bottom(sb);
13986 +               for (bindex = 0; bindex < bbot; bindex++) {
13987 +                       br = au_sbr(sb, bindex);
13988 +                       if (au_br_fhsm(br->br_perm))
13989 +                               nfhsm++;
13990 +               }
13991 +               err = -EMSGSIZE;
13992 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13993 +                       atomic_set(&fhsm->fhsm_readable, 0);
13994 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13995 +                                            count);
13996 +               }
13997 +       }
13998 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13999 +       if (!readable)
14000 +               goto need_data;
14001 +
14002 +out:
14003 +       return err;
14004 +}
14005 +
14006 +static int au_fhsm_release(struct inode *inode, struct file *file)
14007 +{
14008 +       struct au_sbinfo *sbinfo;
14009 +       struct au_fhsm *fhsm;
14010 +
14011 +       /* sb may already be dead */
14012 +       sbinfo = file->private_data;
14013 +       fhsm = &sbinfo->si_fhsm;
14014 +       spin_lock(&fhsm->fhsm_spin);
14015 +       fhsm->fhsm_pid = 0;
14016 +       spin_unlock(&fhsm->fhsm_spin);
14017 +       kobject_put(&sbinfo->si_kobj);
14018 +
14019 +       return 0;
14020 +}
14021 +
14022 +static const struct file_operations au_fhsm_fops = {
14023 +       .owner          = THIS_MODULE,
14024 +       .llseek         = noop_llseek,
14025 +       .read           = au_fhsm_read,
14026 +       .poll           = au_fhsm_poll,
14027 +       .release        = au_fhsm_release
14028 +};
14029 +
14030 +int au_fhsm_fd(struct super_block *sb, int oflags)
14031 +{
14032 +       int err, fd;
14033 +       struct au_sbinfo *sbinfo;
14034 +       struct au_fhsm *fhsm;
14035 +
14036 +       err = -EPERM;
14037 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
14038 +               goto out;
14039 +
14040 +       err = -EINVAL;
14041 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14042 +               goto out;
14043 +
14044 +       err = 0;
14045 +       sbinfo = au_sbi(sb);
14046 +       fhsm = &sbinfo->si_fhsm;
14047 +       spin_lock(&fhsm->fhsm_spin);
14048 +       if (!fhsm->fhsm_pid)
14049 +               fhsm->fhsm_pid = current->pid;
14050 +       else
14051 +               err = -EBUSY;
14052 +       spin_unlock(&fhsm->fhsm_spin);
14053 +       if (unlikely(err))
14054 +               goto out;
14055 +
14056 +       oflags |= O_RDONLY;
14057 +       /* oflags |= FMODE_NONOTIFY; */
14058 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14059 +       err = fd;
14060 +       if (unlikely(fd < 0))
14061 +               goto out_pid;
14062 +
14063 +       /* succeed regardless 'fhsm' status */
14064 +       kobject_get(&sbinfo->si_kobj);
14065 +       si_noflush_read_lock(sb);
14066 +       if (au_ftest_si(sbinfo, FHSM))
14067 +               au_fhsm_wrote_all(sb, /*force*/0);
14068 +       si_read_unlock(sb);
14069 +       goto out; /* success */
14070 +
14071 +out_pid:
14072 +       spin_lock(&fhsm->fhsm_spin);
14073 +       fhsm->fhsm_pid = 0;
14074 +       spin_unlock(&fhsm->fhsm_spin);
14075 +out:
14076 +       AuTraceErr(err);
14077 +       return err;
14078 +}
14079 +
14080 +/* ---------------------------------------------------------------------- */
14081 +
14082 +int au_fhsm_br_alloc(struct au_branch *br)
14083 +{
14084 +       int err;
14085 +
14086 +       err = 0;
14087 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14088 +       if (br->br_fhsm)
14089 +               au_br_fhsm_init(br->br_fhsm);
14090 +       else
14091 +               err = -ENOMEM;
14092 +
14093 +       return err;
14094 +}
14095 +
14096 +/* ---------------------------------------------------------------------- */
14097 +
14098 +void au_fhsm_fin(struct super_block *sb)
14099 +{
14100 +       au_fhsm_notify(sb, /*val*/-1);
14101 +}
14102 +
14103 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14104 +{
14105 +       struct au_fhsm *fhsm;
14106 +
14107 +       fhsm = &sbinfo->si_fhsm;
14108 +       spin_lock_init(&fhsm->fhsm_spin);
14109 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14110 +       atomic_set(&fhsm->fhsm_readable, 0);
14111 +       fhsm->fhsm_expire
14112 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14113 +       fhsm->fhsm_bottom = -1;
14114 +}
14115 +
14116 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14117 +{
14118 +       sbinfo->si_fhsm.fhsm_expire
14119 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14120 +}
14121 +
14122 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14123 +{
14124 +       unsigned int u;
14125 +
14126 +       if (!au_ftest_si(sbinfo, FHSM))
14127 +               return;
14128 +
14129 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14130 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14131 +               seq_printf(seq, ",fhsm_sec=%u", u);
14132 +}
14133 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14134 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14135 +++ linux/fs/aufs/file.c        2020-01-27 10:57:18.172204883 +0100
14136 @@ -0,0 +1,863 @@
14137 +// SPDX-License-Identifier: GPL-2.0
14138 +/*
14139 + * Copyright (C) 2005-2020 Junjiro R. Okajima
14140 + *
14141 + * This program, aufs is free software; you can redistribute it and/or modify
14142 + * it under the terms of the GNU General Public License as published by
14143 + * the Free Software Foundation; either version 2 of the License, or
14144 + * (at your option) any later version.
14145 + *
14146 + * This program is distributed in the hope that it will be useful,
14147 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14148 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14149 + * GNU General Public License for more details.
14150 + *
14151 + * You should have received a copy of the GNU General Public License
14152 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14153 + */
14154 +
14155 +/*
14156 + * handling file/dir, and address_space operation
14157 + */
14158 +
14159 +#ifdef CONFIG_AUFS_DEBUG
14160 +#include <linux/migrate.h>
14161 +#endif
14162 +#include <linux/pagemap.h>
14163 +#include "aufs.h"
14164 +
14165 +/* drop flags for writing */
14166 +unsigned int au_file_roflags(unsigned int flags)
14167 +{
14168 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14169 +       flags |= O_RDONLY | O_NOATIME;
14170 +       return flags;
14171 +}
14172 +
14173 +/* common functions to regular file and dir */
14174 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14175 +                      struct file *file, int force_wr)
14176 +{
14177 +       struct file *h_file;
14178 +       struct dentry *h_dentry;
14179 +       struct inode *h_inode;
14180 +       struct super_block *sb;
14181 +       struct au_branch *br;
14182 +       struct path h_path;
14183 +       int err;
14184 +
14185 +       /* a race condition can happen between open and unlink/rmdir */
14186 +       h_file = ERR_PTR(-ENOENT);
14187 +       h_dentry = au_h_dptr(dentry, bindex);
14188 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14189 +               goto out;
14190 +       h_inode = d_inode(h_dentry);
14191 +       spin_lock(&h_dentry->d_lock);
14192 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14193 +               /* || !d_inode(dentry)->i_nlink */
14194 +               ;
14195 +       spin_unlock(&h_dentry->d_lock);
14196 +       if (unlikely(err))
14197 +               goto out;
14198 +
14199 +       sb = dentry->d_sb;
14200 +       br = au_sbr(sb, bindex);
14201 +       err = au_br_test_oflag(flags, br);
14202 +       h_file = ERR_PTR(err);
14203 +       if (unlikely(err))
14204 +               goto out;
14205 +
14206 +       /* drop flags for writing */
14207 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14208 +               if (force_wr && !(flags & O_WRONLY))
14209 +                       force_wr = 0;
14210 +               flags = au_file_roflags(flags);
14211 +               if (force_wr) {
14212 +                       h_file = ERR_PTR(-EROFS);
14213 +                       flags = au_file_roflags(flags);
14214 +                       if (unlikely(vfsub_native_ro(h_inode)
14215 +                                    || IS_APPEND(h_inode)))
14216 +                               goto out;
14217 +                       flags &= ~O_ACCMODE;
14218 +                       flags |= O_WRONLY;
14219 +               }
14220 +       }
14221 +       flags &= ~O_CREAT;
14222 +       au_lcnt_inc(&br->br_nfiles);
14223 +       h_path.dentry = h_dentry;
14224 +       h_path.mnt = au_br_mnt(br);
14225 +       h_file = vfsub_dentry_open(&h_path, flags);
14226 +       if (IS_ERR(h_file))
14227 +               goto out_br;
14228 +
14229 +       if (flags & __FMODE_EXEC) {
14230 +               err = deny_write_access(h_file);
14231 +               if (unlikely(err)) {
14232 +                       fput(h_file);
14233 +                       h_file = ERR_PTR(err);
14234 +                       goto out_br;
14235 +               }
14236 +       }
14237 +       fsnotify_open(h_file);
14238 +       goto out; /* success */
14239 +
14240 +out_br:
14241 +       au_lcnt_dec(&br->br_nfiles);
14242 +out:
14243 +       return h_file;
14244 +}
14245 +
14246 +static int au_cmoo(struct dentry *dentry)
14247 +{
14248 +       int err, cmoo, matched;
14249 +       unsigned int udba;
14250 +       struct path h_path;
14251 +       struct au_pin pin;
14252 +       struct au_cp_generic cpg = {
14253 +               .dentry = dentry,
14254 +               .bdst   = -1,
14255 +               .bsrc   = -1,
14256 +               .len    = -1,
14257 +               .pin    = &pin,
14258 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14259 +       };
14260 +       struct inode *delegated;
14261 +       struct super_block *sb;
14262 +       struct au_sbinfo *sbinfo;
14263 +       struct au_fhsm *fhsm;
14264 +       pid_t pid;
14265 +       struct au_branch *br;
14266 +       struct dentry *parent;
14267 +       struct au_hinode *hdir;
14268 +
14269 +       DiMustWriteLock(dentry);
14270 +       IiMustWriteLock(d_inode(dentry));
14271 +
14272 +       err = 0;
14273 +       if (IS_ROOT(dentry))
14274 +               goto out;
14275 +       cpg.bsrc = au_dbtop(dentry);
14276 +       if (!cpg.bsrc)
14277 +               goto out;
14278 +
14279 +       sb = dentry->d_sb;
14280 +       sbinfo = au_sbi(sb);
14281 +       fhsm = &sbinfo->si_fhsm;
14282 +       pid = au_fhsm_pid(fhsm);
14283 +       rcu_read_lock();
14284 +       matched = (pid
14285 +                  && (current->pid == pid
14286 +                      || rcu_dereference(current->real_parent)->pid == pid));
14287 +       rcu_read_unlock();
14288 +       if (matched)
14289 +               goto out;
14290 +
14291 +       br = au_sbr(sb, cpg.bsrc);
14292 +       cmoo = au_br_cmoo(br->br_perm);
14293 +       if (!cmoo)
14294 +               goto out;
14295 +       if (!d_is_reg(dentry))
14296 +               cmoo &= AuBrAttr_COO_ALL;
14297 +       if (!cmoo)
14298 +               goto out;
14299 +
14300 +       parent = dget_parent(dentry);
14301 +       di_write_lock_parent(parent);
14302 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14303 +       cpg.bdst = err;
14304 +       if (unlikely(err < 0)) {
14305 +               err = 0;        /* there is no upper writable branch */
14306 +               goto out_dgrade;
14307 +       }
14308 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14309 +
14310 +       /* do not respect the coo attrib for the target branch */
14311 +       err = au_cpup_dirs(dentry, cpg.bdst);
14312 +       if (unlikely(err))
14313 +               goto out_dgrade;
14314 +
14315 +       di_downgrade_lock(parent, AuLock_IR);
14316 +       udba = au_opt_udba(sb);
14317 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14318 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14319 +       if (unlikely(err))
14320 +               goto out_parent;
14321 +
14322 +       err = au_sio_cpup_simple(&cpg);
14323 +       au_unpin(&pin);
14324 +       if (unlikely(err))
14325 +               goto out_parent;
14326 +       if (!(cmoo & AuBrWAttr_MOO))
14327 +               goto out_parent; /* success */
14328 +
14329 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14330 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14331 +       if (unlikely(err))
14332 +               goto out_parent;
14333 +
14334 +       h_path.mnt = au_br_mnt(br);
14335 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14336 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14337 +       delegated = NULL;
14338 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14339 +       au_unpin(&pin);
14340 +       /* todo: keep h_dentry or not? */
14341 +       if (unlikely(err == -EWOULDBLOCK)) {
14342 +               pr_warn("cannot retry for NFSv4 delegation"
14343 +                       " for an internal unlink\n");
14344 +               iput(delegated);
14345 +       }
14346 +       if (unlikely(err)) {
14347 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14348 +                      dentry, err);
14349 +               err = 0;
14350 +       }
14351 +       goto out_parent; /* success */
14352 +
14353 +out_dgrade:
14354 +       di_downgrade_lock(parent, AuLock_IR);
14355 +out_parent:
14356 +       di_read_unlock(parent, AuLock_IR);
14357 +       dput(parent);
14358 +out:
14359 +       AuTraceErr(err);
14360 +       return err;
14361 +}
14362 +
14363 +int au_do_open(struct file *file, struct au_do_open_args *args)
14364 +{
14365 +       int err, aopen = args->aopen;
14366 +       struct dentry *dentry;
14367 +       struct au_finfo *finfo;
14368 +
14369 +       if (!aopen)
14370 +               err = au_finfo_init(file, args->fidir);
14371 +       else {
14372 +               lockdep_off();
14373 +               err = au_finfo_init(file, args->fidir);
14374 +               lockdep_on();
14375 +       }
14376 +       if (unlikely(err))
14377 +               goto out;
14378 +
14379 +       dentry = file->f_path.dentry;
14380 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14381 +       di_write_lock_child(dentry);
14382 +       err = au_cmoo(dentry);
14383 +       di_downgrade_lock(dentry, AuLock_IR);
14384 +       if (!err) {
14385 +               if (!aopen)
14386 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14387 +               else {
14388 +                       lockdep_off();
14389 +                       err = args->open(file, vfsub_file_flags(file),
14390 +                                        args->h_file);
14391 +                       lockdep_on();
14392 +               }
14393 +       }
14394 +       di_read_unlock(dentry, AuLock_IR);
14395 +
14396 +       finfo = au_fi(file);
14397 +       if (!err) {
14398 +               finfo->fi_file = file;
14399 +               au_hbl_add(&finfo->fi_hlist,
14400 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14401 +       }
14402 +       if (!aopen)
14403 +               fi_write_unlock(file);
14404 +       else {
14405 +               lockdep_off();
14406 +               fi_write_unlock(file);
14407 +               lockdep_on();
14408 +       }
14409 +       if (unlikely(err)) {
14410 +               finfo->fi_hdir = NULL;
14411 +               au_finfo_fin(file);
14412 +       }
14413 +
14414 +out:
14415 +       AuTraceErr(err);
14416 +       return err;
14417 +}
14418 +
14419 +int au_reopen_nondir(struct file *file)
14420 +{
14421 +       int err;
14422 +       aufs_bindex_t btop;
14423 +       struct dentry *dentry;
14424 +       struct au_branch *br;
14425 +       struct file *h_file, *h_file_tmp;
14426 +
14427 +       dentry = file->f_path.dentry;
14428 +       btop = au_dbtop(dentry);
14429 +       br = au_sbr(dentry->d_sb, btop);
14430 +       h_file_tmp = NULL;
14431 +       if (au_fbtop(file) == btop) {
14432 +               h_file = au_hf_top(file);
14433 +               if (file->f_mode == h_file->f_mode)
14434 +                       return 0; /* success */
14435 +               h_file_tmp = h_file;
14436 +               get_file(h_file_tmp);
14437 +               au_lcnt_inc(&br->br_nfiles);
14438 +               au_set_h_fptr(file, btop, NULL);
14439 +       }
14440 +       AuDebugOn(au_fi(file)->fi_hdir);
14441 +       /*
14442 +        * it can happen
14443 +        * file exists on both of rw and ro
14444 +        * open --> dbtop and fbtop are both 0
14445 +        * prepend a branch as rw, "rw" become ro
14446 +        * remove rw/file
14447 +        * delete the top branch, "rw" becomes rw again
14448 +        *      --> dbtop is 1, fbtop is still 0
14449 +        * write --> fbtop is 0 but dbtop is 1
14450 +        */
14451 +       /* AuDebugOn(au_fbtop(file) < btop); */
14452 +
14453 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14454 +                          file, /*force_wr*/0);
14455 +       err = PTR_ERR(h_file);
14456 +       if (IS_ERR(h_file)) {
14457 +               if (h_file_tmp) {
14458 +                       /* revert */
14459 +                       au_set_h_fptr(file, btop, h_file_tmp);
14460 +                       h_file_tmp = NULL;
14461 +               }
14462 +               goto out; /* todo: close all? */
14463 +       }
14464 +
14465 +       err = 0;
14466 +       au_set_fbtop(file, btop);
14467 +       au_set_h_fptr(file, btop, h_file);
14468 +       au_update_figen(file);
14469 +       /* todo: necessary? */
14470 +       /* file->f_ra = h_file->f_ra; */
14471 +
14472 +out:
14473 +       if (h_file_tmp) {
14474 +               fput(h_file_tmp);
14475 +               au_lcnt_dec(&br->br_nfiles);
14476 +       }
14477 +       return err;
14478 +}
14479 +
14480 +/* ---------------------------------------------------------------------- */
14481 +
14482 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14483 +                       struct dentry *hi_wh)
14484 +{
14485 +       int err;
14486 +       aufs_bindex_t btop;
14487 +       struct au_dinfo *dinfo;
14488 +       struct dentry *h_dentry;
14489 +       struct au_hdentry *hdp;
14490 +
14491 +       dinfo = au_di(file->f_path.dentry);
14492 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14493 +
14494 +       btop = dinfo->di_btop;
14495 +       dinfo->di_btop = btgt;
14496 +       hdp = au_hdentry(dinfo, btgt);
14497 +       h_dentry = hdp->hd_dentry;
14498 +       hdp->hd_dentry = hi_wh;
14499 +       err = au_reopen_nondir(file);
14500 +       hdp->hd_dentry = h_dentry;
14501 +       dinfo->di_btop = btop;
14502 +
14503 +       return err;
14504 +}
14505 +
14506 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14507 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14508 +{
14509 +       int err;
14510 +       struct inode *inode, *h_inode;
14511 +       struct dentry *h_dentry, *hi_wh;
14512 +       struct au_cp_generic cpg = {
14513 +               .dentry = file->f_path.dentry,
14514 +               .bdst   = bcpup,
14515 +               .bsrc   = -1,
14516 +               .len    = len,
14517 +               .pin    = pin
14518 +       };
14519 +
14520 +       au_update_dbtop(cpg.dentry);
14521 +       inode = d_inode(cpg.dentry);
14522 +       h_inode = NULL;
14523 +       if (au_dbtop(cpg.dentry) <= bcpup
14524 +           && au_dbbot(cpg.dentry) >= bcpup) {
14525 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14526 +               if (h_dentry && d_is_positive(h_dentry))
14527 +                       h_inode = d_inode(h_dentry);
14528 +       }
14529 +       hi_wh = au_hi_wh(inode, bcpup);
14530 +       if (!hi_wh && !h_inode)
14531 +               err = au_sio_cpup_wh(&cpg, file);
14532 +       else
14533 +               /* already copied-up after unlink */
14534 +               err = au_reopen_wh(file, bcpup, hi_wh);
14535 +
14536 +       if (!err
14537 +           && (inode->i_nlink > 1
14538 +               || (inode->i_state & I_LINKABLE))
14539 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14540 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14541 +
14542 +       return err;
14543 +}
14544 +
14545 +/*
14546 + * prepare the @file for writing.
14547 + */
14548 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14549 +{
14550 +       int err;
14551 +       aufs_bindex_t dbtop;
14552 +       struct dentry *parent;
14553 +       struct inode *inode;
14554 +       struct super_block *sb;
14555 +       struct file *h_file;
14556 +       struct au_cp_generic cpg = {
14557 +               .dentry = file->f_path.dentry,
14558 +               .bdst   = -1,
14559 +               .bsrc   = -1,
14560 +               .len    = len,
14561 +               .pin    = pin,
14562 +               .flags  = AuCpup_DTIME
14563 +       };
14564 +
14565 +       sb = cpg.dentry->d_sb;
14566 +       inode = d_inode(cpg.dentry);
14567 +       cpg.bsrc = au_fbtop(file);
14568 +       err = au_test_ro(sb, cpg.bsrc, inode);
14569 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14570 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14571 +                            /*flags*/0);
14572 +               goto out;
14573 +       }
14574 +
14575 +       /* need to cpup or reopen */
14576 +       parent = dget_parent(cpg.dentry);
14577 +       di_write_lock_parent(parent);
14578 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14579 +       cpg.bdst = err;
14580 +       if (unlikely(err < 0))
14581 +               goto out_dgrade;
14582 +       err = 0;
14583 +
14584 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14585 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14586 +               if (unlikely(err))
14587 +                       goto out_dgrade;
14588 +       }
14589 +
14590 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14591 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14592 +       if (unlikely(err))
14593 +               goto out_dgrade;
14594 +
14595 +       dbtop = au_dbtop(cpg.dentry);
14596 +       if (dbtop <= cpg.bdst)
14597 +               cpg.bsrc = cpg.bdst;
14598 +
14599 +       if (dbtop <= cpg.bdst           /* just reopen */
14600 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14601 +               ) {
14602 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14603 +               if (IS_ERR(h_file))
14604 +                       err = PTR_ERR(h_file);
14605 +               else {
14606 +                       di_downgrade_lock(parent, AuLock_IR);
14607 +                       if (dbtop > cpg.bdst)
14608 +                               err = au_sio_cpup_simple(&cpg);
14609 +                       if (!err)
14610 +                               err = au_reopen_nondir(file);
14611 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14612 +               }
14613 +       } else {                        /* copyup as wh and reopen */
14614 +               /*
14615 +                * since writable hfsplus branch is not supported,
14616 +                * h_open_pre/post() are unnecessary.
14617 +                */
14618 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14619 +               di_downgrade_lock(parent, AuLock_IR);
14620 +       }
14621 +
14622 +       if (!err) {
14623 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14624 +               goto out_dput; /* success */
14625 +       }
14626 +       au_unpin(pin);
14627 +       goto out_unlock;
14628 +
14629 +out_dgrade:
14630 +       di_downgrade_lock(parent, AuLock_IR);
14631 +out_unlock:
14632 +       di_read_unlock(parent, AuLock_IR);
14633 +out_dput:
14634 +       dput(parent);
14635 +out:
14636 +       return err;
14637 +}
14638 +
14639 +/* ---------------------------------------------------------------------- */
14640 +
14641 +int au_do_flush(struct file *file, fl_owner_t id,
14642 +               int (*flush)(struct file *file, fl_owner_t id))
14643 +{
14644 +       int err;
14645 +       struct super_block *sb;
14646 +       struct inode *inode;
14647 +
14648 +       inode = file_inode(file);
14649 +       sb = inode->i_sb;
14650 +       si_noflush_read_lock(sb);
14651 +       fi_read_lock(file);
14652 +       ii_read_lock_child(inode);
14653 +
14654 +       err = flush(file, id);
14655 +       au_cpup_attr_timesizes(inode);
14656 +
14657 +       ii_read_unlock(inode);
14658 +       fi_read_unlock(file);
14659 +       si_read_unlock(sb);
14660 +       return err;
14661 +}
14662 +
14663 +/* ---------------------------------------------------------------------- */
14664 +
14665 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14666 +{
14667 +       int err;
14668 +       struct au_pin pin;
14669 +       struct au_finfo *finfo;
14670 +       struct dentry *parent, *hi_wh;
14671 +       struct inode *inode;
14672 +       struct super_block *sb;
14673 +       struct au_cp_generic cpg = {
14674 +               .dentry = file->f_path.dentry,
14675 +               .bdst   = -1,
14676 +               .bsrc   = -1,
14677 +               .len    = -1,
14678 +               .pin    = &pin,
14679 +               .flags  = AuCpup_DTIME
14680 +       };
14681 +
14682 +       FiMustWriteLock(file);
14683 +
14684 +       err = 0;
14685 +       finfo = au_fi(file);
14686 +       sb = cpg.dentry->d_sb;
14687 +       inode = d_inode(cpg.dentry);
14688 +       cpg.bdst = au_ibtop(inode);
14689 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14690 +               goto out;
14691 +
14692 +       parent = dget_parent(cpg.dentry);
14693 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14694 +               di_read_lock_parent(parent, !AuLock_IR);
14695 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14696 +               cpg.bdst = err;
14697 +               di_read_unlock(parent, !AuLock_IR);
14698 +               if (unlikely(err < 0))
14699 +                       goto out_parent;
14700 +               err = 0;
14701 +       }
14702 +
14703 +       di_read_lock_parent(parent, AuLock_IR);
14704 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14705 +       if (!S_ISDIR(inode->i_mode)
14706 +           && au_opt_test(au_mntflags(sb), PLINK)
14707 +           && au_plink_test(inode)
14708 +           && !d_unhashed(cpg.dentry)
14709 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14710 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14711 +               if (unlikely(err))
14712 +                       goto out_unlock;
14713 +
14714 +               /* always superio. */
14715 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14716 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14717 +               if (!err) {
14718 +                       err = au_sio_cpup_simple(&cpg);
14719 +                       au_unpin(&pin);
14720 +               }
14721 +       } else if (hi_wh) {
14722 +               /* already copied-up after unlink */
14723 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14724 +               *need_reopen = 0;
14725 +       }
14726 +
14727 +out_unlock:
14728 +       di_read_unlock(parent, AuLock_IR);
14729 +out_parent:
14730 +       dput(parent);
14731 +out:
14732 +       return err;
14733 +}
14734 +
14735 +static void au_do_refresh_dir(struct file *file)
14736 +{
14737 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14738 +       struct au_hfile *p, tmp, *q;
14739 +       struct au_finfo *finfo;
14740 +       struct super_block *sb;
14741 +       struct au_fidir *fidir;
14742 +
14743 +       FiMustWriteLock(file);
14744 +
14745 +       sb = file->f_path.dentry->d_sb;
14746 +       finfo = au_fi(file);
14747 +       fidir = finfo->fi_hdir;
14748 +       AuDebugOn(!fidir);
14749 +       p = fidir->fd_hfile + finfo->fi_btop;
14750 +       brid = p->hf_br->br_id;
14751 +       bbot = fidir->fd_bbot;
14752 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14753 +               if (!p->hf_file)
14754 +                       continue;
14755 +
14756 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14757 +               if (new_bindex == bindex)
14758 +                       continue;
14759 +               if (new_bindex < 0) {
14760 +                       au_set_h_fptr(file, bindex, NULL);
14761 +                       continue;
14762 +               }
14763 +
14764 +               /* swap two lower inode, and loop again */
14765 +               q = fidir->fd_hfile + new_bindex;
14766 +               tmp = *q;
14767 +               *q = *p;
14768 +               *p = tmp;
14769 +               if (tmp.hf_file) {
14770 +                       bindex--;
14771 +                       p--;
14772 +               }
14773 +       }
14774 +
14775 +       p = fidir->fd_hfile;
14776 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14777 +               bbot = au_sbbot(sb);
14778 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14779 +                    finfo->fi_btop++, p++)
14780 +                       if (p->hf_file) {
14781 +                               if (file_inode(p->hf_file))
14782 +                                       break;
14783 +                               au_hfput(p, /*execed*/0);
14784 +                       }
14785 +       } else {
14786 +               bbot = au_br_index(sb, brid);
14787 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14788 +                    finfo->fi_btop++, p++)
14789 +                       if (p->hf_file)
14790 +                               au_hfput(p, /*execed*/0);
14791 +               bbot = au_sbbot(sb);
14792 +       }
14793 +
14794 +       p = fidir->fd_hfile + bbot;
14795 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14796 +            fidir->fd_bbot--, p--)
14797 +               if (p->hf_file) {
14798 +                       if (file_inode(p->hf_file))
14799 +                               break;
14800 +                       au_hfput(p, /*execed*/0);
14801 +               }
14802 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14803 +}
14804 +
14805 +/*
14806 + * after branch manipulating, refresh the file.
14807 + */
14808 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14809 +{
14810 +       int err, need_reopen, nbr;
14811 +       aufs_bindex_t bbot, bindex;
14812 +       struct dentry *dentry;
14813 +       struct super_block *sb;
14814 +       struct au_finfo *finfo;
14815 +       struct au_hfile *hfile;
14816 +
14817 +       dentry = file->f_path.dentry;
14818 +       sb = dentry->d_sb;
14819 +       nbr = au_sbbot(sb) + 1;
14820 +       finfo = au_fi(file);
14821 +       if (!finfo->fi_hdir) {
14822 +               hfile = &finfo->fi_htop;
14823 +               AuDebugOn(!hfile->hf_file);
14824 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14825 +               AuDebugOn(bindex < 0);
14826 +               if (bindex != finfo->fi_btop)
14827 +                       au_set_fbtop(file, bindex);
14828 +       } else {
14829 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14830 +               if (unlikely(err))
14831 +                       goto out;
14832 +               au_do_refresh_dir(file);
14833 +       }
14834 +
14835 +       err = 0;
14836 +       need_reopen = 1;
14837 +       if (!au_test_mmapped(file))
14838 +               err = au_file_refresh_by_inode(file, &need_reopen);
14839 +       if (finfo->fi_hdir)
14840 +               /* harmless if err */
14841 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14842 +       if (!err && need_reopen && !d_unlinked(dentry))
14843 +               err = reopen(file);
14844 +       if (!err) {
14845 +               au_update_figen(file);
14846 +               goto out; /* success */
14847 +       }
14848 +
14849 +       /* error, close all lower files */
14850 +       if (finfo->fi_hdir) {
14851 +               bbot = au_fbbot_dir(file);
14852 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14853 +                       au_set_h_fptr(file, bindex, NULL);
14854 +       }
14855 +
14856 +out:
14857 +       return err;
14858 +}
14859 +
14860 +/* common function to regular file and dir */
14861 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14862 +                         int wlock, unsigned int fi_lsc)
14863 +{
14864 +       int err;
14865 +       unsigned int sigen, figen;
14866 +       aufs_bindex_t btop;
14867 +       unsigned char pseudo_link;
14868 +       struct dentry *dentry;
14869 +       struct inode *inode;
14870 +
14871 +       err = 0;
14872 +       dentry = file->f_path.dentry;
14873 +       inode = d_inode(dentry);
14874 +       sigen = au_sigen(dentry->d_sb);
14875 +       fi_write_lock_nested(file, fi_lsc);
14876 +       figen = au_figen(file);
14877 +       if (!fi_lsc)
14878 +               di_write_lock_child(dentry);
14879 +       else
14880 +               di_write_lock_child2(dentry);
14881 +       btop = au_dbtop(dentry);
14882 +       pseudo_link = (btop != au_ibtop(inode));
14883 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14884 +               if (!wlock) {
14885 +                       di_downgrade_lock(dentry, AuLock_IR);
14886 +                       fi_downgrade_lock(file);
14887 +               }
14888 +               goto out; /* success */
14889 +       }
14890 +
14891 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14892 +       if (au_digen_test(dentry, sigen)) {
14893 +               err = au_reval_dpath(dentry, sigen);
14894 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14895 +       }
14896 +
14897 +       if (!err)
14898 +               err = refresh_file(file, reopen);
14899 +       if (!err) {
14900 +               if (!wlock) {
14901 +                       di_downgrade_lock(dentry, AuLock_IR);
14902 +                       fi_downgrade_lock(file);
14903 +               }
14904 +       } else {
14905 +               di_write_unlock(dentry);
14906 +               fi_write_unlock(file);
14907 +       }
14908 +
14909 +out:
14910 +       return err;
14911 +}
14912 +
14913 +/* ---------------------------------------------------------------------- */
14914 +
14915 +/* cf. aufs_nopage() */
14916 +/* for madvise(2) */
14917 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14918 +{
14919 +       unlock_page(page);
14920 +       return 0;
14921 +}
14922 +
14923 +/* it will never be called, but necessary to support O_DIRECT */
14924 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14925 +{ BUG(); return 0; }
14926 +
14927 +/* they will never be called. */
14928 +#ifdef CONFIG_AUFS_DEBUG
14929 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14930 +                           loff_t pos, unsigned len, unsigned flags,
14931 +                           struct page **pagep, void **fsdata)
14932 +{ AuUnsupport(); return 0; }
14933 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14934 +                         loff_t pos, unsigned len, unsigned copied,
14935 +                         struct page *page, void *fsdata)
14936 +{ AuUnsupport(); return 0; }
14937 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14938 +{ AuUnsupport(); return 0; }
14939 +
14940 +static int aufs_set_page_dirty(struct page *page)
14941 +{ AuUnsupport(); return 0; }
14942 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14943 +                               unsigned int length)
14944 +{ AuUnsupport(); }
14945 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14946 +{ AuUnsupport(); return 0; }
14947 +#if 0 /* called by memory compaction regardless file */
14948 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14949 +                           struct page *page, enum migrate_mode mode)
14950 +{ AuUnsupport(); return 0; }
14951 +#endif
14952 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14953 +{ AuUnsupport(); return true; }
14954 +static void aufs_putback_page(struct page *page)
14955 +{ AuUnsupport(); }
14956 +static int aufs_launder_page(struct page *page)
14957 +{ AuUnsupport(); return 0; }
14958 +static int aufs_is_partially_uptodate(struct page *page,
14959 +                                     unsigned long from,
14960 +                                     unsigned long count)
14961 +{ AuUnsupport(); return 0; }
14962 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14963 +                                   bool *writeback)
14964 +{ AuUnsupport(); }
14965 +static int aufs_error_remove_page(struct address_space *mapping,
14966 +                                 struct page *page)
14967 +{ AuUnsupport(); return 0; }
14968 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14969 +                             sector_t *span)
14970 +{ AuUnsupport(); return 0; }
14971 +static void aufs_swap_deactivate(struct file *file)
14972 +{ AuUnsupport(); }
14973 +#endif /* CONFIG_AUFS_DEBUG */
14974 +
14975 +const struct address_space_operations aufs_aop = {
14976 +       .readpage               = aufs_readpage,
14977 +       .direct_IO              = aufs_direct_IO,
14978 +#ifdef CONFIG_AUFS_DEBUG
14979 +       .writepage              = aufs_writepage,
14980 +       /* no writepages, because of writepage */
14981 +       .set_page_dirty         = aufs_set_page_dirty,
14982 +       /* no readpages, because of readpage */
14983 +       .write_begin            = aufs_write_begin,
14984 +       .write_end              = aufs_write_end,
14985 +       /* no bmap, no block device */
14986 +       .invalidatepage         = aufs_invalidatepage,
14987 +       .releasepage            = aufs_releasepage,
14988 +       /* is fallback_migrate_page ok? */
14989 +       /* .migratepage         = aufs_migratepage, */
14990 +       .isolate_page           = aufs_isolate_page,
14991 +       .putback_page           = aufs_putback_page,
14992 +       .launder_page           = aufs_launder_page,
14993 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14994 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14995 +       .error_remove_page      = aufs_error_remove_page,
14996 +       .swap_activate          = aufs_swap_activate,
14997 +       .swap_deactivate        = aufs_swap_deactivate
14998 +#endif /* CONFIG_AUFS_DEBUG */
14999 +};
15000 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15001 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
15002 +++ linux/fs/aufs/file.h        2020-01-27 10:57:18.172204883 +0100
15003 @@ -0,0 +1,342 @@
15004 +/* SPDX-License-Identifier: GPL-2.0 */
15005 +/*
15006 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15007 + *
15008 + * This program, aufs is free software; you can redistribute it and/or modify
15009 + * it under the terms of the GNU General Public License as published by
15010 + * the Free Software Foundation; either version 2 of the License, or
15011 + * (at your option) any later version.
15012 + *
15013 + * This program is distributed in the hope that it will be useful,
15014 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15015 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15016 + * GNU General Public License for more details.
15017 + *
15018 + * You should have received a copy of the GNU General Public License
15019 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15020 + */
15021 +
15022 +/*
15023 + * file operations
15024 + */
15025 +
15026 +#ifndef __AUFS_FILE_H__
15027 +#define __AUFS_FILE_H__
15028 +
15029 +#ifdef __KERNEL__
15030 +
15031 +#include <linux/file.h>
15032 +#include <linux/fs.h>
15033 +#include <linux/mm_types.h>
15034 +#include <linux/poll.h>
15035 +#include "rwsem.h"
15036 +
15037 +struct au_branch;
15038 +struct au_hfile {
15039 +       struct file             *hf_file;
15040 +       struct au_branch        *hf_br;
15041 +};
15042 +
15043 +struct au_vdir;
15044 +struct au_fidir {
15045 +       aufs_bindex_t           fd_bbot;
15046 +       aufs_bindex_t           fd_nent;
15047 +       struct au_vdir          *fd_vdir_cache;
15048 +       struct au_hfile         fd_hfile[];
15049 +};
15050 +
15051 +static inline int au_fidir_sz(int nent)
15052 +{
15053 +       AuDebugOn(nent < 0);
15054 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15055 +}
15056 +
15057 +struct au_finfo {
15058 +       atomic_t                fi_generation;
15059 +
15060 +       struct au_rwsem         fi_rwsem;
15061 +       aufs_bindex_t           fi_btop;
15062 +
15063 +       /* do not union them */
15064 +       struct {                                /* for non-dir */
15065 +               struct au_hfile                 fi_htop;
15066 +               atomic_t                        fi_mmapped;
15067 +       };
15068 +       struct au_fidir         *fi_hdir;       /* for dir only */
15069 +
15070 +       struct hlist_bl_node    fi_hlist;
15071 +       struct file             *fi_file;       /* very ugly */
15072 +       struct rcu_head         rcu;
15073 +} ____cacheline_aligned_in_smp;
15074 +
15075 +/* ---------------------------------------------------------------------- */
15076 +
15077 +/* file.c */
15078 +extern const struct address_space_operations aufs_aop;
15079 +unsigned int au_file_roflags(unsigned int flags);
15080 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15081 +                      struct file *file, int force_wr);
15082 +struct au_do_open_args {
15083 +       int             aopen;
15084 +       int             (*open)(struct file *file, int flags,
15085 +                               struct file *h_file);
15086 +       struct au_fidir *fidir;
15087 +       struct file     *h_file;
15088 +};
15089 +int au_do_open(struct file *file, struct au_do_open_args *args);
15090 +int au_reopen_nondir(struct file *file);
15091 +struct au_pin;
15092 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15093 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15094 +                         int wlock, unsigned int fi_lsc);
15095 +int au_do_flush(struct file *file, fl_owner_t id,
15096 +               int (*flush)(struct file *file, fl_owner_t id));
15097 +
15098 +/* poll.c */
15099 +#ifdef CONFIG_AUFS_POLL
15100 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15101 +#endif
15102 +
15103 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15104 +/* hfsplus.c */
15105 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15106 +                          int force_wr);
15107 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15108 +                   struct file *h_file);
15109 +#else
15110 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15111 +       aufs_bindex_t bindex, int force_wr)
15112 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15113 +          struct file *h_file);
15114 +#endif
15115 +
15116 +/* f_op.c */
15117 +extern const struct file_operations aufs_file_fop;
15118 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15119 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15120 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15121 +
15122 +/* finfo.c */
15123 +void au_hfput(struct au_hfile *hf, int execed);
15124 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15125 +                  struct file *h_file);
15126 +
15127 +void au_update_figen(struct file *file);
15128 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15129 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15130 +
15131 +void au_fi_init_once(void *_fi);
15132 +void au_finfo_fin(struct file *file);
15133 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15134 +
15135 +/* ioctl.c */
15136 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15137 +#ifdef CONFIG_COMPAT
15138 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15139 +                          unsigned long arg);
15140 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15141 +                             unsigned long arg);
15142 +#endif
15143 +
15144 +/* ---------------------------------------------------------------------- */
15145 +
15146 +static inline struct au_finfo *au_fi(struct file *file)
15147 +{
15148 +       return file->private_data;
15149 +}
15150 +
15151 +/* ---------------------------------------------------------------------- */
15152 +
15153 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15154 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15155 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15156 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15157 +/*
15158 +#define fi_read_trylock_nested(f) \
15159 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15160 +#define fi_write_trylock_nested(f) \
15161 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15162 +*/
15163 +
15164 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15165 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15166 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15167 +
15168 +/* lock subclass for finfo */
15169 +enum {
15170 +       AuLsc_FI_1,
15171 +       AuLsc_FI_2
15172 +};
15173 +
15174 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15175 +{
15176 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15177 +}
15178 +
15179 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15180 +{
15181 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15182 +}
15183 +
15184 +/*
15185 + * fi_read_lock_1, fi_write_lock_1,
15186 + * fi_read_lock_2, fi_write_lock_2
15187 + */
15188 +#define AuReadLockFunc(name) \
15189 +static inline void fi_read_lock_##name(struct file *f) \
15190 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15191 +
15192 +#define AuWriteLockFunc(name) \
15193 +static inline void fi_write_lock_##name(struct file *f) \
15194 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15195 +
15196 +#define AuRWLockFuncs(name) \
15197 +       AuReadLockFunc(name) \
15198 +       AuWriteLockFunc(name)
15199 +
15200 +AuRWLockFuncs(1);
15201 +AuRWLockFuncs(2);
15202 +
15203 +#undef AuReadLockFunc
15204 +#undef AuWriteLockFunc
15205 +#undef AuRWLockFuncs
15206 +
15207 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15208 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15209 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15210 +
15211 +/* ---------------------------------------------------------------------- */
15212 +
15213 +/* todo: hard/soft set? */
15214 +static inline aufs_bindex_t au_fbtop(struct file *file)
15215 +{
15216 +       FiMustAnyLock(file);
15217 +       return au_fi(file)->fi_btop;
15218 +}
15219 +
15220 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15221 +{
15222 +       FiMustAnyLock(file);
15223 +       AuDebugOn(!au_fi(file)->fi_hdir);
15224 +       return au_fi(file)->fi_hdir->fd_bbot;
15225 +}
15226 +
15227 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15228 +{
15229 +       FiMustAnyLock(file);
15230 +       AuDebugOn(!au_fi(file)->fi_hdir);
15231 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15232 +}
15233 +
15234 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15235 +{
15236 +       FiMustWriteLock(file);
15237 +       au_fi(file)->fi_btop = bindex;
15238 +}
15239 +
15240 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15241 +{
15242 +       FiMustWriteLock(file);
15243 +       AuDebugOn(!au_fi(file)->fi_hdir);
15244 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15245 +}
15246 +
15247 +static inline void au_set_fvdir_cache(struct file *file,
15248 +                                     struct au_vdir *vdir_cache)
15249 +{
15250 +       FiMustWriteLock(file);
15251 +       AuDebugOn(!au_fi(file)->fi_hdir);
15252 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15253 +}
15254 +
15255 +static inline struct file *au_hf_top(struct file *file)
15256 +{
15257 +       FiMustAnyLock(file);
15258 +       AuDebugOn(au_fi(file)->fi_hdir);
15259 +       return au_fi(file)->fi_htop.hf_file;
15260 +}
15261 +
15262 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15263 +{
15264 +       FiMustAnyLock(file);
15265 +       AuDebugOn(!au_fi(file)->fi_hdir);
15266 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15267 +}
15268 +
15269 +/* todo: memory barrier? */
15270 +static inline unsigned int au_figen(struct file *f)
15271 +{
15272 +       return atomic_read(&au_fi(f)->fi_generation);
15273 +}
15274 +
15275 +static inline void au_set_mmapped(struct file *f)
15276 +{
15277 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15278 +               return;
15279 +       pr_warn("fi_mmapped wrapped around\n");
15280 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15281 +               ;
15282 +}
15283 +
15284 +static inline void au_unset_mmapped(struct file *f)
15285 +{
15286 +       atomic_dec(&au_fi(f)->fi_mmapped);
15287 +}
15288 +
15289 +static inline int au_test_mmapped(struct file *f)
15290 +{
15291 +       return atomic_read(&au_fi(f)->fi_mmapped);
15292 +}
15293 +
15294 +/* customize vma->vm_file */
15295 +
15296 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15297 +                                      struct file *file)
15298 +{
15299 +       struct file *f;
15300 +
15301 +       f = vma->vm_file;
15302 +       get_file(file);
15303 +       vma->vm_file = file;
15304 +       fput(f);
15305 +}
15306 +
15307 +#ifdef CONFIG_MMU
15308 +#define AuDbgVmRegion(file, vma) do {} while (0)
15309 +
15310 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15311 +                                   struct file *file)
15312 +{
15313 +       au_do_vm_file_reset(vma, file);
15314 +}
15315 +#else
15316 +#define AuDbgVmRegion(file, vma) \
15317 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15318 +
15319 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15320 +                                   struct file *file)
15321 +{
15322 +       struct file *f;
15323 +
15324 +       au_do_vm_file_reset(vma, file);
15325 +       f = vma->vm_region->vm_file;
15326 +       get_file(file);
15327 +       vma->vm_region->vm_file = file;
15328 +       fput(f);
15329 +}
15330 +#endif /* CONFIG_MMU */
15331 +
15332 +/* handle vma->vm_prfile */
15333 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15334 +                                   struct file *file)
15335 +{
15336 +       get_file(file);
15337 +       vma->vm_prfile = file;
15338 +#ifndef CONFIG_MMU
15339 +       get_file(file);
15340 +       vma->vm_region->vm_prfile = file;
15341 +#endif
15342 +}
15343 +
15344 +#endif /* __KERNEL__ */
15345 +#endif /* __AUFS_FILE_H__ */
15346 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15347 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15348 +++ linux/fs/aufs/finfo.c       2020-01-27 10:57:18.172204883 +0100
15349 @@ -0,0 +1,149 @@
15350 +// SPDX-License-Identifier: GPL-2.0
15351 +/*
15352 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15353 + *
15354 + * This program, aufs is free software; you can redistribute it and/or modify
15355 + * it under the terms of the GNU General Public License as published by
15356 + * the Free Software Foundation; either version 2 of the License, or
15357 + * (at your option) any later version.
15358 + *
15359 + * This program is distributed in the hope that it will be useful,
15360 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15361 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15362 + * GNU General Public License for more details.
15363 + *
15364 + * You should have received a copy of the GNU General Public License
15365 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15366 + */
15367 +
15368 +/*
15369 + * file private data
15370 + */
15371 +
15372 +#include "aufs.h"
15373 +
15374 +void au_hfput(struct au_hfile *hf, int execed)
15375 +{
15376 +       if (execed)
15377 +               allow_write_access(hf->hf_file);
15378 +       fput(hf->hf_file);
15379 +       hf->hf_file = NULL;
15380 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15381 +       hf->hf_br = NULL;
15382 +}
15383 +
15384 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15385 +{
15386 +       struct au_finfo *finfo = au_fi(file);
15387 +       struct au_hfile *hf;
15388 +       struct au_fidir *fidir;
15389 +
15390 +       fidir = finfo->fi_hdir;
15391 +       if (!fidir) {
15392 +               AuDebugOn(finfo->fi_btop != bindex);
15393 +               hf = &finfo->fi_htop;
15394 +       } else
15395 +               hf = fidir->fd_hfile + bindex;
15396 +
15397 +       if (hf && hf->hf_file)
15398 +               au_hfput(hf, vfsub_file_execed(file));
15399 +       if (val) {
15400 +               FiMustWriteLock(file);
15401 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15402 +               hf->hf_file = val;
15403 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15404 +       }
15405 +}
15406 +
15407 +void au_update_figen(struct file *file)
15408 +{
15409 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15410 +       /* smp_mb(); */ /* atomic_set */
15411 +}
15412 +
15413 +/* ---------------------------------------------------------------------- */
15414 +
15415 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15416 +{
15417 +       struct au_fidir *fidir;
15418 +       int nbr;
15419 +
15420 +       nbr = au_sbbot(sb) + 1;
15421 +       if (nbr < 2)
15422 +               nbr = 2; /* initial allocate for 2 branches */
15423 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15424 +       if (fidir) {
15425 +               fidir->fd_bbot = -1;
15426 +               fidir->fd_nent = nbr;
15427 +       }
15428 +
15429 +       return fidir;
15430 +}
15431 +
15432 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15433 +{
15434 +       int err;
15435 +       struct au_fidir *fidir, *p;
15436 +
15437 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15438 +       fidir = finfo->fi_hdir;
15439 +       AuDebugOn(!fidir);
15440 +
15441 +       err = -ENOMEM;
15442 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15443 +                        GFP_NOFS, may_shrink);
15444 +       if (p) {
15445 +               p->fd_nent = nbr;
15446 +               finfo->fi_hdir = p;
15447 +               err = 0;
15448 +       }
15449 +
15450 +       return err;
15451 +}
15452 +
15453 +/* ---------------------------------------------------------------------- */
15454 +
15455 +void au_finfo_fin(struct file *file)
15456 +{
15457 +       struct au_finfo *finfo;
15458 +
15459 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15460 +
15461 +       finfo = au_fi(file);
15462 +       AuDebugOn(finfo->fi_hdir);
15463 +       AuRwDestroy(&finfo->fi_rwsem);
15464 +       au_cache_free_finfo(finfo);
15465 +}
15466 +
15467 +void au_fi_init_once(void *_finfo)
15468 +{
15469 +       struct au_finfo *finfo = _finfo;
15470 +
15471 +       au_rw_init(&finfo->fi_rwsem);
15472 +}
15473 +
15474 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15475 +{
15476 +       int err;
15477 +       struct au_finfo *finfo;
15478 +       struct dentry *dentry;
15479 +
15480 +       err = -ENOMEM;
15481 +       dentry = file->f_path.dentry;
15482 +       finfo = au_cache_alloc_finfo();
15483 +       if (unlikely(!finfo))
15484 +               goto out;
15485 +
15486 +       err = 0;
15487 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15488 +       au_rw_write_lock(&finfo->fi_rwsem);
15489 +       finfo->fi_btop = -1;
15490 +       finfo->fi_hdir = fidir;
15491 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15492 +       /* smp_mb(); */ /* atomic_set */
15493 +
15494 +       file->private_data = finfo;
15495 +
15496 +out:
15497 +       return err;
15498 +}
15499 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15500 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15501 +++ linux/fs/aufs/f_op.c        2020-01-27 10:57:18.172204883 +0100
15502 @@ -0,0 +1,819 @@
15503 +// SPDX-License-Identifier: GPL-2.0
15504 +/*
15505 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15506 + *
15507 + * This program, aufs is free software; you can redistribute it and/or modify
15508 + * it under the terms of the GNU General Public License as published by
15509 + * the Free Software Foundation; either version 2 of the License, or
15510 + * (at your option) any later version.
15511 + *
15512 + * This program is distributed in the hope that it will be useful,
15513 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15514 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15515 + * GNU General Public License for more details.
15516 + *
15517 + * You should have received a copy of the GNU General Public License
15518 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15519 + */
15520 +
15521 +/*
15522 + * file and vm operations
15523 + */
15524 +
15525 +#include <linux/aio.h>
15526 +#include <linux/fs_stack.h>
15527 +#include <linux/mman.h>
15528 +#include <linux/security.h>
15529 +#include "aufs.h"
15530 +
15531 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15532 +{
15533 +       int err;
15534 +       aufs_bindex_t bindex;
15535 +       struct dentry *dentry, *h_dentry;
15536 +       struct au_finfo *finfo;
15537 +       struct inode *h_inode;
15538 +
15539 +       FiMustWriteLock(file);
15540 +
15541 +       err = 0;
15542 +       dentry = file->f_path.dentry;
15543 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15544 +       finfo = au_fi(file);
15545 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15546 +       atomic_set(&finfo->fi_mmapped, 0);
15547 +       bindex = au_dbtop(dentry);
15548 +       if (!h_file) {
15549 +               h_dentry = au_h_dptr(dentry, bindex);
15550 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15551 +               if (unlikely(err))
15552 +                       goto out;
15553 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15554 +               if (IS_ERR(h_file)) {
15555 +                       err = PTR_ERR(h_file);
15556 +                       goto out;
15557 +               }
15558 +       } else {
15559 +               h_dentry = h_file->f_path.dentry;
15560 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15561 +               if (unlikely(err))
15562 +                       goto out;
15563 +               /* br ref is already inc-ed */
15564 +       }
15565 +
15566 +       if ((flags & __O_TMPFILE)
15567 +           && !(flags & O_EXCL)) {
15568 +               h_inode = file_inode(h_file);
15569 +               spin_lock(&h_inode->i_lock);
15570 +               h_inode->i_state |= I_LINKABLE;
15571 +               spin_unlock(&h_inode->i_lock);
15572 +       }
15573 +       au_set_fbtop(file, bindex);
15574 +       au_set_h_fptr(file, bindex, h_file);
15575 +       au_update_figen(file);
15576 +       /* todo: necessary? */
15577 +       /* file->f_ra = h_file->f_ra; */
15578 +
15579 +out:
15580 +       return err;
15581 +}
15582 +
15583 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15584 +                           struct file *file)
15585 +{
15586 +       int err;
15587 +       struct super_block *sb;
15588 +       struct au_do_open_args args = {
15589 +               .open   = au_do_open_nondir
15590 +       };
15591 +
15592 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15593 +             file, vfsub_file_flags(file), file->f_mode);
15594 +
15595 +       sb = file->f_path.dentry->d_sb;
15596 +       si_read_lock(sb, AuLock_FLUSH);
15597 +       err = au_do_open(file, &args);
15598 +       si_read_unlock(sb);
15599 +       return err;
15600 +}
15601 +
15602 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15603 +{
15604 +       struct au_finfo *finfo;
15605 +       aufs_bindex_t bindex;
15606 +
15607 +       finfo = au_fi(file);
15608 +       au_hbl_del(&finfo->fi_hlist,
15609 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15610 +       bindex = finfo->fi_btop;
15611 +       if (bindex >= 0)
15612 +               au_set_h_fptr(file, bindex, NULL);
15613 +
15614 +       au_finfo_fin(file);
15615 +       return 0;
15616 +}
15617 +
15618 +/* ---------------------------------------------------------------------- */
15619 +
15620 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15621 +{
15622 +       int err;
15623 +       struct file *h_file;
15624 +
15625 +       err = 0;
15626 +       h_file = au_hf_top(file);
15627 +       if (h_file)
15628 +               err = vfsub_flush(h_file, id);
15629 +       return err;
15630 +}
15631 +
15632 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15633 +{
15634 +       return au_do_flush(file, id, au_do_flush_nondir);
15635 +}
15636 +
15637 +/* ---------------------------------------------------------------------- */
15638 +/*
15639 + * read and write functions acquire [fdi]_rwsem once, but release before
15640 + * mmap_sem. This is because to stop a race condition between mmap(2).
15641 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15642 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15643 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15644 + */
15645 +
15646 +/* Callers should call au_read_post() or fput() in the end */
15647 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15648 +{
15649 +       struct file *h_file;
15650 +       int err;
15651 +
15652 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15653 +       if (!err) {
15654 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15655 +               h_file = au_hf_top(file);
15656 +               get_file(h_file);
15657 +               if (!keep_fi)
15658 +                       fi_read_unlock(file);
15659 +       } else
15660 +               h_file = ERR_PTR(err);
15661 +
15662 +       return h_file;
15663 +}
15664 +
15665 +static void au_read_post(struct inode *inode, struct file *h_file)
15666 +{
15667 +       /* update without lock, I don't think it a problem */
15668 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15669 +       fput(h_file);
15670 +}
15671 +
15672 +struct au_write_pre {
15673 +       /* input */
15674 +       unsigned int lsc;
15675 +
15676 +       /* output */
15677 +       blkcnt_t blks;
15678 +       aufs_bindex_t btop;
15679 +};
15680 +
15681 +/*
15682 + * return with iinfo is write-locked
15683 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15684 + * end
15685 + */
15686 +static struct file *au_write_pre(struct file *file, int do_ready,
15687 +                                struct au_write_pre *wpre)
15688 +{
15689 +       struct file *h_file;
15690 +       struct dentry *dentry;
15691 +       int err;
15692 +       unsigned int lsc;
15693 +       struct au_pin pin;
15694 +
15695 +       lsc = 0;
15696 +       if (wpre)
15697 +               lsc = wpre->lsc;
15698 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15699 +       h_file = ERR_PTR(err);
15700 +       if (unlikely(err))
15701 +               goto out;
15702 +
15703 +       dentry = file->f_path.dentry;
15704 +       if (do_ready) {
15705 +               err = au_ready_to_write(file, -1, &pin);
15706 +               if (unlikely(err)) {
15707 +                       h_file = ERR_PTR(err);
15708 +                       di_write_unlock(dentry);
15709 +                       goto out_fi;
15710 +               }
15711 +       }
15712 +
15713 +       di_downgrade_lock(dentry, /*flags*/0);
15714 +       if (wpre)
15715 +               wpre->btop = au_fbtop(file);
15716 +       h_file = au_hf_top(file);
15717 +       get_file(h_file);
15718 +       if (wpre)
15719 +               wpre->blks = file_inode(h_file)->i_blocks;
15720 +       if (do_ready)
15721 +               au_unpin(&pin);
15722 +       di_read_unlock(dentry, /*flags*/0);
15723 +
15724 +out_fi:
15725 +       fi_write_unlock(file);
15726 +out:
15727 +       return h_file;
15728 +}
15729 +
15730 +static void au_write_post(struct inode *inode, struct file *h_file,
15731 +                         struct au_write_pre *wpre, ssize_t written)
15732 +{
15733 +       struct inode *h_inode;
15734 +
15735 +       au_cpup_attr_timesizes(inode);
15736 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15737 +       h_inode = file_inode(h_file);
15738 +       inode->i_mode = h_inode->i_mode;
15739 +       ii_write_unlock(inode);
15740 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15741 +       if (written > 0)
15742 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15743 +                             /*force*/h_inode->i_blocks > wpre->blks);
15744 +       fput(h_file);
15745 +}
15746 +
15747 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15748 +                        loff_t *ppos)
15749 +{
15750 +       ssize_t err;
15751 +       struct inode *inode;
15752 +       struct file *h_file;
15753 +       struct super_block *sb;
15754 +
15755 +       inode = file_inode(file);
15756 +       sb = inode->i_sb;
15757 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15758 +
15759 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15760 +       err = PTR_ERR(h_file);
15761 +       if (IS_ERR(h_file))
15762 +               goto out;
15763 +
15764 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
15765 +       err = vfsub_read_u(h_file, buf, count, ppos);
15766 +       /* todo: necessary? */
15767 +       /* file->f_ra = h_file->f_ra; */
15768 +       au_read_post(inode, h_file);
15769 +
15770 +out:
15771 +       si_read_unlock(sb);
15772 +       return err;
15773 +}
15774 +
15775 +/*
15776 + * todo: very ugly
15777 + * it locks both of i_mutex and si_rwsem for read in safe.
15778 + * if the plink maintenance mode continues forever (that is the problem),
15779 + * may loop forever.
15780 + */
15781 +static void au_mtx_and_read_lock(struct inode *inode)
15782 +{
15783 +       int err;
15784 +       struct super_block *sb = inode->i_sb;
15785 +
15786 +       while (1) {
15787 +               inode_lock(inode);
15788 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15789 +               if (!err)
15790 +                       break;
15791 +               inode_unlock(inode);
15792 +               si_read_lock(sb, AuLock_NOPLMW);
15793 +               si_read_unlock(sb);
15794 +       }
15795 +}
15796 +
15797 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15798 +                         size_t count, loff_t *ppos)
15799 +{
15800 +       ssize_t err;
15801 +       struct au_write_pre wpre;
15802 +       struct inode *inode;
15803 +       struct file *h_file;
15804 +       char __user *buf = (char __user *)ubuf;
15805 +
15806 +       inode = file_inode(file);
15807 +       au_mtx_and_read_lock(inode);
15808 +
15809 +       wpre.lsc = 0;
15810 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15811 +       err = PTR_ERR(h_file);
15812 +       if (IS_ERR(h_file))
15813 +               goto out;
15814 +
15815 +       err = vfsub_write_u(h_file, buf, count, ppos);
15816 +       au_write_post(inode, h_file, &wpre, err);
15817 +
15818 +out:
15819 +       si_read_unlock(inode->i_sb);
15820 +       inode_unlock(inode);
15821 +       return err;
15822 +}
15823 +
15824 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15825 +                         struct iov_iter *iov_iter)
15826 +{
15827 +       ssize_t err;
15828 +       struct file *file;
15829 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15830 +
15831 +       err = security_file_permission(h_file, rw);
15832 +       if (unlikely(err))
15833 +               goto out;
15834 +
15835 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15836 +       iter = NULL;
15837 +       if (rw == MAY_READ)
15838 +               iter = h_file->f_op->read_iter;
15839 +       else if (rw == MAY_WRITE)
15840 +               iter = h_file->f_op->write_iter;
15841 +
15842 +       file = kio->ki_filp;
15843 +       kio->ki_filp = h_file;
15844 +       if (iter) {
15845 +               lockdep_off();
15846 +               err = iter(kio, iov_iter);
15847 +               lockdep_on();
15848 +       } else
15849 +               /* currently there is no such fs */
15850 +               WARN_ON_ONCE(1);
15851 +       kio->ki_filp = file;
15852 +
15853 +out:
15854 +       return err;
15855 +}
15856 +
15857 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15858 +{
15859 +       ssize_t err;
15860 +       struct file *file, *h_file;
15861 +       struct inode *inode;
15862 +       struct super_block *sb;
15863 +
15864 +       file = kio->ki_filp;
15865 +       inode = file_inode(file);
15866 +       sb = inode->i_sb;
15867 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15868 +
15869 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15870 +       err = PTR_ERR(h_file);
15871 +       if (IS_ERR(h_file))
15872 +               goto out;
15873 +
15874 +       if (au_test_loopback_kthread()) {
15875 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15876 +               if (file->f_mapping != h_file->f_mapping) {
15877 +                       file->f_mapping = h_file->f_mapping;
15878 +                       smp_mb(); /* unnecessary? */
15879 +               }
15880 +       }
15881 +       fi_read_unlock(file);
15882 +
15883 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15884 +       /* todo: necessary? */
15885 +       /* file->f_ra = h_file->f_ra; */
15886 +       au_read_post(inode, h_file);
15887 +
15888 +out:
15889 +       si_read_unlock(sb);
15890 +       return err;
15891 +}
15892 +
15893 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15894 +{
15895 +       ssize_t err;
15896 +       struct au_write_pre wpre;
15897 +       struct inode *inode;
15898 +       struct file *file, *h_file;
15899 +
15900 +       file = kio->ki_filp;
15901 +       inode = file_inode(file);
15902 +       au_mtx_and_read_lock(inode);
15903 +
15904 +       wpre.lsc = 0;
15905 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15906 +       err = PTR_ERR(h_file);
15907 +       if (IS_ERR(h_file))
15908 +               goto out;
15909 +
15910 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15911 +       au_write_post(inode, h_file, &wpre, err);
15912 +
15913 +out:
15914 +       si_read_unlock(inode->i_sb);
15915 +       inode_unlock(inode);
15916 +       return err;
15917 +}
15918 +
15919 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15920 +                               struct pipe_inode_info *pipe, size_t len,
15921 +                               unsigned int flags)
15922 +{
15923 +       ssize_t err;
15924 +       struct file *h_file;
15925 +       struct inode *inode;
15926 +       struct super_block *sb;
15927 +
15928 +       inode = file_inode(file);
15929 +       sb = inode->i_sb;
15930 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15931 +
15932 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15933 +       err = PTR_ERR(h_file);
15934 +       if (IS_ERR(h_file))
15935 +               goto out;
15936 +
15937 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15938 +       /* todo: necessary? */
15939 +       /* file->f_ra = h_file->f_ra; */
15940 +       au_read_post(inode, h_file);
15941 +
15942 +out:
15943 +       si_read_unlock(sb);
15944 +       return err;
15945 +}
15946 +
15947 +static ssize_t
15948 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15949 +                 size_t len, unsigned int flags)
15950 +{
15951 +       ssize_t err;
15952 +       struct au_write_pre wpre;
15953 +       struct inode *inode;
15954 +       struct file *h_file;
15955 +
15956 +       inode = file_inode(file);
15957 +       au_mtx_and_read_lock(inode);
15958 +
15959 +       wpre.lsc = 0;
15960 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15961 +       err = PTR_ERR(h_file);
15962 +       if (IS_ERR(h_file))
15963 +               goto out;
15964 +
15965 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15966 +       au_write_post(inode, h_file, &wpre, err);
15967 +
15968 +out:
15969 +       si_read_unlock(inode->i_sb);
15970 +       inode_unlock(inode);
15971 +       return err;
15972 +}
15973 +
15974 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15975 +                          loff_t len)
15976 +{
15977 +       long err;
15978 +       struct au_write_pre wpre;
15979 +       struct inode *inode;
15980 +       struct file *h_file;
15981 +
15982 +       inode = file_inode(file);
15983 +       au_mtx_and_read_lock(inode);
15984 +
15985 +       wpre.lsc = 0;
15986 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15987 +       err = PTR_ERR(h_file);
15988 +       if (IS_ERR(h_file))
15989 +               goto out;
15990 +
15991 +       lockdep_off();
15992 +       err = vfs_fallocate(h_file, mode, offset, len);
15993 +       lockdep_on();
15994 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15995 +
15996 +out:
15997 +       si_read_unlock(inode->i_sb);
15998 +       inode_unlock(inode);
15999 +       return err;
16000 +}
16001 +
16002 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16003 +                                   struct file *dst, loff_t dst_pos,
16004 +                                   size_t len, unsigned int flags)
16005 +{
16006 +       ssize_t err;
16007 +       struct au_write_pre wpre;
16008 +       enum { SRC, DST };
16009 +       struct {
16010 +               struct inode *inode;
16011 +               struct file *h_file;
16012 +               struct super_block *h_sb;
16013 +       } a[2];
16014 +#define a_src  a[SRC]
16015 +#define a_dst  a[DST]
16016 +
16017 +       err = -EINVAL;
16018 +       a_src.inode = file_inode(src);
16019 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16020 +               goto out;
16021 +       a_dst.inode = file_inode(dst);
16022 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16023 +               goto out;
16024 +
16025 +       au_mtx_and_read_lock(a_dst.inode);
16026 +       /*
16027 +        * in order to match the order in di_write_lock2_{child,parent}(),
16028 +        * use f_path.dentry for this comparison.
16029 +        */
16030 +       if (src->f_path.dentry < dst->f_path.dentry) {
16031 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16032 +               err = PTR_ERR(a_src.h_file);
16033 +               if (IS_ERR(a_src.h_file))
16034 +                       goto out_si;
16035 +
16036 +               wpre.lsc = AuLsc_FI_2;
16037 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16038 +               err = PTR_ERR(a_dst.h_file);
16039 +               if (IS_ERR(a_dst.h_file)) {
16040 +                       au_read_post(a_src.inode, a_src.h_file);
16041 +                       goto out_si;
16042 +               }
16043 +       } else {
16044 +               wpre.lsc = AuLsc_FI_1;
16045 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16046 +               err = PTR_ERR(a_dst.h_file);
16047 +               if (IS_ERR(a_dst.h_file))
16048 +                       goto out_si;
16049 +
16050 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16051 +               err = PTR_ERR(a_src.h_file);
16052 +               if (IS_ERR(a_src.h_file)) {
16053 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16054 +                                     /*written*/0);
16055 +                       goto out_si;
16056 +               }
16057 +       }
16058 +
16059 +       err = -EXDEV;
16060 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16061 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16062 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16063 +               AuDbgFile(src);
16064 +               AuDbgFile(dst);
16065 +               goto out_file;
16066 +       }
16067 +
16068 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16069 +                                   dst_pos, len, flags);
16070 +
16071 +out_file:
16072 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16073 +       fi_read_unlock(src);
16074 +       au_read_post(a_src.inode, a_src.h_file);
16075 +out_si:
16076 +       si_read_unlock(a_dst.inode->i_sb);
16077 +       inode_unlock(a_dst.inode);
16078 +out:
16079 +       return err;
16080 +#undef a_src
16081 +#undef a_dst
16082 +}
16083 +
16084 +/* ---------------------------------------------------------------------- */
16085 +
16086 +/*
16087 + * The locking order around current->mmap_sem.
16088 + * - in most and regular cases
16089 + *   file I/O syscall -- aufs_read() or something
16090 + *     -- si_rwsem for read -- mmap_sem
16091 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
16092 + * - in mmap case
16093 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16094 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16095 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16096 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16097 + * It means that when aufs acquires si_rwsem for write, the process should never
16098 + * acquire mmap_sem.
16099 + *
16100 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16101 + * problem either since any directory is not able to be mmap-ed.
16102 + * The similar scenario is applied to aufs_readlink() too.
16103 + */
16104 +
16105 +#if 0 /* stop calling security_file_mmap() */
16106 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16107 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16108 +
16109 +static unsigned long au_arch_prot_conv(unsigned long flags)
16110 +{
16111 +       /* currently ppc64 only */
16112 +#ifdef CONFIG_PPC64
16113 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16114 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16115 +       return AuConv_VM_PROT(flags, SAO);
16116 +#else
16117 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16118 +       return 0;
16119 +#endif
16120 +}
16121 +
16122 +static unsigned long au_prot_conv(unsigned long flags)
16123 +{
16124 +       return AuConv_VM_PROT(flags, READ)
16125 +               | AuConv_VM_PROT(flags, WRITE)
16126 +               | AuConv_VM_PROT(flags, EXEC)
16127 +               | au_arch_prot_conv(flags);
16128 +}
16129 +
16130 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16131 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16132 +
16133 +static unsigned long au_flag_conv(unsigned long flags)
16134 +{
16135 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16136 +               | AuConv_VM_MAP(flags, DENYWRITE)
16137 +               | AuConv_VM_MAP(flags, LOCKED);
16138 +}
16139 +#endif
16140 +
16141 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16142 +{
16143 +       int err;
16144 +       const unsigned char wlock
16145 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16146 +       struct super_block *sb;
16147 +       struct file *h_file;
16148 +       struct inode *inode;
16149 +
16150 +       AuDbgVmRegion(file, vma);
16151 +
16152 +       inode = file_inode(file);
16153 +       sb = inode->i_sb;
16154 +       lockdep_off();
16155 +       si_read_lock(sb, AuLock_NOPLMW);
16156 +
16157 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16158 +       lockdep_on();
16159 +       err = PTR_ERR(h_file);
16160 +       if (IS_ERR(h_file))
16161 +               goto out;
16162 +
16163 +       err = 0;
16164 +       au_set_mmapped(file);
16165 +       au_vm_file_reset(vma, h_file);
16166 +       /*
16167 +        * we cannot call security_mmap_file() here since it may acquire
16168 +        * mmap_sem or i_mutex.
16169 +        *
16170 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16171 +        *                       au_flag_conv(vma->vm_flags));
16172 +        */
16173 +       if (!err)
16174 +               err = call_mmap(h_file, vma);
16175 +       if (!err) {
16176 +               au_vm_prfile_set(vma, file);
16177 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16178 +               goto out_fput; /* success */
16179 +       }
16180 +       au_unset_mmapped(file);
16181 +       au_vm_file_reset(vma, file);
16182 +
16183 +out_fput:
16184 +       lockdep_off();
16185 +       ii_write_unlock(inode);
16186 +       lockdep_on();
16187 +       fput(h_file);
16188 +out:
16189 +       lockdep_off();
16190 +       si_read_unlock(sb);
16191 +       lockdep_on();
16192 +       AuTraceErr(err);
16193 +       return err;
16194 +}
16195 +
16196 +/* ---------------------------------------------------------------------- */
16197 +
16198 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16199 +                            int datasync)
16200 +{
16201 +       int err;
16202 +       struct au_write_pre wpre;
16203 +       struct inode *inode;
16204 +       struct file *h_file;
16205 +
16206 +       err = 0; /* -EBADF; */ /* posix? */
16207 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16208 +               goto out;
16209 +
16210 +       inode = file_inode(file);
16211 +       au_mtx_and_read_lock(inode);
16212 +
16213 +       wpre.lsc = 0;
16214 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16215 +       err = PTR_ERR(h_file);
16216 +       if (IS_ERR(h_file))
16217 +               goto out_unlock;
16218 +
16219 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16220 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16221 +
16222 +out_unlock:
16223 +       si_read_unlock(inode->i_sb);
16224 +       inode_unlock(inode);
16225 +out:
16226 +       return err;
16227 +}
16228 +
16229 +static int aufs_fasync(int fd, struct file *file, int flag)
16230 +{
16231 +       int err;
16232 +       struct file *h_file;
16233 +       struct super_block *sb;
16234 +
16235 +       sb = file->f_path.dentry->d_sb;
16236 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16237 +
16238 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16239 +       err = PTR_ERR(h_file);
16240 +       if (IS_ERR(h_file))
16241 +               goto out;
16242 +
16243 +       if (h_file->f_op->fasync)
16244 +               err = h_file->f_op->fasync(fd, h_file, flag);
16245 +       fput(h_file); /* instead of au_read_post() */
16246 +
16247 +out:
16248 +       si_read_unlock(sb);
16249 +       return err;
16250 +}
16251 +
16252 +static int aufs_setfl(struct file *file, unsigned long arg)
16253 +{
16254 +       int err;
16255 +       struct file *h_file;
16256 +       struct super_block *sb;
16257 +
16258 +       sb = file->f_path.dentry->d_sb;
16259 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16260 +
16261 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16262 +       err = PTR_ERR(h_file);
16263 +       if (IS_ERR(h_file))
16264 +               goto out;
16265 +
16266 +       /* stop calling h_file->fasync */
16267 +       arg |= vfsub_file_flags(file) & FASYNC;
16268 +       err = setfl(/*unused fd*/-1, h_file, arg);
16269 +       fput(h_file); /* instead of au_read_post() */
16270 +
16271 +out:
16272 +       si_read_unlock(sb);
16273 +       return err;
16274 +}
16275 +
16276 +/* ---------------------------------------------------------------------- */
16277 +
16278 +/* no one supports this operation, currently */
16279 +#if 0 /* reserved for future use */
16280 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16281 +                            size_t len, loff_t *pos, int more)
16282 +{
16283 +}
16284 +#endif
16285 +
16286 +/* ---------------------------------------------------------------------- */
16287 +
16288 +const struct file_operations aufs_file_fop = {
16289 +       .owner          = THIS_MODULE,
16290 +
16291 +       .llseek         = default_llseek,
16292 +
16293 +       .read           = aufs_read,
16294 +       .write          = aufs_write,
16295 +       .read_iter      = aufs_read_iter,
16296 +       .write_iter     = aufs_write_iter,
16297 +
16298 +#ifdef CONFIG_AUFS_POLL
16299 +       .poll           = aufs_poll,
16300 +#endif
16301 +       .unlocked_ioctl = aufs_ioctl_nondir,
16302 +#ifdef CONFIG_COMPAT
16303 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16304 +#endif
16305 +       .mmap           = aufs_mmap,
16306 +       .open           = aufs_open_nondir,
16307 +       .flush          = aufs_flush_nondir,
16308 +       .release        = aufs_release_nondir,
16309 +       .fsync          = aufs_fsync_nondir,
16310 +       .fasync         = aufs_fasync,
16311 +       /* .sendpage    = aufs_sendpage, */
16312 +       .setfl          = aufs_setfl,
16313 +       .splice_write   = aufs_splice_write,
16314 +       .splice_read    = aufs_splice_read,
16315 +#if 0 /* reserved for future use */
16316 +       .aio_splice_write = aufs_aio_splice_write,
16317 +       .aio_splice_read  = aufs_aio_splice_read,
16318 +#endif
16319 +       .fallocate      = aufs_fallocate,
16320 +       .copy_file_range = aufs_copy_file_range
16321 +};
16322 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16323 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16324 +++ linux/fs/aufs/fstype.h      2020-01-27 10:57:18.172204883 +0100
16325 @@ -0,0 +1,401 @@
16326 +/* SPDX-License-Identifier: GPL-2.0 */
16327 +/*
16328 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16329 + *
16330 + * This program, aufs is free software; you can redistribute it and/or modify
16331 + * it under the terms of the GNU General Public License as published by
16332 + * the Free Software Foundation; either version 2 of the License, or
16333 + * (at your option) any later version.
16334 + *
16335 + * This program is distributed in the hope that it will be useful,
16336 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16337 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16338 + * GNU General Public License for more details.
16339 + *
16340 + * You should have received a copy of the GNU General Public License
16341 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16342 + */
16343 +
16344 +/*
16345 + * judging filesystem type
16346 + */
16347 +
16348 +#ifndef __AUFS_FSTYPE_H__
16349 +#define __AUFS_FSTYPE_H__
16350 +
16351 +#ifdef __KERNEL__
16352 +
16353 +#include <linux/fs.h>
16354 +#include <linux/magic.h>
16355 +#include <linux/nfs_fs.h>
16356 +#include <linux/romfs_fs.h>
16357 +
16358 +static inline int au_test_aufs(struct super_block *sb)
16359 +{
16360 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16361 +}
16362 +
16363 +static inline const char *au_sbtype(struct super_block *sb)
16364 +{
16365 +       return sb->s_type->name;
16366 +}
16367 +
16368 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16369 +{
16370 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16371 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16372 +#else
16373 +       return 0;
16374 +#endif
16375 +}
16376 +
16377 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16378 +{
16379 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16380 +       return sb->s_magic == ROMFS_MAGIC;
16381 +#else
16382 +       return 0;
16383 +#endif
16384 +}
16385 +
16386 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16387 +{
16388 +#if IS_ENABLED(CONFIG_CRAMFS)
16389 +       return sb->s_magic == CRAMFS_MAGIC;
16390 +#endif
16391 +       return 0;
16392 +}
16393 +
16394 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16395 +{
16396 +#if IS_ENABLED(CONFIG_NFS_FS)
16397 +       return sb->s_magic == NFS_SUPER_MAGIC;
16398 +#else
16399 +       return 0;
16400 +#endif
16401 +}
16402 +
16403 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16404 +{
16405 +#if IS_ENABLED(CONFIG_FUSE_FS)
16406 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16407 +#else
16408 +       return 0;
16409 +#endif
16410 +}
16411 +
16412 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16413 +{
16414 +#if IS_ENABLED(CONFIG_XFS_FS)
16415 +       return sb->s_magic == XFS_SB_MAGIC;
16416 +#else
16417 +       return 0;
16418 +#endif
16419 +}
16420 +
16421 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16422 +{
16423 +#ifdef CONFIG_TMPFS
16424 +       return sb->s_magic == TMPFS_MAGIC;
16425 +#else
16426 +       return 0;
16427 +#endif
16428 +}
16429 +
16430 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16431 +{
16432 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16433 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16434 +#else
16435 +       return 0;
16436 +#endif
16437 +}
16438 +
16439 +static inline int au_test_ramfs(struct super_block *sb)
16440 +{
16441 +       return sb->s_magic == RAMFS_MAGIC;
16442 +}
16443 +
16444 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16445 +{
16446 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16447 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16448 +#else
16449 +       return 0;
16450 +#endif
16451 +}
16452 +
16453 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16454 +{
16455 +#ifdef CONFIG_PROC_FS
16456 +       return sb->s_magic == PROC_SUPER_MAGIC;
16457 +#else
16458 +       return 0;
16459 +#endif
16460 +}
16461 +
16462 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16463 +{
16464 +#ifdef CONFIG_SYSFS
16465 +       return sb->s_magic == SYSFS_MAGIC;
16466 +#else
16467 +       return 0;
16468 +#endif
16469 +}
16470 +
16471 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16472 +{
16473 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16474 +       return sb->s_magic == CONFIGFS_MAGIC;
16475 +#else
16476 +       return 0;
16477 +#endif
16478 +}
16479 +
16480 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16481 +{
16482 +#if IS_ENABLED(CONFIG_MINIX_FS)
16483 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16484 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16485 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16486 +               || sb->s_magic == MINIX_SUPER_MAGIC
16487 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16488 +#else
16489 +       return 0;
16490 +#endif
16491 +}
16492 +
16493 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16494 +{
16495 +#if IS_ENABLED(CONFIG_FAT_FS)
16496 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16497 +#else
16498 +       return 0;
16499 +#endif
16500 +}
16501 +
16502 +static inline int au_test_msdos(struct super_block *sb)
16503 +{
16504 +       return au_test_fat(sb);
16505 +}
16506 +
16507 +static inline int au_test_vfat(struct super_block *sb)
16508 +{
16509 +       return au_test_fat(sb);
16510 +}
16511 +
16512 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16513 +{
16514 +#ifdef CONFIG_SECURITYFS
16515 +       return sb->s_magic == SECURITYFS_MAGIC;
16516 +#else
16517 +       return 0;
16518 +#endif
16519 +}
16520 +
16521 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16522 +{
16523 +#if IS_ENABLED(CONFIG_SQUASHFS)
16524 +       return sb->s_magic == SQUASHFS_MAGIC;
16525 +#else
16526 +       return 0;
16527 +#endif
16528 +}
16529 +
16530 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16531 +{
16532 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16533 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16534 +#else
16535 +       return 0;
16536 +#endif
16537 +}
16538 +
16539 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16540 +{
16541 +#if IS_ENABLED(CONFIG_XENFS)
16542 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16543 +#else
16544 +       return 0;
16545 +#endif
16546 +}
16547 +
16548 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16549 +{
16550 +#ifdef CONFIG_DEBUG_FS
16551 +       return sb->s_magic == DEBUGFS_MAGIC;
16552 +#else
16553 +       return 0;
16554 +#endif
16555 +}
16556 +
16557 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16558 +{
16559 +#if IS_ENABLED(CONFIG_NILFS)
16560 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16561 +#else
16562 +       return 0;
16563 +#endif
16564 +}
16565 +
16566 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16567 +{
16568 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16569 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16570 +#else
16571 +       return 0;
16572 +#endif
16573 +}
16574 +
16575 +/* ---------------------------------------------------------------------- */
16576 +/*
16577 + * they can't be an aufs branch.
16578 + */
16579 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16580 +{
16581 +       return
16582 +#ifndef CONFIG_AUFS_BR_RAMFS
16583 +               au_test_ramfs(sb) ||
16584 +#endif
16585 +               au_test_procfs(sb)
16586 +               || au_test_sysfs(sb)
16587 +               || au_test_configfs(sb)
16588 +               || au_test_debugfs(sb)
16589 +               || au_test_securityfs(sb)
16590 +               || au_test_xenfs(sb)
16591 +               || au_test_ecryptfs(sb)
16592 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16593 +               || au_test_aufs(sb); /* will be supported in next version */
16594 +}
16595 +
16596 +static inline int au_test_fs_remote(struct super_block *sb)
16597 +{
16598 +       return !au_test_tmpfs(sb)
16599 +#ifdef CONFIG_AUFS_BR_RAMFS
16600 +               && !au_test_ramfs(sb)
16601 +#endif
16602 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16603 +}
16604 +
16605 +/* ---------------------------------------------------------------------- */
16606 +
16607 +/*
16608 + * Note: these functions (below) are created after reading ->getattr() in all
16609 + * filesystems under linux/fs. it means we have to do so in every update...
16610 + */
16611 +
16612 +/*
16613 + * some filesystems require getattr to refresh the inode attributes before
16614 + * referencing.
16615 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16616 + * and leave the work for d_revalidate()
16617 + */
16618 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16619 +{
16620 +       return au_test_nfs(sb)
16621 +               || au_test_fuse(sb)
16622 +               /* || au_test_btrfs(sb) */      /* untested */
16623 +               ;
16624 +}
16625 +
16626 +/*
16627 + * filesystems which don't maintain i_size or i_blocks.
16628 + */
16629 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16630 +{
16631 +       return au_test_xfs(sb)
16632 +               || au_test_btrfs(sb)
16633 +               || au_test_ubifs(sb)
16634 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16635 +               /* || au_test_minix(sb) */      /* untested */
16636 +               ;
16637 +}
16638 +
16639 +/*
16640 + * filesystems which don't store the correct value in some of their inode
16641 + * attributes.
16642 + */
16643 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16644 +{
16645 +       return au_test_fs_bad_iattr_size(sb)
16646 +               || au_test_fat(sb)
16647 +               || au_test_msdos(sb)
16648 +               || au_test_vfat(sb);
16649 +}
16650 +
16651 +/* they don't check i_nlink in link(2) */
16652 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16653 +{
16654 +       return au_test_tmpfs(sb)
16655 +#ifdef CONFIG_AUFS_BR_RAMFS
16656 +               || au_test_ramfs(sb)
16657 +#endif
16658 +               || au_test_ubifs(sb)
16659 +               || au_test_hfsplus(sb);
16660 +}
16661 +
16662 +/*
16663 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16664 + */
16665 +static inline int au_test_fs_notime(struct super_block *sb)
16666 +{
16667 +       return au_test_nfs(sb)
16668 +               || au_test_fuse(sb)
16669 +               || au_test_ubifs(sb)
16670 +               ;
16671 +}
16672 +
16673 +/* temporary support for i#1 in cramfs */
16674 +static inline int au_test_fs_unique_ino(struct inode *inode)
16675 +{
16676 +       if (au_test_cramfs(inode->i_sb))
16677 +               return inode->i_ino != 1;
16678 +       return 1;
16679 +}
16680 +
16681 +/* ---------------------------------------------------------------------- */
16682 +
16683 +/*
16684 + * the filesystem where the xino files placed must support i/o after unlink and
16685 + * maintain i_size and i_blocks.
16686 + */
16687 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16688 +{
16689 +       return au_test_fs_remote(sb)
16690 +               || au_test_fs_bad_iattr_size(sb)
16691 +               /* don't want unnecessary work for xino */
16692 +               || au_test_aufs(sb)
16693 +               || au_test_ecryptfs(sb)
16694 +               || au_test_nilfs(sb);
16695 +}
16696 +
16697 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16698 +{
16699 +       return au_test_tmpfs(sb)
16700 +               || au_test_ramfs(sb);
16701 +}
16702 +
16703 +/*
16704 + * test if the @sb is real-readonly.
16705 + */
16706 +static inline int au_test_fs_rr(struct super_block *sb)
16707 +{
16708 +       return au_test_squashfs(sb)
16709 +               || au_test_iso9660(sb)
16710 +               || au_test_cramfs(sb)
16711 +               || au_test_romfs(sb);
16712 +}
16713 +
16714 +/*
16715 + * test if the @inode is nfs with 'noacl' option
16716 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16717 + */
16718 +static inline int au_test_nfs_noacl(struct inode *inode)
16719 +{
16720 +       return au_test_nfs(inode->i_sb)
16721 +               /* && IS_POSIXACL(inode) */
16722 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16723 +}
16724 +
16725 +#endif /* __KERNEL__ */
16726 +#endif /* __AUFS_FSTYPE_H__ */
16727 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16728 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16729 +++ linux/fs/aufs/hbl.h 2020-01-27 10:57:18.172204883 +0100
16730 @@ -0,0 +1,65 @@
16731 +/* SPDX-License-Identifier: GPL-2.0 */
16732 +/*
16733 + * Copyright (C) 2017-2020 Junjiro R. Okajima
16734 + *
16735 + * This program, aufs is free software; you can redistribute it and/or modify
16736 + * it under the terms of the GNU General Public License as published by
16737 + * the Free Software Foundation; either version 2 of the License, or
16738 + * (at your option) any later version.
16739 + *
16740 + * This program is distributed in the hope that it will be useful,
16741 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16742 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16743 + * GNU General Public License for more details.
16744 + *
16745 + * You should have received a copy of the GNU General Public License
16746 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16747 + */
16748 +
16749 +/*
16750 + * helpers for hlist_bl.h
16751 + */
16752 +
16753 +#ifndef __AUFS_HBL_H__
16754 +#define __AUFS_HBL_H__
16755 +
16756 +#ifdef __KERNEL__
16757 +
16758 +#include <linux/list_bl.h>
16759 +
16760 +static inline void au_hbl_add(struct hlist_bl_node *node,
16761 +                             struct hlist_bl_head *hbl)
16762 +{
16763 +       hlist_bl_lock(hbl);
16764 +       hlist_bl_add_head(node, hbl);
16765 +       hlist_bl_unlock(hbl);
16766 +}
16767 +
16768 +static inline void au_hbl_del(struct hlist_bl_node *node,
16769 +                             struct hlist_bl_head *hbl)
16770 +{
16771 +       hlist_bl_lock(hbl);
16772 +       hlist_bl_del(node);
16773 +       hlist_bl_unlock(hbl);
16774 +}
16775 +
16776 +#define au_hbl_for_each(pos, head)                                     \
16777 +       for (pos = hlist_bl_first(head);                                \
16778 +            pos;                                                       \
16779 +            pos = pos->next)
16780 +
16781 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16782 +{
16783 +       unsigned long cnt;
16784 +       struct hlist_bl_node *pos;
16785 +
16786 +       cnt = 0;
16787 +       hlist_bl_lock(hbl);
16788 +       au_hbl_for_each(pos, hbl)
16789 +               cnt++;
16790 +       hlist_bl_unlock(hbl);
16791 +       return cnt;
16792 +}
16793 +
16794 +#endif /* __KERNEL__ */
16795 +#endif /* __AUFS_HBL_H__ */
16796 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16797 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16798 +++ linux/fs/aufs/hfsnotify.c   2020-01-27 10:57:18.172204883 +0100
16799 @@ -0,0 +1,288 @@
16800 +// SPDX-License-Identifier: GPL-2.0
16801 +/*
16802 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16803 + *
16804 + * This program, aufs is free software; you can redistribute it and/or modify
16805 + * it under the terms of the GNU General Public License as published by
16806 + * the Free Software Foundation; either version 2 of the License, or
16807 + * (at your option) any later version.
16808 + *
16809 + * This program is distributed in the hope that it will be useful,
16810 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16811 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16812 + * GNU General Public License for more details.
16813 + *
16814 + * You should have received a copy of the GNU General Public License
16815 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16816 + */
16817 +
16818 +/*
16819 + * fsnotify for the lower directories
16820 + */
16821 +
16822 +#include "aufs.h"
16823 +
16824 +/* FS_IN_IGNORED is unnecessary */
16825 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16826 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16827 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16828 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16829 +
16830 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16831 +{
16832 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16833 +                                            hn_mark);
16834 +       /* AuDbg("here\n"); */
16835 +       au_cache_free_hnotify(hn);
16836 +       smp_mb__before_atomic(); /* for atomic64_dec */
16837 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16838 +               wake_up(&au_hfsn_wq);
16839 +}
16840 +
16841 +static int au_hfsn_alloc(struct au_hinode *hinode)
16842 +{
16843 +       int err;
16844 +       struct au_hnotify *hn;
16845 +       struct super_block *sb;
16846 +       struct au_branch *br;
16847 +       struct fsnotify_mark *mark;
16848 +       aufs_bindex_t bindex;
16849 +
16850 +       hn = hinode->hi_notify;
16851 +       sb = hn->hn_aufs_inode->i_sb;
16852 +       bindex = au_br_index(sb, hinode->hi_id);
16853 +       br = au_sbr(sb, bindex);
16854 +       AuDebugOn(!br->br_hfsn);
16855 +
16856 +       mark = &hn->hn_mark;
16857 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16858 +       mark->mask = AuHfsnMask;
16859 +       /*
16860 +        * by udba rename or rmdir, aufs assign a new inode to the known
16861 +        * h_inode, so specify 1 to allow dups.
16862 +        */
16863 +       lockdep_off();
16864 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16865 +       lockdep_on();
16866 +
16867 +       return err;
16868 +}
16869 +
16870 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16871 +{
16872 +       struct fsnotify_mark *mark;
16873 +       unsigned long long ull;
16874 +       struct fsnotify_group *group;
16875 +
16876 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16877 +       BUG_ON(!ull);
16878 +
16879 +       mark = &hn->hn_mark;
16880 +       spin_lock(&mark->lock);
16881 +       group = mark->group;
16882 +       fsnotify_get_group(group);
16883 +       spin_unlock(&mark->lock);
16884 +       lockdep_off();
16885 +       fsnotify_destroy_mark(mark, group);
16886 +       fsnotify_put_mark(mark);
16887 +       fsnotify_put_group(group);
16888 +       lockdep_on();
16889 +
16890 +       /* free hn by myself */
16891 +       return 0;
16892 +}
16893 +
16894 +/* ---------------------------------------------------------------------- */
16895 +
16896 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16897 +{
16898 +       struct fsnotify_mark *mark;
16899 +
16900 +       mark = &hinode->hi_notify->hn_mark;
16901 +       spin_lock(&mark->lock);
16902 +       if (do_set) {
16903 +               AuDebugOn(mark->mask & AuHfsnMask);
16904 +               mark->mask |= AuHfsnMask;
16905 +       } else {
16906 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16907 +               mark->mask &= ~AuHfsnMask;
16908 +       }
16909 +       spin_unlock(&mark->lock);
16910 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16911 +}
16912 +
16913 +/* ---------------------------------------------------------------------- */
16914 +
16915 +/* #define AuDbgHnotify */
16916 +#ifdef AuDbgHnotify
16917 +static char *au_hfsn_name(u32 mask)
16918 +{
16919 +#ifdef CONFIG_AUFS_DEBUG
16920 +#define test_ret(flag)                         \
16921 +       do {                                    \
16922 +               if (mask & flag)                \
16923 +                       return #flag;           \
16924 +       } while (0)
16925 +       test_ret(FS_ACCESS);
16926 +       test_ret(FS_MODIFY);
16927 +       test_ret(FS_ATTRIB);
16928 +       test_ret(FS_CLOSE_WRITE);
16929 +       test_ret(FS_CLOSE_NOWRITE);
16930 +       test_ret(FS_OPEN);
16931 +       test_ret(FS_MOVED_FROM);
16932 +       test_ret(FS_MOVED_TO);
16933 +       test_ret(FS_CREATE);
16934 +       test_ret(FS_DELETE);
16935 +       test_ret(FS_DELETE_SELF);
16936 +       test_ret(FS_MOVE_SELF);
16937 +       test_ret(FS_UNMOUNT);
16938 +       test_ret(FS_Q_OVERFLOW);
16939 +       test_ret(FS_IN_IGNORED);
16940 +       test_ret(FS_ISDIR);
16941 +       test_ret(FS_IN_ONESHOT);
16942 +       test_ret(FS_EVENT_ON_CHILD);
16943 +       return "";
16944 +#undef test_ret
16945 +#else
16946 +       return "??";
16947 +#endif
16948 +}
16949 +#endif
16950 +
16951 +/* ---------------------------------------------------------------------- */
16952 +
16953 +static void au_hfsn_free_group(struct fsnotify_group *group)
16954 +{
16955 +       struct au_br_hfsnotify *hfsn = group->private;
16956 +
16957 +       /* AuDbg("here\n"); */
16958 +       au_kfree_try_rcu(hfsn);
16959 +}
16960 +
16961 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16962 +                               u32 mask, const void *data, int data_type,
16963 +                               struct inode *inode,
16964 +                               const struct qstr *file_name, u32 cookie,
16965 +                               struct fsnotify_iter_info *iter_info)
16966 +{
16967 +       int err;
16968 +       struct au_hnotify *hnotify;
16969 +       struct inode *h_dir, *h_inode;
16970 +       struct fsnotify_mark *inode_mark;
16971 +
16972 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16973 +
16974 +       err = 0;
16975 +       /* if FS_UNMOUNT happens, there must be another bug */
16976 +       AuDebugOn(mask & FS_UNMOUNT);
16977 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16978 +               goto out;
16979 +
16980 +       h_dir = inode;
16981 +       h_inode = NULL;
16982 +#ifdef AuDbgHnotify
16983 +       au_debug_on();
16984 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16985 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16986 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16987 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16988 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16989 +               /* WARN_ON(1); */
16990 +       }
16991 +       au_debug_off();
16992 +#endif
16993 +
16994 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16995 +       AuDebugOn(!inode_mark);
16996 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16997 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16998 +
16999 +out:
17000 +       return err;
17001 +}
17002 +
17003 +static struct fsnotify_ops au_hfsn_ops = {
17004 +       .handle_event           = au_hfsn_handle_event,
17005 +       .free_group_priv        = au_hfsn_free_group,
17006 +       .free_mark              = au_hfsn_free_mark
17007 +};
17008 +
17009 +/* ---------------------------------------------------------------------- */
17010 +
17011 +static void au_hfsn_fin_br(struct au_branch *br)
17012 +{
17013 +       struct au_br_hfsnotify *hfsn;
17014 +
17015 +       hfsn = br->br_hfsn;
17016 +       if (hfsn) {
17017 +               lockdep_off();
17018 +               fsnotify_put_group(hfsn->hfsn_group);
17019 +               lockdep_on();
17020 +       }
17021 +}
17022 +
17023 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17024 +{
17025 +       int err;
17026 +       struct fsnotify_group *group;
17027 +       struct au_br_hfsnotify *hfsn;
17028 +
17029 +       err = 0;
17030 +       br->br_hfsn = NULL;
17031 +       if (!au_br_hnotifyable(perm))
17032 +               goto out;
17033 +
17034 +       err = -ENOMEM;
17035 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17036 +       if (unlikely(!hfsn))
17037 +               goto out;
17038 +
17039 +       err = 0;
17040 +       group = fsnotify_alloc_group(&au_hfsn_ops);
17041 +       if (IS_ERR(group)) {
17042 +               err = PTR_ERR(group);
17043 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
17044 +               goto out_hfsn;
17045 +       }
17046 +
17047 +       group->private = hfsn;
17048 +       hfsn->hfsn_group = group;
17049 +       br->br_hfsn = hfsn;
17050 +       goto out; /* success */
17051 +
17052 +out_hfsn:
17053 +       au_kfree_try_rcu(hfsn);
17054 +out:
17055 +       return err;
17056 +}
17057 +
17058 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17059 +{
17060 +       int err;
17061 +
17062 +       err = 0;
17063 +       if (!br->br_hfsn)
17064 +               err = au_hfsn_init_br(br, perm);
17065 +
17066 +       return err;
17067 +}
17068 +
17069 +/* ---------------------------------------------------------------------- */
17070 +
17071 +static void au_hfsn_fin(void)
17072 +{
17073 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17074 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17075 +}
17076 +
17077 +const struct au_hnotify_op au_hnotify_op = {
17078 +       .ctl            = au_hfsn_ctl,
17079 +       .alloc          = au_hfsn_alloc,
17080 +       .free           = au_hfsn_free,
17081 +
17082 +       .fin            = au_hfsn_fin,
17083 +
17084 +       .reset_br       = au_hfsn_reset_br,
17085 +       .fin_br         = au_hfsn_fin_br,
17086 +       .init_br        = au_hfsn_init_br
17087 +};
17088 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17089 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
17090 +++ linux/fs/aufs/hfsplus.c     2020-01-27 10:57:18.172204883 +0100
17091 @@ -0,0 +1,60 @@
17092 +// SPDX-License-Identifier: GPL-2.0
17093 +/*
17094 + * Copyright (C) 2010-2020 Junjiro R. Okajima
17095 + *
17096 + * This program, aufs is free software; you can redistribute it and/or modify
17097 + * it under the terms of the GNU General Public License as published by
17098 + * the Free Software Foundation; either version 2 of the License, or
17099 + * (at your option) any later version.
17100 + *
17101 + * This program is distributed in the hope that it will be useful,
17102 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17103 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17104 + * GNU General Public License for more details.
17105 + *
17106 + * You should have received a copy of the GNU General Public License
17107 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17108 + */
17109 +
17110 +/*
17111 + * special support for filesystems which acquires an inode mutex
17112 + * at final closing a file, eg, hfsplus.
17113 + *
17114 + * This trick is very simple and stupid, just to open the file before really
17115 + * necessary open to tell hfsplus that this is not the final closing.
17116 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17117 + * and au_h_open_post() after releasing it.
17118 + */
17119 +
17120 +#include "aufs.h"
17121 +
17122 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17123 +                          int force_wr)
17124 +{
17125 +       struct file *h_file;
17126 +       struct dentry *h_dentry;
17127 +
17128 +       h_dentry = au_h_dptr(dentry, bindex);
17129 +       AuDebugOn(!h_dentry);
17130 +       AuDebugOn(d_is_negative(h_dentry));
17131 +
17132 +       h_file = NULL;
17133 +       if (au_test_hfsplus(h_dentry->d_sb)
17134 +           && d_is_reg(h_dentry))
17135 +               h_file = au_h_open(dentry, bindex,
17136 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17137 +                                  /*file*/NULL, force_wr);
17138 +       return h_file;
17139 +}
17140 +
17141 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17142 +                   struct file *h_file)
17143 +{
17144 +       struct au_branch *br;
17145 +
17146 +       if (h_file) {
17147 +               fput(h_file);
17148 +               br = au_sbr(dentry->d_sb, bindex);
17149 +               au_lcnt_dec(&br->br_nfiles);
17150 +       }
17151 +}
17152 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17153 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17154 +++ linux/fs/aufs/hnotify.c     2020-01-27 10:57:18.172204883 +0100
17155 @@ -0,0 +1,715 @@
17156 +// SPDX-License-Identifier: GPL-2.0
17157 +/*
17158 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17159 + *
17160 + * This program, aufs is free software; you can redistribute it and/or modify
17161 + * it under the terms of the GNU General Public License as published by
17162 + * the Free Software Foundation; either version 2 of the License, or
17163 + * (at your option) any later version.
17164 + *
17165 + * This program is distributed in the hope that it will be useful,
17166 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17167 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17168 + * GNU General Public License for more details.
17169 + *
17170 + * You should have received a copy of the GNU General Public License
17171 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17172 + */
17173 +
17174 +/*
17175 + * abstraction to notify the direct changes on lower directories
17176 + */
17177 +
17178 +/* #include <linux/iversion.h> */
17179 +#include "aufs.h"
17180 +
17181 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17182 +{
17183 +       int err;
17184 +       struct au_hnotify *hn;
17185 +
17186 +       err = -ENOMEM;
17187 +       hn = au_cache_alloc_hnotify();
17188 +       if (hn) {
17189 +               hn->hn_aufs_inode = inode;
17190 +               hinode->hi_notify = hn;
17191 +               err = au_hnotify_op.alloc(hinode);
17192 +               AuTraceErr(err);
17193 +               if (unlikely(err)) {
17194 +                       hinode->hi_notify = NULL;
17195 +                       au_cache_free_hnotify(hn);
17196 +                       /*
17197 +                        * The upper dir was removed by udba, but the same named
17198 +                        * dir left. In this case, aufs assigns a new inode
17199 +                        * number and set the monitor again.
17200 +                        * For the lower dir, the old monitor is still left.
17201 +                        */
17202 +                       if (err == -EEXIST)
17203 +                               err = 0;
17204 +               }
17205 +       }
17206 +
17207 +       AuTraceErr(err);
17208 +       return err;
17209 +}
17210 +
17211 +void au_hn_free(struct au_hinode *hinode)
17212 +{
17213 +       struct au_hnotify *hn;
17214 +
17215 +       hn = hinode->hi_notify;
17216 +       if (hn) {
17217 +               hinode->hi_notify = NULL;
17218 +               if (au_hnotify_op.free(hinode, hn))
17219 +                       au_cache_free_hnotify(hn);
17220 +       }
17221 +}
17222 +
17223 +/* ---------------------------------------------------------------------- */
17224 +
17225 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17226 +{
17227 +       if (hinode->hi_notify)
17228 +               au_hnotify_op.ctl(hinode, do_set);
17229 +}
17230 +
17231 +void au_hn_reset(struct inode *inode, unsigned int flags)
17232 +{
17233 +       aufs_bindex_t bindex, bbot;
17234 +       struct inode *hi;
17235 +       struct dentry *iwhdentry;
17236 +
17237 +       bbot = au_ibbot(inode);
17238 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17239 +               hi = au_h_iptr(inode, bindex);
17240 +               if (!hi)
17241 +                       continue;
17242 +
17243 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17244 +               iwhdentry = au_hi_wh(inode, bindex);
17245 +               if (iwhdentry)
17246 +                       dget(iwhdentry);
17247 +               au_igrab(hi);
17248 +               au_set_h_iptr(inode, bindex, NULL, 0);
17249 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17250 +                             flags & ~AuHi_XINO);
17251 +               iput(hi);
17252 +               dput(iwhdentry);
17253 +               /* inode_unlock(hi); */
17254 +       }
17255 +}
17256 +
17257 +/* ---------------------------------------------------------------------- */
17258 +
17259 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17260 +{
17261 +       int err;
17262 +       aufs_bindex_t bindex, bbot, bfound, btop;
17263 +       struct inode *h_i;
17264 +
17265 +       err = 0;
17266 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17267 +               pr_warn("branch root dir was changed\n");
17268 +               goto out;
17269 +       }
17270 +
17271 +       bfound = -1;
17272 +       bbot = au_ibbot(inode);
17273 +       btop = au_ibtop(inode);
17274 +#if 0 /* reserved for future use */
17275 +       if (bindex == bbot) {
17276 +               /* keep this ino in rename case */
17277 +               goto out;
17278 +       }
17279 +#endif
17280 +       for (bindex = btop; bindex <= bbot; bindex++)
17281 +               if (au_h_iptr(inode, bindex) == h_inode) {
17282 +                       bfound = bindex;
17283 +                       break;
17284 +               }
17285 +       if (bfound < 0)
17286 +               goto out;
17287 +
17288 +       for (bindex = btop; bindex <= bbot; bindex++) {
17289 +               h_i = au_h_iptr(inode, bindex);
17290 +               if (!h_i)
17291 +                       continue;
17292 +
17293 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17294 +               /* ignore this error */
17295 +               /* bad action? */
17296 +       }
17297 +
17298 +       /* children inode number will be broken */
17299 +
17300 +out:
17301 +       AuTraceErr(err);
17302 +       return err;
17303 +}
17304 +
17305 +static int hn_gen_tree(struct dentry *dentry)
17306 +{
17307 +       int err, i, j, ndentry;
17308 +       struct au_dcsub_pages dpages;
17309 +       struct au_dpage *dpage;
17310 +       struct dentry **dentries;
17311 +
17312 +       err = au_dpages_init(&dpages, GFP_NOFS);
17313 +       if (unlikely(err))
17314 +               goto out;
17315 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17316 +       if (unlikely(err))
17317 +               goto out_dpages;
17318 +
17319 +       for (i = 0; i < dpages.ndpage; i++) {
17320 +               dpage = dpages.dpages + i;
17321 +               dentries = dpage->dentries;
17322 +               ndentry = dpage->ndentry;
17323 +               for (j = 0; j < ndentry; j++) {
17324 +                       struct dentry *d;
17325 +
17326 +                       d = dentries[j];
17327 +                       if (IS_ROOT(d))
17328 +                               continue;
17329 +
17330 +                       au_digen_dec(d);
17331 +                       if (d_really_is_positive(d))
17332 +                               /* todo: reset children xino?
17333 +                                  cached children only? */
17334 +                               au_iigen_dec(d_inode(d));
17335 +               }
17336 +       }
17337 +
17338 +out_dpages:
17339 +       au_dpages_free(&dpages);
17340 +out:
17341 +       return err;
17342 +}
17343 +
17344 +/*
17345 + * return 0 if processed.
17346 + */
17347 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17348 +                          const unsigned int isdir)
17349 +{
17350 +       int err;
17351 +       struct dentry *d;
17352 +       struct qstr *dname;
17353 +
17354 +       err = 1;
17355 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17356 +               pr_warn("branch root dir was changed\n");
17357 +               err = 0;
17358 +               goto out;
17359 +       }
17360 +
17361 +       if (!isdir) {
17362 +               AuDebugOn(!name);
17363 +               au_iigen_dec(inode);
17364 +               spin_lock(&inode->i_lock);
17365 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17366 +                       spin_lock(&d->d_lock);
17367 +                       dname = &d->d_name;
17368 +                       if (dname->len != nlen
17369 +                           && memcmp(dname->name, name, nlen)) {
17370 +                               spin_unlock(&d->d_lock);
17371 +                               continue;
17372 +                       }
17373 +                       err = 0;
17374 +                       au_digen_dec(d);
17375 +                       spin_unlock(&d->d_lock);
17376 +                       break;
17377 +               }
17378 +               spin_unlock(&inode->i_lock);
17379 +       } else {
17380 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17381 +               d = d_find_any_alias(inode);
17382 +               if (!d) {
17383 +                       au_iigen_dec(inode);
17384 +                       goto out;
17385 +               }
17386 +
17387 +               spin_lock(&d->d_lock);
17388 +               dname = &d->d_name;
17389 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17390 +                       spin_unlock(&d->d_lock);
17391 +                       err = hn_gen_tree(d);
17392 +                       spin_lock(&d->d_lock);
17393 +               }
17394 +               spin_unlock(&d->d_lock);
17395 +               dput(d);
17396 +       }
17397 +
17398 +out:
17399 +       AuTraceErr(err);
17400 +       return err;
17401 +}
17402 +
17403 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17404 +{
17405 +       int err;
17406 +
17407 +       if (IS_ROOT(dentry)) {
17408 +               pr_warn("branch root dir was changed\n");
17409 +               return 0;
17410 +       }
17411 +
17412 +       err = 0;
17413 +       if (!isdir) {
17414 +               au_digen_dec(dentry);
17415 +               if (d_really_is_positive(dentry))
17416 +                       au_iigen_dec(d_inode(dentry));
17417 +       } else {
17418 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17419 +               if (d_really_is_positive(dentry))
17420 +                       err = hn_gen_tree(dentry);
17421 +       }
17422 +
17423 +       AuTraceErr(err);
17424 +       return err;
17425 +}
17426 +
17427 +/* ---------------------------------------------------------------------- */
17428 +
17429 +/* hnotify job flags */
17430 +#define AuHnJob_XINO0          1
17431 +#define AuHnJob_GEN            (1 << 1)
17432 +#define AuHnJob_DIRENT         (1 << 2)
17433 +#define AuHnJob_ISDIR          (1 << 3)
17434 +#define AuHnJob_TRYXINO0       (1 << 4)
17435 +#define AuHnJob_MNTPNT         (1 << 5)
17436 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17437 +#define au_fset_hnjob(flags, name) \
17438 +       do { (flags) |= AuHnJob_##name; } while (0)
17439 +#define au_fclr_hnjob(flags, name) \
17440 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17441 +
17442 +enum {
17443 +       AuHn_CHILD,
17444 +       AuHn_PARENT,
17445 +       AuHnLast
17446 +};
17447 +
17448 +struct au_hnotify_args {
17449 +       struct inode *h_dir, *dir, *h_child_inode;
17450 +       u32 mask;
17451 +       unsigned int flags[AuHnLast];
17452 +       unsigned int h_child_nlen;
17453 +       char h_child_name[];
17454 +};
17455 +
17456 +struct hn_job_args {
17457 +       unsigned int flags;
17458 +       struct inode *inode, *h_inode, *dir, *h_dir;
17459 +       struct dentry *dentry;
17460 +       char *h_name;
17461 +       int h_nlen;
17462 +};
17463 +
17464 +static int hn_job(struct hn_job_args *a)
17465 +{
17466 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17467 +       int e;
17468 +
17469 +       /* reset xino */
17470 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17471 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17472 +
17473 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17474 +           && a->inode
17475 +           && a->h_inode) {
17476 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17477 +               if (!a->h_inode->i_nlink
17478 +                   && !(a->h_inode->i_state & I_LINKABLE))
17479 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17480 +               inode_unlock_shared(a->h_inode);
17481 +       }
17482 +
17483 +       /* make the generation obsolete */
17484 +       if (au_ftest_hnjob(a->flags, GEN)) {
17485 +               e = -1;
17486 +               if (a->inode)
17487 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17488 +                                             isdir);
17489 +               if (e && a->dentry)
17490 +                       hn_gen_by_name(a->dentry, isdir);
17491 +               /* ignore this error */
17492 +       }
17493 +
17494 +       /* make dir entries obsolete */
17495 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17496 +               struct au_vdir *vdir;
17497 +
17498 +               vdir = au_ivdir(a->inode);
17499 +               if (vdir)
17500 +                       vdir->vd_jiffy = 0;
17501 +               /* IMustLock(a->inode); */
17502 +               /* inode_inc_iversion(a->inode); */
17503 +       }
17504 +
17505 +       /* can do nothing but warn */
17506 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17507 +           && a->dentry
17508 +           && d_mountpoint(a->dentry))
17509 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17510 +
17511 +       return 0;
17512 +}
17513 +
17514 +/* ---------------------------------------------------------------------- */
17515 +
17516 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17517 +                                          struct inode *dir)
17518 +{
17519 +       struct dentry *dentry, *d, *parent;
17520 +       struct qstr *dname;
17521 +
17522 +       parent = d_find_any_alias(dir);
17523 +       if (!parent)
17524 +               return NULL;
17525 +
17526 +       dentry = NULL;
17527 +       spin_lock(&parent->d_lock);
17528 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17529 +               /* AuDbg("%pd\n", d); */
17530 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17531 +               dname = &d->d_name;
17532 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17533 +                       goto cont_unlock;
17534 +               if (au_di(d))
17535 +                       au_digen_dec(d);
17536 +               else
17537 +                       goto cont_unlock;
17538 +               if (au_dcount(d) > 0) {
17539 +                       dentry = dget_dlock(d);
17540 +                       spin_unlock(&d->d_lock);
17541 +                       break;
17542 +               }
17543 +
17544 +cont_unlock:
17545 +               spin_unlock(&d->d_lock);
17546 +       }
17547 +       spin_unlock(&parent->d_lock);
17548 +       dput(parent);
17549 +
17550 +       if (dentry)
17551 +               di_write_lock_child(dentry);
17552 +
17553 +       return dentry;
17554 +}
17555 +
17556 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17557 +                                        aufs_bindex_t bindex, ino_t h_ino)
17558 +{
17559 +       struct inode *inode;
17560 +       ino_t ino;
17561 +       int err;
17562 +
17563 +       inode = NULL;
17564 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17565 +       if (!err && ino)
17566 +               inode = ilookup(sb, ino);
17567 +       if (!inode)
17568 +               goto out;
17569 +
17570 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17571 +               pr_warn("wrong root branch\n");
17572 +               iput(inode);
17573 +               inode = NULL;
17574 +               goto out;
17575 +       }
17576 +
17577 +       ii_write_lock_child(inode);
17578 +
17579 +out:
17580 +       return inode;
17581 +}
17582 +
17583 +static void au_hn_bh(void *_args)
17584 +{
17585 +       struct au_hnotify_args *a = _args;
17586 +       struct super_block *sb;
17587 +       aufs_bindex_t bindex, bbot, bfound;
17588 +       unsigned char xino, try_iput;
17589 +       int err;
17590 +       struct inode *inode;
17591 +       ino_t h_ino;
17592 +       struct hn_job_args args;
17593 +       struct dentry *dentry;
17594 +       struct au_sbinfo *sbinfo;
17595 +
17596 +       AuDebugOn(!_args);
17597 +       AuDebugOn(!a->h_dir);
17598 +       AuDebugOn(!a->dir);
17599 +       AuDebugOn(!a->mask);
17600 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17601 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17602 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17603 +
17604 +       inode = NULL;
17605 +       dentry = NULL;
17606 +       /*
17607 +        * do not lock a->dir->i_mutex here
17608 +        * because of d_revalidate() may cause a deadlock.
17609 +        */
17610 +       sb = a->dir->i_sb;
17611 +       AuDebugOn(!sb);
17612 +       sbinfo = au_sbi(sb);
17613 +       AuDebugOn(!sbinfo);
17614 +       si_write_lock(sb, AuLock_NOPLMW);
17615 +
17616 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17617 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17618 +               case FS_MOVED_FROM:
17619 +               case FS_MOVED_TO:
17620 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17621 +                               "for the direct rename(2)\n");
17622 +               }
17623 +
17624 +       ii_read_lock_parent(a->dir);
17625 +       bfound = -1;
17626 +       bbot = au_ibbot(a->dir);
17627 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17628 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17629 +                       bfound = bindex;
17630 +                       break;
17631 +               }
17632 +       ii_read_unlock(a->dir);
17633 +       if (unlikely(bfound < 0))
17634 +               goto out;
17635 +
17636 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17637 +       h_ino = 0;
17638 +       if (a->h_child_inode)
17639 +               h_ino = a->h_child_inode->i_ino;
17640 +
17641 +       if (a->h_child_nlen
17642 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17643 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17644 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17645 +                                             a->dir);
17646 +       try_iput = 0;
17647 +       if (dentry && d_really_is_positive(dentry))
17648 +               inode = d_inode(dentry);
17649 +       if (xino && !inode && h_ino
17650 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17651 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17652 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17653 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17654 +               try_iput = 1;
17655 +       }
17656 +
17657 +       args.flags = a->flags[AuHn_CHILD];
17658 +       args.dentry = dentry;
17659 +       args.inode = inode;
17660 +       args.h_inode = a->h_child_inode;
17661 +       args.dir = a->dir;
17662 +       args.h_dir = a->h_dir;
17663 +       args.h_name = a->h_child_name;
17664 +       args.h_nlen = a->h_child_nlen;
17665 +       err = hn_job(&args);
17666 +       if (dentry) {
17667 +               if (au_di(dentry))
17668 +                       di_write_unlock(dentry);
17669 +               dput(dentry);
17670 +       }
17671 +       if (inode && try_iput) {
17672 +               ii_write_unlock(inode);
17673 +               iput(inode);
17674 +       }
17675 +
17676 +       ii_write_lock_parent(a->dir);
17677 +       args.flags = a->flags[AuHn_PARENT];
17678 +       args.dentry = NULL;
17679 +       args.inode = a->dir;
17680 +       args.h_inode = a->h_dir;
17681 +       args.dir = NULL;
17682 +       args.h_dir = NULL;
17683 +       args.h_name = NULL;
17684 +       args.h_nlen = 0;
17685 +       err = hn_job(&args);
17686 +       ii_write_unlock(a->dir);
17687 +
17688 +out:
17689 +       iput(a->h_child_inode);
17690 +       iput(a->h_dir);
17691 +       iput(a->dir);
17692 +       si_write_unlock(sb);
17693 +       au_nwt_done(&sbinfo->si_nowait);
17694 +       au_kfree_rcu(a);
17695 +}
17696 +
17697 +/* ---------------------------------------------------------------------- */
17698 +
17699 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17700 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17701 +{
17702 +       int err, len;
17703 +       unsigned int flags[AuHnLast], f;
17704 +       unsigned char isdir, isroot, wh;
17705 +       struct inode *dir;
17706 +       struct au_hnotify_args *args;
17707 +       char *p, *h_child_name;
17708 +
17709 +       err = 0;
17710 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17711 +       dir = igrab(hnotify->hn_aufs_inode);
17712 +       if (!dir)
17713 +               goto out;
17714 +
17715 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17716 +       wh = 0;
17717 +       h_child_name = (void *)h_child_qstr->name;
17718 +       len = h_child_qstr->len;
17719 +       if (h_child_name) {
17720 +               if (len > AUFS_WH_PFX_LEN
17721 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17722 +                       h_child_name += AUFS_WH_PFX_LEN;
17723 +                       len -= AUFS_WH_PFX_LEN;
17724 +                       wh = 1;
17725 +               }
17726 +       }
17727 +
17728 +       isdir = 0;
17729 +       if (h_child_inode)
17730 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17731 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17732 +       flags[AuHn_CHILD] = 0;
17733 +       if (isdir)
17734 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17735 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17736 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17737 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17738 +       case FS_MOVED_FROM:
17739 +       case FS_MOVED_TO:
17740 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17741 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17742 +               /*FALLTHROUGH*/
17743 +       case FS_CREATE:
17744 +               AuDebugOn(!h_child_name);
17745 +               break;
17746 +
17747 +       case FS_DELETE:
17748 +               /*
17749 +                * aufs never be able to get this child inode.
17750 +                * revalidation should be in d_revalidate()
17751 +                * by checking i_nlink, i_generation or d_unhashed().
17752 +                */
17753 +               AuDebugOn(!h_child_name);
17754 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17755 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17756 +               break;
17757 +
17758 +       default:
17759 +               AuDebugOn(1);
17760 +       }
17761 +
17762 +       if (wh)
17763 +               h_child_inode = NULL;
17764 +
17765 +       err = -ENOMEM;
17766 +       /* iput() and kfree() will be called in au_hnotify() */
17767 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17768 +       if (unlikely(!args)) {
17769 +               AuErr1("no memory\n");
17770 +               iput(dir);
17771 +               goto out;
17772 +       }
17773 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17774 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17775 +       args->mask = mask;
17776 +       args->dir = dir;
17777 +       args->h_dir = igrab(h_dir);
17778 +       if (h_child_inode)
17779 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17780 +       args->h_child_inode = h_child_inode;
17781 +       args->h_child_nlen = len;
17782 +       if (len) {
17783 +               p = (void *)args;
17784 +               p += sizeof(*args);
17785 +               memcpy(p, h_child_name, len);
17786 +               p[len] = 0;
17787 +       }
17788 +
17789 +       /* NFS fires the event for silly-renamed one from kworker */
17790 +       f = 0;
17791 +       if (!dir->i_nlink
17792 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17793 +               f = AuWkq_NEST;
17794 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17795 +       if (unlikely(err)) {
17796 +               pr_err("wkq %d\n", err);
17797 +               iput(args->h_child_inode);
17798 +               iput(args->h_dir);
17799 +               iput(args->dir);
17800 +               au_kfree_rcu(args);
17801 +       }
17802 +
17803 +out:
17804 +       return err;
17805 +}
17806 +
17807 +/* ---------------------------------------------------------------------- */
17808 +
17809 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17810 +{
17811 +       int err;
17812 +
17813 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17814 +
17815 +       err = 0;
17816 +       if (au_hnotify_op.reset_br)
17817 +               err = au_hnotify_op.reset_br(udba, br, perm);
17818 +
17819 +       return err;
17820 +}
17821 +
17822 +int au_hnotify_init_br(struct au_branch *br, int perm)
17823 +{
17824 +       int err;
17825 +
17826 +       err = 0;
17827 +       if (au_hnotify_op.init_br)
17828 +               err = au_hnotify_op.init_br(br, perm);
17829 +
17830 +       return err;
17831 +}
17832 +
17833 +void au_hnotify_fin_br(struct au_branch *br)
17834 +{
17835 +       if (au_hnotify_op.fin_br)
17836 +               au_hnotify_op.fin_br(br);
17837 +}
17838 +
17839 +static void au_hn_destroy_cache(void)
17840 +{
17841 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17842 +       au_cache[AuCache_HNOTIFY] = NULL;
17843 +}
17844 +
17845 +int __init au_hnotify_init(void)
17846 +{
17847 +       int err;
17848 +
17849 +       err = -ENOMEM;
17850 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17851 +       if (au_cache[AuCache_HNOTIFY]) {
17852 +               err = 0;
17853 +               if (au_hnotify_op.init)
17854 +                       err = au_hnotify_op.init();
17855 +               if (unlikely(err))
17856 +                       au_hn_destroy_cache();
17857 +       }
17858 +       AuTraceErr(err);
17859 +       return err;
17860 +}
17861 +
17862 +void au_hnotify_fin(void)
17863 +{
17864 +       if (au_hnotify_op.fin)
17865 +               au_hnotify_op.fin();
17866 +
17867 +       /* cf. au_cache_fin() */
17868 +       if (au_cache[AuCache_HNOTIFY])
17869 +               au_hn_destroy_cache();
17870 +}
17871 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17872 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17873 +++ linux/fs/aufs/iinfo.c       2020-01-27 10:57:18.175538316 +0100
17874 @@ -0,0 +1,286 @@
17875 +// SPDX-License-Identifier: GPL-2.0
17876 +/*
17877 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17878 + *
17879 + * This program, aufs is free software; you can redistribute it and/or modify
17880 + * it under the terms of the GNU General Public License as published by
17881 + * the Free Software Foundation; either version 2 of the License, or
17882 + * (at your option) any later version.
17883 + *
17884 + * This program is distributed in the hope that it will be useful,
17885 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17886 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17887 + * GNU General Public License for more details.
17888 + *
17889 + * You should have received a copy of the GNU General Public License
17890 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17891 + */
17892 +
17893 +/*
17894 + * inode private data
17895 + */
17896 +
17897 +#include "aufs.h"
17898 +
17899 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17900 +{
17901 +       struct inode *h_inode;
17902 +       struct au_hinode *hinode;
17903 +
17904 +       IiMustAnyLock(inode);
17905 +
17906 +       hinode = au_hinode(au_ii(inode), bindex);
17907 +       h_inode = hinode->hi_inode;
17908 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17909 +       return h_inode;
17910 +}
17911 +
17912 +/* todo: hard/soft set? */
17913 +void au_hiput(struct au_hinode *hinode)
17914 +{
17915 +       au_hn_free(hinode);
17916 +       dput(hinode->hi_whdentry);
17917 +       iput(hinode->hi_inode);
17918 +}
17919 +
17920 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17921 +{
17922 +       unsigned int flags;
17923 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17924 +
17925 +       flags = 0;
17926 +       if (au_opt_test(mnt_flags, XINO))
17927 +               au_fset_hi(flags, XINO);
17928 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17929 +               au_fset_hi(flags, HNOTIFY);
17930 +       return flags;
17931 +}
17932 +
17933 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17934 +                  struct inode *h_inode, unsigned int flags)
17935 +{
17936 +       struct au_hinode *hinode;
17937 +       struct inode *hi;
17938 +       struct au_iinfo *iinfo = au_ii(inode);
17939 +
17940 +       IiMustWriteLock(inode);
17941 +
17942 +       hinode = au_hinode(iinfo, bindex);
17943 +       hi = hinode->hi_inode;
17944 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17945 +
17946 +       if (hi)
17947 +               au_hiput(hinode);
17948 +       hinode->hi_inode = h_inode;
17949 +       if (h_inode) {
17950 +               int err;
17951 +               struct super_block *sb = inode->i_sb;
17952 +               struct au_branch *br;
17953 +
17954 +               AuDebugOn(inode->i_mode
17955 +                         && (h_inode->i_mode & S_IFMT)
17956 +                         != (inode->i_mode & S_IFMT));
17957 +               if (bindex == iinfo->ii_btop)
17958 +                       au_cpup_igen(inode, h_inode);
17959 +               br = au_sbr(sb, bindex);
17960 +               hinode->hi_id = br->br_id;
17961 +               if (au_ftest_hi(flags, XINO)) {
17962 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17963 +                                           inode->i_ino);
17964 +                       if (unlikely(err))
17965 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17966 +               }
17967 +
17968 +               if (au_ftest_hi(flags, HNOTIFY)
17969 +                   && au_br_hnotifyable(br->br_perm)) {
17970 +                       err = au_hn_alloc(hinode, inode);
17971 +                       if (unlikely(err))
17972 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17973 +               }
17974 +       }
17975 +}
17976 +
17977 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17978 +                 struct dentry *h_wh)
17979 +{
17980 +       struct au_hinode *hinode;
17981 +
17982 +       IiMustWriteLock(inode);
17983 +
17984 +       hinode = au_hinode(au_ii(inode), bindex);
17985 +       AuDebugOn(hinode->hi_whdentry);
17986 +       hinode->hi_whdentry = h_wh;
17987 +}
17988 +
17989 +void au_update_iigen(struct inode *inode, int half)
17990 +{
17991 +       struct au_iinfo *iinfo;
17992 +       struct au_iigen *iigen;
17993 +       unsigned int sigen;
17994 +
17995 +       sigen = au_sigen(inode->i_sb);
17996 +       iinfo = au_ii(inode);
17997 +       iigen = &iinfo->ii_generation;
17998 +       spin_lock(&iigen->ig_spin);
17999 +       iigen->ig_generation = sigen;
18000 +       if (half)
18001 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18002 +       else
18003 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
18004 +       spin_unlock(&iigen->ig_spin);
18005 +}
18006 +
18007 +/* it may be called at remount time, too */
18008 +void au_update_ibrange(struct inode *inode, int do_put_zero)
18009 +{
18010 +       struct au_iinfo *iinfo;
18011 +       aufs_bindex_t bindex, bbot;
18012 +
18013 +       AuDebugOn(au_is_bad_inode(inode));
18014 +       IiMustWriteLock(inode);
18015 +
18016 +       iinfo = au_ii(inode);
18017 +       if (do_put_zero && iinfo->ii_btop >= 0) {
18018 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18019 +                    bindex++) {
18020 +                       struct inode *h_i;
18021 +
18022 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
18023 +                       if (h_i
18024 +                           && !h_i->i_nlink
18025 +                           && !(h_i->i_state & I_LINKABLE))
18026 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18027 +               }
18028 +       }
18029 +
18030 +       iinfo->ii_btop = -1;
18031 +       iinfo->ii_bbot = -1;
18032 +       bbot = au_sbbot(inode->i_sb);
18033 +       for (bindex = 0; bindex <= bbot; bindex++)
18034 +               if (au_hinode(iinfo, bindex)->hi_inode) {
18035 +                       iinfo->ii_btop = bindex;
18036 +                       break;
18037 +               }
18038 +       if (iinfo->ii_btop >= 0)
18039 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18040 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
18041 +                               iinfo->ii_bbot = bindex;
18042 +                               break;
18043 +                       }
18044 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
18045 +}
18046 +
18047 +/* ---------------------------------------------------------------------- */
18048 +
18049 +void au_icntnr_init_once(void *_c)
18050 +{
18051 +       struct au_icntnr *c = _c;
18052 +       struct au_iinfo *iinfo = &c->iinfo;
18053 +
18054 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
18055 +       au_rw_init(&iinfo->ii_rwsem);
18056 +       inode_init_once(&c->vfs_inode);
18057 +}
18058 +
18059 +void au_hinode_init(struct au_hinode *hinode)
18060 +{
18061 +       hinode->hi_inode = NULL;
18062 +       hinode->hi_id = -1;
18063 +       au_hn_init(hinode);
18064 +       hinode->hi_whdentry = NULL;
18065 +}
18066 +
18067 +int au_iinfo_init(struct inode *inode)
18068 +{
18069 +       struct au_iinfo *iinfo;
18070 +       struct super_block *sb;
18071 +       struct au_hinode *hi;
18072 +       int nbr, i;
18073 +
18074 +       sb = inode->i_sb;
18075 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18076 +       nbr = au_sbbot(sb) + 1;
18077 +       if (unlikely(nbr <= 0))
18078 +               nbr = 1;
18079 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18080 +       if (hi) {
18081 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
18082 +
18083 +               iinfo->ii_hinode = hi;
18084 +               for (i = 0; i < nbr; i++, hi++)
18085 +                       au_hinode_init(hi);
18086 +
18087 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
18088 +               iinfo->ii_btop = -1;
18089 +               iinfo->ii_bbot = -1;
18090 +               iinfo->ii_vdir = NULL;
18091 +               return 0;
18092 +       }
18093 +       return -ENOMEM;
18094 +}
18095 +
18096 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
18097 +{
18098 +       int err, i;
18099 +       struct au_hinode *hip;
18100 +
18101 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18102 +
18103 +       err = -ENOMEM;
18104 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18105 +                         may_shrink);
18106 +       if (hip) {
18107 +               iinfo->ii_hinode = hip;
18108 +               i = iinfo->ii_bbot + 1;
18109 +               hip += i;
18110 +               for (; i < nbr; i++, hip++)
18111 +                       au_hinode_init(hip);
18112 +               err = 0;
18113 +       }
18114 +
18115 +       return err;
18116 +}
18117 +
18118 +void au_iinfo_fin(struct inode *inode)
18119 +{
18120 +       struct au_iinfo *iinfo;
18121 +       struct au_hinode *hi;
18122 +       struct super_block *sb;
18123 +       aufs_bindex_t bindex, bbot;
18124 +       const unsigned char unlinked = !inode->i_nlink;
18125 +
18126 +       AuDebugOn(au_is_bad_inode(inode));
18127 +
18128 +       sb = inode->i_sb;
18129 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18130 +       if (si_pid_test(sb))
18131 +               au_xino_delete_inode(inode, unlinked);
18132 +       else {
18133 +               /*
18134 +                * it is safe to hide the dependency between sbinfo and
18135 +                * sb->s_umount.
18136 +                */
18137 +               lockdep_off();
18138 +               si_noflush_read_lock(sb);
18139 +               au_xino_delete_inode(inode, unlinked);
18140 +               si_read_unlock(sb);
18141 +               lockdep_on();
18142 +       }
18143 +
18144 +       iinfo = au_ii(inode);
18145 +       if (iinfo->ii_vdir)
18146 +               au_vdir_free(iinfo->ii_vdir);
18147 +
18148 +       bindex = iinfo->ii_btop;
18149 +       if (bindex >= 0) {
18150 +               hi = au_hinode(iinfo, bindex);
18151 +               bbot = iinfo->ii_bbot;
18152 +               while (bindex++ <= bbot) {
18153 +                       if (hi->hi_inode)
18154 +                               au_hiput(hi);
18155 +                       hi++;
18156 +               }
18157 +       }
18158 +       au_kfree_rcu(iinfo->ii_hinode);
18159 +       AuRwDestroy(&iinfo->ii_rwsem);
18160 +}
18161 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18162 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18163 +++ linux/fs/aufs/inode.c       2020-01-27 10:57:18.175538316 +0100
18164 @@ -0,0 +1,529 @@
18165 +// SPDX-License-Identifier: GPL-2.0
18166 +/*
18167 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18168 + *
18169 + * This program, aufs is free software; you can redistribute it and/or modify
18170 + * it under the terms of the GNU General Public License as published by
18171 + * the Free Software Foundation; either version 2 of the License, or
18172 + * (at your option) any later version.
18173 + *
18174 + * This program is distributed in the hope that it will be useful,
18175 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18176 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18177 + * GNU General Public License for more details.
18178 + *
18179 + * You should have received a copy of the GNU General Public License
18180 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18181 + */
18182 +
18183 +/*
18184 + * inode functions
18185 + */
18186 +
18187 +#include <linux/iversion.h>
18188 +#include "aufs.h"
18189 +
18190 +struct inode *au_igrab(struct inode *inode)
18191 +{
18192 +       if (inode) {
18193 +               AuDebugOn(!atomic_read(&inode->i_count));
18194 +               ihold(inode);
18195 +       }
18196 +       return inode;
18197 +}
18198 +
18199 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18200 +{
18201 +       au_cpup_attr_all(inode, /*force*/0);
18202 +       au_update_iigen(inode, /*half*/1);
18203 +       if (do_version)
18204 +               inode_inc_iversion(inode);
18205 +}
18206 +
18207 +static int au_ii_refresh(struct inode *inode, int *update)
18208 +{
18209 +       int err, e, nbr;
18210 +       umode_t type;
18211 +       aufs_bindex_t bindex, new_bindex;
18212 +       struct super_block *sb;
18213 +       struct au_iinfo *iinfo;
18214 +       struct au_hinode *p, *q, tmp;
18215 +
18216 +       AuDebugOn(au_is_bad_inode(inode));
18217 +       IiMustWriteLock(inode);
18218 +
18219 +       *update = 0;
18220 +       sb = inode->i_sb;
18221 +       nbr = au_sbbot(sb) + 1;
18222 +       type = inode->i_mode & S_IFMT;
18223 +       iinfo = au_ii(inode);
18224 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18225 +       if (unlikely(err))
18226 +               goto out;
18227 +
18228 +       AuDebugOn(iinfo->ii_btop < 0);
18229 +       p = au_hinode(iinfo, iinfo->ii_btop);
18230 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18231 +            bindex++, p++) {
18232 +               if (!p->hi_inode)
18233 +                       continue;
18234 +
18235 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18236 +               new_bindex = au_br_index(sb, p->hi_id);
18237 +               if (new_bindex == bindex)
18238 +                       continue;
18239 +
18240 +               if (new_bindex < 0) {
18241 +                       *update = 1;
18242 +                       au_hiput(p);
18243 +                       p->hi_inode = NULL;
18244 +                       continue;
18245 +               }
18246 +
18247 +               if (new_bindex < iinfo->ii_btop)
18248 +                       iinfo->ii_btop = new_bindex;
18249 +               if (iinfo->ii_bbot < new_bindex)
18250 +                       iinfo->ii_bbot = new_bindex;
18251 +               /* swap two lower inode, and loop again */
18252 +               q = au_hinode(iinfo, new_bindex);
18253 +               tmp = *q;
18254 +               *q = *p;
18255 +               *p = tmp;
18256 +               if (tmp.hi_inode) {
18257 +                       bindex--;
18258 +                       p--;
18259 +               }
18260 +       }
18261 +       au_update_ibrange(inode, /*do_put_zero*/0);
18262 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18263 +       e = au_dy_irefresh(inode);
18264 +       if (unlikely(e && !err))
18265 +               err = e;
18266 +
18267 +out:
18268 +       AuTraceErr(err);
18269 +       return err;
18270 +}
18271 +
18272 +void au_refresh_iop(struct inode *inode, int force_getattr)
18273 +{
18274 +       int type;
18275 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18276 +       const struct inode_operations *iop
18277 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18278 +
18279 +       if (inode->i_op == iop)
18280 +               return;
18281 +
18282 +       switch (inode->i_mode & S_IFMT) {
18283 +       case S_IFDIR:
18284 +               type = AuIop_DIR;
18285 +               break;
18286 +       case S_IFLNK:
18287 +               type = AuIop_SYMLINK;
18288 +               break;
18289 +       default:
18290 +               type = AuIop_OTHER;
18291 +               break;
18292 +       }
18293 +
18294 +       inode->i_op = iop + type;
18295 +       /* unnecessary smp_wmb() */
18296 +}
18297 +
18298 +int au_refresh_hinode_self(struct inode *inode)
18299 +{
18300 +       int err, update;
18301 +
18302 +       err = au_ii_refresh(inode, &update);
18303 +       if (!err)
18304 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18305 +
18306 +       AuTraceErr(err);
18307 +       return err;
18308 +}
18309 +
18310 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18311 +{
18312 +       int err, e, update;
18313 +       unsigned int flags;
18314 +       umode_t mode;
18315 +       aufs_bindex_t bindex, bbot;
18316 +       unsigned char isdir;
18317 +       struct au_hinode *p;
18318 +       struct au_iinfo *iinfo;
18319 +
18320 +       err = au_ii_refresh(inode, &update);
18321 +       if (unlikely(err))
18322 +               goto out;
18323 +
18324 +       update = 0;
18325 +       iinfo = au_ii(inode);
18326 +       p = au_hinode(iinfo, iinfo->ii_btop);
18327 +       mode = (inode->i_mode & S_IFMT);
18328 +       isdir = S_ISDIR(mode);
18329 +       flags = au_hi_flags(inode, isdir);
18330 +       bbot = au_dbbot(dentry);
18331 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18332 +               struct inode *h_i, *h_inode;
18333 +               struct dentry *h_d;
18334 +
18335 +               h_d = au_h_dptr(dentry, bindex);
18336 +               if (!h_d || d_is_negative(h_d))
18337 +                       continue;
18338 +
18339 +               h_inode = d_inode(h_d);
18340 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18341 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18342 +                       h_i = au_h_iptr(inode, bindex);
18343 +                       if (h_i) {
18344 +                               if (h_i == h_inode)
18345 +                                       continue;
18346 +                               err = -EIO;
18347 +                               break;
18348 +                       }
18349 +               }
18350 +               if (bindex < iinfo->ii_btop)
18351 +                       iinfo->ii_btop = bindex;
18352 +               if (iinfo->ii_bbot < bindex)
18353 +                       iinfo->ii_bbot = bindex;
18354 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18355 +               update = 1;
18356 +       }
18357 +       au_update_ibrange(inode, /*do_put_zero*/0);
18358 +       e = au_dy_irefresh(inode);
18359 +       if (unlikely(e && !err))
18360 +               err = e;
18361 +       if (!err)
18362 +               au_refresh_hinode_attr(inode, update && isdir);
18363 +
18364 +out:
18365 +       AuTraceErr(err);
18366 +       return err;
18367 +}
18368 +
18369 +static int set_inode(struct inode *inode, struct dentry *dentry)
18370 +{
18371 +       int err;
18372 +       unsigned int flags;
18373 +       umode_t mode;
18374 +       aufs_bindex_t bindex, btop, btail;
18375 +       unsigned char isdir;
18376 +       struct dentry *h_dentry;
18377 +       struct inode *h_inode;
18378 +       struct au_iinfo *iinfo;
18379 +       const struct inode_operations *iop;
18380 +
18381 +       IiMustWriteLock(inode);
18382 +
18383 +       err = 0;
18384 +       isdir = 0;
18385 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18386 +       btop = au_dbtop(dentry);
18387 +       h_dentry = au_h_dptr(dentry, btop);
18388 +       h_inode = d_inode(h_dentry);
18389 +       mode = h_inode->i_mode;
18390 +       switch (mode & S_IFMT) {
18391 +       case S_IFREG:
18392 +               btail = au_dbtail(dentry);
18393 +               inode->i_op = iop + AuIop_OTHER;
18394 +               inode->i_fop = &aufs_file_fop;
18395 +               err = au_dy_iaop(inode, btop, h_inode);
18396 +               if (unlikely(err))
18397 +                       goto out;
18398 +               break;
18399 +       case S_IFDIR:
18400 +               isdir = 1;
18401 +               btail = au_dbtaildir(dentry);
18402 +               inode->i_op = iop + AuIop_DIR;
18403 +               inode->i_fop = &aufs_dir_fop;
18404 +               break;
18405 +       case S_IFLNK:
18406 +               btail = au_dbtail(dentry);
18407 +               inode->i_op = iop + AuIop_SYMLINK;
18408 +               break;
18409 +       case S_IFBLK:
18410 +       case S_IFCHR:
18411 +       case S_IFIFO:
18412 +       case S_IFSOCK:
18413 +               btail = au_dbtail(dentry);
18414 +               inode->i_op = iop + AuIop_OTHER;
18415 +               init_special_inode(inode, mode, h_inode->i_rdev);
18416 +               break;
18417 +       default:
18418 +               AuIOErr("Unknown file type 0%o\n", mode);
18419 +               err = -EIO;
18420 +               goto out;
18421 +       }
18422 +
18423 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18424 +       flags = au_hi_flags(inode, isdir);
18425 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18426 +           && au_ftest_hi(flags, HNOTIFY)
18427 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18428 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18429 +               au_fclr_hi(flags, HNOTIFY);
18430 +       iinfo = au_ii(inode);
18431 +       iinfo->ii_btop = btop;
18432 +       iinfo->ii_bbot = btail;
18433 +       for (bindex = btop; bindex <= btail; bindex++) {
18434 +               h_dentry = au_h_dptr(dentry, bindex);
18435 +               if (h_dentry)
18436 +                       au_set_h_iptr(inode, bindex,
18437 +                                     au_igrab(d_inode(h_dentry)), flags);
18438 +       }
18439 +       au_cpup_attr_all(inode, /*force*/1);
18440 +       /*
18441 +        * to force calling aufs_get_acl() every time,
18442 +        * do not call cache_no_acl() for aufs inode.
18443 +        */
18444 +
18445 +out:
18446 +       return err;
18447 +}
18448 +
18449 +/*
18450 + * successful returns with iinfo write_locked
18451 + * minus: errno
18452 + * zero: success, matched
18453 + * plus: no error, but unmatched
18454 + */
18455 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18456 +{
18457 +       int err;
18458 +       unsigned int gen, igflags;
18459 +       aufs_bindex_t bindex, bbot;
18460 +       struct inode *h_inode, *h_dinode;
18461 +       struct dentry *h_dentry;
18462 +
18463 +       /*
18464 +        * before this function, if aufs got any iinfo lock, it must be only
18465 +        * one, the parent dir.
18466 +        * it can happen by UDBA and the obsoleted inode number.
18467 +        */
18468 +       err = -EIO;
18469 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18470 +               goto out;
18471 +
18472 +       err = 1;
18473 +       ii_write_lock_new_child(inode);
18474 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18475 +       h_dinode = d_inode(h_dentry);
18476 +       bbot = au_ibbot(inode);
18477 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18478 +               h_inode = au_h_iptr(inode, bindex);
18479 +               if (!h_inode || h_inode != h_dinode)
18480 +                       continue;
18481 +
18482 +               err = 0;
18483 +               gen = au_iigen(inode, &igflags);
18484 +               if (gen == au_digen(dentry)
18485 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18486 +                       break;
18487 +
18488 +               /* fully refresh inode using dentry */
18489 +               err = au_refresh_hinode(inode, dentry);
18490 +               if (!err)
18491 +                       au_update_iigen(inode, /*half*/0);
18492 +               break;
18493 +       }
18494 +
18495 +       if (unlikely(err))
18496 +               ii_write_unlock(inode);
18497 +out:
18498 +       return err;
18499 +}
18500 +
18501 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18502 +          unsigned int d_type, ino_t *ino)
18503 +{
18504 +       int err, idx;
18505 +       const int isnondir = d_type != DT_DIR;
18506 +
18507 +       /* prevent hardlinked inode number from race condition */
18508 +       if (isnondir) {
18509 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18510 +               if (unlikely(err))
18511 +                       goto out;
18512 +       }
18513 +
18514 +       err = au_xino_read(sb, bindex, h_ino, ino);
18515 +       if (unlikely(err))
18516 +               goto out_xinondir;
18517 +
18518 +       if (!*ino) {
18519 +               err = -EIO;
18520 +               *ino = au_xino_new_ino(sb);
18521 +               if (unlikely(!*ino))
18522 +                       goto out_xinondir;
18523 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18524 +               if (unlikely(err))
18525 +                       goto out_xinondir;
18526 +       }
18527 +
18528 +out_xinondir:
18529 +       if (isnondir && idx >= 0)
18530 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18531 +out:
18532 +       return err;
18533 +}
18534 +
18535 +/* successful returns with iinfo write_locked */
18536 +/* todo: return with unlocked? */
18537 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18538 +{
18539 +       struct inode *inode, *h_inode;
18540 +       struct dentry *h_dentry;
18541 +       struct super_block *sb;
18542 +       ino_t h_ino, ino;
18543 +       int err, idx, hlinked;
18544 +       aufs_bindex_t btop;
18545 +
18546 +       sb = dentry->d_sb;
18547 +       btop = au_dbtop(dentry);
18548 +       h_dentry = au_h_dptr(dentry, btop);
18549 +       h_inode = d_inode(h_dentry);
18550 +       h_ino = h_inode->i_ino;
18551 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18552 +
18553 +new_ino:
18554 +       /*
18555 +        * stop 'race'-ing between hardlinks under different
18556 +        * parents.
18557 +        */
18558 +       if (hlinked) {
18559 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18560 +               inode = ERR_PTR(err);
18561 +               if (unlikely(err))
18562 +                       goto out;
18563 +       }
18564 +
18565 +       err = au_xino_read(sb, btop, h_ino, &ino);
18566 +       inode = ERR_PTR(err);
18567 +       if (unlikely(err))
18568 +               goto out_xinondir;
18569 +
18570 +       if (!ino) {
18571 +               ino = au_xino_new_ino(sb);
18572 +               if (unlikely(!ino)) {
18573 +                       inode = ERR_PTR(-EIO);
18574 +                       goto out_xinondir;
18575 +               }
18576 +       }
18577 +
18578 +       AuDbg("i%lu\n", (unsigned long)ino);
18579 +       inode = au_iget_locked(sb, ino);
18580 +       err = PTR_ERR(inode);
18581 +       if (IS_ERR(inode))
18582 +               goto out_xinondir;
18583 +
18584 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18585 +       if (inode->i_state & I_NEW) {
18586 +               ii_write_lock_new_child(inode);
18587 +               err = set_inode(inode, dentry);
18588 +               if (!err) {
18589 +                       unlock_new_inode(inode);
18590 +                       goto out_xinondir; /* success */
18591 +               }
18592 +
18593 +               /*
18594 +                * iget_failed() calls iput(), but we need to call
18595 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18596 +                * i_count.
18597 +                */
18598 +               atomic_inc(&inode->i_count);
18599 +               iget_failed(inode);
18600 +               ii_write_unlock(inode);
18601 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18602 +               /* ignore this error */
18603 +               goto out_iput;
18604 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18605 +               /*
18606 +                * horrible race condition between lookup, readdir and copyup
18607 +                * (or something).
18608 +                */
18609 +               if (hlinked && idx >= 0)
18610 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18611 +               err = reval_inode(inode, dentry);
18612 +               if (unlikely(err < 0)) {
18613 +                       hlinked = 0;
18614 +                       goto out_iput;
18615 +               }
18616 +               if (!err)
18617 +                       goto out; /* success */
18618 +               else if (hlinked && idx >= 0) {
18619 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18620 +                       if (unlikely(err)) {
18621 +                               iput(inode);
18622 +                               inode = ERR_PTR(err);
18623 +                               goto out;
18624 +                       }
18625 +               }
18626 +       }
18627 +
18628 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18629 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18630 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18631 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18632 +                       (unsigned long)h_ino, (unsigned long)ino);
18633 +       ino = 0;
18634 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18635 +       if (!err) {
18636 +               iput(inode);
18637 +               if (hlinked && idx >= 0)
18638 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18639 +               goto new_ino;
18640 +       }
18641 +
18642 +out_iput:
18643 +       iput(inode);
18644 +       inode = ERR_PTR(err);
18645 +out_xinondir:
18646 +       if (hlinked && idx >= 0)
18647 +               au_xinondir_leave(sb, btop, h_ino, idx);
18648 +out:
18649 +       return inode;
18650 +}
18651 +
18652 +/* ---------------------------------------------------------------------- */
18653 +
18654 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18655 +              struct inode *inode)
18656 +{
18657 +       int err;
18658 +       struct inode *hi;
18659 +
18660 +       err = au_br_rdonly(au_sbr(sb, bindex));
18661 +
18662 +       /* pseudo-link after flushed may happen out of bounds */
18663 +       if (!err
18664 +           && inode
18665 +           && au_ibtop(inode) <= bindex
18666 +           && bindex <= au_ibbot(inode)) {
18667 +               /*
18668 +                * permission check is unnecessary since vfsub routine
18669 +                * will be called later
18670 +                */
18671 +               hi = au_h_iptr(inode, bindex);
18672 +               if (hi)
18673 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18674 +       }
18675 +
18676 +       return err;
18677 +}
18678 +
18679 +int au_test_h_perm(struct inode *h_inode, int mask)
18680 +{
18681 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18682 +               return 0;
18683 +       return inode_permission(h_inode, mask);
18684 +}
18685 +
18686 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18687 +{
18688 +       if (au_test_nfs(h_inode->i_sb)
18689 +           && (mask & MAY_WRITE)
18690 +           && S_ISDIR(h_inode->i_mode))
18691 +               mask |= MAY_READ; /* force permission check */
18692 +       return au_test_h_perm(h_inode, mask);
18693 +}
18694 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18695 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18696 +++ linux/fs/aufs/inode.h       2020-08-03 09:14:46.095748745 +0200
18697 @@ -0,0 +1,698 @@
18698 +/* SPDX-License-Identifier: GPL-2.0 */
18699 +/*
18700 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18701 + *
18702 + * This program, aufs is free software; you can redistribute it and/or modify
18703 + * it under the terms of the GNU General Public License as published by
18704 + * the Free Software Foundation; either version 2 of the License, or
18705 + * (at your option) any later version.
18706 + *
18707 + * This program is distributed in the hope that it will be useful,
18708 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18709 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18710 + * GNU General Public License for more details.
18711 + *
18712 + * You should have received a copy of the GNU General Public License
18713 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18714 + */
18715 +
18716 +/*
18717 + * inode operations
18718 + */
18719 +
18720 +#ifndef __AUFS_INODE_H__
18721 +#define __AUFS_INODE_H__
18722 +
18723 +#ifdef __KERNEL__
18724 +
18725 +#include <linux/fsnotify.h>
18726 +#include "rwsem.h"
18727 +
18728 +struct vfsmount;
18729 +
18730 +struct au_hnotify {
18731 +#ifdef CONFIG_AUFS_HNOTIFY
18732 +#ifdef CONFIG_AUFS_HFSNOTIFY
18733 +       /* never use fsnotify_add_vfsmount_mark() */
18734 +       struct fsnotify_mark            hn_mark;
18735 +#endif
18736 +       struct inode            *hn_aufs_inode; /* no get/put */
18737 +       struct rcu_head         rcu;
18738 +#endif
18739 +} ____cacheline_aligned_in_smp;
18740 +
18741 +struct au_hinode {
18742 +       struct inode            *hi_inode;
18743 +       aufs_bindex_t           hi_id;
18744 +#ifdef CONFIG_AUFS_HNOTIFY
18745 +       struct au_hnotify       *hi_notify;
18746 +#endif
18747 +
18748 +       /* reference to the copied-up whiteout with get/put */
18749 +       struct dentry           *hi_whdentry;
18750 +};
18751 +
18752 +/* ig_flags */
18753 +#define AuIG_HALF_REFRESHED            1
18754 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18755 +#define au_ig_fset(flags, name) \
18756 +       do { (flags) |= AuIG_##name; } while (0)
18757 +#define au_ig_fclr(flags, name) \
18758 +       do { (flags) &= ~AuIG_##name; } while (0)
18759 +
18760 +struct au_iigen {
18761 +       spinlock_t      ig_spin;
18762 +       __u32           ig_generation, ig_flags;
18763 +};
18764 +
18765 +struct au_vdir;
18766 +struct au_iinfo {
18767 +       struct au_iigen         ii_generation;
18768 +       struct super_block      *ii_hsb1;       /* no get/put */
18769 +
18770 +       struct au_rwsem         ii_rwsem;
18771 +       aufs_bindex_t           ii_btop, ii_bbot;
18772 +       __u32                   ii_higen;
18773 +       struct au_hinode        *ii_hinode;
18774 +       struct au_vdir          *ii_vdir;
18775 +};
18776 +
18777 +struct au_icntnr {
18778 +       struct au_iinfo         iinfo;
18779 +       struct inode            vfs_inode;
18780 +       struct hlist_bl_node    plink;
18781 +       struct rcu_head         rcu;
18782 +} ____cacheline_aligned_in_smp;
18783 +
18784 +/* au_pin flags */
18785 +#define AuPin_DI_LOCKED                1
18786 +#define AuPin_MNT_WRITE                (1 << 1)
18787 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18788 +#define au_fset_pin(flags, name) \
18789 +       do { (flags) |= AuPin_##name; } while (0)
18790 +#define au_fclr_pin(flags, name) \
18791 +       do { (flags) &= ~AuPin_##name; } while (0)
18792 +
18793 +struct au_pin {
18794 +       /* input */
18795 +       struct dentry *dentry;
18796 +       unsigned int udba;
18797 +       unsigned char lsc_di, lsc_hi, flags;
18798 +       aufs_bindex_t bindex;
18799 +
18800 +       /* output */
18801 +       struct dentry *parent;
18802 +       struct au_hinode *hdir;
18803 +       struct vfsmount *h_mnt;
18804 +
18805 +       /* temporary unlock/relock for copyup */
18806 +       struct dentry *h_dentry, *h_parent;
18807 +       struct au_branch *br;
18808 +       struct task_struct *task;
18809 +};
18810 +
18811 +void au_pin_hdir_unlock(struct au_pin *p);
18812 +int au_pin_hdir_lock(struct au_pin *p);
18813 +int au_pin_hdir_relock(struct au_pin *p);
18814 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18815 +void au_pin_hdir_release(struct au_pin *p);
18816 +
18817 +/* ---------------------------------------------------------------------- */
18818 +
18819 +static inline struct au_iinfo *au_ii(struct inode *inode)
18820 +{
18821 +       BUG_ON(is_bad_inode(inode));
18822 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18823 +}
18824 +
18825 +/* ---------------------------------------------------------------------- */
18826 +
18827 +/* inode.c */
18828 +struct inode *au_igrab(struct inode *inode);
18829 +void au_refresh_iop(struct inode *inode, int force_getattr);
18830 +int au_refresh_hinode_self(struct inode *inode);
18831 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18832 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18833 +          unsigned int d_type, ino_t *ino);
18834 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18835 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18836 +              struct inode *inode);
18837 +int au_test_h_perm(struct inode *h_inode, int mask);
18838 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18839 +
18840 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18841 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18842 +{
18843 +#ifdef CONFIG_AUFS_SHWH
18844 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18845 +#else
18846 +       return 0;
18847 +#endif
18848 +}
18849 +
18850 +/* i_op.c */
18851 +enum {
18852 +       AuIop_SYMLINK,
18853 +       AuIop_DIR,
18854 +       AuIop_OTHER,
18855 +       AuIop_Last
18856 +};
18857 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18858 +       aufs_iop_nogetattr[AuIop_Last];
18859 +
18860 +/* au_wr_dir flags */
18861 +#define AuWrDir_ADD_ENTRY      1
18862 +#define AuWrDir_ISDIR          (1 << 1)
18863 +#define AuWrDir_TMPFILE                (1 << 2)
18864 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18865 +#define au_fset_wrdir(flags, name) \
18866 +       do { (flags) |= AuWrDir_##name; } while (0)
18867 +#define au_fclr_wrdir(flags, name) \
18868 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18869 +
18870 +struct au_wr_dir_args {
18871 +       aufs_bindex_t force_btgt;
18872 +       unsigned char flags;
18873 +};
18874 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18875 +             struct au_wr_dir_args *args);
18876 +
18877 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18878 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18879 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18880 +                unsigned int udba, unsigned char flags);
18881 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18882 +          unsigned int udba, unsigned char flags) __must_check;
18883 +int au_do_pin(struct au_pin *pin) __must_check;
18884 +void au_unpin(struct au_pin *pin);
18885 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18886 +
18887 +#define AuIcpup_DID_CPUP       1
18888 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18889 +#define au_fset_icpup(flags, name) \
18890 +       do { (flags) |= AuIcpup_##name; } while (0)
18891 +#define au_fclr_icpup(flags, name) \
18892 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18893 +
18894 +struct au_icpup_args {
18895 +       unsigned char flags;
18896 +       unsigned char pin_flags;
18897 +       aufs_bindex_t btgt;
18898 +       unsigned int udba;
18899 +       struct au_pin pin;
18900 +       struct path h_path;
18901 +       struct inode *h_inode;
18902 +};
18903 +
18904 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18905 +                    struct au_icpup_args *a);
18906 +
18907 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18908 +                     struct path *h_path, int locked);
18909 +
18910 +/* i_op_add.c */
18911 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18912 +              struct dentry *h_parent, int isdir);
18913 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18914 +              dev_t dev);
18915 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18916 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18917 +               bool want_excl);
18918 +struct vfsub_aopen_args;
18919 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18920 +                      struct vfsub_aopen_args *args);
18921 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18922 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18923 +             struct dentry *dentry);
18924 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18925 +
18926 +/* i_op_del.c */
18927 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18928 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18929 +              struct dentry *h_parent, int isdir);
18930 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18931 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18932 +
18933 +/* i_op_ren.c */
18934 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18935 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18936 +               struct inode *dir, struct dentry *dentry,
18937 +               unsigned int flags);
18938 +
18939 +/* iinfo.c */
18940 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18941 +void au_hiput(struct au_hinode *hinode);
18942 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18943 +                 struct dentry *h_wh);
18944 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18945 +
18946 +/* hinode flags */
18947 +#define AuHi_XINO      1
18948 +#define AuHi_HNOTIFY   (1 << 1)
18949 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18950 +#define au_fset_hi(flags, name) \
18951 +       do { (flags) |= AuHi_##name; } while (0)
18952 +#define au_fclr_hi(flags, name) \
18953 +       do { (flags) &= ~AuHi_##name; } while (0)
18954 +
18955 +#ifndef CONFIG_AUFS_HNOTIFY
18956 +#undef AuHi_HNOTIFY
18957 +#define AuHi_HNOTIFY   0
18958 +#endif
18959 +
18960 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18961 +                  struct inode *h_inode, unsigned int flags);
18962 +
18963 +void au_update_iigen(struct inode *inode, int half);
18964 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18965 +
18966 +void au_icntnr_init_once(void *_c);
18967 +void au_hinode_init(struct au_hinode *hinode);
18968 +int au_iinfo_init(struct inode *inode);
18969 +void au_iinfo_fin(struct inode *inode);
18970 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18971 +
18972 +#ifdef CONFIG_PROC_FS
18973 +/* plink.c */
18974 +int au_plink_maint(struct super_block *sb, int flags);
18975 +struct au_sbinfo;
18976 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18977 +int au_plink_maint_enter(struct super_block *sb);
18978 +#ifdef CONFIG_AUFS_DEBUG
18979 +void au_plink_list(struct super_block *sb);
18980 +#else
18981 +AuStubVoid(au_plink_list, struct super_block *sb)
18982 +#endif
18983 +int au_plink_test(struct inode *inode);
18984 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18985 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18986 +                    struct dentry *h_dentry);
18987 +void au_plink_put(struct super_block *sb, int verbose);
18988 +void au_plink_clean(struct super_block *sb, int verbose);
18989 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18990 +#else
18991 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18992 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18993 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18994 +AuStubVoid(au_plink_list, struct super_block *sb);
18995 +AuStubInt0(au_plink_test, struct inode *inode);
18996 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18997 +       struct inode *inode, aufs_bindex_t bindex);
18998 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18999 +          struct dentry *h_dentry);
19000 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19001 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19002 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19003 +#endif /* CONFIG_PROC_FS */
19004 +
19005 +#ifdef CONFIG_AUFS_XATTR
19006 +/* xattr.c */
19007 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19008 +                 unsigned int verbose);
19009 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
19010 +void au_xattr_init(struct super_block *sb);
19011 +#else
19012 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
19013 +          int ignore_flags, unsigned int verbose);
19014 +AuStubVoid(au_xattr_init, struct super_block *sb);
19015 +#endif
19016 +
19017 +#ifdef CONFIG_FS_POSIX_ACL
19018 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19019 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19020 +#endif
19021 +
19022 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19023 +enum {
19024 +       AU_XATTR_SET,
19025 +       AU_ACL_SET
19026 +};
19027 +
19028 +struct au_sxattr {
19029 +       int type;
19030 +       union {
19031 +               struct {
19032 +                       const char      *name;
19033 +                       const void      *value;
19034 +                       size_t          size;
19035 +                       int             flags;
19036 +               } set;
19037 +               struct {
19038 +                       struct posix_acl *acl;
19039 +                       int             type;
19040 +               } acl_set;
19041 +       } u;
19042 +};
19043 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19044 +                 struct au_sxattr *arg);
19045 +#endif
19046 +
19047 +/* ---------------------------------------------------------------------- */
19048 +
19049 +/* lock subclass for iinfo */
19050 +enum {
19051 +       AuLsc_II_CHILD,         /* child first */
19052 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
19053 +       AuLsc_II_CHILD3,        /* copyup dirs */
19054 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
19055 +       AuLsc_II_PARENT2,
19056 +       AuLsc_II_PARENT3,       /* copyup dirs */
19057 +       AuLsc_II_NEW_CHILD
19058 +};
19059 +
19060 +/*
19061 + * ii_read_lock_child, ii_write_lock_child,
19062 + * ii_read_lock_child2, ii_write_lock_child2,
19063 + * ii_read_lock_child3, ii_write_lock_child3,
19064 + * ii_read_lock_parent, ii_write_lock_parent,
19065 + * ii_read_lock_parent2, ii_write_lock_parent2,
19066 + * ii_read_lock_parent3, ii_write_lock_parent3,
19067 + * ii_read_lock_new_child, ii_write_lock_new_child,
19068 + */
19069 +#define AuReadLockFunc(name, lsc) \
19070 +static inline void ii_read_lock_##name(struct inode *i) \
19071 +{ \
19072 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19073 +}
19074 +
19075 +#define AuWriteLockFunc(name, lsc) \
19076 +static inline void ii_write_lock_##name(struct inode *i) \
19077 +{ \
19078 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19079 +}
19080 +
19081 +#define AuRWLockFuncs(name, lsc) \
19082 +       AuReadLockFunc(name, lsc) \
19083 +       AuWriteLockFunc(name, lsc)
19084 +
19085 +AuRWLockFuncs(child, CHILD);
19086 +AuRWLockFuncs(child2, CHILD2);
19087 +AuRWLockFuncs(child3, CHILD3);
19088 +AuRWLockFuncs(parent, PARENT);
19089 +AuRWLockFuncs(parent2, PARENT2);
19090 +AuRWLockFuncs(parent3, PARENT3);
19091 +AuRWLockFuncs(new_child, NEW_CHILD);
19092 +
19093 +#undef AuReadLockFunc
19094 +#undef AuWriteLockFunc
19095 +#undef AuRWLockFuncs
19096 +
19097 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19098 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19099 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19100 +
19101 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19102 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19103 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19104 +
19105 +/* ---------------------------------------------------------------------- */
19106 +
19107 +static inline void au_icntnr_init(struct au_icntnr *c)
19108 +{
19109 +#ifdef CONFIG_AUFS_DEBUG
19110 +       c->vfs_inode.i_mode = 0;
19111 +#endif
19112 +}
19113 +
19114 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19115 +{
19116 +       unsigned int gen;
19117 +       struct au_iinfo *iinfo;
19118 +       struct au_iigen *iigen;
19119 +
19120 +       iinfo = au_ii(inode);
19121 +       iigen = &iinfo->ii_generation;
19122 +       spin_lock(&iigen->ig_spin);
19123 +       if (igflags)
19124 +               *igflags = iigen->ig_flags;
19125 +       gen = iigen->ig_generation;
19126 +       spin_unlock(&iigen->ig_spin);
19127 +
19128 +       return gen;
19129 +}
19130 +
19131 +/* tiny test for inode number */
19132 +/* tmpfs generation is too rough */
19133 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19134 +{
19135 +       struct au_iinfo *iinfo;
19136 +
19137 +       iinfo = au_ii(inode);
19138 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19139 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19140 +                && iinfo->ii_higen == h_inode->i_generation);
19141 +}
19142 +
19143 +static inline void au_iigen_dec(struct inode *inode)
19144 +{
19145 +       struct au_iinfo *iinfo;
19146 +       struct au_iigen *iigen;
19147 +
19148 +       iinfo = au_ii(inode);
19149 +       iigen = &iinfo->ii_generation;
19150 +       spin_lock(&iigen->ig_spin);
19151 +       iigen->ig_generation--;
19152 +       spin_unlock(&iigen->ig_spin);
19153 +}
19154 +
19155 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19156 +{
19157 +       int err;
19158 +
19159 +       err = 0;
19160 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19161 +               err = -EIO;
19162 +
19163 +       return err;
19164 +}
19165 +
19166 +/* ---------------------------------------------------------------------- */
19167 +
19168 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19169 +                                         aufs_bindex_t bindex)
19170 +{
19171 +       return iinfo->ii_hinode + bindex;
19172 +}
19173 +
19174 +static inline int au_is_bad_inode(struct inode *inode)
19175 +{
19176 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19177 +}
19178 +
19179 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19180 +                                       aufs_bindex_t bindex)
19181 +{
19182 +       IiMustAnyLock(inode);
19183 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19184 +}
19185 +
19186 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19187 +{
19188 +       IiMustAnyLock(inode);
19189 +       return au_ii(inode)->ii_btop;
19190 +}
19191 +
19192 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19193 +{
19194 +       IiMustAnyLock(inode);
19195 +       return au_ii(inode)->ii_bbot;
19196 +}
19197 +
19198 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19199 +{
19200 +       IiMustAnyLock(inode);
19201 +       return au_ii(inode)->ii_vdir;
19202 +}
19203 +
19204 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19205 +{
19206 +       IiMustAnyLock(inode);
19207 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19208 +}
19209 +
19210 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19211 +{
19212 +       IiMustWriteLock(inode);
19213 +       au_ii(inode)->ii_btop = bindex;
19214 +}
19215 +
19216 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19217 +{
19218 +       IiMustWriteLock(inode);
19219 +       au_ii(inode)->ii_bbot = bindex;
19220 +}
19221 +
19222 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19223 +{
19224 +       IiMustWriteLock(inode);
19225 +       au_ii(inode)->ii_vdir = vdir;
19226 +}
19227 +
19228 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19229 +{
19230 +       IiMustAnyLock(inode);
19231 +       return au_hinode(au_ii(inode), bindex);
19232 +}
19233 +
19234 +/* ---------------------------------------------------------------------- */
19235 +
19236 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19237 +{
19238 +       if (pin)
19239 +               return pin->parent;
19240 +       return NULL;
19241 +}
19242 +
19243 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19244 +{
19245 +       if (pin && pin->hdir)
19246 +               return pin->hdir->hi_inode;
19247 +       return NULL;
19248 +}
19249 +
19250 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19251 +{
19252 +       if (pin)
19253 +               return pin->hdir;
19254 +       return NULL;
19255 +}
19256 +
19257 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19258 +{
19259 +       if (pin)
19260 +               pin->dentry = dentry;
19261 +}
19262 +
19263 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19264 +                                          unsigned char lflag)
19265 +{
19266 +       if (pin) {
19267 +               if (lflag)
19268 +                       au_fset_pin(pin->flags, DI_LOCKED);
19269 +               else
19270 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19271 +       }
19272 +}
19273 +
19274 +#if 0 /* reserved */
19275 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19276 +{
19277 +       if (pin) {
19278 +               dput(pin->parent);
19279 +               pin->parent = dget(parent);
19280 +       }
19281 +}
19282 +#endif
19283 +
19284 +/* ---------------------------------------------------------------------- */
19285 +
19286 +struct au_branch;
19287 +#ifdef CONFIG_AUFS_HNOTIFY
19288 +struct au_hnotify_op {
19289 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19290 +       int (*alloc)(struct au_hinode *hinode);
19291 +
19292 +       /*
19293 +        * if it returns true, the the caller should free hinode->hi_notify,
19294 +        * otherwise ->free() frees it.
19295 +        */
19296 +       int (*free)(struct au_hinode *hinode,
19297 +                   struct au_hnotify *hn) __must_check;
19298 +
19299 +       void (*fin)(void);
19300 +       int (*init)(void);
19301 +
19302 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19303 +       void (*fin_br)(struct au_branch *br);
19304 +       int (*init_br)(struct au_branch *br, int perm);
19305 +};
19306 +
19307 +/* hnotify.c */
19308 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19309 +void au_hn_free(struct au_hinode *hinode);
19310 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19311 +void au_hn_reset(struct inode *inode, unsigned int flags);
19312 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19313 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19314 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19315 +int au_hnotify_init_br(struct au_branch *br, int perm);
19316 +void au_hnotify_fin_br(struct au_branch *br);
19317 +int __init au_hnotify_init(void);
19318 +void au_hnotify_fin(void);
19319 +
19320 +/* hfsnotify.c */
19321 +extern const struct au_hnotify_op au_hnotify_op;
19322 +
19323 +static inline
19324 +void au_hn_init(struct au_hinode *hinode)
19325 +{
19326 +       hinode->hi_notify = NULL;
19327 +}
19328 +
19329 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19330 +{
19331 +       return hinode->hi_notify;
19332 +}
19333 +
19334 +#else
19335 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19336 +       struct au_hinode *hinode __maybe_unused,
19337 +       struct inode *inode __maybe_unused)
19338 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19339 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19340 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19341 +          int do_set __maybe_unused)
19342 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19343 +          unsigned int flags __maybe_unused)
19344 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19345 +          struct au_branch *br __maybe_unused,
19346 +          int perm __maybe_unused)
19347 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19348 +          int perm __maybe_unused)
19349 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19350 +AuStubInt0(__init au_hnotify_init, void)
19351 +AuStubVoid(au_hnotify_fin, void)
19352 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19353 +#endif /* CONFIG_AUFS_HNOTIFY */
19354 +
19355 +static inline void au_hn_suspend(struct au_hinode *hdir)
19356 +{
19357 +       au_hn_ctl(hdir, /*do_set*/0);
19358 +}
19359 +
19360 +static inline void au_hn_resume(struct au_hinode *hdir)
19361 +{
19362 +       au_hn_ctl(hdir, /*do_set*/1);
19363 +}
19364 +
19365 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19366 +{
19367 +       inode_lock(hdir->hi_inode);
19368 +       au_hn_suspend(hdir);
19369 +}
19370 +
19371 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19372 +                                         unsigned int sc __maybe_unused)
19373 +{
19374 +       inode_lock_nested(hdir->hi_inode, sc);
19375 +       au_hn_suspend(hdir);
19376 +}
19377 +
19378 +#if 0 /* unused */
19379 +#include "vfsub.h"
19380 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19381 +                                                 unsigned int sc)
19382 +{
19383 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19384 +       au_hn_suspend(hdir);
19385 +}
19386 +#endif
19387 +
19388 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19389 +{
19390 +       au_hn_resume(hdir);
19391 +       inode_unlock(hdir->hi_inode);
19392 +}
19393 +
19394 +#endif /* __KERNEL__ */
19395 +#endif /* __AUFS_INODE_H__ */
19396 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19397 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19398 +++ linux/fs/aufs/ioctl.c       2020-01-27 10:57:18.175538316 +0100
19399 @@ -0,0 +1,220 @@
19400 +// SPDX-License-Identifier: GPL-2.0
19401 +/*
19402 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19403 + *
19404 + * This program, aufs is free software; you can redistribute it and/or modify
19405 + * it under the terms of the GNU General Public License as published by
19406 + * the Free Software Foundation; either version 2 of the License, or
19407 + * (at your option) any later version.
19408 + *
19409 + * This program is distributed in the hope that it will be useful,
19410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19412 + * GNU General Public License for more details.
19413 + *
19414 + * You should have received a copy of the GNU General Public License
19415 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19416 + */
19417 +
19418 +/*
19419 + * ioctl
19420 + * plink-management and readdir in userspace.
19421 + * assist the pathconf(3) wrapper library.
19422 + * move-down
19423 + * File-based Hierarchical Storage Management.
19424 + */
19425 +
19426 +#include <linux/compat.h>
19427 +#include <linux/file.h>
19428 +#include "aufs.h"
19429 +
19430 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19431 +{
19432 +       int err, fd;
19433 +       aufs_bindex_t wbi, bindex, bbot;
19434 +       struct file *h_file;
19435 +       struct super_block *sb;
19436 +       struct dentry *root;
19437 +       struct au_branch *br;
19438 +       struct aufs_wbr_fd wbrfd = {
19439 +               .oflags = au_dir_roflags,
19440 +               .brid   = -1
19441 +       };
19442 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19443 +               | O_NOATIME | O_CLOEXEC;
19444 +
19445 +       AuDebugOn(wbrfd.oflags & ~valid);
19446 +
19447 +       if (arg) {
19448 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19449 +               if (unlikely(err)) {
19450 +                       err = -EFAULT;
19451 +                       goto out;
19452 +               }
19453 +
19454 +               err = -EINVAL;
19455 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19456 +               wbrfd.oflags |= au_dir_roflags;
19457 +               AuDbg("0%o\n", wbrfd.oflags);
19458 +               if (unlikely(wbrfd.oflags & ~valid))
19459 +                       goto out;
19460 +       }
19461 +
19462 +       fd = get_unused_fd_flags(0);
19463 +       err = fd;
19464 +       if (unlikely(fd < 0))
19465 +               goto out;
19466 +
19467 +       h_file = ERR_PTR(-EINVAL);
19468 +       wbi = 0;
19469 +       br = NULL;
19470 +       sb = path->dentry->d_sb;
19471 +       root = sb->s_root;
19472 +       aufs_read_lock(root, AuLock_IR);
19473 +       bbot = au_sbbot(sb);
19474 +       if (wbrfd.brid >= 0) {
19475 +               wbi = au_br_index(sb, wbrfd.brid);
19476 +               if (unlikely(wbi < 0 || wbi > bbot))
19477 +                       goto out_unlock;
19478 +       }
19479 +
19480 +       h_file = ERR_PTR(-ENOENT);
19481 +       br = au_sbr(sb, wbi);
19482 +       if (!au_br_writable(br->br_perm)) {
19483 +               if (arg)
19484 +                       goto out_unlock;
19485 +
19486 +               bindex = wbi + 1;
19487 +               wbi = -1;
19488 +               for (; bindex <= bbot; bindex++) {
19489 +                       br = au_sbr(sb, bindex);
19490 +                       if (au_br_writable(br->br_perm)) {
19491 +                               wbi = bindex;
19492 +                               br = au_sbr(sb, wbi);
19493 +                               break;
19494 +                       }
19495 +               }
19496 +       }
19497 +       AuDbg("wbi %d\n", wbi);
19498 +       if (wbi >= 0)
19499 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19500 +                                  /*force_wr*/0);
19501 +
19502 +out_unlock:
19503 +       aufs_read_unlock(root, AuLock_IR);
19504 +       err = PTR_ERR(h_file);
19505 +       if (IS_ERR(h_file))
19506 +               goto out_fd;
19507 +
19508 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19509 +       fd_install(fd, h_file);
19510 +       err = fd;
19511 +       goto out; /* success */
19512 +
19513 +out_fd:
19514 +       put_unused_fd(fd);
19515 +out:
19516 +       AuTraceErr(err);
19517 +       return err;
19518 +}
19519 +
19520 +/* ---------------------------------------------------------------------- */
19521 +
19522 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19523 +{
19524 +       long err;
19525 +       struct dentry *dentry;
19526 +
19527 +       switch (cmd) {
19528 +       case AUFS_CTL_RDU:
19529 +       case AUFS_CTL_RDU_INO:
19530 +               err = au_rdu_ioctl(file, cmd, arg);
19531 +               break;
19532 +
19533 +       case AUFS_CTL_WBR_FD:
19534 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19535 +               break;
19536 +
19537 +       case AUFS_CTL_IBUSY:
19538 +               err = au_ibusy_ioctl(file, arg);
19539 +               break;
19540 +
19541 +       case AUFS_CTL_BRINFO:
19542 +               err = au_brinfo_ioctl(file, arg);
19543 +               break;
19544 +
19545 +       case AUFS_CTL_FHSM_FD:
19546 +               dentry = file->f_path.dentry;
19547 +               if (IS_ROOT(dentry))
19548 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19549 +               else
19550 +                       err = -ENOTTY;
19551 +               break;
19552 +
19553 +       default:
19554 +               /* do not call the lower */
19555 +               AuDbg("0x%x\n", cmd);
19556 +               err = -ENOTTY;
19557 +       }
19558 +
19559 +       AuTraceErr(err);
19560 +       return err;
19561 +}
19562 +
19563 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19564 +{
19565 +       long err;
19566 +
19567 +       switch (cmd) {
19568 +       case AUFS_CTL_MVDOWN:
19569 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19570 +               break;
19571 +
19572 +       case AUFS_CTL_WBR_FD:
19573 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19574 +               break;
19575 +
19576 +       default:
19577 +               /* do not call the lower */
19578 +               AuDbg("0x%x\n", cmd);
19579 +               err = -ENOTTY;
19580 +       }
19581 +
19582 +       AuTraceErr(err);
19583 +       return err;
19584 +}
19585 +
19586 +#ifdef CONFIG_COMPAT
19587 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19588 +                          unsigned long arg)
19589 +{
19590 +       long err;
19591 +
19592 +       switch (cmd) {
19593 +       case AUFS_CTL_RDU:
19594 +       case AUFS_CTL_RDU_INO:
19595 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19596 +               break;
19597 +
19598 +       case AUFS_CTL_IBUSY:
19599 +               err = au_ibusy_compat_ioctl(file, arg);
19600 +               break;
19601 +
19602 +       case AUFS_CTL_BRINFO:
19603 +               err = au_brinfo_compat_ioctl(file, arg);
19604 +               break;
19605 +
19606 +       default:
19607 +               err = aufs_ioctl_dir(file, cmd, arg);
19608 +       }
19609 +
19610 +       AuTraceErr(err);
19611 +       return err;
19612 +}
19613 +
19614 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19615 +                             unsigned long arg)
19616 +{
19617 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19618 +}
19619 +#endif
19620 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19621 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19622 +++ linux/fs/aufs/i_op_add.c    2020-01-27 10:57:18.172204883 +0100
19623 @@ -0,0 +1,936 @@
19624 +// SPDX-License-Identifier: GPL-2.0
19625 +/*
19626 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19627 + *
19628 + * This program, aufs is free software; you can redistribute it and/or modify
19629 + * it under the terms of the GNU General Public License as published by
19630 + * the Free Software Foundation; either version 2 of the License, or
19631 + * (at your option) any later version.
19632 + *
19633 + * This program is distributed in the hope that it will be useful,
19634 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19635 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19636 + * GNU General Public License for more details.
19637 + *
19638 + * You should have received a copy of the GNU General Public License
19639 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19640 + */
19641 +
19642 +/*
19643 + * inode operations (add entry)
19644 + */
19645 +
19646 +#include <linux/iversion.h>
19647 +#include "aufs.h"
19648 +
19649 +/*
19650 + * final procedure of adding a new entry, except link(2).
19651 + * remove whiteout, instantiate, copyup the parent dir's times and size
19652 + * and update version.
19653 + * if it failed, re-create the removed whiteout.
19654 + */
19655 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19656 +                 struct dentry *wh_dentry, struct dentry *dentry)
19657 +{
19658 +       int err, rerr;
19659 +       aufs_bindex_t bwh;
19660 +       struct path h_path;
19661 +       struct super_block *sb;
19662 +       struct inode *inode, *h_dir;
19663 +       struct dentry *wh;
19664 +
19665 +       bwh = -1;
19666 +       sb = dir->i_sb;
19667 +       if (wh_dentry) {
19668 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19669 +               IMustLock(h_dir);
19670 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19671 +               bwh = au_dbwh(dentry);
19672 +               h_path.dentry = wh_dentry;
19673 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19674 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19675 +                                         dentry);
19676 +               if (unlikely(err))
19677 +                       goto out;
19678 +       }
19679 +
19680 +       inode = au_new_inode(dentry, /*must_new*/1);
19681 +       if (!IS_ERR(inode)) {
19682 +               d_instantiate(dentry, inode);
19683 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19684 +               IMustLock(dir);
19685 +               au_dir_ts(dir, bindex);
19686 +               inode_inc_iversion(dir);
19687 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19688 +               return 0; /* success */
19689 +       }
19690 +
19691 +       err = PTR_ERR(inode);
19692 +       if (!wh_dentry)
19693 +               goto out;
19694 +
19695 +       /* revert */
19696 +       /* dir inode is locked */
19697 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19698 +       rerr = PTR_ERR(wh);
19699 +       if (IS_ERR(wh)) {
19700 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19701 +                       dentry, err, rerr);
19702 +               err = -EIO;
19703 +       } else
19704 +               dput(wh);
19705 +
19706 +out:
19707 +       return err;
19708 +}
19709 +
19710 +static int au_d_may_add(struct dentry *dentry)
19711 +{
19712 +       int err;
19713 +
19714 +       err = 0;
19715 +       if (unlikely(d_unhashed(dentry)))
19716 +               err = -ENOENT;
19717 +       if (unlikely(d_really_is_positive(dentry)))
19718 +               err = -EEXIST;
19719 +       return err;
19720 +}
19721 +
19722 +/*
19723 + * simple tests for the adding inode operations.
19724 + * following the checks in vfs, plus the parent-child relationship.
19725 + */
19726 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19727 +              struct dentry *h_parent, int isdir)
19728 +{
19729 +       int err;
19730 +       umode_t h_mode;
19731 +       struct dentry *h_dentry;
19732 +       struct inode *h_inode;
19733 +
19734 +       err = -ENAMETOOLONG;
19735 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19736 +               goto out;
19737 +
19738 +       h_dentry = au_h_dptr(dentry, bindex);
19739 +       if (d_really_is_negative(dentry)) {
19740 +               err = -EEXIST;
19741 +               if (unlikely(d_is_positive(h_dentry)))
19742 +                       goto out;
19743 +       } else {
19744 +               /* rename(2) case */
19745 +               err = -EIO;
19746 +               if (unlikely(d_is_negative(h_dentry)))
19747 +                       goto out;
19748 +               h_inode = d_inode(h_dentry);
19749 +               if (unlikely(!h_inode->i_nlink))
19750 +                       goto out;
19751 +
19752 +               h_mode = h_inode->i_mode;
19753 +               if (!isdir) {
19754 +                       err = -EISDIR;
19755 +                       if (unlikely(S_ISDIR(h_mode)))
19756 +                               goto out;
19757 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19758 +                       err = -ENOTDIR;
19759 +                       goto out;
19760 +               }
19761 +       }
19762 +
19763 +       err = 0;
19764 +       /* expected parent dir is locked */
19765 +       if (unlikely(h_parent != h_dentry->d_parent))
19766 +               err = -EIO;
19767 +
19768 +out:
19769 +       AuTraceErr(err);
19770 +       return err;
19771 +}
19772 +
19773 +/*
19774 + * initial procedure of adding a new entry.
19775 + * prepare writable branch and the parent dir, lock it,
19776 + * and lookup whiteout for the new entry.
19777 + */
19778 +static struct dentry*
19779 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19780 +                 struct dentry *src_dentry, struct au_pin *pin,
19781 +                 struct au_wr_dir_args *wr_dir_args)
19782 +{
19783 +       struct dentry *wh_dentry, *h_parent;
19784 +       struct super_block *sb;
19785 +       struct au_branch *br;
19786 +       int err;
19787 +       unsigned int udba;
19788 +       aufs_bindex_t bcpup;
19789 +
19790 +       AuDbg("%pd\n", dentry);
19791 +
19792 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19793 +       bcpup = err;
19794 +       wh_dentry = ERR_PTR(err);
19795 +       if (unlikely(err < 0))
19796 +               goto out;
19797 +
19798 +       sb = dentry->d_sb;
19799 +       udba = au_opt_udba(sb);
19800 +       err = au_pin(pin, dentry, bcpup, udba,
19801 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19802 +       wh_dentry = ERR_PTR(err);
19803 +       if (unlikely(err))
19804 +               goto out;
19805 +
19806 +       h_parent = au_pinned_h_parent(pin);
19807 +       if (udba != AuOpt_UDBA_NONE
19808 +           && au_dbtop(dentry) == bcpup)
19809 +               err = au_may_add(dentry, bcpup, h_parent,
19810 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19811 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19812 +               err = -ENAMETOOLONG;
19813 +       wh_dentry = ERR_PTR(err);
19814 +       if (unlikely(err))
19815 +               goto out_unpin;
19816 +
19817 +       br = au_sbr(sb, bcpup);
19818 +       if (dt) {
19819 +               struct path tmp = {
19820 +                       .dentry = h_parent,
19821 +                       .mnt    = au_br_mnt(br)
19822 +               };
19823 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19824 +       }
19825 +
19826 +       wh_dentry = NULL;
19827 +       if (bcpup != au_dbwh(dentry))
19828 +               goto out; /* success */
19829 +
19830 +       /*
19831 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19832 +        * would not be able to removed in the future. So we don't allow such
19833 +        * name here and we don't handle ENAMETOOLONG differently here.
19834 +        */
19835 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19836 +
19837 +out_unpin:
19838 +       if (IS_ERR(wh_dentry))
19839 +               au_unpin(pin);
19840 +out:
19841 +       return wh_dentry;
19842 +}
19843 +
19844 +/* ---------------------------------------------------------------------- */
19845 +
19846 +enum { Mknod, Symlink, Creat };
19847 +struct simple_arg {
19848 +       int type;
19849 +       union {
19850 +               struct {
19851 +                       umode_t                 mode;
19852 +                       bool                    want_excl;
19853 +                       bool                    try_aopen;
19854 +                       struct vfsub_aopen_args *aopen;
19855 +               } c;
19856 +               struct {
19857 +                       const char *symname;
19858 +               } s;
19859 +               struct {
19860 +                       umode_t mode;
19861 +                       dev_t dev;
19862 +               } m;
19863 +       } u;
19864 +};
19865 +
19866 +static int add_simple(struct inode *dir, struct dentry *dentry,
19867 +                     struct simple_arg *arg)
19868 +{
19869 +       int err, rerr;
19870 +       aufs_bindex_t btop;
19871 +       unsigned char created;
19872 +       const unsigned char try_aopen
19873 +               = (arg->type == Creat && arg->u.c.try_aopen);
19874 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19875 +       struct dentry *wh_dentry, *parent;
19876 +       struct inode *h_dir;
19877 +       struct super_block *sb;
19878 +       struct au_branch *br;
19879 +       /* to reduce stack size */
19880 +       struct {
19881 +               struct au_dtime dt;
19882 +               struct au_pin pin;
19883 +               struct path h_path;
19884 +               struct au_wr_dir_args wr_dir_args;
19885 +       } *a;
19886 +
19887 +       AuDbg("%pd\n", dentry);
19888 +       IMustLock(dir);
19889 +
19890 +       err = -ENOMEM;
19891 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19892 +       if (unlikely(!a))
19893 +               goto out;
19894 +       a->wr_dir_args.force_btgt = -1;
19895 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19896 +
19897 +       parent = dentry->d_parent; /* dir inode is locked */
19898 +       if (!try_aopen) {
19899 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19900 +               if (unlikely(err))
19901 +                       goto out_free;
19902 +       }
19903 +       err = au_d_may_add(dentry);
19904 +       if (unlikely(err))
19905 +               goto out_unlock;
19906 +       if (!try_aopen)
19907 +               di_write_lock_parent(parent);
19908 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19909 +                                     &a->pin, &a->wr_dir_args);
19910 +       err = PTR_ERR(wh_dentry);
19911 +       if (IS_ERR(wh_dentry))
19912 +               goto out_parent;
19913 +
19914 +       btop = au_dbtop(dentry);
19915 +       sb = dentry->d_sb;
19916 +       br = au_sbr(sb, btop);
19917 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19918 +       a->h_path.mnt = au_br_mnt(br);
19919 +       h_dir = au_pinned_h_dir(&a->pin);
19920 +       switch (arg->type) {
19921 +       case Creat:
19922 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19923 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19924 +                                          arg->u.c.want_excl);
19925 +                       created = !err;
19926 +                       if (!err && try_aopen)
19927 +                               aopen->file->f_mode |= FMODE_CREATED;
19928 +               } else {
19929 +                       aopen->br = br;
19930 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19931 +                       AuDbg("err %d\n", err);
19932 +                       AuDbgFile(aopen->file);
19933 +                       created = err >= 0
19934 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19935 +               }
19936 +               break;
19937 +       case Symlink:
19938 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19939 +               created = !err;
19940 +               break;
19941 +       case Mknod:
19942 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19943 +                                 arg->u.m.dev);
19944 +               created = !err;
19945 +               break;
19946 +       default:
19947 +               BUG();
19948 +       }
19949 +       if (unlikely(err < 0))
19950 +               goto out_unpin;
19951 +
19952 +       err = epilog(dir, btop, wh_dentry, dentry);
19953 +       if (!err)
19954 +               goto out_unpin; /* success */
19955 +
19956 +       /* revert */
19957 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19958 +               /* no delegation since it is just created */
19959 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19960 +                                   /*force*/0);
19961 +               if (rerr) {
19962 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19963 +                               dentry, err, rerr);
19964 +                       err = -EIO;
19965 +               }
19966 +               au_dtime_revert(&a->dt);
19967 +       }
19968 +       if (try_aopen && h_dir->i_op->atomic_open
19969 +           && (aopen->file->f_mode & FMODE_OPENED))
19970 +               /* aopen->file is still opened */
19971 +               au_lcnt_dec(&aopen->br->br_nfiles);
19972 +
19973 +out_unpin:
19974 +       au_unpin(&a->pin);
19975 +       dput(wh_dentry);
19976 +out_parent:
19977 +       if (!try_aopen)
19978 +               di_write_unlock(parent);
19979 +out_unlock:
19980 +       if (unlikely(err)) {
19981 +               au_update_dbtop(dentry);
19982 +               d_drop(dentry);
19983 +       }
19984 +       if (!try_aopen)
19985 +               aufs_read_unlock(dentry, AuLock_DW);
19986 +out_free:
19987 +       au_kfree_rcu(a);
19988 +out:
19989 +       return err;
19990 +}
19991 +
19992 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19993 +              dev_t dev)
19994 +{
19995 +       struct simple_arg arg = {
19996 +               .type = Mknod,
19997 +               .u.m = {
19998 +                       .mode   = mode,
19999 +                       .dev    = dev
20000 +               }
20001 +       };
20002 +       return add_simple(dir, dentry, &arg);
20003 +}
20004 +
20005 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20006 +{
20007 +       struct simple_arg arg = {
20008 +               .type = Symlink,
20009 +               .u.s.symname = symname
20010 +       };
20011 +       return add_simple(dir, dentry, &arg);
20012 +}
20013 +
20014 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20015 +               bool want_excl)
20016 +{
20017 +       struct simple_arg arg = {
20018 +               .type = Creat,
20019 +               .u.c = {
20020 +                       .mode           = mode,
20021 +                       .want_excl      = want_excl
20022 +               }
20023 +       };
20024 +       return add_simple(dir, dentry, &arg);
20025 +}
20026 +
20027 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20028 +                      struct vfsub_aopen_args *aopen_args)
20029 +{
20030 +       struct simple_arg arg = {
20031 +               .type = Creat,
20032 +               .u.c = {
20033 +                       .mode           = aopen_args->create_mode,
20034 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
20035 +                       .try_aopen      = true,
20036 +                       .aopen          = aopen_args
20037 +               }
20038 +       };
20039 +       return add_simple(dir, dentry, &arg);
20040 +}
20041 +
20042 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20043 +{
20044 +       int err;
20045 +       aufs_bindex_t bindex;
20046 +       struct super_block *sb;
20047 +       struct dentry *parent, *h_parent, *h_dentry;
20048 +       struct inode *h_dir, *inode;
20049 +       struct vfsmount *h_mnt;
20050 +       struct au_wr_dir_args wr_dir_args = {
20051 +               .force_btgt     = -1,
20052 +               .flags          = AuWrDir_TMPFILE
20053 +       };
20054 +
20055 +       /* copy-up may happen */
20056 +       inode_lock(dir);
20057 +
20058 +       sb = dir->i_sb;
20059 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20060 +       if (unlikely(err))
20061 +               goto out;
20062 +
20063 +       err = au_di_init(dentry);
20064 +       if (unlikely(err))
20065 +               goto out_si;
20066 +
20067 +       err = -EBUSY;
20068 +       parent = d_find_any_alias(dir);
20069 +       AuDebugOn(!parent);
20070 +       di_write_lock_parent(parent);
20071 +       if (unlikely(d_inode(parent) != dir))
20072 +               goto out_parent;
20073 +
20074 +       err = au_digen_test(parent, au_sigen(sb));
20075 +       if (unlikely(err))
20076 +               goto out_parent;
20077 +
20078 +       bindex = au_dbtop(parent);
20079 +       au_set_dbtop(dentry, bindex);
20080 +       au_set_dbbot(dentry, bindex);
20081 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20082 +       bindex = err;
20083 +       if (unlikely(err < 0))
20084 +               goto out_parent;
20085 +
20086 +       err = -EOPNOTSUPP;
20087 +       h_dir = au_h_iptr(dir, bindex);
20088 +       if (unlikely(!h_dir->i_op->tmpfile))
20089 +               goto out_parent;
20090 +
20091 +       h_mnt = au_sbr_mnt(sb, bindex);
20092 +       err = vfsub_mnt_want_write(h_mnt);
20093 +       if (unlikely(err))
20094 +               goto out_parent;
20095 +
20096 +       h_parent = au_h_dptr(parent, bindex);
20097 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20098 +       if (IS_ERR(h_dentry)) {
20099 +               err = PTR_ERR(h_dentry);
20100 +               goto out_mnt;
20101 +       }
20102 +
20103 +       au_set_dbtop(dentry, bindex);
20104 +       au_set_dbbot(dentry, bindex);
20105 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20106 +       inode = au_new_inode(dentry, /*must_new*/1);
20107 +       if (IS_ERR(inode)) {
20108 +               err = PTR_ERR(inode);
20109 +               au_set_h_dptr(dentry, bindex, NULL);
20110 +               au_set_dbtop(dentry, -1);
20111 +               au_set_dbbot(dentry, -1);
20112 +       } else {
20113 +               if (!inode->i_nlink)
20114 +                       set_nlink(inode, 1);
20115 +               d_tmpfile(dentry, inode);
20116 +               au_di(dentry)->di_tmpfile = 1;
20117 +
20118 +               /* update without i_mutex */
20119 +               if (au_ibtop(dir) == au_dbtop(dentry))
20120 +                       au_cpup_attr_timesizes(dir);
20121 +       }
20122 +       dput(h_dentry);
20123 +
20124 +out_mnt:
20125 +       vfsub_mnt_drop_write(h_mnt);
20126 +out_parent:
20127 +       di_write_unlock(parent);
20128 +       dput(parent);
20129 +       di_write_unlock(dentry);
20130 +       if (unlikely(err)) {
20131 +               au_di_fin(dentry);
20132 +               dentry->d_fsdata = NULL;
20133 +       }
20134 +out_si:
20135 +       si_read_unlock(sb);
20136 +out:
20137 +       inode_unlock(dir);
20138 +       return err;
20139 +}
20140 +
20141 +/* ---------------------------------------------------------------------- */
20142 +
20143 +struct au_link_args {
20144 +       aufs_bindex_t bdst, bsrc;
20145 +       struct au_pin pin;
20146 +       struct path h_path;
20147 +       struct dentry *src_parent, *parent;
20148 +};
20149 +
20150 +static int au_cpup_before_link(struct dentry *src_dentry,
20151 +                              struct au_link_args *a)
20152 +{
20153 +       int err;
20154 +       struct dentry *h_src_dentry;
20155 +       struct au_cp_generic cpg = {
20156 +               .dentry = src_dentry,
20157 +               .bdst   = a->bdst,
20158 +               .bsrc   = a->bsrc,
20159 +               .len    = -1,
20160 +               .pin    = &a->pin,
20161 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20162 +       };
20163 +
20164 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20165 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20166 +       if (unlikely(err))
20167 +               goto out;
20168 +
20169 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20170 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20171 +                    au_opt_udba(src_dentry->d_sb),
20172 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20173 +       if (unlikely(err))
20174 +               goto out;
20175 +
20176 +       err = au_sio_cpup_simple(&cpg);
20177 +       au_unpin(&a->pin);
20178 +
20179 +out:
20180 +       di_read_unlock(a->src_parent, AuLock_IR);
20181 +       return err;
20182 +}
20183 +
20184 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20185 +                          struct au_link_args *a)
20186 +{
20187 +       int err;
20188 +       unsigned char plink;
20189 +       aufs_bindex_t bbot;
20190 +       struct dentry *h_src_dentry;
20191 +       struct inode *h_inode, *inode, *delegated;
20192 +       struct super_block *sb;
20193 +       struct file *h_file;
20194 +
20195 +       plink = 0;
20196 +       h_inode = NULL;
20197 +       sb = src_dentry->d_sb;
20198 +       inode = d_inode(src_dentry);
20199 +       if (au_ibtop(inode) <= a->bdst)
20200 +               h_inode = au_h_iptr(inode, a->bdst);
20201 +       if (!h_inode || !h_inode->i_nlink) {
20202 +               /* copyup src_dentry as the name of dentry. */
20203 +               bbot = au_dbbot(dentry);
20204 +               if (bbot < a->bsrc)
20205 +                       au_set_dbbot(dentry, a->bsrc);
20206 +               au_set_h_dptr(dentry, a->bsrc,
20207 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20208 +               dget(a->h_path.dentry);
20209 +               au_set_h_dptr(dentry, a->bdst, NULL);
20210 +               AuDbg("temporary d_inode...\n");
20211 +               spin_lock(&dentry->d_lock);
20212 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20213 +               spin_unlock(&dentry->d_lock);
20214 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20215 +               if (IS_ERR(h_file))
20216 +                       err = PTR_ERR(h_file);
20217 +               else {
20218 +                       struct au_cp_generic cpg = {
20219 +                               .dentry = dentry,
20220 +                               .bdst   = a->bdst,
20221 +                               .bsrc   = -1,
20222 +                               .len    = -1,
20223 +                               .pin    = &a->pin,
20224 +                               .flags  = AuCpup_KEEPLINO
20225 +                       };
20226 +                       err = au_sio_cpup_simple(&cpg);
20227 +                       au_h_open_post(dentry, a->bsrc, h_file);
20228 +                       if (!err) {
20229 +                               dput(a->h_path.dentry);
20230 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20231 +                       } else
20232 +                               au_set_h_dptr(dentry, a->bdst,
20233 +                                             a->h_path.dentry);
20234 +               }
20235 +               spin_lock(&dentry->d_lock);
20236 +               dentry->d_inode = NULL; /* restore */
20237 +               spin_unlock(&dentry->d_lock);
20238 +               AuDbg("temporary d_inode...done\n");
20239 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20240 +               au_set_dbbot(dentry, bbot);
20241 +       } else {
20242 +               /* the inode of src_dentry already exists on a.bdst branch */
20243 +               h_src_dentry = d_find_alias(h_inode);
20244 +               if (!h_src_dentry && au_plink_test(inode)) {
20245 +                       plink = 1;
20246 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20247 +                       err = PTR_ERR(h_src_dentry);
20248 +                       if (IS_ERR(h_src_dentry))
20249 +                               goto out;
20250 +
20251 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20252 +                               dput(h_src_dentry);
20253 +                               h_src_dentry = NULL;
20254 +                       }
20255 +
20256 +               }
20257 +               if (h_src_dentry) {
20258 +                       delegated = NULL;
20259 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20260 +                                        &a->h_path, &delegated);
20261 +                       if (unlikely(err == -EWOULDBLOCK)) {
20262 +                               pr_warn("cannot retry for NFSv4 delegation"
20263 +                                       " for an internal link\n");
20264 +                               iput(delegated);
20265 +                       }
20266 +                       dput(h_src_dentry);
20267 +               } else {
20268 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20269 +                               h_inode->i_ino, a->bdst);
20270 +                       err = -EIO;
20271 +               }
20272 +       }
20273 +
20274 +       if (!err && !plink)
20275 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20276 +
20277 +out:
20278 +       AuTraceErr(err);
20279 +       return err;
20280 +}
20281 +
20282 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20283 +             struct dentry *dentry)
20284 +{
20285 +       int err, rerr;
20286 +       struct au_dtime dt;
20287 +       struct au_link_args *a;
20288 +       struct dentry *wh_dentry, *h_src_dentry;
20289 +       struct inode *inode, *delegated;
20290 +       struct super_block *sb;
20291 +       struct au_wr_dir_args wr_dir_args = {
20292 +               /* .force_btgt  = -1, */
20293 +               .flags          = AuWrDir_ADD_ENTRY
20294 +       };
20295 +
20296 +       IMustLock(dir);
20297 +       inode = d_inode(src_dentry);
20298 +       IMustLock(inode);
20299 +
20300 +       err = -ENOMEM;
20301 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20302 +       if (unlikely(!a))
20303 +               goto out;
20304 +
20305 +       a->parent = dentry->d_parent; /* dir inode is locked */
20306 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20307 +                                       AuLock_NOPLM | AuLock_GEN);
20308 +       if (unlikely(err))
20309 +               goto out_kfree;
20310 +       err = au_d_linkable(src_dentry);
20311 +       if (unlikely(err))
20312 +               goto out_unlock;
20313 +       err = au_d_may_add(dentry);
20314 +       if (unlikely(err))
20315 +               goto out_unlock;
20316 +
20317 +       a->src_parent = dget_parent(src_dentry);
20318 +       wr_dir_args.force_btgt = au_ibtop(inode);
20319 +
20320 +       di_write_lock_parent(a->parent);
20321 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20322 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20323 +                                     &wr_dir_args);
20324 +       err = PTR_ERR(wh_dentry);
20325 +       if (IS_ERR(wh_dentry))
20326 +               goto out_parent;
20327 +
20328 +       err = 0;
20329 +       sb = dentry->d_sb;
20330 +       a->bdst = au_dbtop(dentry);
20331 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20332 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20333 +       a->bsrc = au_ibtop(inode);
20334 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20335 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20336 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20337 +       if (!h_src_dentry) {
20338 +               a->bsrc = au_dbtop(src_dentry);
20339 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20340 +               AuDebugOn(!h_src_dentry);
20341 +       } else if (IS_ERR(h_src_dentry)) {
20342 +               err = PTR_ERR(h_src_dentry);
20343 +               goto out_parent;
20344 +       }
20345 +
20346 +       /*
20347 +        * aufs doesn't touch the credential so
20348 +        * security_dentry_create_files_as() is unnecessary.
20349 +        */
20350 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20351 +               if (a->bdst < a->bsrc
20352 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20353 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20354 +               else {
20355 +                       delegated = NULL;
20356 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20357 +                                        &a->h_path, &delegated);
20358 +                       if (unlikely(err == -EWOULDBLOCK)) {
20359 +                               pr_warn("cannot retry for NFSv4 delegation"
20360 +                                       " for an internal link\n");
20361 +                               iput(delegated);
20362 +                       }
20363 +               }
20364 +               dput(h_src_dentry);
20365 +       } else {
20366 +               /*
20367 +                * copyup src_dentry to the branch we process,
20368 +                * and then link(2) to it.
20369 +                */
20370 +               dput(h_src_dentry);
20371 +               if (a->bdst < a->bsrc
20372 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20373 +                       au_unpin(&a->pin);
20374 +                       di_write_unlock(a->parent);
20375 +                       err = au_cpup_before_link(src_dentry, a);
20376 +                       di_write_lock_parent(a->parent);
20377 +                       if (!err)
20378 +                               err = au_pin(&a->pin, dentry, a->bdst,
20379 +                                            au_opt_udba(sb),
20380 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20381 +                       if (unlikely(err))
20382 +                               goto out_wh;
20383 +               }
20384 +               if (!err) {
20385 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20386 +                       err = -ENOENT;
20387 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20388 +                               delegated = NULL;
20389 +                               err = vfsub_link(h_src_dentry,
20390 +                                                au_pinned_h_dir(&a->pin),
20391 +                                                &a->h_path, &delegated);
20392 +                               if (unlikely(err == -EWOULDBLOCK)) {
20393 +                                       pr_warn("cannot retry"
20394 +                                               " for NFSv4 delegation"
20395 +                                               " for an internal link\n");
20396 +                                       iput(delegated);
20397 +                               }
20398 +                       }
20399 +               }
20400 +       }
20401 +       if (unlikely(err))
20402 +               goto out_unpin;
20403 +
20404 +       if (wh_dentry) {
20405 +               a->h_path.dentry = wh_dentry;
20406 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20407 +                                         dentry);
20408 +               if (unlikely(err))
20409 +                       goto out_revert;
20410 +       }
20411 +
20412 +       au_dir_ts(dir, a->bdst);
20413 +       inode_inc_iversion(dir);
20414 +       inc_nlink(inode);
20415 +       inode->i_ctime = dir->i_ctime;
20416 +       d_instantiate(dentry, au_igrab(inode));
20417 +       if (d_unhashed(a->h_path.dentry))
20418 +               /* some filesystem calls d_drop() */
20419 +               d_drop(dentry);
20420 +       /* some filesystems consume an inode even hardlink */
20421 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20422 +       goto out_unpin; /* success */
20423 +
20424 +out_revert:
20425 +       /* no delegation since it is just created */
20426 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20427 +                           /*delegated*/NULL, /*force*/0);
20428 +       if (unlikely(rerr)) {
20429 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20430 +               err = -EIO;
20431 +       }
20432 +       au_dtime_revert(&dt);
20433 +out_unpin:
20434 +       au_unpin(&a->pin);
20435 +out_wh:
20436 +       dput(wh_dentry);
20437 +out_parent:
20438 +       di_write_unlock(a->parent);
20439 +       dput(a->src_parent);
20440 +out_unlock:
20441 +       if (unlikely(err)) {
20442 +               au_update_dbtop(dentry);
20443 +               d_drop(dentry);
20444 +       }
20445 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20446 +out_kfree:
20447 +       au_kfree_rcu(a);
20448 +out:
20449 +       AuTraceErr(err);
20450 +       return err;
20451 +}
20452 +
20453 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20454 +{
20455 +       int err, rerr;
20456 +       aufs_bindex_t bindex;
20457 +       unsigned char diropq;
20458 +       struct path h_path;
20459 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20460 +       struct inode *h_inode;
20461 +       struct super_block *sb;
20462 +       struct {
20463 +               struct au_pin pin;
20464 +               struct au_dtime dt;
20465 +       } *a; /* reduce the stack usage */
20466 +       struct au_wr_dir_args wr_dir_args = {
20467 +               .force_btgt     = -1,
20468 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20469 +       };
20470 +
20471 +       IMustLock(dir);
20472 +
20473 +       err = -ENOMEM;
20474 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20475 +       if (unlikely(!a))
20476 +               goto out;
20477 +
20478 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20479 +       if (unlikely(err))
20480 +               goto out_free;
20481 +       err = au_d_may_add(dentry);
20482 +       if (unlikely(err))
20483 +               goto out_unlock;
20484 +
20485 +       parent = dentry->d_parent; /* dir inode is locked */
20486 +       di_write_lock_parent(parent);
20487 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20488 +                                     &a->pin, &wr_dir_args);
20489 +       err = PTR_ERR(wh_dentry);
20490 +       if (IS_ERR(wh_dentry))
20491 +               goto out_parent;
20492 +
20493 +       sb = dentry->d_sb;
20494 +       bindex = au_dbtop(dentry);
20495 +       h_path.dentry = au_h_dptr(dentry, bindex);
20496 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20497 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20498 +       if (unlikely(err))
20499 +               goto out_unpin;
20500 +
20501 +       /* make the dir opaque */
20502 +       diropq = 0;
20503 +       h_inode = d_inode(h_path.dentry);
20504 +       if (wh_dentry
20505 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20506 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20507 +               opq_dentry = au_diropq_create(dentry, bindex);
20508 +               inode_unlock(h_inode);
20509 +               err = PTR_ERR(opq_dentry);
20510 +               if (IS_ERR(opq_dentry))
20511 +                       goto out_dir;
20512 +               dput(opq_dentry);
20513 +               diropq = 1;
20514 +       }
20515 +
20516 +       err = epilog(dir, bindex, wh_dentry, dentry);
20517 +       if (!err) {
20518 +               inc_nlink(dir);
20519 +               goto out_unpin; /* success */
20520 +       }
20521 +
20522 +       /* revert */
20523 +       if (diropq) {
20524 +               AuLabel(revert opq);
20525 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20526 +               rerr = au_diropq_remove(dentry, bindex);
20527 +               inode_unlock(h_inode);
20528 +               if (rerr) {
20529 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20530 +                               dentry, err, rerr);
20531 +                       err = -EIO;
20532 +               }
20533 +       }
20534 +
20535 +out_dir:
20536 +       AuLabel(revert dir);
20537 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20538 +       if (rerr) {
20539 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20540 +                       dentry, err, rerr);
20541 +               err = -EIO;
20542 +       }
20543 +       au_dtime_revert(&a->dt);
20544 +out_unpin:
20545 +       au_unpin(&a->pin);
20546 +       dput(wh_dentry);
20547 +out_parent:
20548 +       di_write_unlock(parent);
20549 +out_unlock:
20550 +       if (unlikely(err)) {
20551 +               au_update_dbtop(dentry);
20552 +               d_drop(dentry);
20553 +       }
20554 +       aufs_read_unlock(dentry, AuLock_DW);
20555 +out_free:
20556 +       au_kfree_rcu(a);
20557 +out:
20558 +       return err;
20559 +}
20560 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20561 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20562 +++ linux/fs/aufs/i_op.c        2020-08-03 09:14:46.095748745 +0200
20563 @@ -0,0 +1,1502 @@
20564 +// SPDX-License-Identifier: GPL-2.0
20565 +/*
20566 + * Copyright (C) 2005-2020 Junjiro R. Okajima
20567 + *
20568 + * This program, aufs is free software; you can redistribute it and/or modify
20569 + * it under the terms of the GNU General Public License as published by
20570 + * the Free Software Foundation; either version 2 of the License, or
20571 + * (at your option) any later version.
20572 + *
20573 + * This program is distributed in the hope that it will be useful,
20574 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20575 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20576 + * GNU General Public License for more details.
20577 + *
20578 + * You should have received a copy of the GNU General Public License
20579 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20580 + */
20581 +
20582 +/*
20583 + * inode operations (except add/del/rename)
20584 + */
20585 +
20586 +#include <linux/device_cgroup.h>
20587 +#include <linux/fs_stack.h>
20588 +#include <linux/iversion.h>
20589 +#include <linux/namei.h>
20590 +#include <linux/security.h>
20591 +#include "aufs.h"
20592 +
20593 +static int h_permission(struct inode *h_inode, int mask,
20594 +                       struct path *h_path, int brperm)
20595 +{
20596 +       int err;
20597 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20598 +
20599 +       err = -EPERM;
20600 +       if (write_mask && IS_IMMUTABLE(h_inode))
20601 +               goto out;
20602 +
20603 +       err = -EACCES;
20604 +       if (((mask & MAY_EXEC)
20605 +            && S_ISREG(h_inode->i_mode)
20606 +            && (path_noexec(h_path)
20607 +                || !(h_inode->i_mode & 0111))))
20608 +               goto out;
20609 +
20610 +       /*
20611 +        * - skip the lower fs test in the case of write to ro branch.
20612 +        * - nfs dir permission write check is optimized, but a policy for
20613 +        *   link/rename requires a real check.
20614 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20615 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20616 +        */
20617 +       if ((write_mask && !au_br_writable(brperm))
20618 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20619 +               && write_mask && !(mask & MAY_READ))
20620 +           || !h_inode->i_op->permission) {
20621 +               /* AuLabel(generic_permission); */
20622 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20623 +               err = generic_permission(h_inode, mask);
20624 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20625 +                       err = h_inode->i_op->permission(h_inode, mask);
20626 +               AuTraceErr(err);
20627 +       } else {
20628 +               /* AuLabel(h_inode->permission); */
20629 +               err = h_inode->i_op->permission(h_inode, mask);
20630 +               AuTraceErr(err);
20631 +       }
20632 +
20633 +       if (!err)
20634 +               err = devcgroup_inode_permission(h_inode, mask);
20635 +       if (!err)
20636 +               err = security_inode_permission(h_inode, mask);
20637 +
20638 +out:
20639 +       return err;
20640 +}
20641 +
20642 +static int aufs_permission(struct inode *inode, int mask)
20643 +{
20644 +       int err;
20645 +       aufs_bindex_t bindex, bbot;
20646 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20647 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20648 +       struct inode *h_inode;
20649 +       struct super_block *sb;
20650 +       struct au_branch *br;
20651 +
20652 +       /* todo: support rcu-walk? */
20653 +       if (mask & MAY_NOT_BLOCK)
20654 +               return -ECHILD;
20655 +
20656 +       sb = inode->i_sb;
20657 +       si_read_lock(sb, AuLock_FLUSH);
20658 +       ii_read_lock_child(inode);
20659 +#if 0 /* reserved for future use */
20660 +       /*
20661 +        * This test may be rather 'too much' since the test is essentially done
20662 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20663 +        * generation doesn't match to the superblock's here.  But it isn't a
20664 +        * big deal I suppose.
20665 +        */
20666 +       err = au_iigen_test(inode, au_sigen(sb));
20667 +       if (unlikely(err))
20668 +               goto out;
20669 +#endif
20670 +
20671 +       if (!isdir
20672 +           || write_mask
20673 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20674 +               err = au_busy_or_stale();
20675 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20676 +               if (unlikely(!h_inode
20677 +                            || (h_inode->i_mode & S_IFMT)
20678 +                            != (inode->i_mode & S_IFMT)))
20679 +                       goto out;
20680 +
20681 +               err = 0;
20682 +               bindex = au_ibtop(inode);
20683 +               br = au_sbr(sb, bindex);
20684 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20685 +               if (write_mask
20686 +                   && !err
20687 +                   && !special_file(h_inode->i_mode)) {
20688 +                       /* test whether the upper writable branch exists */
20689 +                       err = -EROFS;
20690 +                       for (; bindex >= 0; bindex--)
20691 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20692 +                                       err = 0;
20693 +                                       break;
20694 +                               }
20695 +               }
20696 +               goto out;
20697 +       }
20698 +
20699 +       /* non-write to dir */
20700 +       err = 0;
20701 +       bbot = au_ibbot(inode);
20702 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20703 +               h_inode = au_h_iptr(inode, bindex);
20704 +               if (h_inode) {
20705 +                       err = au_busy_or_stale();
20706 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20707 +                               break;
20708 +
20709 +                       br = au_sbr(sb, bindex);
20710 +                       err = h_permission(h_inode, mask, &br->br_path,
20711 +                                          br->br_perm);
20712 +               }
20713 +       }
20714 +
20715 +out:
20716 +       ii_read_unlock(inode);
20717 +       si_read_unlock(sb);
20718 +       return err;
20719 +}
20720 +
20721 +/* ---------------------------------------------------------------------- */
20722 +
20723 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20724 +                                 unsigned int flags)
20725 +{
20726 +       struct dentry *ret, *parent;
20727 +       struct inode *inode;
20728 +       struct super_block *sb;
20729 +       int err, npositive;
20730 +
20731 +       IMustLock(dir);
20732 +
20733 +       /* todo: support rcu-walk? */
20734 +       ret = ERR_PTR(-ECHILD);
20735 +       if (flags & LOOKUP_RCU)
20736 +               goto out;
20737 +
20738 +       ret = ERR_PTR(-ENAMETOOLONG);
20739 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20740 +               goto out;
20741 +
20742 +       sb = dir->i_sb;
20743 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20744 +       ret = ERR_PTR(err);
20745 +       if (unlikely(err))
20746 +               goto out;
20747 +
20748 +       err = au_di_init(dentry);
20749 +       ret = ERR_PTR(err);
20750 +       if (unlikely(err))
20751 +               goto out_si;
20752 +
20753 +       inode = NULL;
20754 +       npositive = 0; /* suppress a warning */
20755 +       parent = dentry->d_parent; /* dir inode is locked */
20756 +       di_read_lock_parent(parent, AuLock_IR);
20757 +       err = au_alive_dir(parent);
20758 +       if (!err)
20759 +               err = au_digen_test(parent, au_sigen(sb));
20760 +       if (!err) {
20761 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20762 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20763 +                                          AuLkup_ALLOW_NEG);
20764 +               err = npositive;
20765 +       }
20766 +       di_read_unlock(parent, AuLock_IR);
20767 +       ret = ERR_PTR(err);
20768 +       if (unlikely(err < 0))
20769 +               goto out_unlock;
20770 +
20771 +       if (npositive) {
20772 +               inode = au_new_inode(dentry, /*must_new*/0);
20773 +               if (IS_ERR(inode)) {
20774 +                       ret = (void *)inode;
20775 +                       inode = NULL;
20776 +                       goto out_unlock;
20777 +               }
20778 +       }
20779 +
20780 +       if (inode)
20781 +               atomic_inc(&inode->i_count);
20782 +       ret = d_splice_alias(inode, dentry);
20783 +#if 0 /* reserved for future use */
20784 +       if (unlikely(d_need_lookup(dentry))) {
20785 +               spin_lock(&dentry->d_lock);
20786 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20787 +               spin_unlock(&dentry->d_lock);
20788 +       } else
20789 +#endif
20790 +       if (inode) {
20791 +               if (!IS_ERR(ret)) {
20792 +                       iput(inode);
20793 +                       if (ret && ret != dentry)
20794 +                               ii_write_unlock(inode);
20795 +               } else {
20796 +                       ii_write_unlock(inode);
20797 +                       iput(inode);
20798 +                       inode = NULL;
20799 +               }
20800 +       }
20801 +
20802 +out_unlock:
20803 +       di_write_unlock(dentry);
20804 +out_si:
20805 +       si_read_unlock(sb);
20806 +out:
20807 +       return ret;
20808 +}
20809 +
20810 +/* ---------------------------------------------------------------------- */
20811 +
20812 +/*
20813 + * very dirty and complicated aufs ->atomic_open().
20814 + * aufs_atomic_open()
20815 + * + au_aopen_or_create()
20816 + *   + add_simple()
20817 + *     + vfsub_atomic_open()
20818 + *       + branch fs ->atomic_open()
20819 + *        may call the actual 'open' for h_file
20820 + *       + inc br_nfiles only if opened
20821 + * + au_aopen_no_open() or au_aopen_do_open()
20822 + *
20823 + * au_aopen_do_open()
20824 + * + finish_open()
20825 + *   + au_do_aopen()
20826 + *     + au_do_open() the body of all 'open'
20827 + *       + au_do_open_nondir()
20828 + *        set the passed h_file
20829 + *
20830 + * au_aopen_no_open()
20831 + * + finish_no_open()
20832 + */
20833 +
20834 +struct aopen_node {
20835 +       struct hlist_bl_node hblist;
20836 +       struct file *file, *h_file;
20837 +};
20838 +
20839 +static int au_do_aopen(struct inode *inode, struct file *file)
20840 +{
20841 +       struct hlist_bl_head *aopen;
20842 +       struct hlist_bl_node *pos;
20843 +       struct aopen_node *node;
20844 +       struct au_do_open_args args = {
20845 +               .aopen  = 1,
20846 +               .open   = au_do_open_nondir
20847 +       };
20848 +
20849 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20850 +       hlist_bl_lock(aopen);
20851 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20852 +               if (node->file == file) {
20853 +                       args.h_file = node->h_file;
20854 +                       break;
20855 +               }
20856 +       hlist_bl_unlock(aopen);
20857 +       /* AuDebugOn(!args.h_file); */
20858 +
20859 +       return au_do_open(file, &args);
20860 +}
20861 +
20862 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20863 +                           struct aopen_node *aopen_node)
20864 +{
20865 +       int err;
20866 +       struct hlist_bl_head *aopen;
20867 +
20868 +       AuLabel(here);
20869 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20870 +       au_hbl_add(&aopen_node->hblist, aopen);
20871 +       err = finish_open(file, dentry, au_do_aopen);
20872 +       au_hbl_del(&aopen_node->hblist, aopen);
20873 +       /* AuDbgFile(file); */
20874 +       AuDbg("%pd%s%s\n", dentry,
20875 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20876 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20877 +
20878 +       AuTraceErr(err);
20879 +       return err;
20880 +}
20881 +
20882 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20883 +{
20884 +       int err;
20885 +
20886 +       AuLabel(here);
20887 +       dget(dentry);
20888 +       err = finish_no_open(file, dentry);
20889 +
20890 +       AuTraceErr(err);
20891 +       return err;
20892 +}
20893 +
20894 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20895 +                           struct file *file, unsigned int open_flag,
20896 +                           umode_t create_mode)
20897 +{
20898 +       int err, did_open;
20899 +       unsigned int lkup_flags;
20900 +       aufs_bindex_t bindex;
20901 +       struct super_block *sb;
20902 +       struct dentry *parent, *d;
20903 +       struct vfsub_aopen_args args = {
20904 +               .open_flag      = open_flag,
20905 +               .create_mode    = create_mode
20906 +       };
20907 +       struct aopen_node aopen_node = {
20908 +               .file   = file
20909 +       };
20910 +
20911 +       IMustLock(dir);
20912 +       AuDbg("open_flag 0%o\n", open_flag);
20913 +       AuDbgDentry(dentry);
20914 +
20915 +       err = 0;
20916 +       if (!au_di(dentry)) {
20917 +               lkup_flags = LOOKUP_OPEN;
20918 +               if (open_flag & O_CREAT)
20919 +                       lkup_flags |= LOOKUP_CREATE;
20920 +               d = aufs_lookup(dir, dentry, lkup_flags);
20921 +               if (IS_ERR(d)) {
20922 +                       err = PTR_ERR(d);
20923 +                       AuTraceErr(err);
20924 +                       goto out;
20925 +               } else if (d) {
20926 +                       /*
20927 +                        * obsoleted dentry found.
20928 +                        * another error will be returned later.
20929 +                        */
20930 +                       d_drop(d);
20931 +                       AuDbgDentry(d);
20932 +                       dput(d);
20933 +               }
20934 +               AuDbgDentry(dentry);
20935 +       }
20936 +
20937 +       if (d_is_positive(dentry)
20938 +           || d_unhashed(dentry)
20939 +           || d_unlinked(dentry)
20940 +           || !(open_flag & O_CREAT)) {
20941 +               err = au_aopen_no_open(file, dentry);
20942 +               goto out; /* success */
20943 +       }
20944 +
20945 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20946 +       if (unlikely(err))
20947 +               goto out;
20948 +
20949 +       sb = dentry->d_sb;
20950 +       parent = dentry->d_parent;      /* dir is locked */
20951 +       di_write_lock_parent(parent);
20952 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20953 +       if (unlikely(err < 0))
20954 +               goto out_parent;
20955 +
20956 +       AuDbgDentry(dentry);
20957 +       if (d_is_positive(dentry)) {
20958 +               err = au_aopen_no_open(file, dentry);
20959 +               goto out_parent; /* success */
20960 +       }
20961 +
20962 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20963 +       err = PTR_ERR(args.file);
20964 +       if (IS_ERR(args.file))
20965 +               goto out_parent;
20966 +
20967 +       bindex = au_dbtop(dentry);
20968 +       err = au_aopen_or_create(dir, dentry, &args);
20969 +       AuTraceErr(err);
20970 +       AuDbgFile(args.file);
20971 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20972 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20973 +       if (!did_open) {
20974 +               fput(args.file);
20975 +               args.file = NULL;
20976 +       }
20977 +       di_write_unlock(parent);
20978 +       di_write_unlock(dentry);
20979 +       if (unlikely(err < 0)) {
20980 +               if (args.file)
20981 +                       fput(args.file);
20982 +               goto out_sb;
20983 +       }
20984 +
20985 +       if (!did_open)
20986 +               err = au_aopen_no_open(file, dentry);
20987 +       else {
20988 +               aopen_node.h_file = args.file;
20989 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20990 +       }
20991 +       if (unlikely(err < 0)) {
20992 +               if (args.file)
20993 +                       fput(args.file);
20994 +               if (did_open)
20995 +                       au_lcnt_dec(&args.br->br_nfiles);
20996 +       }
20997 +       goto out_sb; /* success */
20998 +
20999 +out_parent:
21000 +       di_write_unlock(parent);
21001 +       di_write_unlock(dentry);
21002 +out_sb:
21003 +       si_read_unlock(sb);
21004 +out:
21005 +       AuTraceErr(err);
21006 +       AuDbgFile(file);
21007 +       return err;
21008 +}
21009 +
21010 +
21011 +/* ---------------------------------------------------------------------- */
21012 +
21013 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21014 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
21015 +                         aufs_bindex_t btop)
21016 +{
21017 +       int err;
21018 +       struct dentry *h_parent;
21019 +       struct inode *h_dir;
21020 +
21021 +       if (add_entry)
21022 +               IMustLock(d_inode(parent));
21023 +       else
21024 +               di_write_lock_parent(parent);
21025 +
21026 +       err = 0;
21027 +       if (!au_h_dptr(parent, bcpup)) {
21028 +               if (btop > bcpup)
21029 +                       err = au_cpup_dirs(dentry, bcpup);
21030 +               else if (btop < bcpup)
21031 +                       err = au_cpdown_dirs(dentry, bcpup);
21032 +               else
21033 +                       BUG();
21034 +       }
21035 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
21036 +               h_parent = au_h_dptr(parent, bcpup);
21037 +               h_dir = d_inode(h_parent);
21038 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
21039 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
21040 +               /* todo: no unlock here */
21041 +               inode_unlock_shared(h_dir);
21042 +
21043 +               AuDbg("bcpup %d\n", bcpup);
21044 +               if (!err) {
21045 +                       if (d_really_is_negative(dentry))
21046 +                               au_set_h_dptr(dentry, btop, NULL);
21047 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
21048 +               }
21049 +       }
21050 +
21051 +       if (!add_entry)
21052 +               di_write_unlock(parent);
21053 +       if (!err)
21054 +               err = bcpup; /* success */
21055 +
21056 +       AuTraceErr(err);
21057 +       return err;
21058 +}
21059 +
21060 +/*
21061 + * decide the branch and the parent dir where we will create a new entry.
21062 + * returns new bindex or an error.
21063 + * copyup the parent dir if needed.
21064 + */
21065 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21066 +             struct au_wr_dir_args *args)
21067 +{
21068 +       int err;
21069 +       unsigned int flags;
21070 +       aufs_bindex_t bcpup, btop, src_btop;
21071 +       const unsigned char add_entry
21072 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
21073 +               | au_ftest_wrdir(args->flags, TMPFILE);
21074 +       struct super_block *sb;
21075 +       struct dentry *parent;
21076 +       struct au_sbinfo *sbinfo;
21077 +
21078 +       sb = dentry->d_sb;
21079 +       sbinfo = au_sbi(sb);
21080 +       parent = dget_parent(dentry);
21081 +       btop = au_dbtop(dentry);
21082 +       bcpup = btop;
21083 +       if (args->force_btgt < 0) {
21084 +               if (src_dentry) {
21085 +                       src_btop = au_dbtop(src_dentry);
21086 +                       if (src_btop < btop)
21087 +                               bcpup = src_btop;
21088 +               } else if (add_entry) {
21089 +                       flags = 0;
21090 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21091 +                               au_fset_wbr(flags, DIR);
21092 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21093 +                       bcpup = err;
21094 +               }
21095 +
21096 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21097 +                       if (add_entry)
21098 +                               err = AuWbrCopyup(sbinfo, dentry);
21099 +                       else {
21100 +                               if (!IS_ROOT(dentry)) {
21101 +                                       di_read_lock_parent(parent, !AuLock_IR);
21102 +                                       err = AuWbrCopyup(sbinfo, dentry);
21103 +                                       di_read_unlock(parent, !AuLock_IR);
21104 +                               } else
21105 +                                       err = AuWbrCopyup(sbinfo, dentry);
21106 +                       }
21107 +                       bcpup = err;
21108 +                       if (unlikely(err < 0))
21109 +                               goto out;
21110 +               }
21111 +       } else {
21112 +               bcpup = args->force_btgt;
21113 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21114 +       }
21115 +
21116 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21117 +       err = bcpup;
21118 +       if (bcpup == btop)
21119 +               goto out; /* success */
21120 +
21121 +       /* copyup the new parent into the branch we process */
21122 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21123 +       if (err >= 0) {
21124 +               if (d_really_is_negative(dentry)) {
21125 +                       au_set_h_dptr(dentry, btop, NULL);
21126 +                       au_set_dbtop(dentry, bcpup);
21127 +                       au_set_dbbot(dentry, bcpup);
21128 +               }
21129 +               AuDebugOn(add_entry
21130 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21131 +                         && !au_h_dptr(dentry, bcpup));
21132 +       }
21133 +
21134 +out:
21135 +       dput(parent);
21136 +       return err;
21137 +}
21138 +
21139 +/* ---------------------------------------------------------------------- */
21140 +
21141 +void au_pin_hdir_unlock(struct au_pin *p)
21142 +{
21143 +       if (p->hdir)
21144 +               au_hn_inode_unlock(p->hdir);
21145 +}
21146 +
21147 +int au_pin_hdir_lock(struct au_pin *p)
21148 +{
21149 +       int err;
21150 +
21151 +       err = 0;
21152 +       if (!p->hdir)
21153 +               goto out;
21154 +
21155 +       /* even if an error happens later, keep this lock */
21156 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21157 +
21158 +       err = -EBUSY;
21159 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21160 +               goto out;
21161 +
21162 +       err = 0;
21163 +       if (p->h_dentry)
21164 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21165 +                                 p->h_parent, p->br);
21166 +
21167 +out:
21168 +       return err;
21169 +}
21170 +
21171 +int au_pin_hdir_relock(struct au_pin *p)
21172 +{
21173 +       int err, i;
21174 +       struct inode *h_i;
21175 +       struct dentry *h_d[] = {
21176 +               p->h_dentry,
21177 +               p->h_parent
21178 +       };
21179 +
21180 +       err = au_pin_hdir_lock(p);
21181 +       if (unlikely(err))
21182 +               goto out;
21183 +
21184 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21185 +               if (!h_d[i])
21186 +                       continue;
21187 +               if (d_is_positive(h_d[i])) {
21188 +                       h_i = d_inode(h_d[i]);
21189 +                       err = !h_i->i_nlink;
21190 +               }
21191 +       }
21192 +
21193 +out:
21194 +       return err;
21195 +}
21196 +
21197 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21198 +{
21199 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21200 +}
21201 +
21202 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21203 +{
21204 +       if (p->hdir) {
21205 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21206 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21207 +               au_pin_hdir_set_owner(p, current);
21208 +       }
21209 +}
21210 +
21211 +void au_pin_hdir_release(struct au_pin *p)
21212 +{
21213 +       if (p->hdir) {
21214 +               au_pin_hdir_set_owner(p, p->task);
21215 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21216 +       }
21217 +}
21218 +
21219 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21220 +{
21221 +       if (pin && pin->parent)
21222 +               return au_h_dptr(pin->parent, pin->bindex);
21223 +       return NULL;
21224 +}
21225 +
21226 +void au_unpin(struct au_pin *p)
21227 +{
21228 +       if (p->hdir)
21229 +               au_pin_hdir_unlock(p);
21230 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21231 +               vfsub_mnt_drop_write(p->h_mnt);
21232 +       if (!p->hdir)
21233 +               return;
21234 +
21235 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21236 +               di_read_unlock(p->parent, AuLock_IR);
21237 +       iput(p->hdir->hi_inode);
21238 +       dput(p->parent);
21239 +       p->parent = NULL;
21240 +       p->hdir = NULL;
21241 +       p->h_mnt = NULL;
21242 +       /* do not clear p->task */
21243 +}
21244 +
21245 +int au_do_pin(struct au_pin *p)
21246 +{
21247 +       int err;
21248 +       struct super_block *sb;
21249 +       struct inode *h_dir;
21250 +
21251 +       err = 0;
21252 +       sb = p->dentry->d_sb;
21253 +       p->br = au_sbr(sb, p->bindex);
21254 +       if (IS_ROOT(p->dentry)) {
21255 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21256 +                       p->h_mnt = au_br_mnt(p->br);
21257 +                       err = vfsub_mnt_want_write(p->h_mnt);
21258 +                       if (unlikely(err)) {
21259 +                               au_fclr_pin(p->flags, MNT_WRITE);
21260 +                               goto out_err;
21261 +                       }
21262 +               }
21263 +               goto out;
21264 +       }
21265 +
21266 +       p->h_dentry = NULL;
21267 +       if (p->bindex <= au_dbbot(p->dentry))
21268 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21269 +
21270 +       p->parent = dget_parent(p->dentry);
21271 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21272 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21273 +
21274 +       h_dir = NULL;
21275 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21276 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21277 +       if (p->hdir)
21278 +               h_dir = p->hdir->hi_inode;
21279 +
21280 +       /*
21281 +        * udba case, or
21282 +        * if DI_LOCKED is not set, then p->parent may be different
21283 +        * and h_parent can be NULL.
21284 +        */
21285 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21286 +               err = -EBUSY;
21287 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21288 +                       di_read_unlock(p->parent, AuLock_IR);
21289 +               dput(p->parent);
21290 +               p->parent = NULL;
21291 +               goto out_err;
21292 +       }
21293 +
21294 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21295 +               p->h_mnt = au_br_mnt(p->br);
21296 +               err = vfsub_mnt_want_write(p->h_mnt);
21297 +               if (unlikely(err)) {
21298 +                       au_fclr_pin(p->flags, MNT_WRITE);
21299 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21300 +                               di_read_unlock(p->parent, AuLock_IR);
21301 +                       dput(p->parent);
21302 +                       p->parent = NULL;
21303 +                       goto out_err;
21304 +               }
21305 +       }
21306 +
21307 +       au_igrab(h_dir);
21308 +       err = au_pin_hdir_lock(p);
21309 +       if (!err)
21310 +               goto out; /* success */
21311 +
21312 +       au_unpin(p);
21313 +
21314 +out_err:
21315 +       pr_err("err %d\n", err);
21316 +       err = au_busy_or_stale();
21317 +out:
21318 +       return err;
21319 +}
21320 +
21321 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21322 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21323 +                unsigned int udba, unsigned char flags)
21324 +{
21325 +       p->dentry = dentry;
21326 +       p->udba = udba;
21327 +       p->lsc_di = lsc_di;
21328 +       p->lsc_hi = lsc_hi;
21329 +       p->flags = flags;
21330 +       p->bindex = bindex;
21331 +
21332 +       p->parent = NULL;
21333 +       p->hdir = NULL;
21334 +       p->h_mnt = NULL;
21335 +
21336 +       p->h_dentry = NULL;
21337 +       p->h_parent = NULL;
21338 +       p->br = NULL;
21339 +       p->task = current;
21340 +}
21341 +
21342 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21343 +          unsigned int udba, unsigned char flags)
21344 +{
21345 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21346 +                   udba, flags);
21347 +       return au_do_pin(pin);
21348 +}
21349 +
21350 +/* ---------------------------------------------------------------------- */
21351 +
21352 +/*
21353 + * ->setattr() and ->getattr() are called in various cases.
21354 + * chmod, stat: dentry is revalidated.
21355 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21356 + *               unhashed.
21357 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21358 + */
21359 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21360 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21361 +{
21362 +       int err;
21363 +       struct dentry *parent;
21364 +
21365 +       err = 0;
21366 +       if (au_digen_test(dentry, sigen)) {
21367 +               parent = dget_parent(dentry);
21368 +               di_read_lock_parent(parent, AuLock_IR);
21369 +               err = au_refresh_dentry(dentry, parent);
21370 +               di_read_unlock(parent, AuLock_IR);
21371 +               dput(parent);
21372 +       }
21373 +
21374 +       AuTraceErr(err);
21375 +       return err;
21376 +}
21377 +
21378 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21379 +                    struct au_icpup_args *a)
21380 +{
21381 +       int err;
21382 +       loff_t sz;
21383 +       aufs_bindex_t btop, ibtop;
21384 +       struct dentry *hi_wh, *parent;
21385 +       struct inode *inode;
21386 +       struct au_wr_dir_args wr_dir_args = {
21387 +               .force_btgt     = -1,
21388 +               .flags          = 0
21389 +       };
21390 +
21391 +       if (d_is_dir(dentry))
21392 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21393 +       /* plink or hi_wh() case */
21394 +       btop = au_dbtop(dentry);
21395 +       inode = d_inode(dentry);
21396 +       ibtop = au_ibtop(inode);
21397 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21398 +               wr_dir_args.force_btgt = ibtop;
21399 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21400 +       if (unlikely(err < 0))
21401 +               goto out;
21402 +       a->btgt = err;
21403 +       if (err != btop)
21404 +               au_fset_icpup(a->flags, DID_CPUP);
21405 +
21406 +       err = 0;
21407 +       a->pin_flags = AuPin_MNT_WRITE;
21408 +       parent = NULL;
21409 +       if (!IS_ROOT(dentry)) {
21410 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21411 +               parent = dget_parent(dentry);
21412 +               di_write_lock_parent(parent);
21413 +       }
21414 +
21415 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21416 +       if (unlikely(err))
21417 +               goto out_parent;
21418 +
21419 +       sz = -1;
21420 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21421 +       a->h_inode = d_inode(a->h_path.dentry);
21422 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21423 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21424 +               if (ia->ia_size < i_size_read(a->h_inode))
21425 +                       sz = ia->ia_size;
21426 +               inode_unlock_shared(a->h_inode);
21427 +       }
21428 +
21429 +       hi_wh = NULL;
21430 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21431 +               hi_wh = au_hi_wh(inode, a->btgt);
21432 +               if (!hi_wh) {
21433 +                       struct au_cp_generic cpg = {
21434 +                               .dentry = dentry,
21435 +                               .bdst   = a->btgt,
21436 +                               .bsrc   = -1,
21437 +                               .len    = sz,
21438 +                               .pin    = &a->pin
21439 +                       };
21440 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21441 +                       if (unlikely(err))
21442 +                               goto out_unlock;
21443 +                       hi_wh = au_hi_wh(inode, a->btgt);
21444 +                       /* todo: revalidate hi_wh? */
21445 +               }
21446 +       }
21447 +
21448 +       if (parent) {
21449 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21450 +               di_downgrade_lock(parent, AuLock_IR);
21451 +               dput(parent);
21452 +               parent = NULL;
21453 +       }
21454 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21455 +               goto out; /* success */
21456 +
21457 +       if (!d_unhashed(dentry)) {
21458 +               struct au_cp_generic cpg = {
21459 +                       .dentry = dentry,
21460 +                       .bdst   = a->btgt,
21461 +                       .bsrc   = btop,
21462 +                       .len    = sz,
21463 +                       .pin    = &a->pin,
21464 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21465 +               };
21466 +               err = au_sio_cpup_simple(&cpg);
21467 +               if (!err)
21468 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21469 +       } else if (!hi_wh)
21470 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21471 +       else
21472 +               a->h_path.dentry = hi_wh; /* do not dget here */
21473 +
21474 +out_unlock:
21475 +       a->h_inode = d_inode(a->h_path.dentry);
21476 +       if (!err)
21477 +               goto out; /* success */
21478 +       au_unpin(&a->pin);
21479 +out_parent:
21480 +       if (parent) {
21481 +               di_write_unlock(parent);
21482 +               dput(parent);
21483 +       }
21484 +out:
21485 +       if (!err)
21486 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21487 +       return err;
21488 +}
21489 +
21490 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21491 +{
21492 +       int err;
21493 +       struct inode *inode, *delegated;
21494 +       struct super_block *sb;
21495 +       struct file *file;
21496 +       struct au_icpup_args *a;
21497 +
21498 +       inode = d_inode(dentry);
21499 +       IMustLock(inode);
21500 +
21501 +       err = setattr_prepare(dentry, ia);
21502 +       if (unlikely(err))
21503 +               goto out;
21504 +
21505 +       err = -ENOMEM;
21506 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21507 +       if (unlikely(!a))
21508 +               goto out;
21509 +
21510 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21511 +               ia->ia_valid &= ~ATTR_MODE;
21512 +
21513 +       file = NULL;
21514 +       sb = dentry->d_sb;
21515 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21516 +       if (unlikely(err))
21517 +               goto out_kfree;
21518 +
21519 +       if (ia->ia_valid & ATTR_FILE) {
21520 +               /* currently ftruncate(2) only */
21521 +               AuDebugOn(!d_is_reg(dentry));
21522 +               file = ia->ia_file;
21523 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21524 +                                           /*fi_lsc*/0);
21525 +               if (unlikely(err))
21526 +                       goto out_si;
21527 +               ia->ia_file = au_hf_top(file);
21528 +               a->udba = AuOpt_UDBA_NONE;
21529 +       } else {
21530 +               /* fchmod() doesn't pass ia_file */
21531 +               a->udba = au_opt_udba(sb);
21532 +               di_write_lock_child(dentry);
21533 +               /* no d_unlinked(), to set UDBA_NONE for root */
21534 +               if (d_unhashed(dentry))
21535 +                       a->udba = AuOpt_UDBA_NONE;
21536 +               if (a->udba != AuOpt_UDBA_NONE) {
21537 +                       AuDebugOn(IS_ROOT(dentry));
21538 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21539 +                       if (unlikely(err))
21540 +                               goto out_dentry;
21541 +               }
21542 +       }
21543 +
21544 +       err = au_pin_and_icpup(dentry, ia, a);
21545 +       if (unlikely(err < 0))
21546 +               goto out_dentry;
21547 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21548 +               ia->ia_file = NULL;
21549 +               ia->ia_valid &= ~ATTR_FILE;
21550 +       }
21551 +
21552 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21553 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21554 +           == (ATTR_MODE | ATTR_CTIME)) {
21555 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21556 +               if (unlikely(err))
21557 +                       goto out_unlock;
21558 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21559 +                  && (ia->ia_valid & ATTR_CTIME)) {
21560 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21561 +               if (unlikely(err))
21562 +                       goto out_unlock;
21563 +       }
21564 +
21565 +       if (ia->ia_valid & ATTR_SIZE) {
21566 +               struct file *f;
21567 +
21568 +               if (ia->ia_size < i_size_read(inode))
21569 +                       /* unmap only */
21570 +                       truncate_setsize(inode, ia->ia_size);
21571 +
21572 +               f = NULL;
21573 +               if (ia->ia_valid & ATTR_FILE)
21574 +                       f = ia->ia_file;
21575 +               inode_unlock(a->h_inode);
21576 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21577 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21578 +       } else {
21579 +               delegated = NULL;
21580 +               while (1) {
21581 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21582 +                       if (delegated) {
21583 +                               err = break_deleg_wait(&delegated);
21584 +                               if (!err)
21585 +                                       continue;
21586 +                       }
21587 +                       break;
21588 +               }
21589 +       }
21590 +       /*
21591 +        * regardless aufs 'acl' option setting.
21592 +        * why don't all acl-aware fs call this func from their ->setattr()?
21593 +        */
21594 +       if (!err && (ia->ia_valid & ATTR_MODE))
21595 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21596 +       if (!err)
21597 +               au_cpup_attr_changeable(inode);
21598 +
21599 +out_unlock:
21600 +       inode_unlock(a->h_inode);
21601 +       au_unpin(&a->pin);
21602 +       if (unlikely(err))
21603 +               au_update_dbtop(dentry);
21604 +out_dentry:
21605 +       di_write_unlock(dentry);
21606 +       if (file) {
21607 +               fi_write_unlock(file);
21608 +               ia->ia_file = file;
21609 +               ia->ia_valid |= ATTR_FILE;
21610 +       }
21611 +out_si:
21612 +       si_read_unlock(sb);
21613 +out_kfree:
21614 +       au_kfree_rcu(a);
21615 +out:
21616 +       AuTraceErr(err);
21617 +       return err;
21618 +}
21619 +
21620 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21621 +static int au_h_path_to_set_attr(struct dentry *dentry,
21622 +                                struct au_icpup_args *a, struct path *h_path)
21623 +{
21624 +       int err;
21625 +       struct super_block *sb;
21626 +
21627 +       sb = dentry->d_sb;
21628 +       a->udba = au_opt_udba(sb);
21629 +       /* no d_unlinked(), to set UDBA_NONE for root */
21630 +       if (d_unhashed(dentry))
21631 +               a->udba = AuOpt_UDBA_NONE;
21632 +       if (a->udba != AuOpt_UDBA_NONE) {
21633 +               AuDebugOn(IS_ROOT(dentry));
21634 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21635 +               if (unlikely(err))
21636 +                       goto out;
21637 +       }
21638 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21639 +       if (unlikely(err < 0))
21640 +               goto out;
21641 +
21642 +       h_path->dentry = a->h_path.dentry;
21643 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21644 +
21645 +out:
21646 +       return err;
21647 +}
21648 +
21649 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21650 +                 struct au_sxattr *arg)
21651 +{
21652 +       int err;
21653 +       struct path h_path;
21654 +       struct super_block *sb;
21655 +       struct au_icpup_args *a;
21656 +       struct inode *h_inode;
21657 +
21658 +       IMustLock(inode);
21659 +
21660 +       err = -ENOMEM;
21661 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21662 +       if (unlikely(!a))
21663 +               goto out;
21664 +
21665 +       sb = dentry->d_sb;
21666 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21667 +       if (unlikely(err))
21668 +               goto out_kfree;
21669 +
21670 +       h_path.dentry = NULL;   /* silence gcc */
21671 +       di_write_lock_child(dentry);
21672 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21673 +       if (unlikely(err))
21674 +               goto out_di;
21675 +
21676 +       inode_unlock(a->h_inode);
21677 +       switch (arg->type) {
21678 +       case AU_XATTR_SET:
21679 +               AuDebugOn(d_is_negative(h_path.dentry));
21680 +               err = vfsub_setxattr(h_path.dentry,
21681 +                                    arg->u.set.name, arg->u.set.value,
21682 +                                    arg->u.set.size, arg->u.set.flags);
21683 +               break;
21684 +       case AU_ACL_SET:
21685 +               err = -EOPNOTSUPP;
21686 +               h_inode = d_inode(h_path.dentry);
21687 +               if (h_inode->i_op->set_acl)
21688 +                       /* this will call posix_acl_update_mode */
21689 +                       err = h_inode->i_op->set_acl(h_inode,
21690 +                                                    arg->u.acl_set.acl,
21691 +                                                    arg->u.acl_set.type);
21692 +               break;
21693 +       }
21694 +       if (!err)
21695 +               au_cpup_attr_timesizes(inode);
21696 +
21697 +       au_unpin(&a->pin);
21698 +       if (unlikely(err))
21699 +               au_update_dbtop(dentry);
21700 +
21701 +out_di:
21702 +       di_write_unlock(dentry);
21703 +       si_read_unlock(sb);
21704 +out_kfree:
21705 +       au_kfree_rcu(a);
21706 +out:
21707 +       AuTraceErr(err);
21708 +       return err;
21709 +}
21710 +#endif
21711 +
21712 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21713 +                            unsigned int nlink)
21714 +{
21715 +       unsigned int n;
21716 +
21717 +       inode->i_mode = st->mode;
21718 +       /* don't i_[ug]id_write() here */
21719 +       inode->i_uid = st->uid;
21720 +       inode->i_gid = st->gid;
21721 +       inode->i_atime = st->atime;
21722 +       inode->i_mtime = st->mtime;
21723 +       inode->i_ctime = st->ctime;
21724 +
21725 +       au_cpup_attr_nlink(inode, /*force*/0);
21726 +       if (S_ISDIR(inode->i_mode)) {
21727 +               n = inode->i_nlink;
21728 +               n -= nlink;
21729 +               n += st->nlink;
21730 +               smp_mb(); /* for i_nlink */
21731 +               /* 0 can happen */
21732 +               set_nlink(inode, n);
21733 +       }
21734 +
21735 +       spin_lock(&inode->i_lock);
21736 +       inode->i_blocks = st->blocks;
21737 +       i_size_write(inode, st->size);
21738 +       spin_unlock(&inode->i_lock);
21739 +}
21740 +
21741 +/*
21742 + * common routine for aufs_getattr() and au_getxattr().
21743 + * returns zero or negative (an error).
21744 + * @dentry will be read-locked in success.
21745 + */
21746 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21747 +                     struct path *h_path, int locked)
21748 +{
21749 +       int err;
21750 +       unsigned int mnt_flags, sigen;
21751 +       unsigned char udba_none;
21752 +       aufs_bindex_t bindex;
21753 +       struct super_block *sb, *h_sb;
21754 +
21755 +       h_path->mnt = NULL;
21756 +       h_path->dentry = NULL;
21757 +
21758 +       err = 0;
21759 +       sb = dentry->d_sb;
21760 +       mnt_flags = au_mntflags(sb);
21761 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21762 +
21763 +       if (unlikely(locked))
21764 +               goto body; /* skip locking dinfo */
21765 +
21766 +       /* support fstat(2) */
21767 +       if (!d_unlinked(dentry) && !udba_none) {
21768 +               sigen = au_sigen(sb);
21769 +               err = au_digen_test(dentry, sigen);
21770 +               if (!err) {
21771 +                       di_read_lock_child(dentry, AuLock_IR);
21772 +                       err = au_dbrange_test(dentry);
21773 +                       if (unlikely(err)) {
21774 +                               di_read_unlock(dentry, AuLock_IR);
21775 +                               goto out;
21776 +                       }
21777 +               } else {
21778 +                       AuDebugOn(IS_ROOT(dentry));
21779 +                       di_write_lock_child(dentry);
21780 +                       err = au_dbrange_test(dentry);
21781 +                       if (!err)
21782 +                               err = au_reval_for_attr(dentry, sigen);
21783 +                       if (!err)
21784 +                               di_downgrade_lock(dentry, AuLock_IR);
21785 +                       else {
21786 +                               di_write_unlock(dentry);
21787 +                               goto out;
21788 +                       }
21789 +               }
21790 +       } else
21791 +               di_read_lock_child(dentry, AuLock_IR);
21792 +
21793 +body:
21794 +       if (!inode) {
21795 +               inode = d_inode(dentry);
21796 +               if (unlikely(!inode))
21797 +                       goto out;
21798 +       }
21799 +       bindex = au_ibtop(inode);
21800 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21801 +       h_sb = h_path->mnt->mnt_sb;
21802 +       if (!force
21803 +           && !au_test_fs_bad_iattr(h_sb)
21804 +           && udba_none)
21805 +               goto out; /* success */
21806 +
21807 +       if (au_dbtop(dentry) == bindex)
21808 +               h_path->dentry = au_h_dptr(dentry, bindex);
21809 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21810 +               h_path->dentry = au_plink_lkup(inode, bindex);
21811 +               if (IS_ERR(h_path->dentry))
21812 +                       /* pretending success */
21813 +                       h_path->dentry = NULL;
21814 +               else
21815 +                       dput(h_path->dentry);
21816 +       }
21817 +
21818 +out:
21819 +       return err;
21820 +}
21821 +
21822 +static int aufs_getattr(const struct path *path, struct kstat *st,
21823 +                       u32 request, unsigned int query)
21824 +{
21825 +       int err;
21826 +       unsigned char positive;
21827 +       struct path h_path;
21828 +       struct dentry *dentry;
21829 +       struct inode *inode;
21830 +       struct super_block *sb;
21831 +
21832 +       dentry = path->dentry;
21833 +       inode = d_inode(dentry);
21834 +       sb = dentry->d_sb;
21835 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21836 +       if (unlikely(err))
21837 +               goto out;
21838 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21839 +                               /*locked*/0);
21840 +       if (unlikely(err))
21841 +               goto out_si;
21842 +       if (unlikely(!h_path.dentry))
21843 +               /* illegally overlapped or something */
21844 +               goto out_fill; /* pretending success */
21845 +
21846 +       positive = d_is_positive(h_path.dentry);
21847 +       if (positive)
21848 +               /* no vfsub version */
21849 +               err = vfs_getattr(&h_path, st, request, query);
21850 +       if (!err) {
21851 +               if (positive)
21852 +                       au_refresh_iattr(inode, st,
21853 +                                        d_inode(h_path.dentry)->i_nlink);
21854 +               goto out_fill; /* success */
21855 +       }
21856 +       AuTraceErr(err);
21857 +       goto out_di;
21858 +
21859 +out_fill:
21860 +       generic_fillattr(inode, st);
21861 +out_di:
21862 +       di_read_unlock(dentry, AuLock_IR);
21863 +out_si:
21864 +       si_read_unlock(sb);
21865 +out:
21866 +       AuTraceErr(err);
21867 +       return err;
21868 +}
21869 +
21870 +/* ---------------------------------------------------------------------- */
21871 +
21872 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21873 +                                struct delayed_call *done)
21874 +{
21875 +       const char *ret;
21876 +       struct dentry *h_dentry;
21877 +       struct inode *h_inode;
21878 +       int err;
21879 +       aufs_bindex_t bindex;
21880 +
21881 +       ret = NULL; /* suppress a warning */
21882 +       err = -ECHILD;
21883 +       if (!dentry)
21884 +               goto out;
21885 +
21886 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21887 +       if (unlikely(err))
21888 +               goto out;
21889 +
21890 +       err = au_d_hashed_positive(dentry);
21891 +       if (unlikely(err))
21892 +               goto out_unlock;
21893 +
21894 +       err = -EINVAL;
21895 +       inode = d_inode(dentry);
21896 +       bindex = au_ibtop(inode);
21897 +       h_inode = au_h_iptr(inode, bindex);
21898 +       if (unlikely(!h_inode->i_op->get_link))
21899 +               goto out_unlock;
21900 +
21901 +       err = -EBUSY;
21902 +       h_dentry = NULL;
21903 +       if (au_dbtop(dentry) <= bindex) {
21904 +               h_dentry = au_h_dptr(dentry, bindex);
21905 +               if (h_dentry)
21906 +                       dget(h_dentry);
21907 +       }
21908 +       if (!h_dentry) {
21909 +               h_dentry = d_find_any_alias(h_inode);
21910 +               if (IS_ERR(h_dentry)) {
21911 +                       err = PTR_ERR(h_dentry);
21912 +                       goto out_unlock;
21913 +               }
21914 +       }
21915 +       if (unlikely(!h_dentry))
21916 +               goto out_unlock;
21917 +
21918 +       err = 0;
21919 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21920 +       AuDbgDentry(h_dentry);
21921 +       ret = vfs_get_link(h_dentry, done);
21922 +       dput(h_dentry);
21923 +       if (IS_ERR(ret))
21924 +               err = PTR_ERR(ret);
21925 +
21926 +out_unlock:
21927 +       aufs_read_unlock(dentry, AuLock_IR);
21928 +out:
21929 +       if (unlikely(err))
21930 +               ret = ERR_PTR(err);
21931 +       AuTraceErrPtr(ret);
21932 +       return ret;
21933 +}
21934 +
21935 +/* ---------------------------------------------------------------------- */
21936 +
21937 +static int au_is_special(struct inode *inode)
21938 +{
21939 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21940 +}
21941 +
21942 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21943 +                           int flags)
21944 +{
21945 +       int err;
21946 +       aufs_bindex_t bindex;
21947 +       struct super_block *sb;
21948 +       struct inode *h_inode;
21949 +       struct vfsmount *h_mnt;
21950 +
21951 +       sb = inode->i_sb;
21952 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21953 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21954 +
21955 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21956 +       lockdep_off();
21957 +       si_read_lock(sb, AuLock_FLUSH);
21958 +       ii_write_lock_child(inode);
21959 +
21960 +       err = 0;
21961 +       bindex = au_ibtop(inode);
21962 +       h_inode = au_h_iptr(inode, bindex);
21963 +       if (!au_test_ro(sb, bindex, inode)) {
21964 +               h_mnt = au_sbr_mnt(sb, bindex);
21965 +               err = vfsub_mnt_want_write(h_mnt);
21966 +               if (!err) {
21967 +                       err = vfsub_update_time(h_inode, ts, flags);
21968 +                       vfsub_mnt_drop_write(h_mnt);
21969 +               }
21970 +       } else if (au_is_special(h_inode)) {
21971 +               /*
21972 +                * Never copy-up here.
21973 +                * These special files may already be opened and used for
21974 +                * communicating. If we copied it up, then the communication
21975 +                * would be corrupted.
21976 +                */
21977 +               AuWarn1("timestamps for i%lu are ignored "
21978 +                       "since it is on readonly branch (hi%lu).\n",
21979 +                       inode->i_ino, h_inode->i_ino);
21980 +       } else if (flags & ~S_ATIME) {
21981 +               err = -EIO;
21982 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21983 +               AuDebugOn(1);
21984 +       }
21985 +
21986 +       if (!err)
21987 +               au_cpup_attr_timesizes(inode);
21988 +       ii_write_unlock(inode);
21989 +       si_read_unlock(sb);
21990 +       lockdep_on();
21991 +
21992 +       if (!err && (flags & S_VERSION))
21993 +               inode_inc_iversion(inode);
21994 +
21995 +       return err;
21996 +}
21997 +
21998 +/* ---------------------------------------------------------------------- */
21999 +
22000 +/* no getattr version will be set by module.c:aufs_init() */
22001 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22002 +       aufs_iop[] = {
22003 +       [AuIop_SYMLINK] = {
22004 +               .permission     = aufs_permission,
22005 +#ifdef CONFIG_FS_POSIX_ACL
22006 +               .get_acl        = aufs_get_acl,
22007 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
22008 +#endif
22009 +
22010 +               .setattr        = aufs_setattr,
22011 +               .getattr        = aufs_getattr,
22012 +
22013 +#ifdef CONFIG_AUFS_XATTR
22014 +               .listxattr      = aufs_listxattr,
22015 +#endif
22016 +
22017 +               .get_link       = aufs_get_link,
22018 +
22019 +               /* .update_time = aufs_update_time */
22020 +       },
22021 +       [AuIop_DIR] = {
22022 +               .create         = aufs_create,
22023 +               .lookup         = aufs_lookup,
22024 +               .link           = aufs_link,
22025 +               .unlink         = aufs_unlink,
22026 +               .symlink        = aufs_symlink,
22027 +               .mkdir          = aufs_mkdir,
22028 +               .rmdir          = aufs_rmdir,
22029 +               .mknod          = aufs_mknod,
22030 +               .rename         = aufs_rename,
22031 +
22032 +               .permission     = aufs_permission,
22033 +#ifdef CONFIG_FS_POSIX_ACL
22034 +               .get_acl        = aufs_get_acl,
22035 +               .set_acl        = aufs_set_acl,
22036 +#endif
22037 +
22038 +               .setattr        = aufs_setattr,
22039 +               .getattr        = aufs_getattr,
22040 +
22041 +#ifdef CONFIG_AUFS_XATTR
22042 +               .listxattr      = aufs_listxattr,
22043 +#endif
22044 +
22045 +               .update_time    = aufs_update_time,
22046 +               .atomic_open    = aufs_atomic_open,
22047 +               .tmpfile        = aufs_tmpfile
22048 +       },
22049 +       [AuIop_OTHER] = {
22050 +               .permission     = aufs_permission,
22051 +#ifdef CONFIG_FS_POSIX_ACL
22052 +               .get_acl        = aufs_get_acl,
22053 +               .set_acl        = aufs_set_acl,
22054 +#endif
22055 +
22056 +               .setattr        = aufs_setattr,
22057 +               .getattr        = aufs_getattr,
22058 +
22059 +#ifdef CONFIG_AUFS_XATTR
22060 +               .listxattr      = aufs_listxattr,
22061 +#endif
22062 +
22063 +               .update_time    = aufs_update_time
22064 +       }
22065 +};
22066 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22067 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22068 +++ linux/fs/aufs/i_op_del.c    2020-01-27 10:57:18.172204883 +0100
22069 @@ -0,0 +1,513 @@
22070 +// SPDX-License-Identifier: GPL-2.0
22071 +/*
22072 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22073 + *
22074 + * This program, aufs is free software; you can redistribute it and/or modify
22075 + * it under the terms of the GNU General Public License as published by
22076 + * the Free Software Foundation; either version 2 of the License, or
22077 + * (at your option) any later version.
22078 + *
22079 + * This program is distributed in the hope that it will be useful,
22080 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22081 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22082 + * GNU General Public License for more details.
22083 + *
22084 + * You should have received a copy of the GNU General Public License
22085 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22086 + */
22087 +
22088 +/*
22089 + * inode operations (del entry)
22090 + */
22091 +
22092 +#include <linux/iversion.h>
22093 +#include "aufs.h"
22094 +
22095 +/*
22096 + * decide if a new whiteout for @dentry is necessary or not.
22097 + * when it is necessary, prepare the parent dir for the upper branch whose
22098 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22099 + * be done by caller.
22100 + * return value:
22101 + * 0: wh is unnecessary
22102 + * plus: wh is necessary
22103 + * minus: error
22104 + */
22105 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22106 +{
22107 +       int need_wh, err;
22108 +       aufs_bindex_t btop;
22109 +       struct super_block *sb;
22110 +
22111 +       sb = dentry->d_sb;
22112 +       btop = au_dbtop(dentry);
22113 +       if (*bcpup < 0) {
22114 +               *bcpup = btop;
22115 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22116 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22117 +                       *bcpup = err;
22118 +                       if (unlikely(err < 0))
22119 +                               goto out;
22120 +               }
22121 +       } else
22122 +               AuDebugOn(btop < *bcpup
22123 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22124 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22125 +
22126 +       if (*bcpup != btop) {
22127 +               err = au_cpup_dirs(dentry, *bcpup);
22128 +               if (unlikely(err))
22129 +                       goto out;
22130 +               need_wh = 1;
22131 +       } else {
22132 +               struct au_dinfo *dinfo, *tmp;
22133 +
22134 +               need_wh = -ENOMEM;
22135 +               dinfo = au_di(dentry);
22136 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22137 +               if (tmp) {
22138 +                       au_di_cp(tmp, dinfo);
22139 +                       au_di_swap(tmp, dinfo);
22140 +                       /* returns the number of positive dentries */
22141 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22142 +                                                /* AuLkup_IGNORE_PERM */ 0);
22143 +                       au_di_swap(tmp, dinfo);
22144 +                       au_rw_write_unlock(&tmp->di_rwsem);
22145 +                       au_di_free(tmp);
22146 +               }
22147 +       }
22148 +       AuDbg("need_wh %d\n", need_wh);
22149 +       err = need_wh;
22150 +
22151 +out:
22152 +       return err;
22153 +}
22154 +
22155 +/*
22156 + * simple tests for the del-entry operations.
22157 + * following the checks in vfs, plus the parent-child relationship.
22158 + */
22159 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22160 +              struct dentry *h_parent, int isdir)
22161 +{
22162 +       int err;
22163 +       umode_t h_mode;
22164 +       struct dentry *h_dentry, *h_latest;
22165 +       struct inode *h_inode;
22166 +
22167 +       h_dentry = au_h_dptr(dentry, bindex);
22168 +       if (d_really_is_positive(dentry)) {
22169 +               err = -ENOENT;
22170 +               if (unlikely(d_is_negative(h_dentry)))
22171 +                       goto out;
22172 +               h_inode = d_inode(h_dentry);
22173 +               if (unlikely(!h_inode->i_nlink))
22174 +                       goto out;
22175 +
22176 +               h_mode = h_inode->i_mode;
22177 +               if (!isdir) {
22178 +                       err = -EISDIR;
22179 +                       if (unlikely(S_ISDIR(h_mode)))
22180 +                               goto out;
22181 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22182 +                       err = -ENOTDIR;
22183 +                       goto out;
22184 +               }
22185 +       } else {
22186 +               /* rename(2) case */
22187 +               err = -EIO;
22188 +               if (unlikely(d_is_positive(h_dentry)))
22189 +                       goto out;
22190 +       }
22191 +
22192 +       err = -ENOENT;
22193 +       /* expected parent dir is locked */
22194 +       if (unlikely(h_parent != h_dentry->d_parent))
22195 +               goto out;
22196 +       err = 0;
22197 +
22198 +       /*
22199 +        * rmdir a dir may break the consistency on some filesystem.
22200 +        * let's try heavy test.
22201 +        */
22202 +       err = -EACCES;
22203 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22204 +                    && au_test_h_perm(d_inode(h_parent),
22205 +                                      MAY_EXEC | MAY_WRITE)))
22206 +               goto out;
22207 +
22208 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22209 +       err = -EIO;
22210 +       if (IS_ERR(h_latest))
22211 +               goto out;
22212 +       if (h_latest == h_dentry)
22213 +               err = 0;
22214 +       dput(h_latest);
22215 +
22216 +out:
22217 +       return err;
22218 +}
22219 +
22220 +/*
22221 + * decide the branch where we operate for @dentry. the branch index will be set
22222 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22223 + * dir for reverting.
22224 + * when a new whiteout is necessary, create it.
22225 + */
22226 +static struct dentry*
22227 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22228 +                   struct au_dtime *dt, struct au_pin *pin)
22229 +{
22230 +       struct dentry *wh_dentry;
22231 +       struct super_block *sb;
22232 +       struct path h_path;
22233 +       int err, need_wh;
22234 +       unsigned int udba;
22235 +       aufs_bindex_t bcpup;
22236 +
22237 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22238 +       wh_dentry = ERR_PTR(need_wh);
22239 +       if (unlikely(need_wh < 0))
22240 +               goto out;
22241 +
22242 +       sb = dentry->d_sb;
22243 +       udba = au_opt_udba(sb);
22244 +       bcpup = *rbcpup;
22245 +       err = au_pin(pin, dentry, bcpup, udba,
22246 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22247 +       wh_dentry = ERR_PTR(err);
22248 +       if (unlikely(err))
22249 +               goto out;
22250 +
22251 +       h_path.dentry = au_pinned_h_parent(pin);
22252 +       if (udba != AuOpt_UDBA_NONE
22253 +           && au_dbtop(dentry) == bcpup) {
22254 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22255 +               wh_dentry = ERR_PTR(err);
22256 +               if (unlikely(err))
22257 +                       goto out_unpin;
22258 +       }
22259 +
22260 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22261 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22262 +       wh_dentry = NULL;
22263 +       if (!need_wh)
22264 +               goto out; /* success, no need to create whiteout */
22265 +
22266 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22267 +       if (IS_ERR(wh_dentry))
22268 +               goto out_unpin;
22269 +
22270 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22271 +       goto out; /* success */
22272 +
22273 +out_unpin:
22274 +       au_unpin(pin);
22275 +out:
22276 +       return wh_dentry;
22277 +}
22278 +
22279 +/*
22280 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22281 + * in order to be revertible and save time for removing many child whiteouts
22282 + * under the dir.
22283 + * returns 1 when there are too many child whiteout and caller should remove
22284 + * them asynchronously. returns 0 when the number of children is enough small to
22285 + * remove now or the branch fs is a remote fs.
22286 + * otherwise return an error.
22287 + */
22288 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22289 +                          struct au_nhash *whlist, struct inode *dir)
22290 +{
22291 +       int rmdir_later, err, dirwh;
22292 +       struct dentry *h_dentry;
22293 +       struct super_block *sb;
22294 +       struct inode *inode;
22295 +
22296 +       sb = dentry->d_sb;
22297 +       SiMustAnyLock(sb);
22298 +       h_dentry = au_h_dptr(dentry, bindex);
22299 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22300 +       if (unlikely(err))
22301 +               goto out;
22302 +
22303 +       /* stop monitoring */
22304 +       inode = d_inode(dentry);
22305 +       au_hn_free(au_hi(inode, bindex));
22306 +
22307 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22308 +               dirwh = au_sbi(sb)->si_dirwh;
22309 +               rmdir_later = (dirwh <= 1);
22310 +               if (!rmdir_later)
22311 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22312 +                                                             dirwh);
22313 +               if (rmdir_later)
22314 +                       return rmdir_later;
22315 +       }
22316 +
22317 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22318 +       if (unlikely(err)) {
22319 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22320 +                       h_dentry, bindex, err);
22321 +               err = 0;
22322 +       }
22323 +
22324 +out:
22325 +       AuTraceErr(err);
22326 +       return err;
22327 +}
22328 +
22329 +/*
22330 + * final procedure for deleting a entry.
22331 + * maintain dentry and iattr.
22332 + */
22333 +static void epilog(struct inode *dir, struct dentry *dentry,
22334 +                  aufs_bindex_t bindex)
22335 +{
22336 +       struct inode *inode;
22337 +
22338 +       inode = d_inode(dentry);
22339 +       d_drop(dentry);
22340 +       inode->i_ctime = dir->i_ctime;
22341 +
22342 +       au_dir_ts(dir, bindex);
22343 +       inode_inc_iversion(dir);
22344 +}
22345 +
22346 +/*
22347 + * when an error happened, remove the created whiteout and revert everything.
22348 + */
22349 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22350 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22351 +                    struct dentry *dentry, struct au_dtime *dt)
22352 +{
22353 +       int rerr;
22354 +       struct path h_path = {
22355 +               .dentry = wh_dentry,
22356 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22357 +       };
22358 +
22359 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22360 +       if (!rerr) {
22361 +               au_set_dbwh(dentry, bwh);
22362 +               au_dtime_revert(dt);
22363 +               return 0;
22364 +       }
22365 +
22366 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22367 +       return -EIO;
22368 +}
22369 +
22370 +/* ---------------------------------------------------------------------- */
22371 +
22372 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22373 +{
22374 +       int err;
22375 +       aufs_bindex_t bwh, bindex, btop;
22376 +       struct inode *inode, *h_dir, *delegated;
22377 +       struct dentry *parent, *wh_dentry;
22378 +       /* to reduce stack size */
22379 +       struct {
22380 +               struct au_dtime dt;
22381 +               struct au_pin pin;
22382 +               struct path h_path;
22383 +       } *a;
22384 +
22385 +       IMustLock(dir);
22386 +
22387 +       err = -ENOMEM;
22388 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22389 +       if (unlikely(!a))
22390 +               goto out;
22391 +
22392 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22393 +       if (unlikely(err))
22394 +               goto out_free;
22395 +       err = au_d_hashed_positive(dentry);
22396 +       if (unlikely(err))
22397 +               goto out_unlock;
22398 +       inode = d_inode(dentry);
22399 +       IMustLock(inode);
22400 +       err = -EISDIR;
22401 +       if (unlikely(d_is_dir(dentry)))
22402 +               goto out_unlock; /* possible? */
22403 +
22404 +       btop = au_dbtop(dentry);
22405 +       bwh = au_dbwh(dentry);
22406 +       bindex = -1;
22407 +       parent = dentry->d_parent; /* dir inode is locked */
22408 +       di_write_lock_parent(parent);
22409 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22410 +                                       &a->pin);
22411 +       err = PTR_ERR(wh_dentry);
22412 +       if (IS_ERR(wh_dentry))
22413 +               goto out_parent;
22414 +
22415 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22416 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22417 +       dget(a->h_path.dentry);
22418 +       if (bindex == btop) {
22419 +               h_dir = au_pinned_h_dir(&a->pin);
22420 +               delegated = NULL;
22421 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22422 +               if (unlikely(err == -EWOULDBLOCK)) {
22423 +                       pr_warn("cannot retry for NFSv4 delegation"
22424 +                               " for an internal unlink\n");
22425 +                       iput(delegated);
22426 +               }
22427 +       } else {
22428 +               /* dir inode is locked */
22429 +               h_dir = d_inode(wh_dentry->d_parent);
22430 +               IMustLock(h_dir);
22431 +               err = 0;
22432 +       }
22433 +
22434 +       if (!err) {
22435 +               vfsub_drop_nlink(inode);
22436 +               epilog(dir, dentry, bindex);
22437 +
22438 +               /* update target timestamps */
22439 +               if (bindex == btop) {
22440 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22441 +                       /*ignore*/
22442 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22443 +               } else
22444 +                       /* todo: this timestamp may be reverted later */
22445 +                       inode->i_ctime = h_dir->i_ctime;
22446 +               goto out_unpin; /* success */
22447 +       }
22448 +
22449 +       /* revert */
22450 +       if (wh_dentry) {
22451 +               int rerr;
22452 +
22453 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22454 +                                &a->dt);
22455 +               if (rerr)
22456 +                       err = rerr;
22457 +       }
22458 +
22459 +out_unpin:
22460 +       au_unpin(&a->pin);
22461 +       dput(wh_dentry);
22462 +       dput(a->h_path.dentry);
22463 +out_parent:
22464 +       di_write_unlock(parent);
22465 +out_unlock:
22466 +       aufs_read_unlock(dentry, AuLock_DW);
22467 +out_free:
22468 +       au_kfree_rcu(a);
22469 +out:
22470 +       return err;
22471 +}
22472 +
22473 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22474 +{
22475 +       int err, rmdir_later;
22476 +       aufs_bindex_t bwh, bindex, btop;
22477 +       struct inode *inode;
22478 +       struct dentry *parent, *wh_dentry, *h_dentry;
22479 +       struct au_whtmp_rmdir *args;
22480 +       /* to reduce stack size */
22481 +       struct {
22482 +               struct au_dtime dt;
22483 +               struct au_pin pin;
22484 +       } *a;
22485 +
22486 +       IMustLock(dir);
22487 +
22488 +       err = -ENOMEM;
22489 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22490 +       if (unlikely(!a))
22491 +               goto out;
22492 +
22493 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22494 +       if (unlikely(err))
22495 +               goto out_free;
22496 +       err = au_alive_dir(dentry);
22497 +       if (unlikely(err))
22498 +               goto out_unlock;
22499 +       inode = d_inode(dentry);
22500 +       IMustLock(inode);
22501 +       err = -ENOTDIR;
22502 +       if (unlikely(!d_is_dir(dentry)))
22503 +               goto out_unlock; /* possible? */
22504 +
22505 +       err = -ENOMEM;
22506 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22507 +       if (unlikely(!args))
22508 +               goto out_unlock;
22509 +
22510 +       parent = dentry->d_parent; /* dir inode is locked */
22511 +       di_write_lock_parent(parent);
22512 +       err = au_test_empty(dentry, &args->whlist);
22513 +       if (unlikely(err))
22514 +               goto out_parent;
22515 +
22516 +       btop = au_dbtop(dentry);
22517 +       bwh = au_dbwh(dentry);
22518 +       bindex = -1;
22519 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22520 +                                       &a->pin);
22521 +       err = PTR_ERR(wh_dentry);
22522 +       if (IS_ERR(wh_dentry))
22523 +               goto out_parent;
22524 +
22525 +       h_dentry = au_h_dptr(dentry, btop);
22526 +       dget(h_dentry);
22527 +       rmdir_later = 0;
22528 +       if (bindex == btop) {
22529 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22530 +               if (err > 0) {
22531 +                       rmdir_later = err;
22532 +                       err = 0;
22533 +               }
22534 +       } else {
22535 +               /* stop monitoring */
22536 +               au_hn_free(au_hi(inode, btop));
22537 +
22538 +               /* dir inode is locked */
22539 +               IMustLock(d_inode(wh_dentry->d_parent));
22540 +               err = 0;
22541 +       }
22542 +
22543 +       if (!err) {
22544 +               vfsub_dead_dir(inode);
22545 +               au_set_dbdiropq(dentry, -1);
22546 +               epilog(dir, dentry, bindex);
22547 +
22548 +               if (rmdir_later) {
22549 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22550 +                       args = NULL;
22551 +               }
22552 +
22553 +               goto out_unpin; /* success */
22554 +       }
22555 +
22556 +       /* revert */
22557 +       AuLabel(revert);
22558 +       if (wh_dentry) {
22559 +               int rerr;
22560 +
22561 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22562 +                                &a->dt);
22563 +               if (rerr)
22564 +                       err = rerr;
22565 +       }
22566 +
22567 +out_unpin:
22568 +       au_unpin(&a->pin);
22569 +       dput(wh_dentry);
22570 +       dput(h_dentry);
22571 +out_parent:
22572 +       di_write_unlock(parent);
22573 +       if (args)
22574 +               au_whtmp_rmdir_free(args);
22575 +out_unlock:
22576 +       aufs_read_unlock(dentry, AuLock_DW);
22577 +out_free:
22578 +       au_kfree_rcu(a);
22579 +out:
22580 +       AuTraceErr(err);
22581 +       return err;
22582 +}
22583 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22584 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22585 +++ linux/fs/aufs/i_op_ren.c    2020-01-27 10:57:18.172204883 +0100
22586 @@ -0,0 +1,1250 @@
22587 +// SPDX-License-Identifier: GPL-2.0
22588 +/*
22589 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22590 + *
22591 + * This program, aufs is free software; you can redistribute it and/or modify
22592 + * it under the terms of the GNU General Public License as published by
22593 + * the Free Software Foundation; either version 2 of the License, or
22594 + * (at your option) any later version.
22595 + *
22596 + * This program is distributed in the hope that it will be useful,
22597 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22598 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22599 + * GNU General Public License for more details.
22600 + *
22601 + * You should have received a copy of the GNU General Public License
22602 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22603 + */
22604 +
22605 +/*
22606 + * inode operation (rename entry)
22607 + * todo: this is crazy monster
22608 + */
22609 +
22610 +#include <linux/iversion.h>
22611 +#include "aufs.h"
22612 +
22613 +enum { AuSRC, AuDST, AuSrcDst };
22614 +enum { AuPARENT, AuCHILD, AuParentChild };
22615 +
22616 +#define AuRen_ISDIR_SRC                1
22617 +#define AuRen_ISDIR_DST                (1 << 1)
22618 +#define AuRen_ISSAMEDIR                (1 << 2)
22619 +#define AuRen_WHSRC            (1 << 3)
22620 +#define AuRen_WHDST            (1 << 4)
22621 +#define AuRen_MNT_WRITE                (1 << 5)
22622 +#define AuRen_DT_DSTDIR                (1 << 6)
22623 +#define AuRen_DIROPQ_SRC       (1 << 7)
22624 +#define AuRen_DIROPQ_DST       (1 << 8)
22625 +#define AuRen_DIRREN           (1 << 9)
22626 +#define AuRen_DROPPED_SRC      (1 << 10)
22627 +#define AuRen_DROPPED_DST      (1 << 11)
22628 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22629 +#define au_fset_ren(flags, name) \
22630 +       do { (flags) |= AuRen_##name; } while (0)
22631 +#define au_fclr_ren(flags, name) \
22632 +       do { (flags) &= ~AuRen_##name; } while (0)
22633 +
22634 +#ifndef CONFIG_AUFS_DIRREN
22635 +#undef AuRen_DIRREN
22636 +#define AuRen_DIRREN           0
22637 +#endif
22638 +
22639 +struct au_ren_args {
22640 +       struct {
22641 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22642 +                       *wh_dentry;
22643 +               struct inode *dir, *inode;
22644 +               struct au_hinode *hdir, *hinode;
22645 +               struct au_dtime dt[AuParentChild];
22646 +               aufs_bindex_t btop, bdiropq;
22647 +       } sd[AuSrcDst];
22648 +
22649 +#define src_dentry     sd[AuSRC].dentry
22650 +#define src_dir                sd[AuSRC].dir
22651 +#define src_inode      sd[AuSRC].inode
22652 +#define src_h_dentry   sd[AuSRC].h_dentry
22653 +#define src_parent     sd[AuSRC].parent
22654 +#define src_h_parent   sd[AuSRC].h_parent
22655 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22656 +#define src_hdir       sd[AuSRC].hdir
22657 +#define src_hinode     sd[AuSRC].hinode
22658 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22659 +#define src_dt         sd[AuSRC].dt
22660 +#define src_btop       sd[AuSRC].btop
22661 +#define src_bdiropq    sd[AuSRC].bdiropq
22662 +
22663 +#define dst_dentry     sd[AuDST].dentry
22664 +#define dst_dir                sd[AuDST].dir
22665 +#define dst_inode      sd[AuDST].inode
22666 +#define dst_h_dentry   sd[AuDST].h_dentry
22667 +#define dst_parent     sd[AuDST].parent
22668 +#define dst_h_parent   sd[AuDST].h_parent
22669 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22670 +#define dst_hdir       sd[AuDST].hdir
22671 +#define dst_hinode     sd[AuDST].hinode
22672 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22673 +#define dst_dt         sd[AuDST].dt
22674 +#define dst_btop       sd[AuDST].btop
22675 +#define dst_bdiropq    sd[AuDST].bdiropq
22676 +
22677 +       struct dentry *h_trap;
22678 +       struct au_branch *br;
22679 +       struct path h_path;
22680 +       struct au_nhash whlist;
22681 +       aufs_bindex_t btgt, src_bwh;
22682 +
22683 +       struct {
22684 +               unsigned short auren_flags;
22685 +               unsigned char flags;    /* syscall parameter */
22686 +               unsigned char exchange;
22687 +       } __packed;
22688 +
22689 +       struct au_whtmp_rmdir *thargs;
22690 +       struct dentry *h_dst;
22691 +       struct au_hinode *h_root;
22692 +};
22693 +
22694 +/* ---------------------------------------------------------------------- */
22695 +
22696 +/*
22697 + * functions for reverting.
22698 + * when an error happened in a single rename systemcall, we should revert
22699 + * everything as if nothing happened.
22700 + * we don't need to revert the copied-up/down the parent dir since they are
22701 + * harmless.
22702 + */
22703 +
22704 +#define RevertFailure(fmt, ...) do { \
22705 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22706 +               ##__VA_ARGS__, err, rerr); \
22707 +       err = -EIO; \
22708 +} while (0)
22709 +
22710 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22711 +{
22712 +       int rerr;
22713 +       struct dentry *d;
22714 +#define src_or_dst(member) a->sd[idx].member
22715 +
22716 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22717 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22718 +       rerr = au_diropq_remove(d, a->btgt);
22719 +       au_hn_inode_unlock(src_or_dst(hinode));
22720 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22721 +       if (rerr)
22722 +               RevertFailure("remove diropq %pd", d);
22723 +
22724 +#undef src_or_dst_
22725 +}
22726 +
22727 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22728 +{
22729 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22730 +               au_ren_do_rev_diropq(err, a, AuSRC);
22731 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22732 +               au_ren_do_rev_diropq(err, a, AuDST);
22733 +}
22734 +
22735 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22736 +{
22737 +       int rerr;
22738 +       struct inode *delegated;
22739 +
22740 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22741 +                                         a->src_h_parent);
22742 +       rerr = PTR_ERR(a->h_path.dentry);
22743 +       if (IS_ERR(a->h_path.dentry)) {
22744 +               RevertFailure("lkup one %pd", a->src_dentry);
22745 +               return;
22746 +       }
22747 +
22748 +       delegated = NULL;
22749 +       rerr = vfsub_rename(a->dst_h_dir,
22750 +                           au_h_dptr(a->src_dentry, a->btgt),
22751 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22752 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22753 +               pr_warn("cannot retry for NFSv4 delegation"
22754 +                       " for an internal rename\n");
22755 +               iput(delegated);
22756 +       }
22757 +       d_drop(a->h_path.dentry);
22758 +       dput(a->h_path.dentry);
22759 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22760 +       if (rerr)
22761 +               RevertFailure("rename %pd", a->src_dentry);
22762 +}
22763 +
22764 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22765 +{
22766 +       int rerr;
22767 +       struct inode *delegated;
22768 +
22769 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22770 +                                         a->dst_h_parent);
22771 +       rerr = PTR_ERR(a->h_path.dentry);
22772 +       if (IS_ERR(a->h_path.dentry)) {
22773 +               RevertFailure("lkup one %pd", a->dst_dentry);
22774 +               return;
22775 +       }
22776 +       if (d_is_positive(a->h_path.dentry)) {
22777 +               d_drop(a->h_path.dentry);
22778 +               dput(a->h_path.dentry);
22779 +               return;
22780 +       }
22781 +
22782 +       delegated = NULL;
22783 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22784 +                           &delegated, a->flags);
22785 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22786 +               pr_warn("cannot retry for NFSv4 delegation"
22787 +                       " for an internal rename\n");
22788 +               iput(delegated);
22789 +       }
22790 +       d_drop(a->h_path.dentry);
22791 +       dput(a->h_path.dentry);
22792 +       if (!rerr)
22793 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22794 +       else
22795 +               RevertFailure("rename %pd", a->h_dst);
22796 +}
22797 +
22798 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22799 +{
22800 +       int rerr;
22801 +
22802 +       a->h_path.dentry = a->src_wh_dentry;
22803 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22804 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22805 +       if (rerr)
22806 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22807 +}
22808 +#undef RevertFailure
22809 +
22810 +/* ---------------------------------------------------------------------- */
22811 +
22812 +/*
22813 + * when we have to copyup the renaming entry, do it with the rename-target name
22814 + * in order to minimize the cost (the later actual rename is unnecessary).
22815 + * otherwise rename it on the target branch.
22816 + */
22817 +static int au_ren_or_cpup(struct au_ren_args *a)
22818 +{
22819 +       int err;
22820 +       struct dentry *d;
22821 +       struct inode *delegated;
22822 +
22823 +       d = a->src_dentry;
22824 +       if (au_dbtop(d) == a->btgt) {
22825 +               a->h_path.dentry = a->dst_h_dentry;
22826 +               AuDebugOn(au_dbtop(d) != a->btgt);
22827 +               delegated = NULL;
22828 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22829 +                                  a->dst_h_dir, &a->h_path, &delegated,
22830 +                                  a->flags);
22831 +               if (unlikely(err == -EWOULDBLOCK)) {
22832 +                       pr_warn("cannot retry for NFSv4 delegation"
22833 +                               " for an internal rename\n");
22834 +                       iput(delegated);
22835 +               }
22836 +       } else
22837 +               BUG();
22838 +
22839 +       if (!err && a->h_dst)
22840 +               /* it will be set to dinfo later */
22841 +               dget(a->h_dst);
22842 +
22843 +       return err;
22844 +}
22845 +
22846 +/* cf. aufs_rmdir() */
22847 +static int au_ren_del_whtmp(struct au_ren_args *a)
22848 +{
22849 +       int err;
22850 +       struct inode *dir;
22851 +
22852 +       dir = a->dst_dir;
22853 +       SiMustAnyLock(dir->i_sb);
22854 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22855 +                                    au_sbi(dir->i_sb)->si_dirwh)
22856 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22857 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22858 +               if (unlikely(err))
22859 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22860 +                               "ignored.\n", a->h_dst, err);
22861 +       } else {
22862 +               au_nhash_wh_free(&a->thargs->whlist);
22863 +               a->thargs->whlist = a->whlist;
22864 +               a->whlist.nh_num = 0;
22865 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22866 +               dput(a->h_dst);
22867 +               a->thargs = NULL;
22868 +       }
22869 +
22870 +       return 0;
22871 +}
22872 +
22873 +/* make it 'opaque' dir. */
22874 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22875 +{
22876 +       int err;
22877 +       struct dentry *d, *diropq;
22878 +#define src_or_dst(member) a->sd[idx].member
22879 +
22880 +       err = 0;
22881 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22882 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22883 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22884 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22885 +       diropq = au_diropq_create(d, a->btgt);
22886 +       au_hn_inode_unlock(src_or_dst(hinode));
22887 +       if (IS_ERR(diropq))
22888 +               err = PTR_ERR(diropq);
22889 +       else
22890 +               dput(diropq);
22891 +
22892 +#undef src_or_dst_
22893 +       return err;
22894 +}
22895 +
22896 +static int au_ren_diropq(struct au_ren_args *a)
22897 +{
22898 +       int err;
22899 +       unsigned char always;
22900 +       struct dentry *d;
22901 +
22902 +       err = 0;
22903 +       d = a->dst_dentry; /* already renamed on the branch */
22904 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22905 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22906 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22907 +           && a->btgt != au_dbdiropq(a->src_dentry)
22908 +           && (a->dst_wh_dentry
22909 +               || a->btgt <= au_dbdiropq(d)
22910 +               /* hide the lower to keep xino */
22911 +               /* the lowers may not be a dir, but we hide them anyway */
22912 +               || a->btgt < au_dbbot(d)
22913 +               || always)) {
22914 +               AuDbg("here\n");
22915 +               err = au_ren_do_diropq(a, AuSRC);
22916 +               if (unlikely(err))
22917 +                       goto out;
22918 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22919 +       }
22920 +       if (!a->exchange)
22921 +               goto out; /* success */
22922 +
22923 +       d = a->src_dentry; /* already renamed on the branch */
22924 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22925 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22926 +           && (a->btgt < au_dbdiropq(d)
22927 +               || a->btgt < au_dbbot(d)
22928 +               || always)) {
22929 +               AuDbgDentry(a->src_dentry);
22930 +               AuDbgDentry(a->dst_dentry);
22931 +               err = au_ren_do_diropq(a, AuDST);
22932 +               if (unlikely(err))
22933 +                       goto out_rev_src;
22934 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22935 +       }
22936 +       goto out; /* success */
22937 +
22938 +out_rev_src:
22939 +       AuDbg("err %d, reverting src\n", err);
22940 +       au_ren_rev_diropq(err, a);
22941 +out:
22942 +       return err;
22943 +}
22944 +
22945 +static int do_rename(struct au_ren_args *a)
22946 +{
22947 +       int err;
22948 +       struct dentry *d, *h_d;
22949 +
22950 +       if (!a->exchange) {
22951 +               /* prepare workqueue args for asynchronous rmdir */
22952 +               h_d = a->dst_h_dentry;
22953 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22954 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22955 +                   && d_is_positive(h_d)) {
22956 +                       err = -ENOMEM;
22957 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22958 +                                                        GFP_NOFS);
22959 +                       if (unlikely(!a->thargs))
22960 +                               goto out;
22961 +                       a->h_dst = dget(h_d);
22962 +               }
22963 +
22964 +               /* create whiteout for src_dentry */
22965 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22966 +                       a->src_bwh = au_dbwh(a->src_dentry);
22967 +                       AuDebugOn(a->src_bwh >= 0);
22968 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22969 +                                                       a->src_h_parent);
22970 +                       err = PTR_ERR(a->src_wh_dentry);
22971 +                       if (IS_ERR(a->src_wh_dentry))
22972 +                               goto out_thargs;
22973 +               }
22974 +
22975 +               /* lookup whiteout for dentry */
22976 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22977 +                       h_d = au_wh_lkup(a->dst_h_parent,
22978 +                                        &a->dst_dentry->d_name, a->br);
22979 +                       err = PTR_ERR(h_d);
22980 +                       if (IS_ERR(h_d))
22981 +                               goto out_whsrc;
22982 +                       if (d_is_negative(h_d))
22983 +                               dput(h_d);
22984 +                       else
22985 +                               a->dst_wh_dentry = h_d;
22986 +               }
22987 +
22988 +               /* rename dentry to tmpwh */
22989 +               if (a->thargs) {
22990 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22991 +                       if (unlikely(err))
22992 +                               goto out_whdst;
22993 +
22994 +                       d = a->dst_dentry;
22995 +                       au_set_h_dptr(d, a->btgt, NULL);
22996 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22997 +                       if (unlikely(err))
22998 +                               goto out_whtmp;
22999 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
23000 +               }
23001 +       }
23002 +
23003 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
23004 +#if 0 /* debugging */
23005 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23006 +              && d_is_positive(a->dst_h_dentry)
23007 +              && a->src_btop != a->btgt);
23008 +#endif
23009 +
23010 +       /* rename by vfs_rename or cpup */
23011 +       err = au_ren_or_cpup(a);
23012 +       if (unlikely(err))
23013 +               /* leave the copied-up one */
23014 +               goto out_whtmp;
23015 +
23016 +       /* make dir opaque */
23017 +       err = au_ren_diropq(a);
23018 +       if (unlikely(err))
23019 +               goto out_rename;
23020 +
23021 +       /* update target timestamps */
23022 +       if (a->exchange) {
23023 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23024 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23025 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23026 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23027 +       }
23028 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
23029 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23030 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23031 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23032 +
23033 +       if (!a->exchange) {
23034 +               /* remove whiteout for dentry */
23035 +               if (a->dst_wh_dentry) {
23036 +                       a->h_path.dentry = a->dst_wh_dentry;
23037 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23038 +                                                 a->dst_dentry);
23039 +                       if (unlikely(err))
23040 +                               goto out_diropq;
23041 +               }
23042 +
23043 +               /* remove whtmp */
23044 +               if (a->thargs)
23045 +                       au_ren_del_whtmp(a); /* ignore this error */
23046 +
23047 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23048 +       }
23049 +       err = 0;
23050 +       goto out_success;
23051 +
23052 +out_diropq:
23053 +       au_ren_rev_diropq(err, a);
23054 +out_rename:
23055 +       au_ren_rev_rename(err, a);
23056 +       dput(a->h_dst);
23057 +out_whtmp:
23058 +       if (a->thargs)
23059 +               au_ren_rev_whtmp(err, a);
23060 +out_whdst:
23061 +       dput(a->dst_wh_dentry);
23062 +       a->dst_wh_dentry = NULL;
23063 +out_whsrc:
23064 +       if (a->src_wh_dentry)
23065 +               au_ren_rev_whsrc(err, a);
23066 +out_success:
23067 +       dput(a->src_wh_dentry);
23068 +       dput(a->dst_wh_dentry);
23069 +out_thargs:
23070 +       if (a->thargs) {
23071 +               dput(a->h_dst);
23072 +               au_whtmp_rmdir_free(a->thargs);
23073 +               a->thargs = NULL;
23074 +       }
23075 +out:
23076 +       return err;
23077 +}
23078 +
23079 +/* ---------------------------------------------------------------------- */
23080 +
23081 +/*
23082 + * test if @dentry dir can be rename destination or not.
23083 + * success means, it is a logically empty dir.
23084 + */
23085 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23086 +{
23087 +       return au_test_empty(dentry, whlist);
23088 +}
23089 +
23090 +/*
23091 + * test if @a->src_dentry dir can be rename source or not.
23092 + * if it can, return 0.
23093 + * success means,
23094 + * - it is a logically empty dir.
23095 + * - or, it exists on writable branch and has no children including whiteouts
23096 + *   on the lower branch unless DIRREN is on.
23097 + */
23098 +static int may_rename_srcdir(struct au_ren_args *a)
23099 +{
23100 +       int err;
23101 +       unsigned int rdhash;
23102 +       aufs_bindex_t btop, btgt;
23103 +       struct dentry *dentry;
23104 +       struct super_block *sb;
23105 +       struct au_sbinfo *sbinfo;
23106 +
23107 +       dentry = a->src_dentry;
23108 +       sb = dentry->d_sb;
23109 +       sbinfo = au_sbi(sb);
23110 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23111 +               au_fset_ren(a->auren_flags, DIRREN);
23112 +
23113 +       btgt = a->btgt;
23114 +       btop = au_dbtop(dentry);
23115 +       if (btop != btgt) {
23116 +               struct au_nhash whlist;
23117 +
23118 +               SiMustAnyLock(sb);
23119 +               rdhash = sbinfo->si_rdhash;
23120 +               if (!rdhash)
23121 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23122 +                                                          dentry));
23123 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23124 +               if (unlikely(err))
23125 +                       goto out;
23126 +               err = au_test_empty(dentry, &whlist);
23127 +               au_nhash_wh_free(&whlist);
23128 +               goto out;
23129 +       }
23130 +
23131 +       if (btop == au_dbtaildir(dentry))
23132 +               return 0; /* success */
23133 +
23134 +       err = au_test_empty_lower(dentry);
23135 +
23136 +out:
23137 +       if (err == -ENOTEMPTY) {
23138 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23139 +                       err = 0;
23140 +               } else {
23141 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23142 +                               "branches, is not supported\n");
23143 +                       err = -EXDEV;
23144 +               }
23145 +       }
23146 +       return err;
23147 +}
23148 +
23149 +/* side effect: sets whlist and h_dentry */
23150 +static int au_ren_may_dir(struct au_ren_args *a)
23151 +{
23152 +       int err;
23153 +       unsigned int rdhash;
23154 +       struct dentry *d;
23155 +
23156 +       d = a->dst_dentry;
23157 +       SiMustAnyLock(d->d_sb);
23158 +
23159 +       err = 0;
23160 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23161 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23162 +               if (!rdhash)
23163 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23164 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23165 +               if (unlikely(err))
23166 +                       goto out;
23167 +
23168 +               if (!a->exchange) {
23169 +                       au_set_dbtop(d, a->dst_btop);
23170 +                       err = may_rename_dstdir(d, &a->whlist);
23171 +                       au_set_dbtop(d, a->btgt);
23172 +               } else
23173 +                       err = may_rename_srcdir(a);
23174 +       }
23175 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23176 +       if (unlikely(err))
23177 +               goto out;
23178 +
23179 +       d = a->src_dentry;
23180 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23181 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23182 +               err = may_rename_srcdir(a);
23183 +               if (unlikely(err)) {
23184 +                       au_nhash_wh_free(&a->whlist);
23185 +                       a->whlist.nh_num = 0;
23186 +               }
23187 +       }
23188 +out:
23189 +       return err;
23190 +}
23191 +
23192 +/* ---------------------------------------------------------------------- */
23193 +
23194 +/*
23195 + * simple tests for rename.
23196 + * following the checks in vfs, plus the parent-child relationship.
23197 + */
23198 +static int au_may_ren(struct au_ren_args *a)
23199 +{
23200 +       int err, isdir;
23201 +       struct inode *h_inode;
23202 +
23203 +       if (a->src_btop == a->btgt) {
23204 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23205 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23206 +               if (unlikely(err))
23207 +                       goto out;
23208 +               err = -EINVAL;
23209 +               if (unlikely(a->src_h_dentry == a->h_trap))
23210 +                       goto out;
23211 +       }
23212 +
23213 +       err = 0;
23214 +       if (a->dst_btop != a->btgt)
23215 +               goto out;
23216 +
23217 +       err = -ENOTEMPTY;
23218 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23219 +               goto out;
23220 +
23221 +       err = -EIO;
23222 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23223 +       if (d_really_is_negative(a->dst_dentry)) {
23224 +               if (d_is_negative(a->dst_h_dentry))
23225 +                       err = au_may_add(a->dst_dentry, a->btgt,
23226 +                                        a->dst_h_parent, isdir);
23227 +       } else {
23228 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23229 +                       goto out;
23230 +               h_inode = d_inode(a->dst_h_dentry);
23231 +               if (h_inode->i_nlink)
23232 +                       err = au_may_del(a->dst_dentry, a->btgt,
23233 +                                        a->dst_h_parent, isdir);
23234 +       }
23235 +
23236 +out:
23237 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23238 +               err = -EIO;
23239 +       AuTraceErr(err);
23240 +       return err;
23241 +}
23242 +
23243 +/* ---------------------------------------------------------------------- */
23244 +
23245 +/*
23246 + * locking order
23247 + * (VFS)
23248 + * - src_dir and dir by lock_rename()
23249 + * - inode if exists
23250 + * (aufs)
23251 + * - lock all
23252 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23253 + *     + si_read_lock
23254 + *     + di_write_lock2_child()
23255 + *       + di_write_lock_child()
23256 + *        + ii_write_lock_child()
23257 + *       + di_write_lock_child2()
23258 + *        + ii_write_lock_child2()
23259 + *     + src_parent and parent
23260 + *       + di_write_lock_parent()
23261 + *        + ii_write_lock_parent()
23262 + *       + di_write_lock_parent2()
23263 + *        + ii_write_lock_parent2()
23264 + *   + lower src_dir and dir by vfsub_lock_rename()
23265 + *   + verify the every relationships between child and parent. if any
23266 + *     of them failed, unlock all and return -EBUSY.
23267 + */
23268 +static void au_ren_unlock(struct au_ren_args *a)
23269 +{
23270 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23271 +                           a->dst_h_parent, a->dst_hdir);
23272 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23273 +           && a->h_root)
23274 +               au_hn_inode_unlock(a->h_root);
23275 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23276 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23277 +}
23278 +
23279 +static int au_ren_lock(struct au_ren_args *a)
23280 +{
23281 +       int err;
23282 +       unsigned int udba;
23283 +
23284 +       err = 0;
23285 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23286 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23287 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23288 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23289 +
23290 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23291 +       if (unlikely(err))
23292 +               goto out;
23293 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23294 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23295 +               struct dentry *root;
23296 +               struct inode *dir;
23297 +
23298 +               /*
23299 +                * sbinfo is already locked, so this ii_read_lock is
23300 +                * unnecessary. but our debugging feature checks it.
23301 +                */
23302 +               root = a->src_inode->i_sb->s_root;
23303 +               if (root != a->src_parent && root != a->dst_parent) {
23304 +                       dir = d_inode(root);
23305 +                       ii_read_lock_parent3(dir);
23306 +                       a->h_root = au_hi(dir, a->btgt);
23307 +                       ii_read_unlock(dir);
23308 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23309 +               }
23310 +       }
23311 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23312 +                                     a->dst_h_parent, a->dst_hdir);
23313 +       udba = au_opt_udba(a->src_dentry->d_sb);
23314 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23315 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23316 +               err = au_busy_or_stale();
23317 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23318 +               err = au_h_verify(a->src_h_dentry, udba,
23319 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23320 +                                 a->br);
23321 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23322 +               err = au_h_verify(a->dst_h_dentry, udba,
23323 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23324 +                                 a->br);
23325 +       if (!err)
23326 +               goto out; /* success */
23327 +
23328 +       err = au_busy_or_stale();
23329 +       au_ren_unlock(a);
23330 +
23331 +out:
23332 +       return err;
23333 +}
23334 +
23335 +/* ---------------------------------------------------------------------- */
23336 +
23337 +static void au_ren_refresh_dir(struct au_ren_args *a)
23338 +{
23339 +       struct inode *dir;
23340 +
23341 +       dir = a->dst_dir;
23342 +       inode_inc_iversion(dir);
23343 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23344 +               /* is this updating defined in POSIX? */
23345 +               au_cpup_attr_timesizes(a->src_inode);
23346 +               au_cpup_attr_nlink(dir, /*force*/1);
23347 +       }
23348 +       au_dir_ts(dir, a->btgt);
23349 +
23350 +       if (a->exchange) {
23351 +               dir = a->src_dir;
23352 +               inode_inc_iversion(dir);
23353 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23354 +                       /* is this updating defined in POSIX? */
23355 +                       au_cpup_attr_timesizes(a->dst_inode);
23356 +                       au_cpup_attr_nlink(dir, /*force*/1);
23357 +               }
23358 +               au_dir_ts(dir, a->btgt);
23359 +       }
23360 +
23361 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23362 +               return;
23363 +
23364 +       dir = a->src_dir;
23365 +       inode_inc_iversion(dir);
23366 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23367 +               au_cpup_attr_nlink(dir, /*force*/1);
23368 +       au_dir_ts(dir, a->btgt);
23369 +}
23370 +
23371 +static void au_ren_refresh(struct au_ren_args *a)
23372 +{
23373 +       aufs_bindex_t bbot, bindex;
23374 +       struct dentry *d, *h_d;
23375 +       struct inode *i, *h_i;
23376 +       struct super_block *sb;
23377 +
23378 +       d = a->dst_dentry;
23379 +       d_drop(d);
23380 +       if (a->h_dst)
23381 +               /* already dget-ed by au_ren_or_cpup() */
23382 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23383 +
23384 +       i = a->dst_inode;
23385 +       if (i) {
23386 +               if (!a->exchange) {
23387 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23388 +                               vfsub_drop_nlink(i);
23389 +                       else {
23390 +                               vfsub_dead_dir(i);
23391 +                               au_cpup_attr_timesizes(i);
23392 +                       }
23393 +                       au_update_dbrange(d, /*do_put_zero*/1);
23394 +               } else
23395 +                       au_cpup_attr_nlink(i, /*force*/1);
23396 +       } else {
23397 +               bbot = a->btgt;
23398 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23399 +                       au_set_h_dptr(d, bindex, NULL);
23400 +               bbot = au_dbbot(d);
23401 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23402 +                       au_set_h_dptr(d, bindex, NULL);
23403 +               au_update_dbrange(d, /*do_put_zero*/0);
23404 +       }
23405 +
23406 +       if (a->exchange
23407 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23408 +               d_drop(a->src_dentry);
23409 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23410 +                       au_set_dbwh(a->src_dentry, -1);
23411 +               return;
23412 +       }
23413 +
23414 +       d = a->src_dentry;
23415 +       au_set_dbwh(d, -1);
23416 +       bbot = au_dbbot(d);
23417 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23418 +               h_d = au_h_dptr(d, bindex);
23419 +               if (h_d)
23420 +                       au_set_h_dptr(d, bindex, NULL);
23421 +       }
23422 +       au_set_dbbot(d, a->btgt);
23423 +
23424 +       sb = d->d_sb;
23425 +       i = a->src_inode;
23426 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23427 +               return; /* success */
23428 +
23429 +       bbot = au_ibbot(i);
23430 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23431 +               h_i = au_h_iptr(i, bindex);
23432 +               if (h_i) {
23433 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23434 +                       /* ignore this error */
23435 +                       au_set_h_iptr(i, bindex, NULL, 0);
23436 +               }
23437 +       }
23438 +       au_set_ibbot(i, a->btgt);
23439 +}
23440 +
23441 +/* ---------------------------------------------------------------------- */
23442 +
23443 +/* mainly for link(2) and rename(2) */
23444 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23445 +{
23446 +       aufs_bindex_t bdiropq, bwh;
23447 +       struct dentry *parent;
23448 +       struct au_branch *br;
23449 +
23450 +       parent = dentry->d_parent;
23451 +       IMustLock(d_inode(parent)); /* dir is locked */
23452 +
23453 +       bdiropq = au_dbdiropq(parent);
23454 +       bwh = au_dbwh(dentry);
23455 +       br = au_sbr(dentry->d_sb, btgt);
23456 +       if (au_br_rdonly(br)
23457 +           || (0 <= bdiropq && bdiropq < btgt)
23458 +           || (0 <= bwh && bwh < btgt))
23459 +               btgt = -1;
23460 +
23461 +       AuDbg("btgt %d\n", btgt);
23462 +       return btgt;
23463 +}
23464 +
23465 +/* sets src_btop, dst_btop and btgt */
23466 +static int au_ren_wbr(struct au_ren_args *a)
23467 +{
23468 +       int err;
23469 +       struct au_wr_dir_args wr_dir_args = {
23470 +               /* .force_btgt  = -1, */
23471 +               .flags          = AuWrDir_ADD_ENTRY
23472 +       };
23473 +
23474 +       a->src_btop = au_dbtop(a->src_dentry);
23475 +       a->dst_btop = au_dbtop(a->dst_dentry);
23476 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23477 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23478 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23479 +       wr_dir_args.force_btgt = a->src_btop;
23480 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23481 +               wr_dir_args.force_btgt = a->dst_btop;
23482 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23483 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23484 +       a->btgt = err;
23485 +       if (a->exchange)
23486 +               au_update_dbtop(a->dst_dentry);
23487 +
23488 +       return err;
23489 +}
23490 +
23491 +static void au_ren_dt(struct au_ren_args *a)
23492 +{
23493 +       a->h_path.dentry = a->src_h_parent;
23494 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23495 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23496 +               a->h_path.dentry = a->dst_h_parent;
23497 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23498 +       }
23499 +
23500 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23501 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23502 +           && !a->exchange)
23503 +               return;
23504 +
23505 +       a->h_path.dentry = a->src_h_dentry;
23506 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23507 +       if (d_is_positive(a->dst_h_dentry)) {
23508 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23509 +               a->h_path.dentry = a->dst_h_dentry;
23510 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23511 +       }
23512 +}
23513 +
23514 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23515 +{
23516 +       struct dentry *h_d;
23517 +       struct inode *h_inode;
23518 +
23519 +       au_dtime_revert(a->src_dt + AuPARENT);
23520 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23521 +               au_dtime_revert(a->dst_dt + AuPARENT);
23522 +
23523 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23524 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23525 +               h_inode = d_inode(h_d);
23526 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23527 +               au_dtime_revert(a->src_dt + AuCHILD);
23528 +               inode_unlock(h_inode);
23529 +
23530 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23531 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23532 +                       h_inode = d_inode(h_d);
23533 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23534 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23535 +                       inode_unlock(h_inode);
23536 +               }
23537 +       }
23538 +}
23539 +
23540 +/* ---------------------------------------------------------------------- */
23541 +
23542 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23543 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23544 +               unsigned int _flags)
23545 +{
23546 +       int err, lock_flags;
23547 +       void *rev;
23548 +       /* reduce stack space */
23549 +       struct au_ren_args *a;
23550 +       struct au_pin pin;
23551 +
23552 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23553 +       IMustLock(_src_dir);
23554 +       IMustLock(_dst_dir);
23555 +
23556 +       err = -EINVAL;
23557 +       if (unlikely(_flags & RENAME_WHITEOUT))
23558 +               goto out;
23559 +
23560 +       err = -ENOMEM;
23561 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23562 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23563 +       if (unlikely(!a))
23564 +               goto out;
23565 +
23566 +       a->flags = _flags;
23567 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23568 +                    && RENAME_EXCHANGE > U8_MAX);
23569 +       a->exchange = _flags & RENAME_EXCHANGE;
23570 +       a->src_dir = _src_dir;
23571 +       a->src_dentry = _src_dentry;
23572 +       a->src_inode = NULL;
23573 +       if (d_really_is_positive(a->src_dentry))
23574 +               a->src_inode = d_inode(a->src_dentry);
23575 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23576 +       a->dst_dir = _dst_dir;
23577 +       a->dst_dentry = _dst_dentry;
23578 +       a->dst_inode = NULL;
23579 +       if (d_really_is_positive(a->dst_dentry))
23580 +               a->dst_inode = d_inode(a->dst_dentry);
23581 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23582 +       if (a->dst_inode) {
23583 +               /*
23584 +                * if EXCHANGE && src is non-dir && dst is dir,
23585 +                * dst is not locked.
23586 +                */
23587 +               /* IMustLock(a->dst_inode); */
23588 +               au_igrab(a->dst_inode);
23589 +       }
23590 +
23591 +       err = -ENOTDIR;
23592 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23593 +       if (d_is_dir(a->src_dentry)) {
23594 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23595 +               if (unlikely(!a->exchange
23596 +                            && d_really_is_positive(a->dst_dentry)
23597 +                            && !d_is_dir(a->dst_dentry)))
23598 +                       goto out_free;
23599 +               lock_flags |= AuLock_DIRS;
23600 +       }
23601 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23602 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23603 +               if (unlikely(!a->exchange
23604 +                            && d_really_is_positive(a->src_dentry)
23605 +                            && !d_is_dir(a->src_dentry)))
23606 +                       goto out_free;
23607 +               lock_flags |= AuLock_DIRS;
23608 +       }
23609 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23610 +                                       lock_flags);
23611 +       if (unlikely(err))
23612 +               goto out_free;
23613 +
23614 +       err = au_d_hashed_positive(a->src_dentry);
23615 +       if (unlikely(err))
23616 +               goto out_unlock;
23617 +       err = -ENOENT;
23618 +       if (a->dst_inode) {
23619 +               /*
23620 +                * If it is a dir, VFS unhash it before this
23621 +                * function. It means we cannot rely upon d_unhashed().
23622 +                */
23623 +               if (unlikely(!a->dst_inode->i_nlink))
23624 +                       goto out_unlock;
23625 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23626 +                       err = au_d_hashed_positive(a->dst_dentry);
23627 +                       if (unlikely(err && !a->exchange))
23628 +                               goto out_unlock;
23629 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23630 +                       goto out_unlock;
23631 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23632 +               goto out_unlock;
23633 +
23634 +       /*
23635 +        * is it possible?
23636 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23637 +        * there may exist a problem somewhere else.
23638 +        */
23639 +       err = -EINVAL;
23640 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23641 +               goto out_unlock;
23642 +
23643 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23644 +       di_write_lock_parent(a->dst_parent);
23645 +
23646 +       /* which branch we process */
23647 +       err = au_ren_wbr(a);
23648 +       if (unlikely(err < 0))
23649 +               goto out_parent;
23650 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23651 +       a->h_path.mnt = au_br_mnt(a->br);
23652 +
23653 +       /* are they available to be renamed */
23654 +       err = au_ren_may_dir(a);
23655 +       if (unlikely(err))
23656 +               goto out_children;
23657 +
23658 +       /* prepare the writable parent dir on the same branch */
23659 +       if (a->dst_btop == a->btgt) {
23660 +               au_fset_ren(a->auren_flags, WHDST);
23661 +       } else {
23662 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23663 +               if (unlikely(err))
23664 +                       goto out_children;
23665 +       }
23666 +
23667 +       err = 0;
23668 +       if (!a->exchange) {
23669 +               if (a->src_dir != a->dst_dir) {
23670 +                       /*
23671 +                        * this temporary unlock is safe,
23672 +                        * because both dir->i_mutex are locked.
23673 +                        */
23674 +                       di_write_unlock(a->dst_parent);
23675 +                       di_write_lock_parent(a->src_parent);
23676 +                       err = au_wr_dir_need_wh(a->src_dentry,
23677 +                                               au_ftest_ren(a->auren_flags,
23678 +                                                            ISDIR_SRC),
23679 +                                               &a->btgt);
23680 +                       di_write_unlock(a->src_parent);
23681 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23682 +                                             /*isdir*/1);
23683 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23684 +               } else
23685 +                       err = au_wr_dir_need_wh(a->src_dentry,
23686 +                                               au_ftest_ren(a->auren_flags,
23687 +                                                            ISDIR_SRC),
23688 +                                               &a->btgt);
23689 +       }
23690 +       if (unlikely(err < 0))
23691 +               goto out_children;
23692 +       if (err)
23693 +               au_fset_ren(a->auren_flags, WHSRC);
23694 +
23695 +       /* cpup src */
23696 +       if (a->src_btop != a->btgt) {
23697 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23698 +                            au_opt_udba(a->src_dentry->d_sb),
23699 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23700 +               if (!err) {
23701 +                       struct au_cp_generic cpg = {
23702 +                               .dentry = a->src_dentry,
23703 +                               .bdst   = a->btgt,
23704 +                               .bsrc   = a->src_btop,
23705 +                               .len    = -1,
23706 +                               .pin    = &pin,
23707 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23708 +                       };
23709 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23710 +                       err = au_sio_cpup_simple(&cpg);
23711 +                       au_unpin(&pin);
23712 +               }
23713 +               if (unlikely(err))
23714 +                       goto out_children;
23715 +               a->src_btop = a->btgt;
23716 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23717 +               if (!a->exchange)
23718 +                       au_fset_ren(a->auren_flags, WHSRC);
23719 +       }
23720 +
23721 +       /* cpup dst */
23722 +       if (a->exchange && a->dst_inode
23723 +           && a->dst_btop != a->btgt) {
23724 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23725 +                            au_opt_udba(a->dst_dentry->d_sb),
23726 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23727 +               if (!err) {
23728 +                       struct au_cp_generic cpg = {
23729 +                               .dentry = a->dst_dentry,
23730 +                               .bdst   = a->btgt,
23731 +                               .bsrc   = a->dst_btop,
23732 +                               .len    = -1,
23733 +                               .pin    = &pin,
23734 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23735 +                       };
23736 +                       err = au_sio_cpup_simple(&cpg);
23737 +                       au_unpin(&pin);
23738 +               }
23739 +               if (unlikely(err))
23740 +                       goto out_children;
23741 +               a->dst_btop = a->btgt;
23742 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23743 +       }
23744 +
23745 +       /* lock them all */
23746 +       err = au_ren_lock(a);
23747 +       if (unlikely(err))
23748 +               /* leave the copied-up one */
23749 +               goto out_children;
23750 +
23751 +       if (!a->exchange) {
23752 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23753 +                       err = au_may_ren(a);
23754 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23755 +                       err = -ENAMETOOLONG;
23756 +               if (unlikely(err))
23757 +                       goto out_hdir;
23758 +       }
23759 +
23760 +       /* store timestamps to be revertible */
23761 +       au_ren_dt(a);
23762 +
23763 +       /* store dirren info */
23764 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23765 +               err = au_dr_rename(a->src_dentry, a->btgt,
23766 +                                  &a->dst_dentry->d_name, &rev);
23767 +               AuTraceErr(err);
23768 +               if (unlikely(err))
23769 +                       goto out_dt;
23770 +       }
23771 +
23772 +       /* here we go */
23773 +       err = do_rename(a);
23774 +       if (unlikely(err))
23775 +               goto out_dirren;
23776 +
23777 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23778 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23779 +
23780 +       /* update dir attributes */
23781 +       au_ren_refresh_dir(a);
23782 +
23783 +       /* dput/iput all lower dentries */
23784 +       au_ren_refresh(a);
23785 +
23786 +       goto out_hdir; /* success */
23787 +
23788 +out_dirren:
23789 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23790 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23791 +out_dt:
23792 +       au_ren_rev_dt(err, a);
23793 +out_hdir:
23794 +       au_ren_unlock(a);
23795 +out_children:
23796 +       au_nhash_wh_free(&a->whlist);
23797 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23798 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23799 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23800 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23801 +       }
23802 +out_parent:
23803 +       if (!err) {
23804 +               if (d_unhashed(a->src_dentry))
23805 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23806 +               if (d_unhashed(a->dst_dentry))
23807 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23808 +               if (!a->exchange)
23809 +                       d_move(a->src_dentry, a->dst_dentry);
23810 +               else {
23811 +                       d_exchange(a->src_dentry, a->dst_dentry);
23812 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23813 +                               d_drop(a->dst_dentry);
23814 +               }
23815 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23816 +                       d_drop(a->src_dentry);
23817 +       } else {
23818 +               au_update_dbtop(a->dst_dentry);
23819 +               if (!a->dst_inode)
23820 +                       d_drop(a->dst_dentry);
23821 +       }
23822 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23823 +               di_write_unlock(a->dst_parent);
23824 +       else
23825 +               di_write_unlock2(a->src_parent, a->dst_parent);
23826 +out_unlock:
23827 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23828 +out_free:
23829 +       iput(a->dst_inode);
23830 +       if (a->thargs)
23831 +               au_whtmp_rmdir_free(a->thargs);
23832 +       au_kfree_rcu(a);
23833 +out:
23834 +       AuTraceErr(err);
23835 +       return err;
23836 +}
23837 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23838 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23839 +++ linux/fs/aufs/Kconfig       2019-07-11 15:42:14.458904362 +0200
23840 @@ -0,0 +1,199 @@
23841 +# SPDX-License-Identifier: GPL-2.0
23842 +config AUFS_FS
23843 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23844 +       help
23845 +       Aufs is a stackable unification filesystem such as Unionfs,
23846 +       which unifies several directories and provides a merged single
23847 +       directory.
23848 +       In the early days, aufs was entirely re-designed and
23849 +       re-implemented Unionfs Version 1.x series. Introducing many
23850 +       original ideas, approaches and improvements, it becomes totally
23851 +       different from Unionfs while keeping the basic features.
23852 +
23853 +if AUFS_FS
23854 +choice
23855 +       prompt "Maximum number of branches"
23856 +       default AUFS_BRANCH_MAX_127
23857 +       help
23858 +       Specifies the maximum number of branches (or member directories)
23859 +       in a single aufs. The larger value consumes more system
23860 +       resources and has a minor impact to performance.
23861 +config AUFS_BRANCH_MAX_127
23862 +       bool "127"
23863 +       help
23864 +       Specifies the maximum number of branches (or member directories)
23865 +       in a single aufs. The larger value consumes more system
23866 +       resources and has a minor impact to performance.
23867 +config AUFS_BRANCH_MAX_511
23868 +       bool "511"
23869 +       help
23870 +       Specifies the maximum number of branches (or member directories)
23871 +       in a single aufs. The larger value consumes more system
23872 +       resources and has a minor impact to performance.
23873 +config AUFS_BRANCH_MAX_1023
23874 +       bool "1023"
23875 +       help
23876 +       Specifies the maximum number of branches (or member directories)
23877 +       in a single aufs. The larger value consumes more system
23878 +       resources and has a minor impact to performance.
23879 +config AUFS_BRANCH_MAX_32767
23880 +       bool "32767"
23881 +       help
23882 +       Specifies the maximum number of branches (or member directories)
23883 +       in a single aufs. The larger value consumes more system
23884 +       resources and has a minor impact to performance.
23885 +endchoice
23886 +
23887 +config AUFS_SBILIST
23888 +       bool
23889 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23890 +       default y
23891 +       help
23892 +       Automatic configuration for internal use.
23893 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23894 +
23895 +config AUFS_HNOTIFY
23896 +       bool "Detect direct branch access (bypassing aufs)"
23897 +       help
23898 +       If you want to modify files on branches directly, eg. bypassing aufs,
23899 +       and want aufs to detect the changes of them fully, then enable this
23900 +       option and use 'udba=notify' mount option.
23901 +       Currently there is only one available configuration, "fsnotify".
23902 +       It will have a negative impact to the performance.
23903 +       See detail in aufs.5.
23904 +
23905 +choice
23906 +       prompt "method" if AUFS_HNOTIFY
23907 +       default AUFS_HFSNOTIFY
23908 +config AUFS_HFSNOTIFY
23909 +       bool "fsnotify"
23910 +       select FSNOTIFY
23911 +endchoice
23912 +
23913 +config AUFS_EXPORT
23914 +       bool "NFS-exportable aufs"
23915 +       depends on EXPORTFS
23916 +       help
23917 +       If you want to export your mounted aufs via NFS, then enable this
23918 +       option. There are several requirements for this configuration.
23919 +       See detail in aufs.5.
23920 +
23921 +config AUFS_INO_T_64
23922 +       bool
23923 +       depends on AUFS_EXPORT
23924 +       depends on 64BIT && !(ALPHA || S390)
23925 +       default y
23926 +       help
23927 +       Automatic configuration for internal use.
23928 +       /* typedef unsigned long/int __kernel_ino_t */
23929 +       /* alpha and s390x are int */
23930 +
23931 +config AUFS_XATTR
23932 +       bool "support for XATTR/EA (including Security Labels)"
23933 +       help
23934 +       If your branch fs supports XATTR/EA and you want to make them
23935 +       available in aufs too, then enable this opsion and specify the
23936 +       branch attributes for EA.
23937 +       See detail in aufs.5.
23938 +
23939 +config AUFS_FHSM
23940 +       bool "File-based Hierarchical Storage Management"
23941 +       help
23942 +       Hierarchical Storage Management (or HSM) is a well-known feature
23943 +       in the storage world. Aufs provides this feature as file-based.
23944 +       with multiple branches.
23945 +       These multiple branches are prioritized, ie. the topmost one
23946 +       should be the fastest drive and be used heavily.
23947 +
23948 +config AUFS_RDU
23949 +       bool "Readdir in userspace"
23950 +       help
23951 +       Aufs has two methods to provide a merged view for a directory,
23952 +       by a user-space library and by kernel-space natively. The latter
23953 +       is always enabled but sometimes large and slow.
23954 +       If you enable this option, install the library in aufs2-util
23955 +       package, and set some environment variables for your readdir(3),
23956 +       then the work will be handled in user-space which generally
23957 +       shows better performance in most cases.
23958 +       See detail in aufs.5.
23959 +
23960 +config AUFS_DIRREN
23961 +       bool "Workaround for rename(2)-ing a directory"
23962 +       help
23963 +       By default, aufs returns EXDEV error in renameing a dir who has
23964 +       his child on the lower branch, since it is a bad idea to issue
23965 +       rename(2) internally for every lower branch. But user may not
23966 +       accept this behaviour. So here is a workaround to allow such
23967 +       rename(2) and store some extra infromation on the writable
23968 +       branch. Obviously this costs high (and I don't like it).
23969 +       To use this feature, you need to enable this configuration AND
23970 +       to specify the mount option `dirren.'
23971 +       See details in aufs.5 and the design documents.
23972 +
23973 +config AUFS_SHWH
23974 +       bool "Show whiteouts"
23975 +       help
23976 +       If you want to make the whiteouts in aufs visible, then enable
23977 +       this option and specify 'shwh' mount option. Although it may
23978 +       sounds like philosophy or something, but in technically it
23979 +       simply shows the name of whiteout with keeping its behaviour.
23980 +
23981 +config AUFS_BR_RAMFS
23982 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23983 +       help
23984 +       If you want to use ramfs as an aufs branch fs, then enable this
23985 +       option. Generally tmpfs is recommended.
23986 +       Aufs prohibited them to be a branch fs by default, because
23987 +       initramfs becomes unusable after switch_root or something
23988 +       generally. If you sets initramfs as an aufs branch and boot your
23989 +       system by switch_root, you will meet a problem easily since the
23990 +       files in initramfs may be inaccessible.
23991 +       Unless you are going to use ramfs as an aufs branch fs without
23992 +       switch_root or something, leave it N.
23993 +
23994 +config AUFS_BR_FUSE
23995 +       bool "Fuse fs as an aufs branch"
23996 +       depends on FUSE_FS
23997 +       select AUFS_POLL
23998 +       help
23999 +       If you want to use fuse-based userspace filesystem as an aufs
24000 +       branch fs, then enable this option.
24001 +       It implements the internal poll(2) operation which is
24002 +       implemented by fuse only (curretnly).
24003 +
24004 +config AUFS_POLL
24005 +       bool
24006 +       help
24007 +       Automatic configuration for internal use.
24008 +
24009 +config AUFS_BR_HFSPLUS
24010 +       bool "Hfsplus as an aufs branch"
24011 +       depends on HFSPLUS_FS
24012 +       default y
24013 +       help
24014 +       If you want to use hfsplus fs as an aufs branch fs, then enable
24015 +       this option. This option introduces a small overhead at
24016 +       copying-up a file on hfsplus.
24017 +
24018 +config AUFS_BDEV_LOOP
24019 +       bool
24020 +       depends on BLK_DEV_LOOP
24021 +       default y
24022 +       help
24023 +       Automatic configuration for internal use.
24024 +       Convert =[ym] into =y.
24025 +
24026 +config AUFS_DEBUG
24027 +       bool "Debug aufs"
24028 +       help
24029 +       Enable this to compile aufs internal debug code.
24030 +       It will have a negative impact to the performance.
24031 +
24032 +config AUFS_MAGIC_SYSRQ
24033 +       bool
24034 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
24035 +       default y
24036 +       help
24037 +       Automatic configuration for internal use.
24038 +       When aufs supports Magic SysRq, enabled automatically.
24039 +endif
24040 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24041 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
24042 +++ linux/fs/aufs/lcnt.h        2020-01-27 10:57:18.175538316 +0100
24043 @@ -0,0 +1,186 @@
24044 +/* SPDX-License-Identifier: GPL-2.0 */
24045 +/*
24046 + * Copyright (C) 2018-2020 Junjiro R. Okajima
24047 + *
24048 + * This program, aufs is free software; you can redistribute it and/or modify
24049 + * it under the terms of the GNU General Public License as published by
24050 + * the Free Software Foundation; either version 2 of the License, or
24051 + * (at your option) any later version.
24052 + *
24053 + * This program is distributed in the hope that it will be useful,
24054 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24055 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24056 + * GNU General Public License for more details.
24057 + *
24058 + * You should have received a copy of the GNU General Public License
24059 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24060 + */
24061 +
24062 +/*
24063 + * simple long counter wrapper
24064 + */
24065 +
24066 +#ifndef __AUFS_LCNT_H__
24067 +#define __AUFS_LCNT_H__
24068 +
24069 +#ifdef __KERNEL__
24070 +
24071 +#include "debug.h"
24072 +
24073 +#define AuLCntATOMIC   1
24074 +#define AuLCntPCPUCNT  2
24075 +/*
24076 + * why does percpu_refcount require extra synchronize_rcu()s in
24077 + * au_br_do_free()
24078 + */
24079 +#define AuLCntPCPUREF  3
24080 +
24081 +/* #define AuLCntChosen        AuLCntATOMIC */
24082 +#define AuLCntChosen   AuLCntPCPUCNT
24083 +/* #define AuLCntChosen        AuLCntPCPUREF */
24084 +
24085 +#if AuLCntChosen == AuLCntATOMIC
24086 +#include <linux/atomic.h>
24087 +
24088 +typedef atomic_long_t au_lcnt_t;
24089 +
24090 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24091 +{
24092 +       atomic_long_set(cnt, 0);
24093 +       return 0;
24094 +}
24095 +
24096 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24097 +{
24098 +       /* empty */
24099 +}
24100 +
24101 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24102 +                              int do_sync __maybe_unused)
24103 +{
24104 +       /* empty */
24105 +}
24106 +
24107 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24108 +{
24109 +       atomic_long_inc(cnt);
24110 +}
24111 +
24112 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24113 +{
24114 +       atomic_long_dec(cnt);
24115 +}
24116 +
24117 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24118 +{
24119 +       return atomic_long_read(cnt);
24120 +}
24121 +#endif
24122 +
24123 +#if AuLCntChosen == AuLCntPCPUCNT
24124 +#include <linux/percpu_counter.h>
24125 +
24126 +typedef struct percpu_counter au_lcnt_t;
24127 +
24128 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24129 +{
24130 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24131 +}
24132 +
24133 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24134 +{
24135 +       /* empty */
24136 +}
24137 +
24138 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24139 +{
24140 +       percpu_counter_destroy(cnt);
24141 +}
24142 +
24143 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24144 +{
24145 +       percpu_counter_inc(cnt);
24146 +}
24147 +
24148 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24149 +{
24150 +       percpu_counter_dec(cnt);
24151 +}
24152 +
24153 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24154 +{
24155 +       s64 n;
24156 +
24157 +       n = percpu_counter_sum(cnt);
24158 +       BUG_ON(n < 0);
24159 +       if (LONG_MAX != LLONG_MAX
24160 +           && n > LONG_MAX)
24161 +               AuWarn1("%s\n", "wrap-around");
24162 +
24163 +       return n;
24164 +}
24165 +#endif
24166 +
24167 +#if AuLCntChosen == AuLCntPCPUREF
24168 +#include <linux/percpu-refcount.h>
24169 +
24170 +typedef struct percpu_ref au_lcnt_t;
24171 +
24172 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24173 +{
24174 +       if (!release)
24175 +               release = percpu_ref_exit;
24176 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24177 +}
24178 +
24179 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24180 +{
24181 +       synchronize_rcu();
24182 +}
24183 +
24184 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24185 +{
24186 +       percpu_ref_kill(cnt);
24187 +       if (do_sync)
24188 +               au_lcnt_wait_for_fin(cnt);
24189 +}
24190 +
24191 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24192 +{
24193 +       percpu_ref_get(cnt);
24194 +}
24195 +
24196 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24197 +{
24198 +       percpu_ref_put(cnt);
24199 +}
24200 +
24201 +/*
24202 + * avoid calling this func as possible.
24203 + */
24204 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24205 +{
24206 +       long l;
24207 +
24208 +       percpu_ref_switch_to_atomic_sync(cnt);
24209 +       l = atomic_long_read(&cnt->count);
24210 +       if (do_rev)
24211 +               percpu_ref_switch_to_percpu(cnt);
24212 +
24213 +       /* percpu_ref is initialized by 1 instead of 0 */
24214 +       return l - 1;
24215 +}
24216 +#endif
24217 +
24218 +#ifdef CONFIG_AUFS_DEBUG
24219 +#define AuLCntZero(val) do {                   \
24220 +       long l = val;                           \
24221 +       if (l)                                  \
24222 +               AuDbg("%s = %ld\n", #val, l);   \
24223 +} while (0)
24224 +#else
24225 +#define AuLCntZero(val)                do {} while (0)
24226 +#endif
24227 +
24228 +#endif /* __KERNEL__ */
24229 +#endif /* __AUFS_LCNT_H__ */
24230 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24231 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24232 +++ linux/fs/aufs/loop.c        2020-01-27 10:57:18.175538316 +0100
24233 @@ -0,0 +1,148 @@
24234 +// SPDX-License-Identifier: GPL-2.0
24235 +/*
24236 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24237 + *
24238 + * This program, aufs is free software; you can redistribute it and/or modify
24239 + * it under the terms of the GNU General Public License as published by
24240 + * the Free Software Foundation; either version 2 of the License, or
24241 + * (at your option) any later version.
24242 + *
24243 + * This program is distributed in the hope that it will be useful,
24244 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24245 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24246 + * GNU General Public License for more details.
24247 + *
24248 + * You should have received a copy of the GNU General Public License
24249 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24250 + */
24251 +
24252 +/*
24253 + * support for loopback block device as a branch
24254 + */
24255 +
24256 +#include "aufs.h"
24257 +
24258 +/* added into drivers/block/loop.c */
24259 +static struct file *(*backing_file_func)(struct super_block *sb);
24260 +
24261 +/*
24262 + * test if two lower dentries have overlapping branches.
24263 + */
24264 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24265 +{
24266 +       struct super_block *h_sb;
24267 +       struct file *backing_file;
24268 +
24269 +       if (unlikely(!backing_file_func)) {
24270 +               /* don't load "loop" module here */
24271 +               backing_file_func = symbol_get(loop_backing_file);
24272 +               if (unlikely(!backing_file_func))
24273 +                       /* "loop" module is not loaded */
24274 +                       return 0;
24275 +       }
24276 +
24277 +       h_sb = h_adding->d_sb;
24278 +       backing_file = backing_file_func(h_sb);
24279 +       if (!backing_file)
24280 +               return 0;
24281 +
24282 +       h_adding = backing_file->f_path.dentry;
24283 +       /*
24284 +        * h_adding can be local NFS.
24285 +        * in this case aufs cannot detect the loop.
24286 +        */
24287 +       if (unlikely(h_adding->d_sb == sb))
24288 +               return 1;
24289 +       return !!au_test_subdir(h_adding, sb->s_root);
24290 +}
24291 +
24292 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24293 +int au_test_loopback_kthread(void)
24294 +{
24295 +       int ret;
24296 +       struct task_struct *tsk = current;
24297 +       char c, comm[sizeof(tsk->comm)];
24298 +
24299 +       ret = 0;
24300 +       if (tsk->flags & PF_KTHREAD) {
24301 +               get_task_comm(comm, tsk);
24302 +               c = comm[4];
24303 +               ret = ('0' <= c && c <= '9'
24304 +                      && !strncmp(comm, "loop", 4));
24305 +       }
24306 +
24307 +       return ret;
24308 +}
24309 +
24310 +/* ---------------------------------------------------------------------- */
24311 +
24312 +#define au_warn_loopback_step  16
24313 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24314 +static unsigned long *au_warn_loopback_array;
24315 +
24316 +void au_warn_loopback(struct super_block *h_sb)
24317 +{
24318 +       int i, new_nelem;
24319 +       unsigned long *a, magic;
24320 +       static DEFINE_SPINLOCK(spin);
24321 +
24322 +       magic = h_sb->s_magic;
24323 +       spin_lock(&spin);
24324 +       a = au_warn_loopback_array;
24325 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24326 +               if (a[i] == magic) {
24327 +                       spin_unlock(&spin);
24328 +                       return;
24329 +               }
24330 +
24331 +       /* h_sb is new to us, print it */
24332 +       if (i < au_warn_loopback_nelem) {
24333 +               a[i] = magic;
24334 +               goto pr;
24335 +       }
24336 +
24337 +       /* expand the array */
24338 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24339 +       a = au_kzrealloc(au_warn_loopback_array,
24340 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24341 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24342 +                        /*may_shrink*/0);
24343 +       if (a) {
24344 +               au_warn_loopback_nelem = new_nelem;
24345 +               au_warn_loopback_array = a;
24346 +               a[i] = magic;
24347 +               goto pr;
24348 +       }
24349 +
24350 +       spin_unlock(&spin);
24351 +       AuWarn1("realloc failed, ignored\n");
24352 +       return;
24353 +
24354 +pr:
24355 +       spin_unlock(&spin);
24356 +       pr_warn("you may want to try another patch for loopback file "
24357 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24358 +}
24359 +
24360 +int au_loopback_init(void)
24361 +{
24362 +       int err;
24363 +       struct super_block *sb __maybe_unused;
24364 +
24365 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24366 +
24367 +       err = 0;
24368 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24369 +                                        sizeof(unsigned long), GFP_NOFS);
24370 +       if (unlikely(!au_warn_loopback_array))
24371 +               err = -ENOMEM;
24372 +
24373 +       return err;
24374 +}
24375 +
24376 +void au_loopback_fin(void)
24377 +{
24378 +       if (backing_file_func)
24379 +               symbol_put(loop_backing_file);
24380 +       au_kfree_try_rcu(au_warn_loopback_array);
24381 +}
24382 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24383 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24384 +++ linux/fs/aufs/loop.h        2020-01-27 10:57:18.175538316 +0100
24385 @@ -0,0 +1,55 @@
24386 +/* SPDX-License-Identifier: GPL-2.0 */
24387 +/*
24388 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24389 + *
24390 + * This program, aufs is free software; you can redistribute it and/or modify
24391 + * it under the terms of the GNU General Public License as published by
24392 + * the Free Software Foundation; either version 2 of the License, or
24393 + * (at your option) any later version.
24394 + *
24395 + * This program is distributed in the hope that it will be useful,
24396 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24397 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24398 + * GNU General Public License for more details.
24399 + *
24400 + * You should have received a copy of the GNU General Public License
24401 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24402 + */
24403 +
24404 +/*
24405 + * support for loopback mount as a branch
24406 + */
24407 +
24408 +#ifndef __AUFS_LOOP_H__
24409 +#define __AUFS_LOOP_H__
24410 +
24411 +#ifdef __KERNEL__
24412 +
24413 +struct dentry;
24414 +struct super_block;
24415 +
24416 +#ifdef CONFIG_AUFS_BDEV_LOOP
24417 +/* drivers/block/loop.c */
24418 +struct file *loop_backing_file(struct super_block *sb);
24419 +
24420 +/* loop.c */
24421 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24422 +int au_test_loopback_kthread(void);
24423 +void au_warn_loopback(struct super_block *h_sb);
24424 +
24425 +int au_loopback_init(void);
24426 +void au_loopback_fin(void);
24427 +#else
24428 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24429 +
24430 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24431 +          struct dentry *h_adding)
24432 +AuStubInt0(au_test_loopback_kthread, void)
24433 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24434 +
24435 +AuStubInt0(au_loopback_init, void)
24436 +AuStubVoid(au_loopback_fin, void)
24437 +#endif /* BLK_DEV_LOOP */
24438 +
24439 +#endif /* __KERNEL__ */
24440 +#endif /* __AUFS_LOOP_H__ */
24441 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24442 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24443 +++ linux/fs/aufs/magic.mk      2019-07-11 15:42:14.468904634 +0200
24444 @@ -0,0 +1,31 @@
24445 +# SPDX-License-Identifier: GPL-2.0
24446 +
24447 +# defined in ${srctree}/fs/fuse/inode.c
24448 +# tristate
24449 +ifdef CONFIG_FUSE_FS
24450 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24451 +endif
24452 +
24453 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24454 +# tristate
24455 +ifdef CONFIG_XFS_FS
24456 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24457 +endif
24458 +
24459 +# defined in ${srctree}/fs/configfs/mount.c
24460 +# tristate
24461 +ifdef CONFIG_CONFIGFS_FS
24462 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24463 +endif
24464 +
24465 +# defined in ${srctree}/fs/ubifs/ubifs.h
24466 +# tristate
24467 +ifdef CONFIG_UBIFS_FS
24468 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24469 +endif
24470 +
24471 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24472 +# tristate
24473 +ifdef CONFIG_HFSPLUS_FS
24474 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24475 +endif
24476 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24477 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24478 +++ linux/fs/aufs/Makefile      2019-07-11 15:42:14.462237786 +0200
24479 @@ -0,0 +1,46 @@
24480 +# SPDX-License-Identifier: GPL-2.0
24481 +
24482 +include ${src}/magic.mk
24483 +ifeq (${CONFIG_AUFS_FS},m)
24484 +include ${src}/conf.mk
24485 +endif
24486 +-include ${src}/priv_def.mk
24487 +
24488 +# cf. include/linux/kernel.h
24489 +# enable pr_debug
24490 +ccflags-y += -DDEBUG
24491 +# sparse requires the full pathname
24492 +ifdef M
24493 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24494 +else
24495 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24496 +endif
24497 +
24498 +obj-$(CONFIG_AUFS_FS) += aufs.o
24499 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24500 +       wkq.o vfsub.o dcsub.o \
24501 +       cpup.o whout.o wbr_policy.o \
24502 +       dinfo.o dentry.o \
24503 +       dynop.o \
24504 +       finfo.o file.o f_op.o \
24505 +       dir.o vdir.o \
24506 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24507 +       mvdown.o ioctl.o
24508 +
24509 +# all are boolean
24510 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24511 +aufs-$(CONFIG_SYSFS) += sysfs.o
24512 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24513 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24514 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24515 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24516 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24517 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24518 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24519 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24520 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24521 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24522 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24523 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24524 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24525 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24526 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24527 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24528 +++ linux/fs/aufs/module.c      2020-01-27 10:57:18.175538316 +0100
24529 @@ -0,0 +1,273 @@
24530 +// SPDX-License-Identifier: GPL-2.0
24531 +/*
24532 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24533 + *
24534 + * This program, aufs is free software; you can redistribute it and/or modify
24535 + * it under the terms of the GNU General Public License as published by
24536 + * the Free Software Foundation; either version 2 of the License, or
24537 + * (at your option) any later version.
24538 + *
24539 + * This program is distributed in the hope that it will be useful,
24540 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24541 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24542 + * GNU General Public License for more details.
24543 + *
24544 + * You should have received a copy of the GNU General Public License
24545 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24546 + */
24547 +
24548 +/*
24549 + * module global variables and operations
24550 + */
24551 +
24552 +#include <linux/module.h>
24553 +#include <linux/seq_file.h>
24554 +#include "aufs.h"
24555 +
24556 +/* shrinkable realloc */
24557 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24558 +{
24559 +       size_t sz;
24560 +       int diff;
24561 +
24562 +       sz = 0;
24563 +       diff = -1;
24564 +       if (p) {
24565 +#if 0 /* unused */
24566 +               if (!new_sz) {
24567 +                       au_kfree_rcu(p);
24568 +                       p = NULL;
24569 +                       goto out;
24570 +               }
24571 +#else
24572 +               AuDebugOn(!new_sz);
24573 +#endif
24574 +               sz = ksize(p);
24575 +               diff = au_kmidx_sub(sz, new_sz);
24576 +       }
24577 +       if (sz && !diff)
24578 +               goto out;
24579 +
24580 +       if (sz < new_sz)
24581 +               /* expand or SLOB */
24582 +               p = krealloc(p, new_sz, gfp);
24583 +       else if (new_sz < sz && may_shrink) {
24584 +               /* shrink */
24585 +               void *q;
24586 +
24587 +               q = kmalloc(new_sz, gfp);
24588 +               if (q) {
24589 +                       if (p) {
24590 +                               memcpy(q, p, new_sz);
24591 +                               au_kfree_try_rcu(p);
24592 +                       }
24593 +                       p = q;
24594 +               } else
24595 +                       p = NULL;
24596 +       }
24597 +
24598 +out:
24599 +       return p;
24600 +}
24601 +
24602 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24603 +                  int may_shrink)
24604 +{
24605 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24606 +       if (p && new_sz > nused)
24607 +               memset(p + nused, 0, new_sz - nused);
24608 +       return p;
24609 +}
24610 +
24611 +/* ---------------------------------------------------------------------- */
24612 +/*
24613 + * aufs caches
24614 + */
24615 +struct kmem_cache *au_cache[AuCache_Last];
24616 +
24617 +static void au_cache_fin(void)
24618 +{
24619 +       int i;
24620 +
24621 +       /*
24622 +        * Make sure all delayed rcu free inodes are flushed before we
24623 +        * destroy cache.
24624 +        */
24625 +       rcu_barrier();
24626 +
24627 +       /* excluding AuCache_HNOTIFY */
24628 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24629 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24630 +               kmem_cache_destroy(au_cache[i]);
24631 +               au_cache[i] = NULL;
24632 +       }
24633 +}
24634 +
24635 +static int __init au_cache_init(void)
24636 +{
24637 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24638 +       if (au_cache[AuCache_DINFO])
24639 +               /* SLAB_DESTROY_BY_RCU */
24640 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24641 +                                                      au_icntnr_init_once);
24642 +       if (au_cache[AuCache_ICNTNR])
24643 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24644 +                                                     au_fi_init_once);
24645 +       if (au_cache[AuCache_FINFO])
24646 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24647 +       if (au_cache[AuCache_VDIR])
24648 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24649 +       if (au_cache[AuCache_DEHSTR])
24650 +               return 0;
24651 +
24652 +       au_cache_fin();
24653 +       return -ENOMEM;
24654 +}
24655 +
24656 +/* ---------------------------------------------------------------------- */
24657 +
24658 +int au_dir_roflags;
24659 +
24660 +#ifdef CONFIG_AUFS_SBILIST
24661 +/*
24662 + * iterate_supers_type() doesn't protect us from
24663 + * remounting (branch management)
24664 + */
24665 +struct hlist_bl_head au_sbilist;
24666 +#endif
24667 +
24668 +/*
24669 + * functions for module interface.
24670 + */
24671 +MODULE_LICENSE("GPL");
24672 +/* MODULE_LICENSE("GPL v2"); */
24673 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24674 +MODULE_DESCRIPTION(AUFS_NAME
24675 +       " -- Advanced multi layered unification filesystem");
24676 +MODULE_VERSION(AUFS_VERSION);
24677 +MODULE_ALIAS_FS(AUFS_NAME);
24678 +
24679 +/* this module parameter has no meaning when SYSFS is disabled */
24680 +int sysaufs_brs = 1;
24681 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24682 +module_param_named(brs, sysaufs_brs, int, 0444);
24683 +
24684 +/* this module parameter has no meaning when USER_NS is disabled */
24685 +bool au_userns;
24686 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24687 +module_param_named(allow_userns, au_userns, bool, 0444);
24688 +
24689 +/* ---------------------------------------------------------------------- */
24690 +
24691 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24692 +
24693 +int au_seq_path(struct seq_file *seq, struct path *path)
24694 +{
24695 +       int err;
24696 +
24697 +       err = seq_path(seq, path, au_esc_chars);
24698 +       if (err >= 0)
24699 +               err = 0;
24700 +       else
24701 +               err = -ENOMEM;
24702 +
24703 +       return err;
24704 +}
24705 +
24706 +/* ---------------------------------------------------------------------- */
24707 +
24708 +static int __init aufs_init(void)
24709 +{
24710 +       int err, i;
24711 +       char *p;
24712 +
24713 +       p = au_esc_chars;
24714 +       for (i = 1; i <= ' '; i++)
24715 +               *p++ = i;
24716 +       *p++ = '\\';
24717 +       *p++ = '\x7f';
24718 +       *p = 0;
24719 +
24720 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24721 +
24722 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24723 +       for (i = 0; i < AuIop_Last; i++)
24724 +               aufs_iop_nogetattr[i].getattr = NULL;
24725 +
24726 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24727 +
24728 +       au_sbilist_init();
24729 +       sysaufs_brs_init();
24730 +       au_debug_init();
24731 +       au_dy_init();
24732 +       err = sysaufs_init();
24733 +       if (unlikely(err))
24734 +               goto out;
24735 +       err = dbgaufs_init();
24736 +       if (unlikely(err))
24737 +               goto out_sysaufs;
24738 +       err = au_procfs_init();
24739 +       if (unlikely(err))
24740 +               goto out_dbgaufs;
24741 +       err = au_wkq_init();
24742 +       if (unlikely(err))
24743 +               goto out_procfs;
24744 +       err = au_loopback_init();
24745 +       if (unlikely(err))
24746 +               goto out_wkq;
24747 +       err = au_hnotify_init();
24748 +       if (unlikely(err))
24749 +               goto out_loopback;
24750 +       err = au_sysrq_init();
24751 +       if (unlikely(err))
24752 +               goto out_hin;
24753 +       err = au_cache_init();
24754 +       if (unlikely(err))
24755 +               goto out_sysrq;
24756 +
24757 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24758 +       err = register_filesystem(&aufs_fs_type);
24759 +       if (unlikely(err))
24760 +               goto out_cache;
24761 +
24762 +       /* since we define pr_fmt, call printk directly */
24763 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24764 +       goto out; /* success */
24765 +
24766 +out_cache:
24767 +       au_cache_fin();
24768 +out_sysrq:
24769 +       au_sysrq_fin();
24770 +out_hin:
24771 +       au_hnotify_fin();
24772 +out_loopback:
24773 +       au_loopback_fin();
24774 +out_wkq:
24775 +       au_wkq_fin();
24776 +out_procfs:
24777 +       au_procfs_fin();
24778 +out_dbgaufs:
24779 +       dbgaufs_fin();
24780 +out_sysaufs:
24781 +       sysaufs_fin();
24782 +       au_dy_fin();
24783 +out:
24784 +       return err;
24785 +}
24786 +
24787 +static void __exit aufs_exit(void)
24788 +{
24789 +       unregister_filesystem(&aufs_fs_type);
24790 +       au_cache_fin();
24791 +       au_sysrq_fin();
24792 +       au_hnotify_fin();
24793 +       au_loopback_fin();
24794 +       au_wkq_fin();
24795 +       au_procfs_fin();
24796 +       dbgaufs_fin();
24797 +       sysaufs_fin();
24798 +       au_dy_fin();
24799 +}
24800 +
24801 +module_init(aufs_init);
24802 +module_exit(aufs_exit);
24803 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24804 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24805 +++ linux/fs/aufs/module.h      2020-01-27 10:57:18.175538316 +0100
24806 @@ -0,0 +1,166 @@
24807 +/* SPDX-License-Identifier: GPL-2.0 */
24808 +/*
24809 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24810 + *
24811 + * This program, aufs is free software; you can redistribute it and/or modify
24812 + * it under the terms of the GNU General Public License as published by
24813 + * the Free Software Foundation; either version 2 of the License, or
24814 + * (at your option) any later version.
24815 + *
24816 + * This program is distributed in the hope that it will be useful,
24817 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24818 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24819 + * GNU General Public License for more details.
24820 + *
24821 + * You should have received a copy of the GNU General Public License
24822 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24823 + */
24824 +
24825 +/*
24826 + * module initialization and module-global
24827 + */
24828 +
24829 +#ifndef __AUFS_MODULE_H__
24830 +#define __AUFS_MODULE_H__
24831 +
24832 +#ifdef __KERNEL__
24833 +
24834 +#include <linux/slab.h>
24835 +#include "debug.h"
24836 +#include "dentry.h"
24837 +#include "dir.h"
24838 +#include "file.h"
24839 +#include "inode.h"
24840 +
24841 +struct path;
24842 +struct seq_file;
24843 +
24844 +/* module parameters */
24845 +extern int sysaufs_brs;
24846 +extern bool au_userns;
24847 +
24848 +/* ---------------------------------------------------------------------- */
24849 +
24850 +extern int au_dir_roflags;
24851 +
24852 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24853 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24854 +                  int may_shrink);
24855 +
24856 +/*
24857 + * Comparing the size of the object with sizeof(struct rcu_head)
24858 + * case 1: object is always larger
24859 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24860 + * case 2: object is always smaller
24861 + *     --> au_kfree_small()
24862 + * case 3: object can be any size
24863 + *     --> au_kfree_try_rcu()
24864 + */
24865 +
24866 +static inline void au_kfree_do_rcu(const void *p)
24867 +{
24868 +       struct {
24869 +               struct rcu_head rcu;
24870 +       } *a = (void *)p;
24871 +
24872 +       kfree_rcu(a, rcu);
24873 +}
24874 +
24875 +#define au_kfree_rcu(_p) do {                                          \
24876 +               typeof(_p) p = (_p);                                    \
24877 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24878 +               if (p)                                                  \
24879 +                       au_kfree_do_rcu(p);                             \
24880 +       } while (0)
24881 +
24882 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24883 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24884 +
24885 +static inline void au_kfree_try_rcu(const void *p)
24886 +{
24887 +       if (!p)
24888 +               return;
24889 +       if (au_kfree_sz_test(p))
24890 +               au_kfree_do_rcu(p);
24891 +       else
24892 +               kfree(p);
24893 +}
24894 +
24895 +static inline void au_kfree_small(const void *p)
24896 +{
24897 +       if (!p)
24898 +               return;
24899 +       AuDebugOn(au_kfree_sz_test(p));
24900 +       kfree(p);
24901 +}
24902 +
24903 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24904 +{
24905 +#ifndef CONFIG_SLOB
24906 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24907 +#else
24908 +       return -1; /* SLOB is untested */
24909 +#endif
24910 +}
24911 +
24912 +int au_seq_path(struct seq_file *seq, struct path *path);
24913 +
24914 +#ifdef CONFIG_PROC_FS
24915 +/* procfs.c */
24916 +int __init au_procfs_init(void);
24917 +void au_procfs_fin(void);
24918 +#else
24919 +AuStubInt0(au_procfs_init, void);
24920 +AuStubVoid(au_procfs_fin, void);
24921 +#endif
24922 +
24923 +/* ---------------------------------------------------------------------- */
24924 +
24925 +/* kmem cache */
24926 +enum {
24927 +       AuCache_DINFO,
24928 +       AuCache_ICNTNR,
24929 +       AuCache_FINFO,
24930 +       AuCache_VDIR,
24931 +       AuCache_DEHSTR,
24932 +       AuCache_HNOTIFY, /* must be last */
24933 +       AuCache_Last
24934 +};
24935 +
24936 +extern struct kmem_cache *au_cache[AuCache_Last];
24937 +
24938 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24939 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24940 +#define AuCacheCtor(type, ctor)        \
24941 +       kmem_cache_create(#type, sizeof(struct type), \
24942 +                         __alignof__(struct type), AuCacheFlags, ctor)
24943 +
24944 +#define AuCacheFuncs(name, index)                                      \
24945 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24946 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24947 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24948 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24949 +                                                                       \
24950 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24951 +       { void *p = rcu;                                                \
24952 +               p -= offsetof(struct au_##name, rcu);                   \
24953 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24954 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24955 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24956 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24957 +                                                                       \
24958 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24959 +       { /* au_cache_free_##name##_norcu(p); */                        \
24960 +               au_cache_free_##name##_rcu(p); }
24961 +
24962 +AuCacheFuncs(dinfo, DINFO);
24963 +AuCacheFuncs(icntnr, ICNTNR);
24964 +AuCacheFuncs(finfo, FINFO);
24965 +AuCacheFuncs(vdir, VDIR);
24966 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24967 +#ifdef CONFIG_AUFS_HNOTIFY
24968 +AuCacheFuncs(hnotify, HNOTIFY);
24969 +#endif
24970 +
24971 +#endif /* __KERNEL__ */
24972 +#endif /* __AUFS_MODULE_H__ */
24973 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24974 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24975 +++ linux/fs/aufs/mvdown.c      2020-01-27 10:57:18.175538316 +0100
24976 @@ -0,0 +1,706 @@
24977 +// SPDX-License-Identifier: GPL-2.0
24978 +/*
24979 + * Copyright (C) 2011-2020 Junjiro R. Okajima
24980 + *
24981 + * This program, aufs is free software; you can redistribute it and/or modify
24982 + * it under the terms of the GNU General Public License as published by
24983 + * the Free Software Foundation; either version 2 of the License, or
24984 + * (at your option) any later version.
24985 + *
24986 + * This program is distributed in the hope that it will be useful,
24987 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24988 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24989 + * GNU General Public License for more details.
24990 + *
24991 + * You should have received a copy of the GNU General Public License
24992 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24993 + */
24994 +
24995 +/*
24996 + * move-down, opposite of copy-up
24997 + */
24998 +
24999 +#include "aufs.h"
25000 +
25001 +struct au_mvd_args {
25002 +       struct {
25003 +               struct super_block *h_sb;
25004 +               struct dentry *h_parent;
25005 +               struct au_hinode *hdir;
25006 +               struct inode *h_dir, *h_inode;
25007 +               struct au_pin pin;
25008 +       } info[AUFS_MVDOWN_NARRAY];
25009 +
25010 +       struct aufs_mvdown mvdown;
25011 +       struct dentry *dentry, *parent;
25012 +       struct inode *inode, *dir;
25013 +       struct super_block *sb;
25014 +       aufs_bindex_t bopq, bwh, bfound;
25015 +       unsigned char rename_lock;
25016 +};
25017 +
25018 +#define mvd_errno              mvdown.au_errno
25019 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25020 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25021 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25022 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
25023 +
25024 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
25025 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
25026 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
25027 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
25028 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
25029 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
25030 +
25031 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
25032 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
25033 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
25034 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
25035 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
25036 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
25037 +
25038 +#define AU_MVD_PR(flag, ...) do {                      \
25039 +               if (flag)                               \
25040 +                       pr_err(__VA_ARGS__);            \
25041 +       } while (0)
25042 +
25043 +static int find_lower_writable(struct au_mvd_args *a)
25044 +{
25045 +       struct super_block *sb;
25046 +       aufs_bindex_t bindex, bbot;
25047 +       struct au_branch *br;
25048 +
25049 +       sb = a->sb;
25050 +       bindex = a->mvd_bsrc;
25051 +       bbot = au_sbbot(sb);
25052 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
25053 +               for (bindex++; bindex <= bbot; bindex++) {
25054 +                       br = au_sbr(sb, bindex);
25055 +                       if (au_br_fhsm(br->br_perm)
25056 +                           && !sb_rdonly(au_br_sb(br)))
25057 +                               return bindex;
25058 +               }
25059 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
25060 +               for (bindex++; bindex <= bbot; bindex++) {
25061 +                       br = au_sbr(sb, bindex);
25062 +                       if (!au_br_rdonly(br))
25063 +                               return bindex;
25064 +               }
25065 +       else
25066 +               for (bindex++; bindex <= bbot; bindex++) {
25067 +                       br = au_sbr(sb, bindex);
25068 +                       if (!sb_rdonly(au_br_sb(br))) {
25069 +                               if (au_br_rdonly(br))
25070 +                                       a->mvdown.flags
25071 +                                               |= AUFS_MVDOWN_ROLOWER_R;
25072 +                               return bindex;
25073 +                       }
25074 +               }
25075 +
25076 +       return -1;
25077 +}
25078 +
25079 +/* make the parent dir on bdst */
25080 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25081 +{
25082 +       int err;
25083 +
25084 +       err = 0;
25085 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25086 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25087 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25088 +       a->mvd_h_dst_parent = NULL;
25089 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
25090 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25091 +       if (!a->mvd_h_dst_parent) {
25092 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25093 +               if (unlikely(err)) {
25094 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25095 +                       goto out;
25096 +               }
25097 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25098 +       }
25099 +
25100 +out:
25101 +       AuTraceErr(err);
25102 +       return err;
25103 +}
25104 +
25105 +/* lock them all */
25106 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25107 +{
25108 +       int err;
25109 +       struct dentry *h_trap;
25110 +
25111 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25112 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25113 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25114 +                    au_opt_udba(a->sb),
25115 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25116 +       AuTraceErr(err);
25117 +       if (unlikely(err)) {
25118 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25119 +               goto out;
25120 +       }
25121 +
25122 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25123 +               a->rename_lock = 0;
25124 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25125 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25126 +                           au_opt_udba(a->sb),
25127 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25128 +               err = au_do_pin(&a->mvd_pin_src);
25129 +               AuTraceErr(err);
25130 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25131 +               if (unlikely(err)) {
25132 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25133 +                       goto out_dst;
25134 +               }
25135 +               goto out; /* success */
25136 +       }
25137 +
25138 +       a->rename_lock = 1;
25139 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25140 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25141 +                    au_opt_udba(a->sb),
25142 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
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 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25148 +               goto out_dst;
25149 +       }
25150 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25151 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25152 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25153 +       if (h_trap) {
25154 +               err = (h_trap != a->mvd_h_src_parent);
25155 +               if (err)
25156 +                       err = (h_trap != a->mvd_h_dst_parent);
25157 +       }
25158 +       BUG_ON(err); /* it should never happen */
25159 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25160 +               err = -EBUSY;
25161 +               AuTraceErr(err);
25162 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25163 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25164 +               au_pin_hdir_lock(&a->mvd_pin_src);
25165 +               au_unpin(&a->mvd_pin_src);
25166 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25167 +               goto out_dst;
25168 +       }
25169 +       goto out; /* success */
25170 +
25171 +out_dst:
25172 +       au_unpin(&a->mvd_pin_dst);
25173 +out:
25174 +       AuTraceErr(err);
25175 +       return err;
25176 +}
25177 +
25178 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25179 +{
25180 +       if (!a->rename_lock)
25181 +               au_unpin(&a->mvd_pin_src);
25182 +       else {
25183 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25184 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25185 +               au_pin_hdir_lock(&a->mvd_pin_src);
25186 +               au_unpin(&a->mvd_pin_src);
25187 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25188 +       }
25189 +       au_unpin(&a->mvd_pin_dst);
25190 +}
25191 +
25192 +/* copy-down the file */
25193 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25194 +{
25195 +       int err;
25196 +       struct au_cp_generic cpg = {
25197 +               .dentry = a->dentry,
25198 +               .bdst   = a->mvd_bdst,
25199 +               .bsrc   = a->mvd_bsrc,
25200 +               .len    = -1,
25201 +               .pin    = &a->mvd_pin_dst,
25202 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25203 +       };
25204 +
25205 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25206 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25207 +               au_fset_cpup(cpg.flags, OVERWRITE);
25208 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25209 +               au_fset_cpup(cpg.flags, RWDST);
25210 +       err = au_sio_cpdown_simple(&cpg);
25211 +       if (unlikely(err))
25212 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25213 +
25214 +       AuTraceErr(err);
25215 +       return err;
25216 +}
25217 +
25218 +/*
25219 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25220 + * were sleeping
25221 + */
25222 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25223 +{
25224 +       int err;
25225 +       struct path h_path;
25226 +       struct au_branch *br;
25227 +       struct inode *delegated;
25228 +
25229 +       br = au_sbr(a->sb, a->mvd_bdst);
25230 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25231 +       err = PTR_ERR(h_path.dentry);
25232 +       if (IS_ERR(h_path.dentry)) {
25233 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25234 +               goto out;
25235 +       }
25236 +
25237 +       err = 0;
25238 +       if (d_is_positive(h_path.dentry)) {
25239 +               h_path.mnt = au_br_mnt(br);
25240 +               delegated = NULL;
25241 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25242 +                                  &delegated, /*force*/0);
25243 +               if (unlikely(err == -EWOULDBLOCK)) {
25244 +                       pr_warn("cannot retry for NFSv4 delegation"
25245 +                               " for an internal unlink\n");
25246 +                       iput(delegated);
25247 +               }
25248 +               if (unlikely(err))
25249 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25250 +       }
25251 +       dput(h_path.dentry);
25252 +
25253 +out:
25254 +       AuTraceErr(err);
25255 +       return err;
25256 +}
25257 +
25258 +/*
25259 + * unlink the topmost h_dentry
25260 + */
25261 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25262 +{
25263 +       int err;
25264 +       struct path h_path;
25265 +       struct inode *delegated;
25266 +
25267 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25268 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25269 +       delegated = NULL;
25270 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25271 +       if (unlikely(err == -EWOULDBLOCK)) {
25272 +               pr_warn("cannot retry for NFSv4 delegation"
25273 +                       " for an internal unlink\n");
25274 +               iput(delegated);
25275 +       }
25276 +       if (unlikely(err))
25277 +               AU_MVD_PR(dmsg, "unlink failed\n");
25278 +
25279 +       AuTraceErr(err);
25280 +       return err;
25281 +}
25282 +
25283 +/* Since mvdown succeeded, we ignore an error of this function */
25284 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25285 +{
25286 +       int err;
25287 +       struct au_branch *br;
25288 +
25289 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25290 +       br = au_sbr(a->sb, a->mvd_bsrc);
25291 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25292 +       if (!err) {
25293 +               br = au_sbr(a->sb, a->mvd_bdst);
25294 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25295 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25296 +       }
25297 +       if (!err)
25298 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25299 +       else
25300 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25301 +}
25302 +
25303 +/*
25304 + * copy-down the file and unlink the bsrc file.
25305 + * - unlink the bdst whout if exist
25306 + * - copy-down the file (with whtmp name and rename)
25307 + * - unlink the bsrc file
25308 + */
25309 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25310 +{
25311 +       int err;
25312 +
25313 +       err = au_do_mkdir(dmsg, a);
25314 +       if (!err)
25315 +               err = au_do_lock(dmsg, a);
25316 +       if (unlikely(err))
25317 +               goto out;
25318 +
25319 +       /*
25320 +        * do not revert the activities we made on bdst since they should be
25321 +        * harmless in aufs.
25322 +        */
25323 +
25324 +       err = au_do_cpdown(dmsg, a);
25325 +       if (!err)
25326 +               err = au_do_unlink_wh(dmsg, a);
25327 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25328 +               err = au_do_unlink(dmsg, a);
25329 +       if (unlikely(err))
25330 +               goto out_unlock;
25331 +
25332 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25333 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25334 +       if (find_lower_writable(a) < 0)
25335 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25336 +
25337 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25338 +               au_do_stfs(dmsg, a);
25339 +
25340 +       /* maintain internal array */
25341 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25342 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25343 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25344 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25345 +               au_set_ibtop(a->inode, a->mvd_bdst);
25346 +       } else {
25347 +               /* hide the lower */
25348 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25349 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25350 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25351 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25352 +       }
25353 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25354 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25355 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25356 +               au_set_ibbot(a->inode, a->mvd_bdst);
25357 +
25358 +out_unlock:
25359 +       au_do_unlock(dmsg, a);
25360 +out:
25361 +       AuTraceErr(err);
25362 +       return err;
25363 +}
25364 +
25365 +/* ---------------------------------------------------------------------- */
25366 +
25367 +/* make sure the file is idle */
25368 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25369 +{
25370 +       int err, plinked;
25371 +
25372 +       err = 0;
25373 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25374 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25375 +           && au_dcount(a->dentry) == 1
25376 +           && atomic_read(&a->inode->i_count) == 1
25377 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25378 +           && (!plinked || !au_plink_test(a->inode))
25379 +           && a->inode->i_nlink == 1)
25380 +               goto out;
25381 +
25382 +       err = -EBUSY;
25383 +       AU_MVD_PR(dmsg,
25384 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25385 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25386 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25387 +                 a->mvd_h_src_inode->i_nlink,
25388 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25389 +
25390 +out:
25391 +       AuTraceErr(err);
25392 +       return err;
25393 +}
25394 +
25395 +/* make sure the parent dir is fine */
25396 +static int au_mvd_args_parent(const unsigned char dmsg,
25397 +                             struct au_mvd_args *a)
25398 +{
25399 +       int err;
25400 +       aufs_bindex_t bindex;
25401 +
25402 +       err = 0;
25403 +       if (unlikely(au_alive_dir(a->parent))) {
25404 +               err = -ENOENT;
25405 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25406 +               goto out;
25407 +       }
25408 +
25409 +       a->bopq = au_dbdiropq(a->parent);
25410 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25411 +       AuDbg("b%d\n", bindex);
25412 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25413 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25414 +               err = -EINVAL;
25415 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25416 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25417 +                         a->bopq, a->mvd_bdst);
25418 +       }
25419 +
25420 +out:
25421 +       AuTraceErr(err);
25422 +       return err;
25423 +}
25424 +
25425 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25426 +                                   struct au_mvd_args *a)
25427 +{
25428 +       int err;
25429 +       struct au_dinfo *dinfo, *tmp;
25430 +
25431 +       /* lookup the next lower positive entry */
25432 +       err = -ENOMEM;
25433 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25434 +       if (unlikely(!tmp))
25435 +               goto out;
25436 +
25437 +       a->bfound = -1;
25438 +       a->bwh = -1;
25439 +       dinfo = au_di(a->dentry);
25440 +       au_di_cp(tmp, dinfo);
25441 +       au_di_swap(tmp, dinfo);
25442 +
25443 +       /* returns the number of positive dentries */
25444 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25445 +                            /* AuLkup_IGNORE_PERM */ 0);
25446 +       if (!err)
25447 +               a->bwh = au_dbwh(a->dentry);
25448 +       else if (err > 0)
25449 +               a->bfound = au_dbtop(a->dentry);
25450 +
25451 +       au_di_swap(tmp, dinfo);
25452 +       au_rw_write_unlock(&tmp->di_rwsem);
25453 +       au_di_free(tmp);
25454 +       if (unlikely(err < 0))
25455 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25456 +
25457 +       /*
25458 +        * here, we have these cases.
25459 +        * bfound == -1
25460 +        *      no positive dentry under bsrc. there are more sub-cases.
25461 +        *      bwh < 0
25462 +        *              there no whiteout, we can safely move-down.
25463 +        *      bwh <= bsrc
25464 +        *              impossible
25465 +        *      bsrc < bwh && bwh < bdst
25466 +        *              there is a whiteout on RO branch. cannot proceed.
25467 +        *      bwh == bdst
25468 +        *              there is a whiteout on the RW target branch. it should
25469 +        *              be removed.
25470 +        *      bdst < bwh
25471 +        *              there is a whiteout somewhere unrelated branch.
25472 +        * -1 < bfound && bfound <= bsrc
25473 +        *      impossible.
25474 +        * bfound < bdst
25475 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25476 +        *      proceed.
25477 +        * bfound == bdst
25478 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25479 +        *      error.
25480 +        * bdst < bfound
25481 +        *      found, after we create the file on bdst, it will be hidden.
25482 +        */
25483 +
25484 +       AuDebugOn(a->bfound == -1
25485 +                 && a->bwh != -1
25486 +                 && a->bwh <= a->mvd_bsrc);
25487 +       AuDebugOn(-1 < a->bfound
25488 +                 && a->bfound <= a->mvd_bsrc);
25489 +
25490 +       err = -EINVAL;
25491 +       if (a->bfound == -1
25492 +           && a->mvd_bsrc < a->bwh
25493 +           && a->bwh != -1
25494 +           && a->bwh < a->mvd_bdst) {
25495 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25496 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25497 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25498 +               goto out;
25499 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25500 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25501 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25502 +                         a->mvd_bdst, a->bfound);
25503 +               goto out;
25504 +       }
25505 +
25506 +       err = 0; /* success */
25507 +
25508 +out:
25509 +       AuTraceErr(err);
25510 +       return err;
25511 +}
25512 +
25513 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25514 +{
25515 +       int err;
25516 +
25517 +       err = 0;
25518 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25519 +           && a->bfound == a->mvd_bdst)
25520 +               err = -EEXIST;
25521 +       AuTraceErr(err);
25522 +       return err;
25523 +}
25524 +
25525 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25526 +{
25527 +       int err;
25528 +       struct au_branch *br;
25529 +
25530 +       err = -EISDIR;
25531 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25532 +               goto out;
25533 +
25534 +       err = -EINVAL;
25535 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25536 +               a->mvd_bsrc = au_ibtop(a->inode);
25537 +       else {
25538 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25539 +               if (unlikely(a->mvd_bsrc < 0
25540 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25541 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25542 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25543 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25544 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25545 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25546 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25547 +                       AU_MVD_PR(dmsg, "no upper\n");
25548 +                       goto out;
25549 +               }
25550 +       }
25551 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25552 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25553 +               AU_MVD_PR(dmsg, "on the bottom\n");
25554 +               goto out;
25555 +       }
25556 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25557 +       br = au_sbr(a->sb, a->mvd_bsrc);
25558 +       err = au_br_rdonly(br);
25559 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25560 +               if (unlikely(err))
25561 +                       goto out;
25562 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25563 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25564 +               if (err)
25565 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25566 +               /* go on */
25567 +       } else
25568 +               goto out;
25569 +
25570 +       err = -EINVAL;
25571 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25572 +               a->mvd_bdst = find_lower_writable(a);
25573 +               if (unlikely(a->mvd_bdst < 0)) {
25574 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25575 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25576 +                       goto out;
25577 +               }
25578 +       } else {
25579 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25580 +               if (unlikely(a->mvd_bdst < 0
25581 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25582 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25583 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25584 +                       goto out;
25585 +               }
25586 +       }
25587 +
25588 +       err = au_mvd_args_busy(dmsg, a);
25589 +       if (!err)
25590 +               err = au_mvd_args_parent(dmsg, a);
25591 +       if (!err)
25592 +               err = au_mvd_args_intermediate(dmsg, a);
25593 +       if (!err)
25594 +               err = au_mvd_args_exist(dmsg, a);
25595 +       if (!err)
25596 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25597 +
25598 +out:
25599 +       AuTraceErr(err);
25600 +       return err;
25601 +}
25602 +
25603 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25604 +{
25605 +       int err, e;
25606 +       unsigned char dmsg;
25607 +       struct au_mvd_args *args;
25608 +       struct inode *inode;
25609 +
25610 +       inode = d_inode(dentry);
25611 +       err = -EPERM;
25612 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25613 +               goto out;
25614 +
25615 +       err = -ENOMEM;
25616 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25617 +       if (unlikely(!args))
25618 +               goto out;
25619 +
25620 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25621 +       if (!err)
25622 +               /* VERIFY_WRITE */
25623 +               err = !access_ok(uarg, sizeof(*uarg));
25624 +       if (unlikely(err)) {
25625 +               err = -EFAULT;
25626 +               AuTraceErr(err);
25627 +               goto out_free;
25628 +       }
25629 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25630 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25631 +       args->mvdown.au_errno = 0;
25632 +       args->dentry = dentry;
25633 +       args->inode = inode;
25634 +       args->sb = dentry->d_sb;
25635 +
25636 +       err = -ENOENT;
25637 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25638 +       args->parent = dget_parent(dentry);
25639 +       args->dir = d_inode(args->parent);
25640 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25641 +       dput(args->parent);
25642 +       if (unlikely(args->parent != dentry->d_parent)) {
25643 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25644 +               goto out_dir;
25645 +       }
25646 +
25647 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25648 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25649 +       if (unlikely(err))
25650 +               goto out_inode;
25651 +
25652 +       di_write_lock_parent(args->parent);
25653 +       err = au_mvd_args(dmsg, args);
25654 +       if (unlikely(err))
25655 +               goto out_parent;
25656 +
25657 +       err = au_do_mvdown(dmsg, args);
25658 +       if (unlikely(err))
25659 +               goto out_parent;
25660 +
25661 +       au_cpup_attr_timesizes(args->dir);
25662 +       au_cpup_attr_timesizes(inode);
25663 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25664 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25665 +       /* au_digen_dec(dentry); */
25666 +
25667 +out_parent:
25668 +       di_write_unlock(args->parent);
25669 +       aufs_read_unlock(dentry, AuLock_DW);
25670 +out_inode:
25671 +       inode_unlock(inode);
25672 +out_dir:
25673 +       inode_unlock(args->dir);
25674 +out_free:
25675 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25676 +       if (unlikely(e))
25677 +               err = -EFAULT;
25678 +       au_kfree_rcu(args);
25679 +out:
25680 +       AuTraceErr(err);
25681 +       return err;
25682 +}
25683 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25684 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25685 +++ linux/fs/aufs/opts.c        2020-01-27 10:57:18.175538316 +0100
25686 @@ -0,0 +1,1880 @@
25687 +// SPDX-License-Identifier: GPL-2.0
25688 +/*
25689 + * Copyright (C) 2005-2020 Junjiro R. Okajima
25690 + *
25691 + * This program, aufs is free software; you can redistribute it and/or modify
25692 + * it under the terms of the GNU General Public License as published by
25693 + * the Free Software Foundation; either version 2 of the License, or
25694 + * (at your option) any later version.
25695 + *
25696 + * This program is distributed in the hope that it will be useful,
25697 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25698 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25699 + * GNU General Public License for more details.
25700 + *
25701 + * You should have received a copy of the GNU General Public License
25702 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25703 + */
25704 +
25705 +/*
25706 + * mount options/flags
25707 + */
25708 +
25709 +#include <linux/namei.h>
25710 +#include <linux/types.h> /* a distribution requires */
25711 +#include <linux/parser.h>
25712 +#include "aufs.h"
25713 +
25714 +/* ---------------------------------------------------------------------- */
25715 +
25716 +enum {
25717 +       Opt_br,
25718 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25719 +       Opt_idel, Opt_imod,
25720 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25721 +       Opt_rdblk_def, Opt_rdhash_def,
25722 +       Opt_xino, Opt_noxino,
25723 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25724 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25725 +       Opt_trunc_xib, Opt_notrunc_xib,
25726 +       Opt_shwh, Opt_noshwh,
25727 +       Opt_plink, Opt_noplink, Opt_list_plink,
25728 +       Opt_udba,
25729 +       Opt_dio, Opt_nodio,
25730 +       Opt_diropq_a, Opt_diropq_w,
25731 +       Opt_warn_perm, Opt_nowarn_perm,
25732 +       Opt_wbr_copyup, Opt_wbr_create,
25733 +       Opt_fhsm_sec,
25734 +       Opt_verbose, Opt_noverbose,
25735 +       Opt_sum, Opt_nosum, Opt_wsum,
25736 +       Opt_dirperm1, Opt_nodirperm1,
25737 +       Opt_dirren, Opt_nodirren,
25738 +       Opt_acl, Opt_noacl,
25739 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25740 +};
25741 +
25742 +static match_table_t options = {
25743 +       {Opt_br, "br=%s"},
25744 +       {Opt_br, "br:%s"},
25745 +
25746 +       {Opt_add, "add=%d:%s"},
25747 +       {Opt_add, "add:%d:%s"},
25748 +       {Opt_add, "ins=%d:%s"},
25749 +       {Opt_add, "ins:%d:%s"},
25750 +       {Opt_append, "append=%s"},
25751 +       {Opt_append, "append:%s"},
25752 +       {Opt_prepend, "prepend=%s"},
25753 +       {Opt_prepend, "prepend:%s"},
25754 +
25755 +       {Opt_del, "del=%s"},
25756 +       {Opt_del, "del:%s"},
25757 +       /* {Opt_idel, "idel:%d"}, */
25758 +       {Opt_mod, "mod=%s"},
25759 +       {Opt_mod, "mod:%s"},
25760 +       /* {Opt_imod, "imod:%d:%s"}, */
25761 +
25762 +       {Opt_dirwh, "dirwh=%d"},
25763 +
25764 +       {Opt_xino, "xino=%s"},
25765 +       {Opt_noxino, "noxino"},
25766 +       {Opt_trunc_xino, "trunc_xino"},
25767 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25768 +       {Opt_notrunc_xino, "notrunc_xino"},
25769 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25770 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25771 +       /* {Opt_zxino, "zxino=%s"}, */
25772 +       {Opt_trunc_xib, "trunc_xib"},
25773 +       {Opt_notrunc_xib, "notrunc_xib"},
25774 +
25775 +#ifdef CONFIG_PROC_FS
25776 +       {Opt_plink, "plink"},
25777 +#else
25778 +       {Opt_ignore_silent, "plink"},
25779 +#endif
25780 +
25781 +       {Opt_noplink, "noplink"},
25782 +
25783 +#ifdef CONFIG_AUFS_DEBUG
25784 +       {Opt_list_plink, "list_plink"},
25785 +#endif
25786 +
25787 +       {Opt_udba, "udba=%s"},
25788 +
25789 +       {Opt_dio, "dio"},
25790 +       {Opt_nodio, "nodio"},
25791 +
25792 +#ifdef CONFIG_AUFS_DIRREN
25793 +       {Opt_dirren, "dirren"},
25794 +       {Opt_nodirren, "nodirren"},
25795 +#else
25796 +       {Opt_ignore, "dirren"},
25797 +       {Opt_ignore_silent, "nodirren"},
25798 +#endif
25799 +
25800 +#ifdef CONFIG_AUFS_FHSM
25801 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25802 +#else
25803 +       {Opt_ignore, "fhsm_sec=%d"},
25804 +#endif
25805 +
25806 +       {Opt_diropq_a, "diropq=always"},
25807 +       {Opt_diropq_a, "diropq=a"},
25808 +       {Opt_diropq_w, "diropq=whiteouted"},
25809 +       {Opt_diropq_w, "diropq=w"},
25810 +
25811 +       {Opt_warn_perm, "warn_perm"},
25812 +       {Opt_nowarn_perm, "nowarn_perm"},
25813 +
25814 +       /* keep them temporary */
25815 +       {Opt_ignore_silent, "nodlgt"},
25816 +       {Opt_ignore, "clean_plink"},
25817 +
25818 +#ifdef CONFIG_AUFS_SHWH
25819 +       {Opt_shwh, "shwh"},
25820 +#endif
25821 +       {Opt_noshwh, "noshwh"},
25822 +
25823 +       {Opt_dirperm1, "dirperm1"},
25824 +       {Opt_nodirperm1, "nodirperm1"},
25825 +
25826 +       {Opt_verbose, "verbose"},
25827 +       {Opt_verbose, "v"},
25828 +       {Opt_noverbose, "noverbose"},
25829 +       {Opt_noverbose, "quiet"},
25830 +       {Opt_noverbose, "q"},
25831 +       {Opt_noverbose, "silent"},
25832 +
25833 +       {Opt_sum, "sum"},
25834 +       {Opt_nosum, "nosum"},
25835 +       {Opt_wsum, "wsum"},
25836 +
25837 +       {Opt_rdcache, "rdcache=%d"},
25838 +       {Opt_rdblk, "rdblk=%d"},
25839 +       {Opt_rdblk_def, "rdblk=def"},
25840 +       {Opt_rdhash, "rdhash=%d"},
25841 +       {Opt_rdhash_def, "rdhash=def"},
25842 +
25843 +       {Opt_wbr_create, "create=%s"},
25844 +       {Opt_wbr_create, "create_policy=%s"},
25845 +       {Opt_wbr_copyup, "cpup=%s"},
25846 +       {Opt_wbr_copyup, "copyup=%s"},
25847 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25848 +
25849 +       /* generic VFS flag */
25850 +#ifdef CONFIG_FS_POSIX_ACL
25851 +       {Opt_acl, "acl"},
25852 +       {Opt_noacl, "noacl"},
25853 +#else
25854 +       {Opt_ignore, "acl"},
25855 +       {Opt_ignore_silent, "noacl"},
25856 +#endif
25857 +
25858 +       /* internal use for the scripts */
25859 +       {Opt_ignore_silent, "si=%s"},
25860 +
25861 +       {Opt_br, "dirs=%s"},
25862 +       {Opt_ignore, "debug=%d"},
25863 +       {Opt_ignore, "delete=whiteout"},
25864 +       {Opt_ignore, "delete=all"},
25865 +       {Opt_ignore, "imap=%s"},
25866 +
25867 +       /* temporary workaround, due to old mount(8)? */
25868 +       {Opt_ignore_silent, "relatime"},
25869 +
25870 +       {Opt_err, NULL}
25871 +};
25872 +
25873 +/* ---------------------------------------------------------------------- */
25874 +
25875 +static const char *au_parser_pattern(int val, match_table_t tbl)
25876 +{
25877 +       struct match_token *p;
25878 +
25879 +       p = tbl;
25880 +       while (p->pattern) {
25881 +               if (p->token == val)
25882 +                       return p->pattern;
25883 +               p++;
25884 +       }
25885 +       BUG();
25886 +       return "??";
25887 +}
25888 +
25889 +static const char *au_optstr(int *val, match_table_t tbl)
25890 +{
25891 +       struct match_token *p;
25892 +       int v;
25893 +
25894 +       v = *val;
25895 +       if (!v)
25896 +               goto out;
25897 +       p = tbl;
25898 +       while (p->pattern) {
25899 +               if (p->token
25900 +                   && (v & p->token) == p->token) {
25901 +                       *val &= ~p->token;
25902 +                       return p->pattern;
25903 +               }
25904 +               p++;
25905 +       }
25906 +
25907 +out:
25908 +       return NULL;
25909 +}
25910 +
25911 +/* ---------------------------------------------------------------------- */
25912 +
25913 +static match_table_t brperm = {
25914 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25915 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25916 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25917 +       {0, NULL}
25918 +};
25919 +
25920 +static match_table_t brattr = {
25921 +       /* general */
25922 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25923 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25924 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25925 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25926 +#ifdef CONFIG_AUFS_FHSM
25927 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25928 +#endif
25929 +#ifdef CONFIG_AUFS_XATTR
25930 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25931 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25932 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25933 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25934 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25935 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25936 +#endif
25937 +
25938 +       /* ro/rr branch */
25939 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25940 +
25941 +       /* rw branch */
25942 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25943 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25944 +
25945 +       {0, NULL}
25946 +};
25947 +
25948 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25949 +{
25950 +       int attr, v;
25951 +       char *p;
25952 +
25953 +       attr = 0;
25954 +       do {
25955 +               p = strchr(str, '+');
25956 +               if (p)
25957 +                       *p = 0;
25958 +               v = match_token(str, table, args);
25959 +               if (v) {
25960 +                       if (v & AuBrAttr_CMOO_Mask)
25961 +                               attr &= ~AuBrAttr_CMOO_Mask;
25962 +                       attr |= v;
25963 +               } else {
25964 +                       if (p)
25965 +                               *p = '+';
25966 +                       pr_warn("ignored branch attribute %s\n", str);
25967 +                       break;
25968 +               }
25969 +               if (p)
25970 +                       str = p + 1;
25971 +       } while (p);
25972 +
25973 +       return attr;
25974 +}
25975 +
25976 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25977 +{
25978 +       int sz;
25979 +       const char *p;
25980 +       char *q;
25981 +
25982 +       q = str->a;
25983 +       *q = 0;
25984 +       p = au_optstr(&perm, brattr);
25985 +       if (p) {
25986 +               sz = strlen(p);
25987 +               memcpy(q, p, sz + 1);
25988 +               q += sz;
25989 +       } else
25990 +               goto out;
25991 +
25992 +       do {
25993 +               p = au_optstr(&perm, brattr);
25994 +               if (p) {
25995 +                       *q++ = '+';
25996 +                       sz = strlen(p);
25997 +                       memcpy(q, p, sz + 1);
25998 +                       q += sz;
25999 +               }
26000 +       } while (p);
26001 +
26002 +out:
26003 +       return q - str->a;
26004 +}
26005 +
26006 +static int noinline_for_stack br_perm_val(char *perm)
26007 +{
26008 +       int val, bad, sz;
26009 +       char *p;
26010 +       substring_t args[MAX_OPT_ARGS];
26011 +       au_br_perm_str_t attr;
26012 +
26013 +       p = strchr(perm, '+');
26014 +       if (p)
26015 +               *p = 0;
26016 +       val = match_token(perm, brperm, args);
26017 +       if (!val) {
26018 +               if (p)
26019 +                       *p = '+';
26020 +               pr_warn("ignored branch permission %s\n", perm);
26021 +               val = AuBrPerm_RO;
26022 +               goto out;
26023 +       }
26024 +       if (!p)
26025 +               goto out;
26026 +
26027 +       val |= br_attr_val(p + 1, brattr, args);
26028 +
26029 +       bad = 0;
26030 +       switch (val & AuBrPerm_Mask) {
26031 +       case AuBrPerm_RO:
26032 +       case AuBrPerm_RR:
26033 +               bad = val & AuBrWAttr_Mask;
26034 +               val &= ~AuBrWAttr_Mask;
26035 +               break;
26036 +       case AuBrPerm_RW:
26037 +               bad = val & AuBrRAttr_Mask;
26038 +               val &= ~AuBrRAttr_Mask;
26039 +               break;
26040 +       }
26041 +
26042 +       /*
26043 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26044 +        * does not treat it as an error, just warning.
26045 +        * this is a tiny guard for the user operation.
26046 +        */
26047 +       if (val & AuBrAttr_UNPIN) {
26048 +               bad |= AuBrAttr_UNPIN;
26049 +               val &= ~AuBrAttr_UNPIN;
26050 +       }
26051 +
26052 +       if (unlikely(bad)) {
26053 +               sz = au_do_optstr_br_attr(&attr, bad);
26054 +               AuDebugOn(!sz);
26055 +               pr_warn("ignored branch attribute %s\n", attr.a);
26056 +       }
26057 +
26058 +out:
26059 +       return val;
26060 +}
26061 +
26062 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
26063 +{
26064 +       au_br_perm_str_t attr;
26065 +       const char *p;
26066 +       char *q;
26067 +       int sz;
26068 +
26069 +       q = str->a;
26070 +       p = au_optstr(&perm, brperm);
26071 +       AuDebugOn(!p || !*p);
26072 +       sz = strlen(p);
26073 +       memcpy(q, p, sz + 1);
26074 +       q += sz;
26075 +
26076 +       sz = au_do_optstr_br_attr(&attr, perm);
26077 +       if (sz) {
26078 +               *q++ = '+';
26079 +               memcpy(q, attr.a, sz + 1);
26080 +       }
26081 +
26082 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
26083 +}
26084 +
26085 +/* ---------------------------------------------------------------------- */
26086 +
26087 +static match_table_t udbalevel = {
26088 +       {AuOpt_UDBA_REVAL, "reval"},
26089 +       {AuOpt_UDBA_NONE, "none"},
26090 +#ifdef CONFIG_AUFS_HNOTIFY
26091 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26092 +#ifdef CONFIG_AUFS_HFSNOTIFY
26093 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26094 +#endif
26095 +#endif
26096 +       {-1, NULL}
26097 +};
26098 +
26099 +static int noinline_for_stack udba_val(char *str)
26100 +{
26101 +       substring_t args[MAX_OPT_ARGS];
26102 +
26103 +       return match_token(str, udbalevel, args);
26104 +}
26105 +
26106 +const char *au_optstr_udba(int udba)
26107 +{
26108 +       return au_parser_pattern(udba, udbalevel);
26109 +}
26110 +
26111 +/* ---------------------------------------------------------------------- */
26112 +
26113 +static match_table_t au_wbr_create_policy = {
26114 +       {AuWbrCreate_TDP, "tdp"},
26115 +       {AuWbrCreate_TDP, "top-down-parent"},
26116 +       {AuWbrCreate_RR, "rr"},
26117 +       {AuWbrCreate_RR, "round-robin"},
26118 +       {AuWbrCreate_MFS, "mfs"},
26119 +       {AuWbrCreate_MFS, "most-free-space"},
26120 +       {AuWbrCreate_MFSV, "mfs:%d"},
26121 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26122 +
26123 +       /* top-down regardless the parent, and then mfs */
26124 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26125 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26126 +
26127 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26128 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26129 +       {AuWbrCreate_PMFS, "pmfs"},
26130 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26131 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26132 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26133 +
26134 +       {-1, NULL}
26135 +};
26136 +
26137 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26138 +                           struct au_opt_wbr_create *create)
26139 +{
26140 +       int err;
26141 +       unsigned long long ull;
26142 +
26143 +       err = 0;
26144 +       if (!match_u64(arg, &ull))
26145 +               create->mfsrr_watermark = ull;
26146 +       else {
26147 +               pr_err("bad integer in %s\n", str);
26148 +               err = -EINVAL;
26149 +       }
26150 +
26151 +       return err;
26152 +}
26153 +
26154 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26155 +                         struct au_opt_wbr_create *create)
26156 +{
26157 +       int n, err;
26158 +
26159 +       err = 0;
26160 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26161 +               create->mfs_second = n;
26162 +       else {
26163 +               pr_err("bad integer in %s\n", str);
26164 +               err = -EINVAL;
26165 +       }
26166 +
26167 +       return err;
26168 +}
26169 +
26170 +static int noinline_for_stack
26171 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26172 +{
26173 +       int err, e;
26174 +       substring_t args[MAX_OPT_ARGS];
26175 +
26176 +       err = match_token(str, au_wbr_create_policy, args);
26177 +       create->wbr_create = err;
26178 +       switch (err) {
26179 +       case AuWbrCreate_MFSRRV:
26180 +       case AuWbrCreate_TDMFSV:
26181 +       case AuWbrCreate_PMFSRRV:
26182 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26183 +               if (!e)
26184 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26185 +               if (unlikely(e))
26186 +                       err = e;
26187 +               break;
26188 +       case AuWbrCreate_MFSRR:
26189 +       case AuWbrCreate_TDMFS:
26190 +       case AuWbrCreate_PMFSRR:
26191 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26192 +               if (unlikely(e)) {
26193 +                       err = e;
26194 +                       break;
26195 +               }
26196 +               /*FALLTHROUGH*/
26197 +       case AuWbrCreate_MFS:
26198 +       case AuWbrCreate_PMFS:
26199 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26200 +               break;
26201 +       case AuWbrCreate_MFSV:
26202 +       case AuWbrCreate_PMFSV:
26203 +               e = au_wbr_mfs_sec(&args[0], str, create);
26204 +               if (unlikely(e))
26205 +                       err = e;
26206 +               break;
26207 +       }
26208 +
26209 +       return err;
26210 +}
26211 +
26212 +const char *au_optstr_wbr_create(int wbr_create)
26213 +{
26214 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26215 +}
26216 +
26217 +static match_table_t au_wbr_copyup_policy = {
26218 +       {AuWbrCopyup_TDP, "tdp"},
26219 +       {AuWbrCopyup_TDP, "top-down-parent"},
26220 +       {AuWbrCopyup_BUP, "bup"},
26221 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26222 +       {AuWbrCopyup_BU, "bu"},
26223 +       {AuWbrCopyup_BU, "bottom-up"},
26224 +       {-1, NULL}
26225 +};
26226 +
26227 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26228 +{
26229 +       substring_t args[MAX_OPT_ARGS];
26230 +
26231 +       return match_token(str, au_wbr_copyup_policy, args);
26232 +}
26233 +
26234 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26235 +{
26236 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26237 +}
26238 +
26239 +/* ---------------------------------------------------------------------- */
26240 +
26241 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26242 +
26243 +static void dump_opts(struct au_opts *opts)
26244 +{
26245 +#ifdef CONFIG_AUFS_DEBUG
26246 +       /* reduce stack space */
26247 +       union {
26248 +               struct au_opt_add *add;
26249 +               struct au_opt_del *del;
26250 +               struct au_opt_mod *mod;
26251 +               struct au_opt_xino *xino;
26252 +               struct au_opt_xino_itrunc *xino_itrunc;
26253 +               struct au_opt_wbr_create *create;
26254 +       } u;
26255 +       struct au_opt *opt;
26256 +
26257 +       opt = opts->opt;
26258 +       while (opt->type != Opt_tail) {
26259 +               switch (opt->type) {
26260 +               case Opt_add:
26261 +                       u.add = &opt->add;
26262 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26263 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26264 +                                 u.add->path.dentry);
26265 +                       break;
26266 +               case Opt_del:
26267 +               case Opt_idel:
26268 +                       u.del = &opt->del;
26269 +                       AuDbg("del {%s, %p}\n",
26270 +                             u.del->pathname, u.del->h_path.dentry);
26271 +                       break;
26272 +               case Opt_mod:
26273 +               case Opt_imod:
26274 +                       u.mod = &opt->mod;
26275 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26276 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26277 +                       break;
26278 +               case Opt_append:
26279 +                       u.add = &opt->add;
26280 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26281 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26282 +                                 u.add->path.dentry);
26283 +                       break;
26284 +               case Opt_prepend:
26285 +                       u.add = &opt->add;
26286 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26287 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26288 +                                 u.add->path.dentry);
26289 +                       break;
26290 +               case Opt_dirwh:
26291 +                       AuDbg("dirwh %d\n", opt->dirwh);
26292 +                       break;
26293 +               case Opt_rdcache:
26294 +                       AuDbg("rdcache %d\n", opt->rdcache);
26295 +                       break;
26296 +               case Opt_rdblk:
26297 +                       AuDbg("rdblk %u\n", opt->rdblk);
26298 +                       break;
26299 +               case Opt_rdblk_def:
26300 +                       AuDbg("rdblk_def\n");
26301 +                       break;
26302 +               case Opt_rdhash:
26303 +                       AuDbg("rdhash %u\n", opt->rdhash);
26304 +                       break;
26305 +               case Opt_rdhash_def:
26306 +                       AuDbg("rdhash_def\n");
26307 +                       break;
26308 +               case Opt_xino:
26309 +                       u.xino = &opt->xino;
26310 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26311 +                       break;
26312 +               case Opt_trunc_xino:
26313 +                       AuLabel(trunc_xino);
26314 +                       break;
26315 +               case Opt_notrunc_xino:
26316 +                       AuLabel(notrunc_xino);
26317 +                       break;
26318 +               case Opt_trunc_xino_path:
26319 +               case Opt_itrunc_xino:
26320 +                       u.xino_itrunc = &opt->xino_itrunc;
26321 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26322 +                       break;
26323 +               case Opt_noxino:
26324 +                       AuLabel(noxino);
26325 +                       break;
26326 +               case Opt_trunc_xib:
26327 +                       AuLabel(trunc_xib);
26328 +                       break;
26329 +               case Opt_notrunc_xib:
26330 +                       AuLabel(notrunc_xib);
26331 +                       break;
26332 +               case Opt_shwh:
26333 +                       AuLabel(shwh);
26334 +                       break;
26335 +               case Opt_noshwh:
26336 +                       AuLabel(noshwh);
26337 +                       break;
26338 +               case Opt_dirperm1:
26339 +                       AuLabel(dirperm1);
26340 +                       break;
26341 +               case Opt_nodirperm1:
26342 +                       AuLabel(nodirperm1);
26343 +                       break;
26344 +               case Opt_plink:
26345 +                       AuLabel(plink);
26346 +                       break;
26347 +               case Opt_noplink:
26348 +                       AuLabel(noplink);
26349 +                       break;
26350 +               case Opt_list_plink:
26351 +                       AuLabel(list_plink);
26352 +                       break;
26353 +               case Opt_udba:
26354 +                       AuDbg("udba %d, %s\n",
26355 +                                 opt->udba, au_optstr_udba(opt->udba));
26356 +                       break;
26357 +               case Opt_dio:
26358 +                       AuLabel(dio);
26359 +                       break;
26360 +               case Opt_nodio:
26361 +                       AuLabel(nodio);
26362 +                       break;
26363 +               case Opt_diropq_a:
26364 +                       AuLabel(diropq_a);
26365 +                       break;
26366 +               case Opt_diropq_w:
26367 +                       AuLabel(diropq_w);
26368 +                       break;
26369 +               case Opt_warn_perm:
26370 +                       AuLabel(warn_perm);
26371 +                       break;
26372 +               case Opt_nowarn_perm:
26373 +                       AuLabel(nowarn_perm);
26374 +                       break;
26375 +               case Opt_verbose:
26376 +                       AuLabel(verbose);
26377 +                       break;
26378 +               case Opt_noverbose:
26379 +                       AuLabel(noverbose);
26380 +                       break;
26381 +               case Opt_sum:
26382 +                       AuLabel(sum);
26383 +                       break;
26384 +               case Opt_nosum:
26385 +                       AuLabel(nosum);
26386 +                       break;
26387 +               case Opt_wsum:
26388 +                       AuLabel(wsum);
26389 +                       break;
26390 +               case Opt_wbr_create:
26391 +                       u.create = &opt->wbr_create;
26392 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26393 +                                 au_optstr_wbr_create(u.create->wbr_create));
26394 +                       switch (u.create->wbr_create) {
26395 +                       case AuWbrCreate_MFSV:
26396 +                       case AuWbrCreate_PMFSV:
26397 +                               AuDbg("%d sec\n", u.create->mfs_second);
26398 +                               break;
26399 +                       case AuWbrCreate_MFSRR:
26400 +                       case AuWbrCreate_TDMFS:
26401 +                               AuDbg("%llu watermark\n",
26402 +                                         u.create->mfsrr_watermark);
26403 +                               break;
26404 +                       case AuWbrCreate_MFSRRV:
26405 +                       case AuWbrCreate_TDMFSV:
26406 +                       case AuWbrCreate_PMFSRRV:
26407 +                               AuDbg("%llu watermark, %d sec\n",
26408 +                                         u.create->mfsrr_watermark,
26409 +                                         u.create->mfs_second);
26410 +                               break;
26411 +                       }
26412 +                       break;
26413 +               case Opt_wbr_copyup:
26414 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26415 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26416 +                       break;
26417 +               case Opt_fhsm_sec:
26418 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26419 +                       break;
26420 +               case Opt_dirren:
26421 +                       AuLabel(dirren);
26422 +                       break;
26423 +               case Opt_nodirren:
26424 +                       AuLabel(nodirren);
26425 +                       break;
26426 +               case Opt_acl:
26427 +                       AuLabel(acl);
26428 +                       break;
26429 +               case Opt_noacl:
26430 +                       AuLabel(noacl);
26431 +                       break;
26432 +               default:
26433 +                       BUG();
26434 +               }
26435 +               opt++;
26436 +       }
26437 +#endif
26438 +}
26439 +
26440 +void au_opts_free(struct au_opts *opts)
26441 +{
26442 +       struct au_opt *opt;
26443 +
26444 +       opt = opts->opt;
26445 +       while (opt->type != Opt_tail) {
26446 +               switch (opt->type) {
26447 +               case Opt_add:
26448 +               case Opt_append:
26449 +               case Opt_prepend:
26450 +                       path_put(&opt->add.path);
26451 +                       break;
26452 +               case Opt_del:
26453 +               case Opt_idel:
26454 +                       path_put(&opt->del.h_path);
26455 +                       break;
26456 +               case Opt_mod:
26457 +               case Opt_imod:
26458 +                       dput(opt->mod.h_root);
26459 +                       break;
26460 +               case Opt_xino:
26461 +                       fput(opt->xino.file);
26462 +                       break;
26463 +               }
26464 +               opt++;
26465 +       }
26466 +}
26467 +
26468 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26469 +                  aufs_bindex_t bindex)
26470 +{
26471 +       int err;
26472 +       struct au_opt_add *add = &opt->add;
26473 +       char *p;
26474 +
26475 +       add->bindex = bindex;
26476 +       add->perm = AuBrPerm_RO;
26477 +       add->pathname = opt_str;
26478 +       p = strchr(opt_str, '=');
26479 +       if (p) {
26480 +               *p++ = 0;
26481 +               if (*p)
26482 +                       add->perm = br_perm_val(p);
26483 +       }
26484 +
26485 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26486 +       if (!err) {
26487 +               if (!p) {
26488 +                       add->perm = AuBrPerm_RO;
26489 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26490 +                               add->perm = AuBrPerm_RR;
26491 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26492 +                               add->perm = AuBrPerm_RW;
26493 +               }
26494 +               opt->type = Opt_add;
26495 +               goto out;
26496 +       }
26497 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26498 +       err = -EINVAL;
26499 +
26500 +out:
26501 +       return err;
26502 +}
26503 +
26504 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26505 +{
26506 +       int err;
26507 +
26508 +       del->pathname = args[0].from;
26509 +       AuDbg("del path %s\n", del->pathname);
26510 +
26511 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26512 +       if (unlikely(err))
26513 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26514 +
26515 +       return err;
26516 +}
26517 +
26518 +#if 0 /* reserved for future use */
26519 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26520 +                             struct au_opt_del *del, substring_t args[])
26521 +{
26522 +       int err;
26523 +       struct dentry *root;
26524 +
26525 +       err = -EINVAL;
26526 +       root = sb->s_root;
26527 +       aufs_read_lock(root, AuLock_FLUSH);
26528 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26529 +               pr_err("out of bounds, %d\n", bindex);
26530 +               goto out;
26531 +       }
26532 +
26533 +       err = 0;
26534 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26535 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26536 +
26537 +out:
26538 +       aufs_read_unlock(root, !AuLock_IR);
26539 +       return err;
26540 +}
26541 +#endif
26542 +
26543 +static int noinline_for_stack
26544 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26545 +{
26546 +       int err;
26547 +       struct path path;
26548 +       char *p;
26549 +
26550 +       err = -EINVAL;
26551 +       mod->path = args[0].from;
26552 +       p = strchr(mod->path, '=');
26553 +       if (unlikely(!p)) {
26554 +               pr_err("no permission %s\n", args[0].from);
26555 +               goto out;
26556 +       }
26557 +
26558 +       *p++ = 0;
26559 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26560 +       if (unlikely(err)) {
26561 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26562 +               goto out;
26563 +       }
26564 +
26565 +       mod->perm = br_perm_val(p);
26566 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26567 +       mod->h_root = dget(path.dentry);
26568 +       path_put(&path);
26569 +
26570 +out:
26571 +       return err;
26572 +}
26573 +
26574 +#if 0 /* reserved for future use */
26575 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26576 +                             struct au_opt_mod *mod, substring_t args[])
26577 +{
26578 +       int err;
26579 +       struct dentry *root;
26580 +
26581 +       err = -EINVAL;
26582 +       root = sb->s_root;
26583 +       aufs_read_lock(root, AuLock_FLUSH);
26584 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26585 +               pr_err("out of bounds, %d\n", bindex);
26586 +               goto out;
26587 +       }
26588 +
26589 +       err = 0;
26590 +       mod->perm = br_perm_val(args[1].from);
26591 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26592 +             mod->path, mod->perm, args[1].from);
26593 +       mod->h_root = dget(au_h_dptr(root, bindex));
26594 +
26595 +out:
26596 +       aufs_read_unlock(root, !AuLock_IR);
26597 +       return err;
26598 +}
26599 +#endif
26600 +
26601 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26602 +                             substring_t args[])
26603 +{
26604 +       int err;
26605 +       struct file *file;
26606 +
26607 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26608 +       err = PTR_ERR(file);
26609 +       if (IS_ERR(file))
26610 +               goto out;
26611 +
26612 +       err = -EINVAL;
26613 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26614 +               fput(file);
26615 +               pr_err("%s must be outside\n", args[0].from);
26616 +               goto out;
26617 +       }
26618 +
26619 +       err = 0;
26620 +       xino->file = file;
26621 +       xino->path = args[0].from;
26622 +
26623 +out:
26624 +       return err;
26625 +}
26626 +
26627 +static int noinline_for_stack
26628 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26629 +                              struct au_opt_xino_itrunc *xino_itrunc,
26630 +                              substring_t args[])
26631 +{
26632 +       int err;
26633 +       aufs_bindex_t bbot, bindex;
26634 +       struct path path;
26635 +       struct dentry *root;
26636 +
26637 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26638 +       if (unlikely(err)) {
26639 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26640 +               goto out;
26641 +       }
26642 +
26643 +       xino_itrunc->bindex = -1;
26644 +       root = sb->s_root;
26645 +       aufs_read_lock(root, AuLock_FLUSH);
26646 +       bbot = au_sbbot(sb);
26647 +       for (bindex = 0; bindex <= bbot; bindex++) {
26648 +               if (au_h_dptr(root, bindex) == path.dentry) {
26649 +                       xino_itrunc->bindex = bindex;
26650 +                       break;
26651 +               }
26652 +       }
26653 +       aufs_read_unlock(root, !AuLock_IR);
26654 +       path_put(&path);
26655 +
26656 +       if (unlikely(xino_itrunc->bindex < 0)) {
26657 +               pr_err("no such branch %s\n", args[0].from);
26658 +               err = -EINVAL;
26659 +       }
26660 +
26661 +out:
26662 +       return err;
26663 +}
26664 +
26665 +/* called without aufs lock */
26666 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26667 +{
26668 +       int err, n, token;
26669 +       aufs_bindex_t bindex;
26670 +       unsigned char skipped;
26671 +       struct dentry *root;
26672 +       struct au_opt *opt, *opt_tail;
26673 +       char *opt_str;
26674 +       /* reduce the stack space */
26675 +       union {
26676 +               struct au_opt_xino_itrunc *xino_itrunc;
26677 +               struct au_opt_wbr_create *create;
26678 +       } u;
26679 +       struct {
26680 +               substring_t args[MAX_OPT_ARGS];
26681 +       } *a;
26682 +
26683 +       err = -ENOMEM;
26684 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26685 +       if (unlikely(!a))
26686 +               goto out;
26687 +
26688 +       root = sb->s_root;
26689 +       err = 0;
26690 +       bindex = 0;
26691 +       opt = opts->opt;
26692 +       opt_tail = opt + opts->max_opt - 1;
26693 +       opt->type = Opt_tail;
26694 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26695 +               err = -EINVAL;
26696 +               skipped = 0;
26697 +               token = match_token(opt_str, options, a->args);
26698 +               switch (token) {
26699 +               case Opt_br:
26700 +                       err = 0;
26701 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26702 +                              && *opt_str) {
26703 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26704 +                                             bindex++);
26705 +                               if (unlikely(!err && ++opt > opt_tail)) {
26706 +                                       err = -E2BIG;
26707 +                                       break;
26708 +                               }
26709 +                               opt->type = Opt_tail;
26710 +                               skipped = 1;
26711 +                       }
26712 +                       break;
26713 +               case Opt_add:
26714 +                       if (unlikely(match_int(&a->args[0], &n))) {
26715 +                               pr_err("bad integer in %s\n", opt_str);
26716 +                               break;
26717 +                       }
26718 +                       bindex = n;
26719 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26720 +                                     bindex);
26721 +                       if (!err)
26722 +                               opt->type = token;
26723 +                       break;
26724 +               case Opt_append:
26725 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26726 +                                     /*dummy bindex*/1);
26727 +                       if (!err)
26728 +                               opt->type = token;
26729 +                       break;
26730 +               case Opt_prepend:
26731 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26732 +                                     /*bindex*/0);
26733 +                       if (!err)
26734 +                               opt->type = token;
26735 +                       break;
26736 +               case Opt_del:
26737 +                       err = au_opts_parse_del(&opt->del, a->args);
26738 +                       if (!err)
26739 +                               opt->type = token;
26740 +                       break;
26741 +#if 0 /* reserved for future use */
26742 +               case Opt_idel:
26743 +                       del->pathname = "(indexed)";
26744 +                       if (unlikely(match_int(&args[0], &n))) {
26745 +                               pr_err("bad integer in %s\n", opt_str);
26746 +                               break;
26747 +                       }
26748 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26749 +                       if (!err)
26750 +                               opt->type = token;
26751 +                       break;
26752 +#endif
26753 +               case Opt_mod:
26754 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26755 +                       if (!err)
26756 +                               opt->type = token;
26757 +                       break;
26758 +#ifdef IMOD /* reserved for future use */
26759 +               case Opt_imod:
26760 +                       u.mod->path = "(indexed)";
26761 +                       if (unlikely(match_int(&a->args[0], &n))) {
26762 +                               pr_err("bad integer in %s\n", opt_str);
26763 +                               break;
26764 +                       }
26765 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26766 +                       if (!err)
26767 +                               opt->type = token;
26768 +                       break;
26769 +#endif
26770 +               case Opt_xino:
26771 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26772 +                       if (!err)
26773 +                               opt->type = token;
26774 +                       break;
26775 +
26776 +               case Opt_trunc_xino_path:
26777 +                       err = au_opts_parse_xino_itrunc_path
26778 +                               (sb, &opt->xino_itrunc, a->args);
26779 +                       if (!err)
26780 +                               opt->type = token;
26781 +                       break;
26782 +
26783 +               case Opt_itrunc_xino:
26784 +                       u.xino_itrunc = &opt->xino_itrunc;
26785 +                       if (unlikely(match_int(&a->args[0], &n))) {
26786 +                               pr_err("bad integer in %s\n", opt_str);
26787 +                               break;
26788 +                       }
26789 +                       u.xino_itrunc->bindex = n;
26790 +                       aufs_read_lock(root, AuLock_FLUSH);
26791 +                       if (n < 0 || au_sbbot(sb) < n) {
26792 +                               pr_err("out of bounds, %d\n", n);
26793 +                               aufs_read_unlock(root, !AuLock_IR);
26794 +                               break;
26795 +                       }
26796 +                       aufs_read_unlock(root, !AuLock_IR);
26797 +                       err = 0;
26798 +                       opt->type = token;
26799 +                       break;
26800 +
26801 +               case Opt_dirwh:
26802 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26803 +                               break;
26804 +                       err = 0;
26805 +                       opt->type = token;
26806 +                       break;
26807 +
26808 +               case Opt_rdcache:
26809 +                       if (unlikely(match_int(&a->args[0], &n))) {
26810 +                               pr_err("bad integer in %s\n", opt_str);
26811 +                               break;
26812 +                       }
26813 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26814 +                               pr_err("rdcache must be smaller than %d\n",
26815 +                                      AUFS_RDCACHE_MAX);
26816 +                               break;
26817 +                       }
26818 +                       opt->rdcache = n;
26819 +                       err = 0;
26820 +                       opt->type = token;
26821 +                       break;
26822 +               case Opt_rdblk:
26823 +                       if (unlikely(match_int(&a->args[0], &n)
26824 +                                    || n < 0
26825 +                                    || n > KMALLOC_MAX_SIZE)) {
26826 +                               pr_err("bad integer in %s\n", opt_str);
26827 +                               break;
26828 +                       }
26829 +                       if (unlikely(n && n < NAME_MAX)) {
26830 +                               pr_err("rdblk must be larger than %d\n",
26831 +                                      NAME_MAX);
26832 +                               break;
26833 +                       }
26834 +                       opt->rdblk = n;
26835 +                       err = 0;
26836 +                       opt->type = token;
26837 +                       break;
26838 +               case Opt_rdhash:
26839 +                       if (unlikely(match_int(&a->args[0], &n)
26840 +                                    || n < 0
26841 +                                    || n * sizeof(struct hlist_head)
26842 +                                    > KMALLOC_MAX_SIZE)) {
26843 +                               pr_err("bad integer in %s\n", opt_str);
26844 +                               break;
26845 +                       }
26846 +                       opt->rdhash = n;
26847 +                       err = 0;
26848 +                       opt->type = token;
26849 +                       break;
26850 +
26851 +               case Opt_trunc_xino:
26852 +               case Opt_notrunc_xino:
26853 +               case Opt_noxino:
26854 +               case Opt_trunc_xib:
26855 +               case Opt_notrunc_xib:
26856 +               case Opt_shwh:
26857 +               case Opt_noshwh:
26858 +               case Opt_dirperm1:
26859 +               case Opt_nodirperm1:
26860 +               case Opt_plink:
26861 +               case Opt_noplink:
26862 +               case Opt_list_plink:
26863 +               case Opt_dio:
26864 +               case Opt_nodio:
26865 +               case Opt_diropq_a:
26866 +               case Opt_diropq_w:
26867 +               case Opt_warn_perm:
26868 +               case Opt_nowarn_perm:
26869 +               case Opt_verbose:
26870 +               case Opt_noverbose:
26871 +               case Opt_sum:
26872 +               case Opt_nosum:
26873 +               case Opt_wsum:
26874 +               case Opt_rdblk_def:
26875 +               case Opt_rdhash_def:
26876 +               case Opt_dirren:
26877 +               case Opt_nodirren:
26878 +               case Opt_acl:
26879 +               case Opt_noacl:
26880 +                       err = 0;
26881 +                       opt->type = token;
26882 +                       break;
26883 +
26884 +               case Opt_udba:
26885 +                       opt->udba = udba_val(a->args[0].from);
26886 +                       if (opt->udba >= 0) {
26887 +                               err = 0;
26888 +                               opt->type = token;
26889 +                       } else
26890 +                               pr_err("wrong value, %s\n", opt_str);
26891 +                       break;
26892 +
26893 +               case Opt_wbr_create:
26894 +                       u.create = &opt->wbr_create;
26895 +                       u.create->wbr_create
26896 +                               = au_wbr_create_val(a->args[0].from, u.create);
26897 +                       if (u.create->wbr_create >= 0) {
26898 +                               err = 0;
26899 +                               opt->type = token;
26900 +                       } else
26901 +                               pr_err("wrong value, %s\n", opt_str);
26902 +                       break;
26903 +               case Opt_wbr_copyup:
26904 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26905 +                       if (opt->wbr_copyup >= 0) {
26906 +                               err = 0;
26907 +                               opt->type = token;
26908 +                       } else
26909 +                               pr_err("wrong value, %s\n", opt_str);
26910 +                       break;
26911 +
26912 +               case Opt_fhsm_sec:
26913 +                       if (unlikely(match_int(&a->args[0], &n)
26914 +                                    || n < 0)) {
26915 +                               pr_err("bad integer in %s\n", opt_str);
26916 +                               break;
26917 +                       }
26918 +                       if (sysaufs_brs) {
26919 +                               opt->fhsm_second = n;
26920 +                               opt->type = token;
26921 +                       } else
26922 +                               pr_warn("ignored %s\n", opt_str);
26923 +                       err = 0;
26924 +                       break;
26925 +
26926 +               case Opt_ignore:
26927 +                       pr_warn("ignored %s\n", opt_str);
26928 +                       /*FALLTHROUGH*/
26929 +               case Opt_ignore_silent:
26930 +                       skipped = 1;
26931 +                       err = 0;
26932 +                       break;
26933 +               case Opt_err:
26934 +                       pr_err("unknown option %s\n", opt_str);
26935 +                       break;
26936 +               }
26937 +
26938 +               if (!err && !skipped) {
26939 +                       if (unlikely(++opt > opt_tail)) {
26940 +                               err = -E2BIG;
26941 +                               opt--;
26942 +                               opt->type = Opt_tail;
26943 +                               break;
26944 +                       }
26945 +                       opt->type = Opt_tail;
26946 +               }
26947 +       }
26948 +
26949 +       au_kfree_rcu(a);
26950 +       dump_opts(opts);
26951 +       if (unlikely(err))
26952 +               au_opts_free(opts);
26953 +
26954 +out:
26955 +       return err;
26956 +}
26957 +
26958 +static int au_opt_wbr_create(struct super_block *sb,
26959 +                            struct au_opt_wbr_create *create)
26960 +{
26961 +       int err;
26962 +       struct au_sbinfo *sbinfo;
26963 +
26964 +       SiMustWriteLock(sb);
26965 +
26966 +       err = 1; /* handled */
26967 +       sbinfo = au_sbi(sb);
26968 +       if (sbinfo->si_wbr_create_ops->fin) {
26969 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26970 +               if (!err)
26971 +                       err = 1;
26972 +       }
26973 +
26974 +       sbinfo->si_wbr_create = create->wbr_create;
26975 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26976 +       switch (create->wbr_create) {
26977 +       case AuWbrCreate_MFSRRV:
26978 +       case AuWbrCreate_MFSRR:
26979 +       case AuWbrCreate_TDMFS:
26980 +       case AuWbrCreate_TDMFSV:
26981 +       case AuWbrCreate_PMFSRR:
26982 +       case AuWbrCreate_PMFSRRV:
26983 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26984 +               /*FALLTHROUGH*/
26985 +       case AuWbrCreate_MFS:
26986 +       case AuWbrCreate_MFSV:
26987 +       case AuWbrCreate_PMFS:
26988 +       case AuWbrCreate_PMFSV:
26989 +               sbinfo->si_wbr_mfs.mfs_expire
26990 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26991 +               break;
26992 +       }
26993 +
26994 +       if (sbinfo->si_wbr_create_ops->init)
26995 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26996 +
26997 +       return err;
26998 +}
26999 +
27000 +/*
27001 + * returns,
27002 + * plus: processed without an error
27003 + * zero: unprocessed
27004 + */
27005 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27006 +                        struct au_opts *opts)
27007 +{
27008 +       int err;
27009 +       struct au_sbinfo *sbinfo;
27010 +
27011 +       SiMustWriteLock(sb);
27012 +
27013 +       err = 1; /* handled */
27014 +       sbinfo = au_sbi(sb);
27015 +       switch (opt->type) {
27016 +       case Opt_udba:
27017 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27018 +               sbinfo->si_mntflags |= opt->udba;
27019 +               opts->given_udba |= opt->udba;
27020 +               break;
27021 +
27022 +       case Opt_plink:
27023 +               au_opt_set(sbinfo->si_mntflags, PLINK);
27024 +               break;
27025 +       case Opt_noplink:
27026 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27027 +                       au_plink_put(sb, /*verbose*/1);
27028 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
27029 +               break;
27030 +       case Opt_list_plink:
27031 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27032 +                       au_plink_list(sb);
27033 +               break;
27034 +
27035 +       case Opt_dio:
27036 +               au_opt_set(sbinfo->si_mntflags, DIO);
27037 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27038 +               break;
27039 +       case Opt_nodio:
27040 +               au_opt_clr(sbinfo->si_mntflags, DIO);
27041 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27042 +               break;
27043 +
27044 +       case Opt_fhsm_sec:
27045 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27046 +               break;
27047 +
27048 +       case Opt_diropq_a:
27049 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27050 +               break;
27051 +       case Opt_diropq_w:
27052 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27053 +               break;
27054 +
27055 +       case Opt_warn_perm:
27056 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27057 +               break;
27058 +       case Opt_nowarn_perm:
27059 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27060 +               break;
27061 +
27062 +       case Opt_verbose:
27063 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
27064 +               break;
27065 +       case Opt_noverbose:
27066 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27067 +               break;
27068 +
27069 +       case Opt_sum:
27070 +               au_opt_set(sbinfo->si_mntflags, SUM);
27071 +               break;
27072 +       case Opt_wsum:
27073 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27074 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27075 +               break;
27076 +       case Opt_nosum:
27077 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27078 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
27079 +               break;
27080 +
27081 +       case Opt_wbr_create:
27082 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27083 +               break;
27084 +       case Opt_wbr_copyup:
27085 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27086 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27087 +               break;
27088 +
27089 +       case Opt_dirwh:
27090 +               sbinfo->si_dirwh = opt->dirwh;
27091 +               break;
27092 +
27093 +       case Opt_rdcache:
27094 +               sbinfo->si_rdcache
27095 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27096 +               break;
27097 +       case Opt_rdblk:
27098 +               sbinfo->si_rdblk = opt->rdblk;
27099 +               break;
27100 +       case Opt_rdblk_def:
27101 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27102 +               break;
27103 +       case Opt_rdhash:
27104 +               sbinfo->si_rdhash = opt->rdhash;
27105 +               break;
27106 +       case Opt_rdhash_def:
27107 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27108 +               break;
27109 +
27110 +       case Opt_shwh:
27111 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27112 +               break;
27113 +       case Opt_noshwh:
27114 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27115 +               break;
27116 +
27117 +       case Opt_dirperm1:
27118 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27119 +               break;
27120 +       case Opt_nodirperm1:
27121 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27122 +               break;
27123 +
27124 +       case Opt_trunc_xino:
27125 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27126 +               break;
27127 +       case Opt_notrunc_xino:
27128 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27129 +               break;
27130 +
27131 +       case Opt_trunc_xino_path:
27132 +       case Opt_itrunc_xino:
27133 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27134 +                                   /*idx_begin*/0);
27135 +               if (!err)
27136 +                       err = 1;
27137 +               break;
27138 +
27139 +       case Opt_trunc_xib:
27140 +               au_fset_opts(opts->flags, TRUNC_XIB);
27141 +               break;
27142 +       case Opt_notrunc_xib:
27143 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27144 +               break;
27145 +
27146 +       case Opt_dirren:
27147 +               err = 1;
27148 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27149 +                       err = au_dr_opt_set(sb);
27150 +                       if (!err)
27151 +                               err = 1;
27152 +               }
27153 +               if (err == 1)
27154 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27155 +               break;
27156 +       case Opt_nodirren:
27157 +               err = 1;
27158 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27159 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27160 +                                                             DR_FLUSHED));
27161 +                       if (!err)
27162 +                               err = 1;
27163 +               }
27164 +               if (err == 1)
27165 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27166 +               break;
27167 +
27168 +       case Opt_acl:
27169 +               sb->s_flags |= SB_POSIXACL;
27170 +               break;
27171 +       case Opt_noacl:
27172 +               sb->s_flags &= ~SB_POSIXACL;
27173 +               break;
27174 +
27175 +       default:
27176 +               err = 0;
27177 +               break;
27178 +       }
27179 +
27180 +       return err;
27181 +}
27182 +
27183 +/*
27184 + * returns tri-state.
27185 + * plus: processed without an error
27186 + * zero: unprocessed
27187 + * minus: error
27188 + */
27189 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27190 +                    struct au_opts *opts)
27191 +{
27192 +       int err, do_refresh;
27193 +
27194 +       err = 0;
27195 +       switch (opt->type) {
27196 +       case Opt_append:
27197 +               opt->add.bindex = au_sbbot(sb) + 1;
27198 +               if (opt->add.bindex < 0)
27199 +                       opt->add.bindex = 0;
27200 +               goto add;
27201 +               /* Always goto add, not fallthrough */
27202 +       case Opt_prepend:
27203 +               opt->add.bindex = 0;
27204 +               /* fallthrough */
27205 +       add: /* indented label */
27206 +       case Opt_add:
27207 +               err = au_br_add(sb, &opt->add,
27208 +                               au_ftest_opts(opts->flags, REMOUNT));
27209 +               if (!err) {
27210 +                       err = 1;
27211 +                       au_fset_opts(opts->flags, REFRESH);
27212 +               }
27213 +               break;
27214 +
27215 +       case Opt_del:
27216 +       case Opt_idel:
27217 +               err = au_br_del(sb, &opt->del,
27218 +                               au_ftest_opts(opts->flags, REMOUNT));
27219 +               if (!err) {
27220 +                       err = 1;
27221 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27222 +                       au_fset_opts(opts->flags, REFRESH);
27223 +               }
27224 +               break;
27225 +
27226 +       case Opt_mod:
27227 +       case Opt_imod:
27228 +               err = au_br_mod(sb, &opt->mod,
27229 +                               au_ftest_opts(opts->flags, REMOUNT),
27230 +                               &do_refresh);
27231 +               if (!err) {
27232 +                       err = 1;
27233 +                       if (do_refresh)
27234 +                               au_fset_opts(opts->flags, REFRESH);
27235 +               }
27236 +               break;
27237 +       }
27238 +       return err;
27239 +}
27240 +
27241 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27242 +                      struct au_opt_xino **opt_xino,
27243 +                      struct au_opts *opts)
27244 +{
27245 +       int err;
27246 +
27247 +       err = 0;
27248 +       switch (opt->type) {
27249 +       case Opt_xino:
27250 +               err = au_xino_set(sb, &opt->xino,
27251 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27252 +               if (unlikely(err))
27253 +                       break;
27254 +
27255 +               *opt_xino = &opt->xino;
27256 +               break;
27257 +
27258 +       case Opt_noxino:
27259 +               au_xino_clr(sb);
27260 +               *opt_xino = (void *)-1;
27261 +               break;
27262 +       }
27263 +
27264 +       return err;
27265 +}
27266 +
27267 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27268 +                  unsigned int pending)
27269 +{
27270 +       int err, fhsm;
27271 +       aufs_bindex_t bindex, bbot;
27272 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27273 +       struct au_branch *br;
27274 +       struct au_wbr *wbr;
27275 +       struct dentry *root, *dentry;
27276 +       struct inode *dir, *h_dir;
27277 +       struct au_sbinfo *sbinfo;
27278 +       struct au_hinode *hdir;
27279 +
27280 +       SiMustAnyLock(sb);
27281 +
27282 +       sbinfo = au_sbi(sb);
27283 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27284 +
27285 +       if (!(sb_flags & SB_RDONLY)) {
27286 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27287 +                       pr_warn("first branch should be rw\n");
27288 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27289 +                       pr_warn_once("shwh should be used with ro\n");
27290 +       }
27291 +
27292 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27293 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27294 +               pr_warn_once("udba=*notify requires xino\n");
27295 +
27296 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27297 +               pr_warn_once("dirperm1 breaks the protection"
27298 +                            " by the permission bits on the lower branch\n");
27299 +
27300 +       err = 0;
27301 +       fhsm = 0;
27302 +       root = sb->s_root;
27303 +       dir = d_inode(root);
27304 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27305 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27306 +                                     UDBA_NONE);
27307 +       bbot = au_sbbot(sb);
27308 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27309 +               skip = 0;
27310 +               h_dir = au_h_iptr(dir, bindex);
27311 +               br = au_sbr(sb, bindex);
27312 +
27313 +               if ((br->br_perm & AuBrAttr_ICEX)
27314 +                   && !h_dir->i_op->listxattr)
27315 +                       br->br_perm &= ~AuBrAttr_ICEX;
27316 +#if 0 /* untested */
27317 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27318 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27319 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27320 +#endif
27321 +
27322 +               do_free = 0;
27323 +               wbr = br->br_wbr;
27324 +               if (wbr)
27325 +                       wbr_wh_read_lock(wbr);
27326 +
27327 +               if (!au_br_writable(br->br_perm)) {
27328 +                       do_free = !!wbr;
27329 +                       skip = (!wbr
27330 +                               || (!wbr->wbr_whbase
27331 +                                   && !wbr->wbr_plink
27332 +                                   && !wbr->wbr_orph));
27333 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27334 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27335 +                       skip = (!wbr || !wbr->wbr_whbase);
27336 +                       if (skip && wbr) {
27337 +                               if (do_plink)
27338 +                                       skip = !!wbr->wbr_plink;
27339 +                               else
27340 +                                       skip = !wbr->wbr_plink;
27341 +                       }
27342 +               } else {
27343 +                       /* skip = (br->br_whbase && br->br_ohph); */
27344 +                       skip = (wbr && wbr->wbr_whbase);
27345 +                       if (skip) {
27346 +                               if (do_plink)
27347 +                                       skip = !!wbr->wbr_plink;
27348 +                               else
27349 +                                       skip = !wbr->wbr_plink;
27350 +                       }
27351 +               }
27352 +               if (wbr)
27353 +                       wbr_wh_read_unlock(wbr);
27354 +
27355 +               if (can_no_dreval) {
27356 +                       dentry = br->br_path.dentry;
27357 +                       spin_lock(&dentry->d_lock);
27358 +                       if (dentry->d_flags &
27359 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27360 +                               can_no_dreval = 0;
27361 +                       spin_unlock(&dentry->d_lock);
27362 +               }
27363 +
27364 +               if (au_br_fhsm(br->br_perm)) {
27365 +                       fhsm++;
27366 +                       AuDebugOn(!br->br_fhsm);
27367 +               }
27368 +
27369 +               if (skip)
27370 +                       continue;
27371 +
27372 +               hdir = au_hi(dir, bindex);
27373 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27374 +               if (wbr)
27375 +                       wbr_wh_write_lock(wbr);
27376 +               err = au_wh_init(br, sb);
27377 +               if (wbr)
27378 +                       wbr_wh_write_unlock(wbr);
27379 +               au_hn_inode_unlock(hdir);
27380 +
27381 +               if (!err && do_free) {
27382 +                       au_kfree_rcu(wbr);
27383 +                       br->br_wbr = NULL;
27384 +               }
27385 +       }
27386 +
27387 +       if (can_no_dreval)
27388 +               au_fset_si(sbinfo, NO_DREVAL);
27389 +       else
27390 +               au_fclr_si(sbinfo, NO_DREVAL);
27391 +
27392 +       if (fhsm >= 2) {
27393 +               au_fset_si(sbinfo, FHSM);
27394 +               for (bindex = bbot; bindex >= 0; bindex--) {
27395 +                       br = au_sbr(sb, bindex);
27396 +                       if (au_br_fhsm(br->br_perm)) {
27397 +                               au_fhsm_set_bottom(sb, bindex);
27398 +                               break;
27399 +                       }
27400 +               }
27401 +       } else {
27402 +               au_fclr_si(sbinfo, FHSM);
27403 +               au_fhsm_set_bottom(sb, -1);
27404 +       }
27405 +
27406 +       return err;
27407 +}
27408 +
27409 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27410 +{
27411 +       int err;
27412 +       unsigned int tmp;
27413 +       aufs_bindex_t bindex, bbot;
27414 +       struct au_opt *opt;
27415 +       struct au_opt_xino *opt_xino, xino;
27416 +       struct au_sbinfo *sbinfo;
27417 +       struct au_branch *br;
27418 +       struct inode *dir;
27419 +
27420 +       SiMustWriteLock(sb);
27421 +
27422 +       err = 0;
27423 +       opt_xino = NULL;
27424 +       opt = opts->opt;
27425 +       while (err >= 0 && opt->type != Opt_tail)
27426 +               err = au_opt_simple(sb, opt++, opts);
27427 +       if (err > 0)
27428 +               err = 0;
27429 +       else if (unlikely(err < 0))
27430 +               goto out;
27431 +
27432 +       /* disable xino and udba temporary */
27433 +       sbinfo = au_sbi(sb);
27434 +       tmp = sbinfo->si_mntflags;
27435 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27436 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27437 +
27438 +       opt = opts->opt;
27439 +       while (err >= 0 && opt->type != Opt_tail)
27440 +               err = au_opt_br(sb, opt++, opts);
27441 +       if (err > 0)
27442 +               err = 0;
27443 +       else if (unlikely(err < 0))
27444 +               goto out;
27445 +
27446 +       bbot = au_sbbot(sb);
27447 +       if (unlikely(bbot < 0)) {
27448 +               err = -EINVAL;
27449 +               pr_err("no branches\n");
27450 +               goto out;
27451 +       }
27452 +
27453 +       if (au_opt_test(tmp, XINO))
27454 +               au_opt_set(sbinfo->si_mntflags, XINO);
27455 +       opt = opts->opt;
27456 +       while (!err && opt->type != Opt_tail)
27457 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27458 +       if (unlikely(err))
27459 +               goto out;
27460 +
27461 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27462 +       if (unlikely(err))
27463 +               goto out;
27464 +
27465 +       /* restore xino */
27466 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27467 +               xino.file = au_xino_def(sb);
27468 +               err = PTR_ERR(xino.file);
27469 +               if (IS_ERR(xino.file))
27470 +                       goto out;
27471 +
27472 +               err = au_xino_set(sb, &xino, /*remount*/0);
27473 +               fput(xino.file);
27474 +               if (unlikely(err))
27475 +                       goto out;
27476 +       }
27477 +
27478 +       /* restore udba */
27479 +       tmp &= AuOptMask_UDBA;
27480 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27481 +       sbinfo->si_mntflags |= tmp;
27482 +       bbot = au_sbbot(sb);
27483 +       for (bindex = 0; bindex <= bbot; bindex++) {
27484 +               br = au_sbr(sb, bindex);
27485 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27486 +               if (unlikely(err))
27487 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27488 +                               bindex, err);
27489 +               /* go on even if err */
27490 +       }
27491 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27492 +               dir = d_inode(sb->s_root);
27493 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27494 +       }
27495 +
27496 +out:
27497 +       return err;
27498 +}
27499 +
27500 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27501 +{
27502 +       int err, rerr;
27503 +       unsigned char no_dreval;
27504 +       struct inode *dir;
27505 +       struct au_opt_xino *opt_xino;
27506 +       struct au_opt *opt;
27507 +       struct au_sbinfo *sbinfo;
27508 +
27509 +       SiMustWriteLock(sb);
27510 +
27511 +       err = au_dr_opt_flush(sb);
27512 +       if (unlikely(err))
27513 +               goto out;
27514 +       au_fset_opts(opts->flags, DR_FLUSHED);
27515 +
27516 +       dir = d_inode(sb->s_root);
27517 +       sbinfo = au_sbi(sb);
27518 +       opt_xino = NULL;
27519 +       opt = opts->opt;
27520 +       while (err >= 0 && opt->type != Opt_tail) {
27521 +               err = au_opt_simple(sb, opt, opts);
27522 +               if (!err)
27523 +                       err = au_opt_br(sb, opt, opts);
27524 +               if (!err)
27525 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27526 +               opt++;
27527 +       }
27528 +       if (err > 0)
27529 +               err = 0;
27530 +       AuTraceErr(err);
27531 +       /* go on even err */
27532 +
27533 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27534 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27535 +       if (unlikely(rerr && !err))
27536 +               err = rerr;
27537 +
27538 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27539 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27540 +
27541 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27542 +               rerr = au_xib_trunc(sb);
27543 +               if (unlikely(rerr && !err))
27544 +                       err = rerr;
27545 +       }
27546 +
27547 +       /* will be handled by the caller */
27548 +       if (!au_ftest_opts(opts->flags, REFRESH)
27549 +           && (opts->given_udba
27550 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27551 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27552 +                   ))
27553 +               au_fset_opts(opts->flags, REFRESH);
27554 +
27555 +       AuDbg("status 0x%x\n", opts->flags);
27556 +
27557 +out:
27558 +       return err;
27559 +}
27560 +
27561 +/* ---------------------------------------------------------------------- */
27562 +
27563 +unsigned int au_opt_udba(struct super_block *sb)
27564 +{
27565 +       return au_mntflags(sb) & AuOptMask_UDBA;
27566 +}
27567 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27568 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27569 +++ linux/fs/aufs/opts.h        2020-01-27 10:57:18.175538316 +0100
27570 @@ -0,0 +1,225 @@
27571 +/* SPDX-License-Identifier: GPL-2.0 */
27572 +/*
27573 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27574 + *
27575 + * This program, aufs is free software; you can redistribute it and/or modify
27576 + * it under the terms of the GNU General Public License as published by
27577 + * the Free Software Foundation; either version 2 of the License, or
27578 + * (at your option) any later version.
27579 + *
27580 + * This program is distributed in the hope that it will be useful,
27581 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27582 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27583 + * GNU General Public License for more details.
27584 + *
27585 + * You should have received a copy of the GNU General Public License
27586 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27587 + */
27588 +
27589 +/*
27590 + * mount options/flags
27591 + */
27592 +
27593 +#ifndef __AUFS_OPTS_H__
27594 +#define __AUFS_OPTS_H__
27595 +
27596 +#ifdef __KERNEL__
27597 +
27598 +#include <linux/path.h>
27599 +
27600 +struct file;
27601 +
27602 +/* ---------------------------------------------------------------------- */
27603 +
27604 +/* mount flags */
27605 +#define AuOpt_XINO             1               /* external inode number bitmap
27606 +                                                  and translation table */
27607 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27608 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27609 +#define AuOpt_UDBA_REVAL       (1 << 3)
27610 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27611 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27612 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27613 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27614 +                                                  bits */
27615 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27616 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27617 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27618 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27619 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27620 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27621 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27622 +
27623 +#ifndef CONFIG_AUFS_HNOTIFY
27624 +#undef AuOpt_UDBA_HNOTIFY
27625 +#define AuOpt_UDBA_HNOTIFY     0
27626 +#endif
27627 +#ifndef CONFIG_AUFS_DIRREN
27628 +#undef AuOpt_DIRREN
27629 +#define AuOpt_DIRREN           0
27630 +#endif
27631 +#ifndef CONFIG_AUFS_SHWH
27632 +#undef AuOpt_SHWH
27633 +#define AuOpt_SHWH             0
27634 +#endif
27635 +
27636 +#define AuOpt_Def      (AuOpt_XINO \
27637 +                        | AuOpt_UDBA_REVAL \
27638 +                        | AuOpt_PLINK \
27639 +                        /* | AuOpt_DIRPERM1 */ \
27640 +                        | AuOpt_WARN_PERM)
27641 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27642 +                        | AuOpt_UDBA_REVAL \
27643 +                        | AuOpt_UDBA_HNOTIFY)
27644 +
27645 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27646 +#define au_opt_set(flags, name) do { \
27647 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27648 +       ((flags) |= AuOpt_##name); \
27649 +} while (0)
27650 +#define au_opt_set_udba(flags, name) do { \
27651 +       (flags) &= ~AuOptMask_UDBA; \
27652 +       ((flags) |= AuOpt_##name); \
27653 +} while (0)
27654 +#define au_opt_clr(flags, name) do { \
27655 +       ((flags) &= ~AuOpt_##name); \
27656 +} while (0)
27657 +
27658 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27659 +{
27660 +#ifdef CONFIG_PROC_FS
27661 +       return mntflags;
27662 +#else
27663 +       return mntflags & ~AuOpt_PLINK;
27664 +#endif
27665 +}
27666 +
27667 +/* ---------------------------------------------------------------------- */
27668 +
27669 +/* policies to select one among multiple writable branches */
27670 +enum {
27671 +       AuWbrCreate_TDP,        /* top down parent */
27672 +       AuWbrCreate_RR,         /* round robin */
27673 +       AuWbrCreate_MFS,        /* most free space */
27674 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27675 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27676 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27677 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27678 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27679 +       AuWbrCreate_PMFS,       /* parent and mfs */
27680 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27681 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27682 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27683 +
27684 +       AuWbrCreate_Def = AuWbrCreate_TDP
27685 +};
27686 +
27687 +enum {
27688 +       AuWbrCopyup_TDP,        /* top down parent */
27689 +       AuWbrCopyup_BUP,        /* bottom up parent */
27690 +       AuWbrCopyup_BU,         /* bottom up */
27691 +
27692 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27693 +};
27694 +
27695 +/* ---------------------------------------------------------------------- */
27696 +
27697 +struct au_opt_add {
27698 +       aufs_bindex_t   bindex;
27699 +       char            *pathname;
27700 +       int             perm;
27701 +       struct path     path;
27702 +};
27703 +
27704 +struct au_opt_del {
27705 +       char            *pathname;
27706 +       struct path     h_path;
27707 +};
27708 +
27709 +struct au_opt_mod {
27710 +       char            *path;
27711 +       int             perm;
27712 +       struct dentry   *h_root;
27713 +};
27714 +
27715 +struct au_opt_xino {
27716 +       char            *path;
27717 +       struct file     *file;
27718 +};
27719 +
27720 +struct au_opt_xino_itrunc {
27721 +       aufs_bindex_t   bindex;
27722 +};
27723 +
27724 +struct au_opt_wbr_create {
27725 +       int                     wbr_create;
27726 +       int                     mfs_second;
27727 +       unsigned long long      mfsrr_watermark;
27728 +};
27729 +
27730 +struct au_opt {
27731 +       int type;
27732 +       union {
27733 +               struct au_opt_xino      xino;
27734 +               struct au_opt_xino_itrunc xino_itrunc;
27735 +               struct au_opt_add       add;
27736 +               struct au_opt_del       del;
27737 +               struct au_opt_mod       mod;
27738 +               int                     dirwh;
27739 +               int                     rdcache;
27740 +               unsigned int            rdblk;
27741 +               unsigned int            rdhash;
27742 +               int                     udba;
27743 +               struct au_opt_wbr_create wbr_create;
27744 +               int                     wbr_copyup;
27745 +               unsigned int            fhsm_second;
27746 +       };
27747 +};
27748 +
27749 +/* opts flags */
27750 +#define AuOpts_REMOUNT         1
27751 +#define AuOpts_REFRESH         (1 << 1)
27752 +#define AuOpts_TRUNC_XIB       (1 << 2)
27753 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27754 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27755 +#define AuOpts_DR_FLUSHED      (1 << 5)
27756 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27757 +#define au_fset_opts(flags, name) \
27758 +       do { (flags) |= AuOpts_##name; } while (0)
27759 +#define au_fclr_opts(flags, name) \
27760 +       do { (flags) &= ~AuOpts_##name; } while (0)
27761 +
27762 +#ifndef CONFIG_AUFS_DIRREN
27763 +#undef AuOpts_DR_FLUSHED
27764 +#define AuOpts_DR_FLUSHED      0
27765 +#endif
27766 +
27767 +struct au_opts {
27768 +       struct au_opt   *opt;
27769 +       int             max_opt;
27770 +
27771 +       unsigned int    given_udba;
27772 +       unsigned int    flags;
27773 +       unsigned long   sb_flags;
27774 +};
27775 +
27776 +/* ---------------------------------------------------------------------- */
27777 +
27778 +/* opts.c */
27779 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27780 +const char *au_optstr_udba(int udba);
27781 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27782 +const char *au_optstr_wbr_create(int wbr_create);
27783 +
27784 +void au_opts_free(struct au_opts *opts);
27785 +struct super_block;
27786 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27787 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27788 +                  unsigned int pending);
27789 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27790 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27791 +
27792 +unsigned int au_opt_udba(struct super_block *sb);
27793 +
27794 +#endif /* __KERNEL__ */
27795 +#endif /* __AUFS_OPTS_H__ */
27796 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27797 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27798 +++ linux/fs/aufs/plink.c       2020-01-27 10:57:18.175538316 +0100
27799 @@ -0,0 +1,516 @@
27800 +// SPDX-License-Identifier: GPL-2.0
27801 +/*
27802 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27803 + *
27804 + * This program, aufs is free software; you can redistribute it and/or modify
27805 + * it under the terms of the GNU General Public License as published by
27806 + * the Free Software Foundation; either version 2 of the License, or
27807 + * (at your option) any later version.
27808 + *
27809 + * This program is distributed in the hope that it will be useful,
27810 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27811 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27812 + * GNU General Public License for more details.
27813 + *
27814 + * You should have received a copy of the GNU General Public License
27815 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27816 + */
27817 +
27818 +/*
27819 + * pseudo-link
27820 + */
27821 +
27822 +#include "aufs.h"
27823 +
27824 +/*
27825 + * the pseudo-link maintenance mode.
27826 + * during a user process maintains the pseudo-links,
27827 + * prohibit adding a new plink and branch manipulation.
27828 + *
27829 + * Flags
27830 + * NOPLM:
27831 + *     For entry functions which will handle plink, and i_mutex is already held
27832 + *     in VFS.
27833 + *     They cannot wait and should return an error at once.
27834 + *     Callers has to check the error.
27835 + * NOPLMW:
27836 + *     For entry functions which will handle plink, but i_mutex is not held
27837 + *     in VFS.
27838 + *     They can wait the plink maintenance mode to finish.
27839 + *
27840 + * They behave like F_SETLK and F_SETLKW.
27841 + * If the caller never handle plink, then both flags are unnecessary.
27842 + */
27843 +
27844 +int au_plink_maint(struct super_block *sb, int flags)
27845 +{
27846 +       int err;
27847 +       pid_t pid, ppid;
27848 +       struct task_struct *parent, *prev;
27849 +       struct au_sbinfo *sbi;
27850 +
27851 +       SiMustAnyLock(sb);
27852 +
27853 +       err = 0;
27854 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27855 +               goto out;
27856 +
27857 +       sbi = au_sbi(sb);
27858 +       pid = sbi->si_plink_maint_pid;
27859 +       if (!pid || pid == current->pid)
27860 +               goto out;
27861 +
27862 +       /* todo: it highly depends upon /sbin/mount.aufs */
27863 +       prev = NULL;
27864 +       parent = current;
27865 +       ppid = 0;
27866 +       rcu_read_lock();
27867 +       while (1) {
27868 +               parent = rcu_dereference(parent->real_parent);
27869 +               if (parent == prev)
27870 +                       break;
27871 +               ppid = task_pid_vnr(parent);
27872 +               if (pid == ppid) {
27873 +                       rcu_read_unlock();
27874 +                       goto out;
27875 +               }
27876 +               prev = parent;
27877 +       }
27878 +       rcu_read_unlock();
27879 +
27880 +       if (au_ftest_lock(flags, NOPLMW)) {
27881 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27882 +               /* AuDebugOn(!lockdep_depth(current)); */
27883 +               while (sbi->si_plink_maint_pid) {
27884 +                       si_read_unlock(sb);
27885 +                       /* gave up wake_up_bit() */
27886 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27887 +
27888 +                       if (au_ftest_lock(flags, FLUSH))
27889 +                               au_nwt_flush(&sbi->si_nowait);
27890 +                       si_noflush_read_lock(sb);
27891 +               }
27892 +       } else if (au_ftest_lock(flags, NOPLM)) {
27893 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27894 +               err = -EAGAIN;
27895 +       }
27896 +
27897 +out:
27898 +       return err;
27899 +}
27900 +
27901 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27902 +{
27903 +       spin_lock(&sbinfo->si_plink_maint_lock);
27904 +       sbinfo->si_plink_maint_pid = 0;
27905 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27906 +       wake_up_all(&sbinfo->si_plink_wq);
27907 +}
27908 +
27909 +int au_plink_maint_enter(struct super_block *sb)
27910 +{
27911 +       int err;
27912 +       struct au_sbinfo *sbinfo;
27913 +
27914 +       err = 0;
27915 +       sbinfo = au_sbi(sb);
27916 +       /* make sure i am the only one in this fs */
27917 +       si_write_lock(sb, AuLock_FLUSH);
27918 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27919 +               spin_lock(&sbinfo->si_plink_maint_lock);
27920 +               if (!sbinfo->si_plink_maint_pid)
27921 +                       sbinfo->si_plink_maint_pid = current->pid;
27922 +               else
27923 +                       err = -EBUSY;
27924 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27925 +       }
27926 +       si_write_unlock(sb);
27927 +
27928 +       return err;
27929 +}
27930 +
27931 +/* ---------------------------------------------------------------------- */
27932 +
27933 +#ifdef CONFIG_AUFS_DEBUG
27934 +void au_plink_list(struct super_block *sb)
27935 +{
27936 +       int i;
27937 +       struct au_sbinfo *sbinfo;
27938 +       struct hlist_bl_head *hbl;
27939 +       struct hlist_bl_node *pos;
27940 +       struct au_icntnr *icntnr;
27941 +
27942 +       SiMustAnyLock(sb);
27943 +
27944 +       sbinfo = au_sbi(sb);
27945 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27946 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27947 +
27948 +       for (i = 0; i < AuPlink_NHASH; i++) {
27949 +               hbl = sbinfo->si_plink + i;
27950 +               hlist_bl_lock(hbl);
27951 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27952 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27953 +               hlist_bl_unlock(hbl);
27954 +       }
27955 +}
27956 +#endif
27957 +
27958 +/* is the inode pseudo-linked? */
27959 +int au_plink_test(struct inode *inode)
27960 +{
27961 +       int found, i;
27962 +       struct au_sbinfo *sbinfo;
27963 +       struct hlist_bl_head *hbl;
27964 +       struct hlist_bl_node *pos;
27965 +       struct au_icntnr *icntnr;
27966 +
27967 +       sbinfo = au_sbi(inode->i_sb);
27968 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27969 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27970 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27971 +
27972 +       found = 0;
27973 +       i = au_plink_hash(inode->i_ino);
27974 +       hbl =  sbinfo->si_plink + i;
27975 +       hlist_bl_lock(hbl);
27976 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27977 +               if (&icntnr->vfs_inode == inode) {
27978 +                       found = 1;
27979 +                       break;
27980 +               }
27981 +       hlist_bl_unlock(hbl);
27982 +       return found;
27983 +}
27984 +
27985 +/* ---------------------------------------------------------------------- */
27986 +
27987 +/*
27988 + * generate a name for plink.
27989 + * the file will be stored under AUFS_WH_PLINKDIR.
27990 + */
27991 +/* 20 is max digits length of ulong 64 */
27992 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27993 +
27994 +static int plink_name(char *name, int len, struct inode *inode,
27995 +                     aufs_bindex_t bindex)
27996 +{
27997 +       int rlen;
27998 +       struct inode *h_inode;
27999 +
28000 +       h_inode = au_h_iptr(inode, bindex);
28001 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28002 +       return rlen;
28003 +}
28004 +
28005 +struct au_do_plink_lkup_args {
28006 +       struct dentry **errp;
28007 +       struct qstr *tgtname;
28008 +       struct dentry *h_parent;
28009 +       struct au_branch *br;
28010 +};
28011 +
28012 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28013 +                                      struct dentry *h_parent,
28014 +                                      struct au_branch *br)
28015 +{
28016 +       struct dentry *h_dentry;
28017 +       struct inode *h_inode;
28018 +
28019 +       h_inode = d_inode(h_parent);
28020 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28021 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
28022 +       inode_unlock_shared(h_inode);
28023 +       return h_dentry;
28024 +}
28025 +
28026 +static void au_call_do_plink_lkup(void *args)
28027 +{
28028 +       struct au_do_plink_lkup_args *a = args;
28029 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28030 +}
28031 +
28032 +/* lookup the plink-ed @inode under the branch at @bindex */
28033 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28034 +{
28035 +       struct dentry *h_dentry, *h_parent;
28036 +       struct au_branch *br;
28037 +       int wkq_err;
28038 +       char a[PLINK_NAME_LEN];
28039 +       struct qstr tgtname = QSTR_INIT(a, 0);
28040 +
28041 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28042 +
28043 +       br = au_sbr(inode->i_sb, bindex);
28044 +       h_parent = br->br_wbr->wbr_plink;
28045 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28046 +
28047 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28048 +               struct au_do_plink_lkup_args args = {
28049 +                       .errp           = &h_dentry,
28050 +                       .tgtname        = &tgtname,
28051 +                       .h_parent       = h_parent,
28052 +                       .br             = br
28053 +               };
28054 +
28055 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28056 +               if (unlikely(wkq_err))
28057 +                       h_dentry = ERR_PTR(wkq_err);
28058 +       } else
28059 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28060 +
28061 +       return h_dentry;
28062 +}
28063 +
28064 +/* create a pseudo-link */
28065 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28066 +                     struct dentry *h_dentry, struct au_branch *br)
28067 +{
28068 +       int err;
28069 +       struct path h_path = {
28070 +               .mnt = au_br_mnt(br)
28071 +       };
28072 +       struct inode *h_dir, *delegated;
28073 +
28074 +       h_dir = d_inode(h_parent);
28075 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28076 +again:
28077 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
28078 +       err = PTR_ERR(h_path.dentry);
28079 +       if (IS_ERR(h_path.dentry))
28080 +               goto out;
28081 +
28082 +       err = 0;
28083 +       /* wh.plink dir is not monitored */
28084 +       /* todo: is it really safe? */
28085 +       if (d_is_positive(h_path.dentry)
28086 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28087 +               delegated = NULL;
28088 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28089 +               if (unlikely(err == -EWOULDBLOCK)) {
28090 +                       pr_warn("cannot retry for NFSv4 delegation"
28091 +                               " for an internal unlink\n");
28092 +                       iput(delegated);
28093 +               }
28094 +               dput(h_path.dentry);
28095 +               h_path.dentry = NULL;
28096 +               if (!err)
28097 +                       goto again;
28098 +       }
28099 +       if (!err && d_is_negative(h_path.dentry)) {
28100 +               delegated = NULL;
28101 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28102 +               if (unlikely(err == -EWOULDBLOCK)) {
28103 +                       pr_warn("cannot retry for NFSv4 delegation"
28104 +                               " for an internal link\n");
28105 +                       iput(delegated);
28106 +               }
28107 +       }
28108 +       dput(h_path.dentry);
28109 +
28110 +out:
28111 +       inode_unlock(h_dir);
28112 +       return err;
28113 +}
28114 +
28115 +struct do_whplink_args {
28116 +       int *errp;
28117 +       struct qstr *tgt;
28118 +       struct dentry *h_parent;
28119 +       struct dentry *h_dentry;
28120 +       struct au_branch *br;
28121 +};
28122 +
28123 +static void call_do_whplink(void *args)
28124 +{
28125 +       struct do_whplink_args *a = args;
28126 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28127 +}
28128 +
28129 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28130 +                  aufs_bindex_t bindex, struct au_branch *br)
28131 +{
28132 +       int err, wkq_err;
28133 +       struct au_wbr *wbr;
28134 +       struct dentry *h_parent;
28135 +       char a[PLINK_NAME_LEN];
28136 +       struct qstr tgtname = QSTR_INIT(a, 0);
28137 +
28138 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28139 +       h_parent = wbr->wbr_plink;
28140 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28141 +
28142 +       /* always superio. */
28143 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28144 +               struct do_whplink_args args = {
28145 +                       .errp           = &err,
28146 +                       .tgt            = &tgtname,
28147 +                       .h_parent       = h_parent,
28148 +                       .h_dentry       = h_dentry,
28149 +                       .br             = br
28150 +               };
28151 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28152 +               if (unlikely(wkq_err))
28153 +                       err = wkq_err;
28154 +       } else
28155 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
28156 +
28157 +       return err;
28158 +}
28159 +
28160 +/*
28161 + * create a new pseudo-link for @h_dentry on @bindex.
28162 + * the linked inode is held in aufs @inode.
28163 + */
28164 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28165 +                    struct dentry *h_dentry)
28166 +{
28167 +       struct super_block *sb;
28168 +       struct au_sbinfo *sbinfo;
28169 +       struct hlist_bl_head *hbl;
28170 +       struct hlist_bl_node *pos;
28171 +       struct au_icntnr *icntnr;
28172 +       int found, err, cnt, i;
28173 +
28174 +       sb = inode->i_sb;
28175 +       sbinfo = au_sbi(sb);
28176 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28177 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28178 +
28179 +       found = au_plink_test(inode);
28180 +       if (found)
28181 +               return;
28182 +
28183 +       i = au_plink_hash(inode->i_ino);
28184 +       hbl = sbinfo->si_plink + i;
28185 +       au_igrab(inode);
28186 +
28187 +       hlist_bl_lock(hbl);
28188 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28189 +               if (&icntnr->vfs_inode == inode) {
28190 +                       found = 1;
28191 +                       break;
28192 +               }
28193 +       }
28194 +       if (!found) {
28195 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28196 +               hlist_bl_add_head(&icntnr->plink, hbl);
28197 +       }
28198 +       hlist_bl_unlock(hbl);
28199 +       if (!found) {
28200 +               cnt = au_hbl_count(hbl);
28201 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28202 +               if (cnt > AUFS_PLINK_WARN)
28203 +                       AuWarn1(msg ", %d\n", cnt);
28204 +#undef msg
28205 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28206 +               if (unlikely(err)) {
28207 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28208 +                       au_hbl_del(&icntnr->plink, hbl);
28209 +                       iput(&icntnr->vfs_inode);
28210 +               }
28211 +       } else
28212 +               iput(&icntnr->vfs_inode);
28213 +}
28214 +
28215 +/* free all plinks */
28216 +void au_plink_put(struct super_block *sb, int verbose)
28217 +{
28218 +       int i, warned;
28219 +       struct au_sbinfo *sbinfo;
28220 +       struct hlist_bl_head *hbl;
28221 +       struct hlist_bl_node *pos, *tmp;
28222 +       struct au_icntnr *icntnr;
28223 +
28224 +       SiMustWriteLock(sb);
28225 +
28226 +       sbinfo = au_sbi(sb);
28227 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28228 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28229 +
28230 +       /* no spin_lock since sbinfo is write-locked */
28231 +       warned = 0;
28232 +       for (i = 0; i < AuPlink_NHASH; i++) {
28233 +               hbl = sbinfo->si_plink + i;
28234 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28235 +                       pr_warn("pseudo-link is not flushed");
28236 +                       warned = 1;
28237 +               }
28238 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28239 +                       iput(&icntnr->vfs_inode);
28240 +               INIT_HLIST_BL_HEAD(hbl);
28241 +       }
28242 +}
28243 +
28244 +void au_plink_clean(struct super_block *sb, int verbose)
28245 +{
28246 +       struct dentry *root;
28247 +
28248 +       root = sb->s_root;
28249 +       aufs_write_lock(root);
28250 +       if (au_opt_test(au_mntflags(sb), PLINK))
28251 +               au_plink_put(sb, verbose);
28252 +       aufs_write_unlock(root);
28253 +}
28254 +
28255 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28256 +{
28257 +       int do_put;
28258 +       aufs_bindex_t btop, bbot, bindex;
28259 +
28260 +       do_put = 0;
28261 +       btop = au_ibtop(inode);
28262 +       bbot = au_ibbot(inode);
28263 +       if (btop >= 0) {
28264 +               for (bindex = btop; bindex <= bbot; bindex++) {
28265 +                       if (!au_h_iptr(inode, bindex)
28266 +                           || au_ii_br_id(inode, bindex) != br_id)
28267 +                               continue;
28268 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28269 +                       do_put = 1;
28270 +                       break;
28271 +               }
28272 +               if (do_put)
28273 +                       for (bindex = btop; bindex <= bbot; bindex++)
28274 +                               if (au_h_iptr(inode, bindex)) {
28275 +                                       do_put = 0;
28276 +                                       break;
28277 +                               }
28278 +       } else
28279 +               do_put = 1;
28280 +
28281 +       return do_put;
28282 +}
28283 +
28284 +/* free the plinks on a branch specified by @br_id */
28285 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28286 +{
28287 +       struct au_sbinfo *sbinfo;
28288 +       struct hlist_bl_head *hbl;
28289 +       struct hlist_bl_node *pos, *tmp;
28290 +       struct au_icntnr *icntnr;
28291 +       struct inode *inode;
28292 +       int i, do_put;
28293 +
28294 +       SiMustWriteLock(sb);
28295 +
28296 +       sbinfo = au_sbi(sb);
28297 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28298 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28299 +
28300 +       /* no bit_lock since sbinfo is write-locked */
28301 +       for (i = 0; i < AuPlink_NHASH; i++) {
28302 +               hbl = sbinfo->si_plink + i;
28303 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28304 +                       inode = au_igrab(&icntnr->vfs_inode);
28305 +                       ii_write_lock_child(inode);
28306 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28307 +                       if (do_put) {
28308 +                               hlist_bl_del(&icntnr->plink);
28309 +                               iput(inode);
28310 +                       }
28311 +                       ii_write_unlock(inode);
28312 +                       iput(inode);
28313 +               }
28314 +       }
28315 +}
28316 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28317 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28318 +++ linux/fs/aufs/poll.c        2020-01-27 10:57:18.175538316 +0100
28319 @@ -0,0 +1,51 @@
28320 +// SPDX-License-Identifier: GPL-2.0
28321 +/*
28322 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28323 + *
28324 + * This program, aufs is free software; you can redistribute it and/or modify
28325 + * it under the terms of the GNU General Public License as published by
28326 + * the Free Software Foundation; either version 2 of the License, or
28327 + * (at your option) any later version.
28328 + *
28329 + * This program is distributed in the hope that it will be useful,
28330 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28331 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28332 + * GNU General Public License for more details.
28333 + *
28334 + * You should have received a copy of the GNU General Public License
28335 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28336 + */
28337 +
28338 +/*
28339 + * poll operation
28340 + * There is only one filesystem which implements ->poll operation, currently.
28341 + */
28342 +
28343 +#include "aufs.h"
28344 +
28345 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28346 +{
28347 +       __poll_t mask;
28348 +       struct file *h_file;
28349 +       struct super_block *sb;
28350 +
28351 +       /* We should pretend an error happened. */
28352 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28353 +       sb = file->f_path.dentry->d_sb;
28354 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28355 +
28356 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28357 +       if (IS_ERR(h_file)) {
28358 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28359 +               goto out;
28360 +       }
28361 +
28362 +       mask = vfs_poll(h_file, pt);
28363 +       fput(h_file); /* instead of au_read_post() */
28364 +
28365 +out:
28366 +       si_read_unlock(sb);
28367 +       if (mask & EPOLLERR)
28368 +               AuDbg("mask 0x%x\n", mask);
28369 +       return mask;
28370 +}
28371 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28372 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28373 +++ linux/fs/aufs/posix_acl.c   2020-01-27 10:57:18.175538316 +0100
28374 @@ -0,0 +1,105 @@
28375 +// SPDX-License-Identifier: GPL-2.0
28376 +/*
28377 + * Copyright (C) 2014-2020 Junjiro R. Okajima
28378 + *
28379 + * This program, aufs is free software; you can redistribute it and/or modify
28380 + * it under the terms of the GNU General Public License as published by
28381 + * the Free Software Foundation; either version 2 of the License, or
28382 + * (at your option) any later version.
28383 + *
28384 + * This program is distributed in the hope that it will be useful,
28385 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28386 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28387 + * GNU General Public License for more details.
28388 + *
28389 + * You should have received a copy of the GNU General Public License
28390 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28391 + */
28392 +
28393 +/*
28394 + * posix acl operations
28395 + */
28396 +
28397 +#include <linux/fs.h>
28398 +#include "aufs.h"
28399 +
28400 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28401 +{
28402 +       struct posix_acl *acl;
28403 +       int err;
28404 +       aufs_bindex_t bindex;
28405 +       struct inode *h_inode;
28406 +       struct super_block *sb;
28407 +
28408 +       acl = NULL;
28409 +       sb = inode->i_sb;
28410 +       si_read_lock(sb, AuLock_FLUSH);
28411 +       ii_read_lock_child(inode);
28412 +       if (!(sb->s_flags & SB_POSIXACL))
28413 +               goto out;
28414 +
28415 +       bindex = au_ibtop(inode);
28416 +       h_inode = au_h_iptr(inode, bindex);
28417 +       if (unlikely(!h_inode
28418 +                    || ((h_inode->i_mode & S_IFMT)
28419 +                        != (inode->i_mode & S_IFMT)))) {
28420 +               err = au_busy_or_stale();
28421 +               acl = ERR_PTR(err);
28422 +               goto out;
28423 +       }
28424 +
28425 +       /* always topmost only */
28426 +       acl = get_acl(h_inode, type);
28427 +       if (IS_ERR(acl))
28428 +               forget_cached_acl(inode, type);
28429 +       else
28430 +               set_cached_acl(inode, type, acl);
28431 +
28432 +out:
28433 +       ii_read_unlock(inode);
28434 +       si_read_unlock(sb);
28435 +
28436 +       AuTraceErrPtr(acl);
28437 +       return acl;
28438 +}
28439 +
28440 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28441 +{
28442 +       int err;
28443 +       ssize_t ssz;
28444 +       struct dentry *dentry;
28445 +       struct au_sxattr arg = {
28446 +               .type = AU_ACL_SET,
28447 +               .u.acl_set = {
28448 +                       .acl    = acl,
28449 +                       .type   = type
28450 +               },
28451 +       };
28452 +
28453 +       IMustLock(inode);
28454 +
28455 +       if (inode->i_ino == AUFS_ROOT_INO)
28456 +               dentry = dget(inode->i_sb->s_root);
28457 +       else {
28458 +               dentry = d_find_alias(inode);
28459 +               if (!dentry)
28460 +                       dentry = d_find_any_alias(inode);
28461 +               if (!dentry) {
28462 +                       pr_warn("cannot handle this inode, "
28463 +                               "please report to aufs-users ML\n");
28464 +                       err = -ENOENT;
28465 +                       goto out;
28466 +               }
28467 +       }
28468 +
28469 +       ssz = au_sxattr(dentry, inode, &arg);
28470 +       /* forget even it if succeeds since the branch might set differently */
28471 +       forget_cached_acl(inode, type);
28472 +       dput(dentry);
28473 +       err = ssz;
28474 +       if (ssz >= 0)
28475 +               err = 0;
28476 +
28477 +out:
28478 +       return err;
28479 +}
28480 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28481 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28482 +++ linux/fs/aufs/procfs.c      2020-08-03 09:14:43.142321747 +0200
28483 @@ -0,0 +1,170 @@
28484 +// SPDX-License-Identifier: GPL-2.0
28485 +/*
28486 + * Copyright (C) 2010-2020 Junjiro R. Okajima
28487 + *
28488 + * This program, aufs is free software; you can redistribute it and/or modify
28489 + * it under the terms of the GNU General Public License as published by
28490 + * the Free Software Foundation; either version 2 of the License, or
28491 + * (at your option) any later version.
28492 + *
28493 + * This program is distributed in the hope that it will be useful,
28494 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28495 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28496 + * GNU General Public License for more details.
28497 + *
28498 + * You should have received a copy of the GNU General Public License
28499 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28500 + */
28501 +
28502 +/*
28503 + * procfs interfaces
28504 + */
28505 +
28506 +#include <linux/proc_fs.h>
28507 +#include "aufs.h"
28508 +
28509 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28510 +{
28511 +       struct au_sbinfo *sbinfo;
28512 +
28513 +       sbinfo = file->private_data;
28514 +       if (sbinfo) {
28515 +               au_plink_maint_leave(sbinfo);
28516 +               kobject_put(&sbinfo->si_kobj);
28517 +       }
28518 +
28519 +       return 0;
28520 +}
28521 +
28522 +static void au_procfs_plm_write_clean(struct file *file)
28523 +{
28524 +       struct au_sbinfo *sbinfo;
28525 +
28526 +       sbinfo = file->private_data;
28527 +       if (sbinfo)
28528 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28529 +}
28530 +
28531 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28532 +{
28533 +       int err;
28534 +       struct super_block *sb;
28535 +       struct au_sbinfo *sbinfo;
28536 +       struct hlist_bl_node *pos;
28537 +
28538 +       err = -EBUSY;
28539 +       if (unlikely(file->private_data))
28540 +               goto out;
28541 +
28542 +       sb = NULL;
28543 +       /* don't use au_sbilist_lock() here */
28544 +       hlist_bl_lock(&au_sbilist);
28545 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28546 +               if (id == sysaufs_si_id(sbinfo)) {
28547 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28548 +                               sb = sbinfo->si_sb;
28549 +                       break;
28550 +               }
28551 +       hlist_bl_unlock(&au_sbilist);
28552 +
28553 +       err = -EINVAL;
28554 +       if (unlikely(!sb))
28555 +               goto out;
28556 +
28557 +       err = au_plink_maint_enter(sb);
28558 +       if (!err)
28559 +               /* keep kobject_get() */
28560 +               file->private_data = sbinfo;
28561 +       else
28562 +               kobject_put(&sbinfo->si_kobj);
28563 +out:
28564 +       return err;
28565 +}
28566 +
28567 +/*
28568 + * Accept a valid "si=xxxx" only.
28569 + * Once it is accepted successfully, accept "clean" too.
28570 + */
28571 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28572 +                                  size_t count, loff_t *ppos)
28573 +{
28574 +       ssize_t err;
28575 +       unsigned long id;
28576 +       /* last newline is allowed */
28577 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28578 +
28579 +       err = -EACCES;
28580 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28581 +               goto out;
28582 +
28583 +       err = -EINVAL;
28584 +       if (unlikely(count > sizeof(buf)))
28585 +               goto out;
28586 +
28587 +       err = copy_from_user(buf, ubuf, count);
28588 +       if (unlikely(err)) {
28589 +               err = -EFAULT;
28590 +               goto out;
28591 +       }
28592 +       buf[count] = 0;
28593 +
28594 +       err = -EINVAL;
28595 +       if (!strcmp("clean", buf)) {
28596 +               au_procfs_plm_write_clean(file);
28597 +               goto out_success;
28598 +       } else if (unlikely(strncmp("si=", buf, 3)))
28599 +               goto out;
28600 +
28601 +       err = kstrtoul(buf + 3, 16, &id);
28602 +       if (unlikely(err))
28603 +               goto out;
28604 +
28605 +       err = au_procfs_plm_write_si(file, id);
28606 +       if (unlikely(err))
28607 +               goto out;
28608 +
28609 +out_success:
28610 +       err = count; /* success */
28611 +out:
28612 +       return err;
28613 +}
28614 +
28615 +static const struct proc_ops au_procfs_plm_op = {
28616 +       .proc_write     = au_procfs_plm_write,
28617 +       .proc_release   = au_procfs_plm_release
28618 +};
28619 +
28620 +/* ---------------------------------------------------------------------- */
28621 +
28622 +static struct proc_dir_entry *au_procfs_dir;
28623 +
28624 +void au_procfs_fin(void)
28625 +{
28626 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28627 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28628 +}
28629 +
28630 +int __init au_procfs_init(void)
28631 +{
28632 +       int err;
28633 +       struct proc_dir_entry *entry;
28634 +
28635 +       err = -ENOMEM;
28636 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28637 +       if (unlikely(!au_procfs_dir))
28638 +               goto out;
28639 +
28640 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28641 +                           au_procfs_dir, &au_procfs_plm_op);
28642 +       if (unlikely(!entry))
28643 +               goto out_dir;
28644 +
28645 +       err = 0;
28646 +       goto out; /* success */
28647 +
28648 +
28649 +out_dir:
28650 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28651 +out:
28652 +       return err;
28653 +}
28654 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28655 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28656 +++ linux/fs/aufs/rdu.c 2020-01-27 10:57:18.178871751 +0100
28657 @@ -0,0 +1,384 @@
28658 +// SPDX-License-Identifier: GPL-2.0
28659 +/*
28660 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28661 + *
28662 + * This program, aufs is free software; you can redistribute it and/or modify
28663 + * it under the terms of the GNU General Public License as published by
28664 + * the Free Software Foundation; either version 2 of the License, or
28665 + * (at your option) any later version.
28666 + *
28667 + * This program is distributed in the hope that it will be useful,
28668 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28669 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28670 + * GNU General Public License for more details.
28671 + *
28672 + * You should have received a copy of the GNU General Public License
28673 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28674 + */
28675 +
28676 +/*
28677 + * readdir in userspace.
28678 + */
28679 +
28680 +#include <linux/compat.h>
28681 +#include <linux/fs_stack.h>
28682 +#include <linux/security.h>
28683 +#include "aufs.h"
28684 +
28685 +/* bits for struct aufs_rdu.flags */
28686 +#define        AuRdu_CALLED    1
28687 +#define        AuRdu_CONT      (1 << 1)
28688 +#define        AuRdu_FULL      (1 << 2)
28689 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28690 +#define au_fset_rdu(flags, name) \
28691 +       do { (flags) |= AuRdu_##name; } while (0)
28692 +#define au_fclr_rdu(flags, name) \
28693 +       do { (flags) &= ~AuRdu_##name; } while (0)
28694 +
28695 +struct au_rdu_arg {
28696 +       struct dir_context              ctx;
28697 +       struct aufs_rdu                 *rdu;
28698 +       union au_rdu_ent_ul             ent;
28699 +       unsigned long                   end;
28700 +
28701 +       struct super_block              *sb;
28702 +       int                             err;
28703 +};
28704 +
28705 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28706 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28707 +{
28708 +       int err, len;
28709 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28710 +       struct aufs_rdu *rdu = arg->rdu;
28711 +       struct au_rdu_ent ent;
28712 +
28713 +       err = 0;
28714 +       arg->err = 0;
28715 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28716 +       len = au_rdu_len(nlen);
28717 +       if (arg->ent.ul + len  < arg->end) {
28718 +               ent.ino = h_ino;
28719 +               ent.bindex = rdu->cookie.bindex;
28720 +               ent.type = d_type;
28721 +               ent.nlen = nlen;
28722 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28723 +                       ent.type = DT_UNKNOWN;
28724 +
28725 +               /* unnecessary to support mmap_sem since this is a dir */
28726 +               err = -EFAULT;
28727 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28728 +                       goto out;
28729 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28730 +                       goto out;
28731 +               /* the terminating NULL */
28732 +               if (__put_user(0, arg->ent.e->name + nlen))
28733 +                       goto out;
28734 +               err = 0;
28735 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28736 +               arg->ent.ul += len;
28737 +               rdu->rent++;
28738 +       } else {
28739 +               err = -EFAULT;
28740 +               au_fset_rdu(rdu->cookie.flags, FULL);
28741 +               rdu->full = 1;
28742 +               rdu->tail = arg->ent;
28743 +       }
28744 +
28745 +out:
28746 +       /* AuTraceErr(err); */
28747 +       return err;
28748 +}
28749 +
28750 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28751 +{
28752 +       int err;
28753 +       loff_t offset;
28754 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28755 +
28756 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28757 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28758 +       err = offset;
28759 +       if (unlikely(offset != cookie->h_pos))
28760 +               goto out;
28761 +
28762 +       err = 0;
28763 +       do {
28764 +               arg->err = 0;
28765 +               au_fclr_rdu(cookie->flags, CALLED);
28766 +               /* smp_mb(); */
28767 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28768 +               if (err >= 0)
28769 +                       err = arg->err;
28770 +       } while (!err
28771 +                && au_ftest_rdu(cookie->flags, CALLED)
28772 +                && !au_ftest_rdu(cookie->flags, FULL));
28773 +       cookie->h_pos = h_file->f_pos;
28774 +
28775 +out:
28776 +       AuTraceErr(err);
28777 +       return err;
28778 +}
28779 +
28780 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28781 +{
28782 +       int err;
28783 +       aufs_bindex_t bbot;
28784 +       struct au_rdu_arg arg = {
28785 +               .ctx = {
28786 +                       .actor = au_rdu_fill
28787 +               }
28788 +       };
28789 +       struct dentry *dentry;
28790 +       struct inode *inode;
28791 +       struct file *h_file;
28792 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28793 +
28794 +       /* VERIFY_WRITE */
28795 +       err = !access_ok(rdu->ent.e, rdu->sz);
28796 +       if (unlikely(err)) {
28797 +               err = -EFAULT;
28798 +               AuTraceErr(err);
28799 +               goto out;
28800 +       }
28801 +       rdu->rent = 0;
28802 +       rdu->tail = rdu->ent;
28803 +       rdu->full = 0;
28804 +       arg.rdu = rdu;
28805 +       arg.ent = rdu->ent;
28806 +       arg.end = arg.ent.ul;
28807 +       arg.end += rdu->sz;
28808 +
28809 +       err = -ENOTDIR;
28810 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28811 +               goto out;
28812 +
28813 +       err = security_file_permission(file, MAY_READ);
28814 +       AuTraceErr(err);
28815 +       if (unlikely(err))
28816 +               goto out;
28817 +
28818 +       dentry = file->f_path.dentry;
28819 +       inode = d_inode(dentry);
28820 +       inode_lock_shared(inode);
28821 +
28822 +       arg.sb = inode->i_sb;
28823 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28824 +       if (unlikely(err))
28825 +               goto out_mtx;
28826 +       err = au_alive_dir(dentry);
28827 +       if (unlikely(err))
28828 +               goto out_si;
28829 +       /* todo: reval? */
28830 +       fi_read_lock(file);
28831 +
28832 +       err = -EAGAIN;
28833 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28834 +                    && cookie->generation != au_figen(file)))
28835 +               goto out_unlock;
28836 +
28837 +       err = 0;
28838 +       if (!rdu->blk) {
28839 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28840 +               if (!rdu->blk)
28841 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28842 +       }
28843 +       bbot = au_fbtop(file);
28844 +       if (cookie->bindex < bbot)
28845 +               cookie->bindex = bbot;
28846 +       bbot = au_fbbot_dir(file);
28847 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28848 +       for (; !err && cookie->bindex <= bbot;
28849 +            cookie->bindex++, cookie->h_pos = 0) {
28850 +               h_file = au_hf_dir(file, cookie->bindex);
28851 +               if (!h_file)
28852 +                       continue;
28853 +
28854 +               au_fclr_rdu(cookie->flags, FULL);
28855 +               err = au_rdu_do(h_file, &arg);
28856 +               AuTraceErr(err);
28857 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28858 +                       break;
28859 +       }
28860 +       AuDbg("rent %llu\n", rdu->rent);
28861 +
28862 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28863 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28864 +               au_fset_rdu(cookie->flags, CONT);
28865 +               cookie->generation = au_figen(file);
28866 +       }
28867 +
28868 +       ii_read_lock_child(inode);
28869 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28870 +       ii_read_unlock(inode);
28871 +
28872 +out_unlock:
28873 +       fi_read_unlock(file);
28874 +out_si:
28875 +       si_read_unlock(arg.sb);
28876 +out_mtx:
28877 +       inode_unlock_shared(inode);
28878 +out:
28879 +       AuTraceErr(err);
28880 +       return err;
28881 +}
28882 +
28883 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28884 +{
28885 +       int err;
28886 +       ino_t ino;
28887 +       unsigned long long nent;
28888 +       union au_rdu_ent_ul *u;
28889 +       struct au_rdu_ent ent;
28890 +       struct super_block *sb;
28891 +
28892 +       err = 0;
28893 +       nent = rdu->nent;
28894 +       u = &rdu->ent;
28895 +       sb = file->f_path.dentry->d_sb;
28896 +       si_read_lock(sb, AuLock_FLUSH);
28897 +       while (nent-- > 0) {
28898 +               /* unnecessary to support mmap_sem since this is a dir */
28899 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28900 +               if (!err)
28901 +                       /* VERIFY_WRITE */
28902 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28903 +               if (unlikely(err)) {
28904 +                       err = -EFAULT;
28905 +                       AuTraceErr(err);
28906 +                       break;
28907 +               }
28908 +
28909 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28910 +               if (!ent.wh)
28911 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28912 +               else
28913 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28914 +                                       &ino);
28915 +               if (unlikely(err)) {
28916 +                       AuTraceErr(err);
28917 +                       break;
28918 +               }
28919 +
28920 +               err = __put_user(ino, &u->e->ino);
28921 +               if (unlikely(err)) {
28922 +                       err = -EFAULT;
28923 +                       AuTraceErr(err);
28924 +                       break;
28925 +               }
28926 +               u->ul += au_rdu_len(ent.nlen);
28927 +       }
28928 +       si_read_unlock(sb);
28929 +
28930 +       return err;
28931 +}
28932 +
28933 +/* ---------------------------------------------------------------------- */
28934 +
28935 +static int au_rdu_verify(struct aufs_rdu *rdu)
28936 +{
28937 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28938 +             "%llu, b%d, 0x%x, g%u}\n",
28939 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28940 +             rdu->blk,
28941 +             rdu->rent, rdu->shwh, rdu->full,
28942 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28943 +             rdu->cookie.generation);
28944 +
28945 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28946 +               return 0;
28947 +
28948 +       AuDbg("%u:%u\n",
28949 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28950 +       return -EINVAL;
28951 +}
28952 +
28953 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28954 +{
28955 +       long err, e;
28956 +       struct aufs_rdu rdu;
28957 +       void __user *p = (void __user *)arg;
28958 +
28959 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28960 +       if (unlikely(err)) {
28961 +               err = -EFAULT;
28962 +               AuTraceErr(err);
28963 +               goto out;
28964 +       }
28965 +       err = au_rdu_verify(&rdu);
28966 +       if (unlikely(err))
28967 +               goto out;
28968 +
28969 +       switch (cmd) {
28970 +       case AUFS_CTL_RDU:
28971 +               err = au_rdu(file, &rdu);
28972 +               if (unlikely(err))
28973 +                       break;
28974 +
28975 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28976 +               if (unlikely(e)) {
28977 +                       err = -EFAULT;
28978 +                       AuTraceErr(err);
28979 +               }
28980 +               break;
28981 +       case AUFS_CTL_RDU_INO:
28982 +               err = au_rdu_ino(file, &rdu);
28983 +               break;
28984 +
28985 +       default:
28986 +               /* err = -ENOTTY; */
28987 +               err = -EINVAL;
28988 +       }
28989 +
28990 +out:
28991 +       AuTraceErr(err);
28992 +       return err;
28993 +}
28994 +
28995 +#ifdef CONFIG_COMPAT
28996 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28997 +{
28998 +       long err, e;
28999 +       struct aufs_rdu rdu;
29000 +       void __user *p = compat_ptr(arg);
29001 +
29002 +       /* todo: get_user()? */
29003 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29004 +       if (unlikely(err)) {
29005 +               err = -EFAULT;
29006 +               AuTraceErr(err);
29007 +               goto out;
29008 +       }
29009 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29010 +       err = au_rdu_verify(&rdu);
29011 +       if (unlikely(err))
29012 +               goto out;
29013 +
29014 +       switch (cmd) {
29015 +       case AUFS_CTL_RDU:
29016 +               err = au_rdu(file, &rdu);
29017 +               if (unlikely(err))
29018 +                       break;
29019 +
29020 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29021 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29022 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29023 +               if (unlikely(e)) {
29024 +                       err = -EFAULT;
29025 +                       AuTraceErr(err);
29026 +               }
29027 +               break;
29028 +       case AUFS_CTL_RDU_INO:
29029 +               err = au_rdu_ino(file, &rdu);
29030 +               break;
29031 +
29032 +       default:
29033 +               /* err = -ENOTTY; */
29034 +               err = -EINVAL;
29035 +       }
29036 +
29037 +out:
29038 +       AuTraceErr(err);
29039 +       return err;
29040 +}
29041 +#endif
29042 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29043 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29044 +++ linux/fs/aufs/rwsem.h       2020-01-27 10:57:18.178871751 +0100
29045 @@ -0,0 +1,73 @@
29046 +/* SPDX-License-Identifier: GPL-2.0 */
29047 +/*
29048 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29049 + *
29050 + * This program, aufs is free software; you can redistribute it and/or modify
29051 + * it under the terms of the GNU General Public License as published by
29052 + * the Free Software Foundation; either version 2 of the License, or
29053 + * (at your option) any later version.
29054 + *
29055 + * This program is distributed in the hope that it will be useful,
29056 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29057 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29058 + * GNU General Public License for more details.
29059 + *
29060 + * You should have received a copy of the GNU General Public License
29061 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29062 + */
29063 +
29064 +/*
29065 + * simple read-write semaphore wrappers
29066 + */
29067 +
29068 +#ifndef __AUFS_RWSEM_H__
29069 +#define __AUFS_RWSEM_H__
29070 +
29071 +#ifdef __KERNEL__
29072 +
29073 +#include "debug.h"
29074 +
29075 +/* in the future, the name 'au_rwsem' will be totally gone */
29076 +#define au_rwsem       rw_semaphore
29077 +
29078 +/* to debug easier, do not make them inlined functions */
29079 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29080 +/* rwsem_is_locked() is unusable */
29081 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
29082 +                                         && debug_locks \
29083 +                                         && !lockdep_is_held_type(rw, 1))
29084 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
29085 +                                         && debug_locks \
29086 +                                         && !lockdep_is_held_type(rw, 0))
29087 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
29088 +                                         && debug_locks \
29089 +                                         && !lockdep_is_held(rw))
29090 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
29091 +                                         && debug_locks \
29092 +                                         && lockdep_is_held(rw))
29093 +
29094 +#define au_rw_init(rw) init_rwsem(rw)
29095 +
29096 +#define au_rw_init_wlock(rw) do {              \
29097 +               au_rw_init(rw);                 \
29098 +               down_write(rw);                 \
29099 +       } while (0)
29100 +
29101 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29102 +               au_rw_init(rw);                 \
29103 +               down_write_nested(rw, lsc);     \
29104 +       } while (0)
29105 +
29106 +#define au_rw_read_lock(rw)            down_read(rw)
29107 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29108 +#define au_rw_read_unlock(rw)          up_read(rw)
29109 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29110 +#define au_rw_write_lock(rw)           down_write(rw)
29111 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29112 +#define au_rw_write_unlock(rw)         up_write(rw)
29113 +/* why is not _nested version defined? */
29114 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29115 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29116 +
29117 +#endif /* __KERNEL__ */
29118 +#endif /* __AUFS_RWSEM_H__ */
29119 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29120 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29121 +++ linux/fs/aufs/sbinfo.c      2020-01-27 10:57:18.178871751 +0100
29122 @@ -0,0 +1,314 @@
29123 +// SPDX-License-Identifier: GPL-2.0
29124 +/*
29125 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29126 + *
29127 + * This program, aufs is free software; you can redistribute it and/or modify
29128 + * it under the terms of the GNU General Public License as published by
29129 + * the Free Software Foundation; either version 2 of the License, or
29130 + * (at your option) any later version.
29131 + *
29132 + * This program is distributed in the hope that it will be useful,
29133 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29134 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29135 + * GNU General Public License for more details.
29136 + *
29137 + * You should have received a copy of the GNU General Public License
29138 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29139 + */
29140 +
29141 +/*
29142 + * superblock private data
29143 + */
29144 +
29145 +#include <linux/iversion.h>
29146 +#include "aufs.h"
29147 +
29148 +/*
29149 + * they are necessary regardless sysfs is disabled.
29150 + */
29151 +void au_si_free(struct kobject *kobj)
29152 +{
29153 +       int i;
29154 +       struct au_sbinfo *sbinfo;
29155 +       char *locked __maybe_unused; /* debug only */
29156 +
29157 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29158 +       for (i = 0; i < AuPlink_NHASH; i++)
29159 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29160 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29161 +
29162 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29163 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29164 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29165 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29166 +
29167 +       dbgaufs_si_fin(sbinfo);
29168 +       au_rw_write_lock(&sbinfo->si_rwsem);
29169 +       au_br_free(sbinfo);
29170 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29171 +
29172 +       au_kfree_try_rcu(sbinfo->si_branch);
29173 +       mutex_destroy(&sbinfo->si_xib_mtx);
29174 +       AuRwDestroy(&sbinfo->si_rwsem);
29175 +
29176 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29177 +       /* si_nfiles is waited too */
29178 +       au_kfree_rcu(sbinfo);
29179 +}
29180 +
29181 +int au_si_alloc(struct super_block *sb)
29182 +{
29183 +       int err, i;
29184 +       struct au_sbinfo *sbinfo;
29185 +
29186 +       err = -ENOMEM;
29187 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29188 +       if (unlikely(!sbinfo))
29189 +               goto out;
29190 +
29191 +       /* will be reallocated separately */
29192 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29193 +       if (unlikely(!sbinfo->si_branch))
29194 +               goto out_sbinfo;
29195 +
29196 +       err = sysaufs_si_init(sbinfo);
29197 +       if (!err) {
29198 +               dbgaufs_si_null(sbinfo);
29199 +               err = dbgaufs_si_init(sbinfo);
29200 +               if (unlikely(err))
29201 +                       kobject_put(&sbinfo->si_kobj);
29202 +       }
29203 +       if (unlikely(err))
29204 +               goto out_br;
29205 +
29206 +       au_nwt_init(&sbinfo->si_nowait);
29207 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29208 +
29209 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29210 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29211 +
29212 +       sbinfo->si_bbot = -1;
29213 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29214 +
29215 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29216 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29217 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29218 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29219 +
29220 +       au_fhsm_init(sbinfo);
29221 +
29222 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29223 +
29224 +       sbinfo->si_xino_jiffy = jiffies;
29225 +       sbinfo->si_xino_expire
29226 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29227 +       mutex_init(&sbinfo->si_xib_mtx);
29228 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29229 +
29230 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29231 +
29232 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29233 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29234 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29235 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29236 +
29237 +       for (i = 0; i < AuPlink_NHASH; i++)
29238 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29239 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29240 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29241 +
29242 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29243 +
29244 +       /* with getattr by default */
29245 +       sbinfo->si_iop_array = aufs_iop;
29246 +
29247 +       /* leave other members for sysaufs and si_mnt. */
29248 +       sbinfo->si_sb = sb;
29249 +       sb->s_fs_info = sbinfo;
29250 +       si_pid_set(sb);
29251 +       return 0; /* success */
29252 +
29253 +out_br:
29254 +       au_kfree_try_rcu(sbinfo->si_branch);
29255 +out_sbinfo:
29256 +       au_kfree_rcu(sbinfo);
29257 +out:
29258 +       return err;
29259 +}
29260 +
29261 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29262 +{
29263 +       int err, sz;
29264 +       struct au_branch **brp;
29265 +
29266 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29267 +
29268 +       err = -ENOMEM;
29269 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29270 +       if (unlikely(!sz))
29271 +               sz = sizeof(*brp);
29272 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29273 +                          may_shrink);
29274 +       if (brp) {
29275 +               sbinfo->si_branch = brp;
29276 +               err = 0;
29277 +       }
29278 +
29279 +       return err;
29280 +}
29281 +
29282 +/* ---------------------------------------------------------------------- */
29283 +
29284 +unsigned int au_sigen_inc(struct super_block *sb)
29285 +{
29286 +       unsigned int gen;
29287 +       struct inode *inode;
29288 +
29289 +       SiMustWriteLock(sb);
29290 +
29291 +       gen = ++au_sbi(sb)->si_generation;
29292 +       au_update_digen(sb->s_root);
29293 +       inode = d_inode(sb->s_root);
29294 +       au_update_iigen(inode, /*half*/0);
29295 +       inode_inc_iversion(inode);
29296 +       return gen;
29297 +}
29298 +
29299 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29300 +{
29301 +       aufs_bindex_t br_id;
29302 +       int i;
29303 +       struct au_sbinfo *sbinfo;
29304 +
29305 +       SiMustWriteLock(sb);
29306 +
29307 +       sbinfo = au_sbi(sb);
29308 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29309 +               br_id = ++sbinfo->si_last_br_id;
29310 +               AuDebugOn(br_id < 0);
29311 +               if (br_id && au_br_index(sb, br_id) < 0)
29312 +                       return br_id;
29313 +       }
29314 +
29315 +       return -1;
29316 +}
29317 +
29318 +/* ---------------------------------------------------------------------- */
29319 +
29320 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29321 +int si_read_lock(struct super_block *sb, int flags)
29322 +{
29323 +       int err;
29324 +
29325 +       err = 0;
29326 +       if (au_ftest_lock(flags, FLUSH))
29327 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29328 +
29329 +       si_noflush_read_lock(sb);
29330 +       err = au_plink_maint(sb, flags);
29331 +       if (unlikely(err))
29332 +               si_read_unlock(sb);
29333 +
29334 +       return err;
29335 +}
29336 +
29337 +int si_write_lock(struct super_block *sb, int flags)
29338 +{
29339 +       int err;
29340 +
29341 +       if (au_ftest_lock(flags, FLUSH))
29342 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29343 +
29344 +       si_noflush_write_lock(sb);
29345 +       err = au_plink_maint(sb, flags);
29346 +       if (unlikely(err))
29347 +               si_write_unlock(sb);
29348 +
29349 +       return err;
29350 +}
29351 +
29352 +/* dentry and super_block lock. call at entry point */
29353 +int aufs_read_lock(struct dentry *dentry, int flags)
29354 +{
29355 +       int err;
29356 +       struct super_block *sb;
29357 +
29358 +       sb = dentry->d_sb;
29359 +       err = si_read_lock(sb, flags);
29360 +       if (unlikely(err))
29361 +               goto out;
29362 +
29363 +       if (au_ftest_lock(flags, DW))
29364 +               di_write_lock_child(dentry);
29365 +       else
29366 +               di_read_lock_child(dentry, flags);
29367 +
29368 +       if (au_ftest_lock(flags, GEN)) {
29369 +               err = au_digen_test(dentry, au_sigen(sb));
29370 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29371 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29372 +               else if (!err)
29373 +                       err = au_dbrange_test(dentry);
29374 +               if (unlikely(err))
29375 +                       aufs_read_unlock(dentry, flags);
29376 +       }
29377 +
29378 +out:
29379 +       return err;
29380 +}
29381 +
29382 +void aufs_read_unlock(struct dentry *dentry, int flags)
29383 +{
29384 +       if (au_ftest_lock(flags, DW))
29385 +               di_write_unlock(dentry);
29386 +       else
29387 +               di_read_unlock(dentry, flags);
29388 +       si_read_unlock(dentry->d_sb);
29389 +}
29390 +
29391 +void aufs_write_lock(struct dentry *dentry)
29392 +{
29393 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29394 +       di_write_lock_child(dentry);
29395 +}
29396 +
29397 +void aufs_write_unlock(struct dentry *dentry)
29398 +{
29399 +       di_write_unlock(dentry);
29400 +       si_write_unlock(dentry->d_sb);
29401 +}
29402 +
29403 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29404 +{
29405 +       int err;
29406 +       unsigned int sigen;
29407 +       struct super_block *sb;
29408 +
29409 +       sb = d1->d_sb;
29410 +       err = si_read_lock(sb, flags);
29411 +       if (unlikely(err))
29412 +               goto out;
29413 +
29414 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29415 +
29416 +       if (au_ftest_lock(flags, GEN)) {
29417 +               sigen = au_sigen(sb);
29418 +               err = au_digen_test(d1, sigen);
29419 +               AuDebugOn(!err && au_dbrange_test(d1));
29420 +               if (!err) {
29421 +                       err = au_digen_test(d2, sigen);
29422 +                       AuDebugOn(!err && au_dbrange_test(d2));
29423 +               }
29424 +               if (unlikely(err))
29425 +                       aufs_read_and_write_unlock2(d1, d2);
29426 +       }
29427 +
29428 +out:
29429 +       return err;
29430 +}
29431 +
29432 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29433 +{
29434 +       di_write_unlock2(d1, d2);
29435 +       si_read_unlock(d1->d_sb);
29436 +}
29437 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29438 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29439 +++ linux/fs/aufs/super.c       2020-01-27 10:57:18.178871751 +0100
29440 @@ -0,0 +1,1047 @@
29441 +// SPDX-License-Identifier: GPL-2.0
29442 +/*
29443 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29444 + *
29445 + * This program, aufs is free software; you can redistribute it and/or modify
29446 + * it under the terms of the GNU General Public License as published by
29447 + * the Free Software Foundation; either version 2 of the License, or
29448 + * (at your option) any later version.
29449 + *
29450 + * This program is distributed in the hope that it will be useful,
29451 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29452 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29453 + * GNU General Public License for more details.
29454 + *
29455 + * You should have received a copy of the GNU General Public License
29456 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29457 + */
29458 +
29459 +/*
29460 + * mount and super_block operations
29461 + */
29462 +
29463 +#include <linux/iversion.h>
29464 +#include <linux/mm.h>
29465 +#include <linux/seq_file.h>
29466 +#include <linux/statfs.h>
29467 +#include <linux/vmalloc.h>
29468 +#include "aufs.h"
29469 +
29470 +/*
29471 + * super_operations
29472 + */
29473 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29474 +{
29475 +       struct au_icntnr *c;
29476 +
29477 +       c = au_cache_alloc_icntnr();
29478 +       if (c) {
29479 +               au_icntnr_init(c);
29480 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29481 +               c->iinfo.ii_hinode = NULL;
29482 +               return &c->vfs_inode;
29483 +       }
29484 +       return NULL;
29485 +}
29486 +
29487 +static void aufs_destroy_inode(struct inode *inode)
29488 +{
29489 +       if (!au_is_bad_inode(inode))
29490 +               au_iinfo_fin(inode);
29491 +}
29492 +
29493 +static void aufs_free_inode(struct inode *inode)
29494 +{
29495 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29496 +}
29497 +
29498 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29499 +{
29500 +       struct inode *inode;
29501 +       int err;
29502 +
29503 +       inode = iget_locked(sb, ino);
29504 +       if (unlikely(!inode)) {
29505 +               inode = ERR_PTR(-ENOMEM);
29506 +               goto out;
29507 +       }
29508 +       if (!(inode->i_state & I_NEW))
29509 +               goto out;
29510 +
29511 +       err = au_xigen_new(inode);
29512 +       if (!err)
29513 +               err = au_iinfo_init(inode);
29514 +       if (!err)
29515 +               inode_inc_iversion(inode);
29516 +       else {
29517 +               iget_failed(inode);
29518 +               inode = ERR_PTR(err);
29519 +       }
29520 +
29521 +out:
29522 +       /* never return NULL */
29523 +       AuDebugOn(!inode);
29524 +       AuTraceErrPtr(inode);
29525 +       return inode;
29526 +}
29527 +
29528 +/* lock free root dinfo */
29529 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29530 +{
29531 +       int err;
29532 +       aufs_bindex_t bindex, bbot;
29533 +       struct path path;
29534 +       struct au_hdentry *hdp;
29535 +       struct au_branch *br;
29536 +       au_br_perm_str_t perm;
29537 +
29538 +       err = 0;
29539 +       bbot = au_sbbot(sb);
29540 +       bindex = 0;
29541 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29542 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29543 +               br = au_sbr(sb, bindex);
29544 +               path.mnt = au_br_mnt(br);
29545 +               path.dentry = hdp->hd_dentry;
29546 +               err = au_seq_path(seq, &path);
29547 +               if (!err) {
29548 +                       au_optstr_br_perm(&perm, br->br_perm);
29549 +                       seq_printf(seq, "=%s", perm.a);
29550 +                       if (bindex != bbot)
29551 +                               seq_putc(seq, ':');
29552 +               }
29553 +       }
29554 +       if (unlikely(err || seq_has_overflowed(seq)))
29555 +               err = -E2BIG;
29556 +
29557 +       return err;
29558 +}
29559 +
29560 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29561 +                      const char *append)
29562 +{
29563 +       char *p;
29564 +
29565 +       p = fmt;
29566 +       while (*pat != ':')
29567 +               *p++ = *pat++;
29568 +       *p++ = *pat++;
29569 +       strcpy(p, append);
29570 +       AuDebugOn(strlen(fmt) >= len);
29571 +}
29572 +
29573 +static void au_show_wbr_create(struct seq_file *m, int v,
29574 +                              struct au_sbinfo *sbinfo)
29575 +{
29576 +       const char *pat;
29577 +       char fmt[32];
29578 +       struct au_wbr_mfs *mfs;
29579 +
29580 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29581 +
29582 +       seq_puts(m, ",create=");
29583 +       pat = au_optstr_wbr_create(v);
29584 +       mfs = &sbinfo->si_wbr_mfs;
29585 +       switch (v) {
29586 +       case AuWbrCreate_TDP:
29587 +       case AuWbrCreate_RR:
29588 +       case AuWbrCreate_MFS:
29589 +       case AuWbrCreate_PMFS:
29590 +               seq_puts(m, pat);
29591 +               break;
29592 +       case AuWbrCreate_MFSRR:
29593 +       case AuWbrCreate_TDMFS:
29594 +       case AuWbrCreate_PMFSRR:
29595 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29596 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29597 +               break;
29598 +       case AuWbrCreate_MFSV:
29599 +       case AuWbrCreate_PMFSV:
29600 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29601 +               seq_printf(m, fmt,
29602 +                          jiffies_to_msecs(mfs->mfs_expire)
29603 +                          / MSEC_PER_SEC);
29604 +               break;
29605 +       case AuWbrCreate_MFSRRV:
29606 +       case AuWbrCreate_TDMFSV:
29607 +       case AuWbrCreate_PMFSRRV:
29608 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29609 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29610 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29611 +               break;
29612 +       default:
29613 +               BUG();
29614 +       }
29615 +}
29616 +
29617 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29618 +{
29619 +#ifdef CONFIG_SYSFS
29620 +       return 0;
29621 +#else
29622 +       int err;
29623 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29624 +       aufs_bindex_t bindex, brid;
29625 +       struct qstr *name;
29626 +       struct file *f;
29627 +       struct dentry *d, *h_root;
29628 +       struct au_branch *br;
29629 +
29630 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29631 +
29632 +       err = 0;
29633 +       f = au_sbi(sb)->si_xib;
29634 +       if (!f)
29635 +               goto out;
29636 +
29637 +       /* stop printing the default xino path on the first writable branch */
29638 +       h_root = NULL;
29639 +       bindex = au_xi_root(sb, f->f_path.dentry);
29640 +       if (bindex >= 0) {
29641 +               br = au_sbr_sb(sb, bindex);
29642 +               h_root = au_br_dentry(br);
29643 +       }
29644 +
29645 +       d = f->f_path.dentry;
29646 +       name = &d->d_name;
29647 +       /* safe ->d_parent because the file is unlinked */
29648 +       if (d->d_parent == h_root
29649 +           && name->len == len
29650 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29651 +               goto out;
29652 +
29653 +       seq_puts(seq, ",xino=");
29654 +       err = au_xino_path(seq, f);
29655 +
29656 +out:
29657 +       return err;
29658 +#endif
29659 +}
29660 +
29661 +/* seq_file will re-call me in case of too long string */
29662 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29663 +{
29664 +       int err;
29665 +       unsigned int mnt_flags, v;
29666 +       struct super_block *sb;
29667 +       struct au_sbinfo *sbinfo;
29668 +
29669 +#define AuBool(name, str) do { \
29670 +       v = au_opt_test(mnt_flags, name); \
29671 +       if (v != au_opt_test(AuOpt_Def, name)) \
29672 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29673 +} while (0)
29674 +
29675 +#define AuStr(name, str) do { \
29676 +       v = mnt_flags & AuOptMask_##name; \
29677 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29678 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29679 +} while (0)
29680 +
29681 +#define AuUInt(name, str, val) do { \
29682 +       if (val != AUFS_##name##_DEF) \
29683 +               seq_printf(m, "," #str "=%u", val); \
29684 +} while (0)
29685 +
29686 +       sb = dentry->d_sb;
29687 +       if (sb->s_flags & SB_POSIXACL)
29688 +               seq_puts(m, ",acl");
29689 +#if 0 /* reserved for future use */
29690 +       if (sb->s_flags & SB_I_VERSION)
29691 +               seq_puts(m, ",i_version");
29692 +#endif
29693 +
29694 +       /* lock free root dinfo */
29695 +       si_noflush_read_lock(sb);
29696 +       sbinfo = au_sbi(sb);
29697 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29698 +
29699 +       mnt_flags = au_mntflags(sb);
29700 +       if (au_opt_test(mnt_flags, XINO)) {
29701 +               err = au_show_xino(m, sb);
29702 +               if (unlikely(err))
29703 +                       goto out;
29704 +       } else
29705 +               seq_puts(m, ",noxino");
29706 +
29707 +       AuBool(TRUNC_XINO, trunc_xino);
29708 +       AuStr(UDBA, udba);
29709 +       AuBool(SHWH, shwh);
29710 +       AuBool(PLINK, plink);
29711 +       AuBool(DIO, dio);
29712 +       AuBool(DIRPERM1, dirperm1);
29713 +
29714 +       v = sbinfo->si_wbr_create;
29715 +       if (v != AuWbrCreate_Def)
29716 +               au_show_wbr_create(m, v, sbinfo);
29717 +
29718 +       v = sbinfo->si_wbr_copyup;
29719 +       if (v != AuWbrCopyup_Def)
29720 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29721 +
29722 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29723 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29724 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29725 +
29726 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29727 +
29728 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29729 +       AuUInt(RDCACHE, rdcache, v);
29730 +
29731 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29732 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29733 +
29734 +       au_fhsm_show(m, sbinfo);
29735 +
29736 +       AuBool(DIRREN, dirren);
29737 +       AuBool(SUM, sum);
29738 +       /* AuBool(SUM_W, wsum); */
29739 +       AuBool(WARN_PERM, warn_perm);
29740 +       AuBool(VERBOSE, verbose);
29741 +
29742 +out:
29743 +       /* be sure to print "br:" last */
29744 +       if (!sysaufs_brs) {
29745 +               seq_puts(m, ",br:");
29746 +               au_show_brs(m, sb);
29747 +       }
29748 +       si_read_unlock(sb);
29749 +       return 0;
29750 +
29751 +#undef AuBool
29752 +#undef AuStr
29753 +#undef AuUInt
29754 +}
29755 +
29756 +/* ---------------------------------------------------------------------- */
29757 +
29758 +/* sum mode which returns the summation for statfs(2) */
29759 +
29760 +static u64 au_add_till_max(u64 a, u64 b)
29761 +{
29762 +       u64 old;
29763 +
29764 +       old = a;
29765 +       a += b;
29766 +       if (old <= a)
29767 +               return a;
29768 +       return ULLONG_MAX;
29769 +}
29770 +
29771 +static u64 au_mul_till_max(u64 a, long mul)
29772 +{
29773 +       u64 old;
29774 +
29775 +       old = a;
29776 +       a *= mul;
29777 +       if (old <= a)
29778 +               return a;
29779 +       return ULLONG_MAX;
29780 +}
29781 +
29782 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29783 +{
29784 +       int err;
29785 +       long bsize, factor;
29786 +       u64 blocks, bfree, bavail, files, ffree;
29787 +       aufs_bindex_t bbot, bindex, i;
29788 +       unsigned char shared;
29789 +       struct path h_path;
29790 +       struct super_block *h_sb;
29791 +
29792 +       err = 0;
29793 +       bsize = LONG_MAX;
29794 +       files = 0;
29795 +       ffree = 0;
29796 +       blocks = 0;
29797 +       bfree = 0;
29798 +       bavail = 0;
29799 +       bbot = au_sbbot(sb);
29800 +       for (bindex = 0; bindex <= bbot; bindex++) {
29801 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29802 +               h_sb = h_path.mnt->mnt_sb;
29803 +               shared = 0;
29804 +               for (i = 0; !shared && i < bindex; i++)
29805 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29806 +               if (shared)
29807 +                       continue;
29808 +
29809 +               /* sb->s_root for NFS is unreliable */
29810 +               h_path.dentry = h_path.mnt->mnt_root;
29811 +               err = vfs_statfs(&h_path, buf);
29812 +               if (unlikely(err))
29813 +                       goto out;
29814 +
29815 +               if (bsize > buf->f_bsize) {
29816 +                       /*
29817 +                        * we will reduce bsize, so we have to expand blocks
29818 +                        * etc. to match them again
29819 +                        */
29820 +                       factor = (bsize / buf->f_bsize);
29821 +                       blocks = au_mul_till_max(blocks, factor);
29822 +                       bfree = au_mul_till_max(bfree, factor);
29823 +                       bavail = au_mul_till_max(bavail, factor);
29824 +                       bsize = buf->f_bsize;
29825 +               }
29826 +
29827 +               factor = (buf->f_bsize / bsize);
29828 +               blocks = au_add_till_max(blocks,
29829 +                               au_mul_till_max(buf->f_blocks, factor));
29830 +               bfree = au_add_till_max(bfree,
29831 +                               au_mul_till_max(buf->f_bfree, factor));
29832 +               bavail = au_add_till_max(bavail,
29833 +                               au_mul_till_max(buf->f_bavail, factor));
29834 +               files = au_add_till_max(files, buf->f_files);
29835 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29836 +       }
29837 +
29838 +       buf->f_bsize = bsize;
29839 +       buf->f_blocks = blocks;
29840 +       buf->f_bfree = bfree;
29841 +       buf->f_bavail = bavail;
29842 +       buf->f_files = files;
29843 +       buf->f_ffree = ffree;
29844 +       buf->f_frsize = 0;
29845 +
29846 +out:
29847 +       return err;
29848 +}
29849 +
29850 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29851 +{
29852 +       int err;
29853 +       struct path h_path;
29854 +       struct super_block *sb;
29855 +
29856 +       /* lock free root dinfo */
29857 +       sb = dentry->d_sb;
29858 +       si_noflush_read_lock(sb);
29859 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29860 +               /* sb->s_root for NFS is unreliable */
29861 +               h_path.mnt = au_sbr_mnt(sb, 0);
29862 +               h_path.dentry = h_path.mnt->mnt_root;
29863 +               err = vfs_statfs(&h_path, buf);
29864 +       } else
29865 +               err = au_statfs_sum(sb, buf);
29866 +       si_read_unlock(sb);
29867 +
29868 +       if (!err) {
29869 +               buf->f_type = AUFS_SUPER_MAGIC;
29870 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29871 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29872 +       }
29873 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29874 +
29875 +       return err;
29876 +}
29877 +
29878 +/* ---------------------------------------------------------------------- */
29879 +
29880 +static int aufs_sync_fs(struct super_block *sb, int wait)
29881 +{
29882 +       int err, e;
29883 +       aufs_bindex_t bbot, bindex;
29884 +       struct au_branch *br;
29885 +       struct super_block *h_sb;
29886 +
29887 +       err = 0;
29888 +       si_noflush_read_lock(sb);
29889 +       bbot = au_sbbot(sb);
29890 +       for (bindex = 0; bindex <= bbot; bindex++) {
29891 +               br = au_sbr(sb, bindex);
29892 +               if (!au_br_writable(br->br_perm))
29893 +                       continue;
29894 +
29895 +               h_sb = au_sbr_sb(sb, bindex);
29896 +               e = vfsub_sync_filesystem(h_sb, wait);
29897 +               if (unlikely(e && !err))
29898 +                       err = e;
29899 +               /* go on even if an error happens */
29900 +       }
29901 +       si_read_unlock(sb);
29902 +
29903 +       return err;
29904 +}
29905 +
29906 +/* ---------------------------------------------------------------------- */
29907 +
29908 +/* final actions when unmounting a file system */
29909 +static void aufs_put_super(struct super_block *sb)
29910 +{
29911 +       struct au_sbinfo *sbinfo;
29912 +
29913 +       sbinfo = au_sbi(sb);
29914 +       if (sbinfo)
29915 +               kobject_put(&sbinfo->si_kobj);
29916 +}
29917 +
29918 +/* ---------------------------------------------------------------------- */
29919 +
29920 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29921 +                    struct super_block *sb, void *arg)
29922 +{
29923 +       void *array;
29924 +       unsigned long long n, sz;
29925 +
29926 +       array = NULL;
29927 +       n = 0;
29928 +       if (!*hint)
29929 +               goto out;
29930 +
29931 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29932 +               array = ERR_PTR(-EMFILE);
29933 +               pr_err("hint %llu\n", *hint);
29934 +               goto out;
29935 +       }
29936 +
29937 +       sz = sizeof(array) * *hint;
29938 +       array = kzalloc(sz, GFP_NOFS);
29939 +       if (unlikely(!array))
29940 +               array = vzalloc(sz);
29941 +       if (unlikely(!array)) {
29942 +               array = ERR_PTR(-ENOMEM);
29943 +               goto out;
29944 +       }
29945 +
29946 +       n = cb(sb, array, *hint, arg);
29947 +       AuDebugOn(n > *hint);
29948 +
29949 +out:
29950 +       *hint = n;
29951 +       return array;
29952 +}
29953 +
29954 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29955 +                                      unsigned long long max __maybe_unused,
29956 +                                      void *arg)
29957 +{
29958 +       unsigned long long n;
29959 +       struct inode **p, *inode;
29960 +       struct list_head *head;
29961 +
29962 +       n = 0;
29963 +       p = a;
29964 +       head = arg;
29965 +       spin_lock(&sb->s_inode_list_lock);
29966 +       list_for_each_entry(inode, head, i_sb_list) {
29967 +               if (!au_is_bad_inode(inode)
29968 +                   && au_ii(inode)->ii_btop >= 0) {
29969 +                       spin_lock(&inode->i_lock);
29970 +                       if (atomic_read(&inode->i_count)) {
29971 +                               au_igrab(inode);
29972 +                               *p++ = inode;
29973 +                               n++;
29974 +                               AuDebugOn(n > max);
29975 +                       }
29976 +                       spin_unlock(&inode->i_lock);
29977 +               }
29978 +       }
29979 +       spin_unlock(&sb->s_inode_list_lock);
29980 +
29981 +       return n;
29982 +}
29983 +
29984 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29985 +{
29986 +       struct au_sbinfo *sbi;
29987 +
29988 +       sbi = au_sbi(sb);
29989 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29990 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29991 +}
29992 +
29993 +void au_iarray_free(struct inode **a, unsigned long long max)
29994 +{
29995 +       unsigned long long ull;
29996 +
29997 +       for (ull = 0; ull < max; ull++)
29998 +               iput(a[ull]);
29999 +       kvfree(a);
30000 +}
30001 +
30002 +/* ---------------------------------------------------------------------- */
30003 +
30004 +/*
30005 + * refresh dentry and inode at remount time.
30006 + */
30007 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30008 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30009 +                     struct dentry *parent)
30010 +{
30011 +       int err;
30012 +
30013 +       di_write_lock_child(dentry);
30014 +       di_read_lock_parent(parent, AuLock_IR);
30015 +       err = au_refresh_dentry(dentry, parent);
30016 +       if (!err && dir_flags)
30017 +               au_hn_reset(d_inode(dentry), dir_flags);
30018 +       di_read_unlock(parent, AuLock_IR);
30019 +       di_write_unlock(dentry);
30020 +
30021 +       return err;
30022 +}
30023 +
30024 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30025 +                          struct au_sbinfo *sbinfo,
30026 +                          const unsigned int dir_flags, unsigned int do_idop)
30027 +{
30028 +       int err;
30029 +       struct dentry *parent;
30030 +
30031 +       err = 0;
30032 +       parent = dget_parent(dentry);
30033 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30034 +               if (d_really_is_positive(dentry)) {
30035 +                       if (!d_is_dir(dentry))
30036 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30037 +                                                parent);
30038 +                       else {
30039 +                               err = au_do_refresh(dentry, dir_flags, parent);
30040 +                               if (unlikely(err))
30041 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30042 +                       }
30043 +               } else
30044 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30045 +               AuDbgDentry(dentry);
30046 +       }
30047 +       dput(parent);
30048 +
30049 +       if (!err) {
30050 +               if (do_idop)
30051 +                       au_refresh_dop(dentry, /*force_reval*/0);
30052 +       } else
30053 +               au_refresh_dop(dentry, /*force_reval*/1);
30054 +
30055 +       AuTraceErr(err);
30056 +       return err;
30057 +}
30058 +
30059 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30060 +{
30061 +       int err, i, j, ndentry, e;
30062 +       unsigned int sigen;
30063 +       struct au_dcsub_pages dpages;
30064 +       struct au_dpage *dpage;
30065 +       struct dentry **dentries, *d;
30066 +       struct au_sbinfo *sbinfo;
30067 +       struct dentry *root = sb->s_root;
30068 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30069 +
30070 +       if (do_idop)
30071 +               au_refresh_dop(root, /*force_reval*/0);
30072 +
30073 +       err = au_dpages_init(&dpages, GFP_NOFS);
30074 +       if (unlikely(err))
30075 +               goto out;
30076 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30077 +       if (unlikely(err))
30078 +               goto out_dpages;
30079 +
30080 +       sigen = au_sigen(sb);
30081 +       sbinfo = au_sbi(sb);
30082 +       for (i = 0; i < dpages.ndpage; i++) {
30083 +               dpage = dpages.dpages + i;
30084 +               dentries = dpage->dentries;
30085 +               ndentry = dpage->ndentry;
30086 +               for (j = 0; j < ndentry; j++) {
30087 +                       d = dentries[j];
30088 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30089 +                                           do_idop);
30090 +                       if (unlikely(e && !err))
30091 +                               err = e;
30092 +                       /* go on even err */
30093 +               }
30094 +       }
30095 +
30096 +out_dpages:
30097 +       au_dpages_free(&dpages);
30098 +out:
30099 +       return err;
30100 +}
30101 +
30102 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30103 +{
30104 +       int err, e;
30105 +       unsigned int sigen;
30106 +       unsigned long long max, ull;
30107 +       struct inode *inode, **array;
30108 +
30109 +       array = au_iarray_alloc(sb, &max);
30110 +       err = PTR_ERR(array);
30111 +       if (IS_ERR(array))
30112 +               goto out;
30113 +
30114 +       err = 0;
30115 +       sigen = au_sigen(sb);
30116 +       for (ull = 0; ull < max; ull++) {
30117 +               inode = array[ull];
30118 +               if (unlikely(!inode))
30119 +                       break;
30120 +
30121 +               e = 0;
30122 +               ii_write_lock_child(inode);
30123 +               if (au_iigen(inode, NULL) != sigen) {
30124 +                       e = au_refresh_hinode_self(inode);
30125 +                       if (unlikely(e)) {
30126 +                               au_refresh_iop(inode, /*force_getattr*/1);
30127 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30128 +                               if (!err)
30129 +                                       err = e;
30130 +                               /* go on even if err */
30131 +                       }
30132 +               }
30133 +               if (!e && do_idop)
30134 +                       au_refresh_iop(inode, /*force_getattr*/0);
30135 +               ii_write_unlock(inode);
30136 +       }
30137 +
30138 +       au_iarray_free(array, max);
30139 +
30140 +out:
30141 +       return err;
30142 +}
30143 +
30144 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30145 +{
30146 +       int err, e;
30147 +       unsigned int udba;
30148 +       aufs_bindex_t bindex, bbot;
30149 +       struct dentry *root;
30150 +       struct inode *inode;
30151 +       struct au_branch *br;
30152 +       struct au_sbinfo *sbi;
30153 +
30154 +       au_sigen_inc(sb);
30155 +       sbi = au_sbi(sb);
30156 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30157 +
30158 +       root = sb->s_root;
30159 +       DiMustNoWaiters(root);
30160 +       inode = d_inode(root);
30161 +       IiMustNoWaiters(inode);
30162 +
30163 +       udba = au_opt_udba(sb);
30164 +       bbot = au_sbbot(sb);
30165 +       for (bindex = 0; bindex <= bbot; bindex++) {
30166 +               br = au_sbr(sb, bindex);
30167 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30168 +               if (unlikely(err))
30169 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30170 +                               bindex, err);
30171 +               /* go on even if err */
30172 +       }
30173 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30174 +
30175 +       if (do_idop) {
30176 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30177 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30178 +                       sb->s_d_op = &aufs_dop_noreval;
30179 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30180 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30181 +               } else {
30182 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30183 +                       sb->s_d_op = &aufs_dop;
30184 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30185 +                       sbi->si_iop_array = aufs_iop;
30186 +               }
30187 +               pr_info("reset to %ps and %ps\n",
30188 +                       sb->s_d_op, sbi->si_iop_array);
30189 +       }
30190 +
30191 +       di_write_unlock(root);
30192 +       err = au_refresh_d(sb, do_idop);
30193 +       e = au_refresh_i(sb, do_idop);
30194 +       if (unlikely(e && !err))
30195 +               err = e;
30196 +       /* aufs_write_lock() calls ..._child() */
30197 +       di_write_lock_child(root);
30198 +
30199 +       au_cpup_attr_all(inode, /*force*/1);
30200 +
30201 +       if (unlikely(err))
30202 +               AuIOErr("refresh failed, ignored, %d\n", err);
30203 +}
30204 +
30205 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30206 +static int cvt_err(int err)
30207 +{
30208 +       AuTraceErr(err);
30209 +
30210 +       switch (err) {
30211 +       case -ENOENT:
30212 +       case -ENOTDIR:
30213 +       case -EEXIST:
30214 +       case -EIO:
30215 +               err = -EINVAL;
30216 +       }
30217 +       return err;
30218 +}
30219 +
30220 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30221 +{
30222 +       int err, do_dx;
30223 +       unsigned int mntflags;
30224 +       struct au_opts opts = {
30225 +               .opt = NULL
30226 +       };
30227 +       struct dentry *root;
30228 +       struct inode *inode;
30229 +       struct au_sbinfo *sbinfo;
30230 +
30231 +       err = 0;
30232 +       root = sb->s_root;
30233 +       if (!data || !*data) {
30234 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30235 +               if (!err) {
30236 +                       di_write_lock_child(root);
30237 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30238 +                       aufs_write_unlock(root);
30239 +               }
30240 +               goto out;
30241 +       }
30242 +
30243 +       err = -ENOMEM;
30244 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30245 +       if (unlikely(!opts.opt))
30246 +               goto out;
30247 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30248 +       opts.flags = AuOpts_REMOUNT;
30249 +       opts.sb_flags = *flags;
30250 +
30251 +       /* parse it before aufs lock */
30252 +       err = au_opts_parse(sb, data, &opts);
30253 +       if (unlikely(err))
30254 +               goto out_opts;
30255 +
30256 +       sbinfo = au_sbi(sb);
30257 +       inode = d_inode(root);
30258 +       inode_lock(inode);
30259 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30260 +       if (unlikely(err))
30261 +               goto out_mtx;
30262 +       di_write_lock_child(root);
30263 +
30264 +       /* au_opts_remount() may return an error */
30265 +       err = au_opts_remount(sb, &opts);
30266 +       au_opts_free(&opts);
30267 +
30268 +       if (au_ftest_opts(opts.flags, REFRESH))
30269 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30270 +
30271 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30272 +               mntflags = au_mntflags(sb);
30273 +               do_dx = !!au_opt_test(mntflags, DIO);
30274 +               au_dy_arefresh(do_dx);
30275 +       }
30276 +
30277 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30278 +       aufs_write_unlock(root);
30279 +
30280 +out_mtx:
30281 +       inode_unlock(inode);
30282 +out_opts:
30283 +       free_page((unsigned long)opts.opt);
30284 +out:
30285 +       err = cvt_err(err);
30286 +       AuTraceErr(err);
30287 +       return err;
30288 +}
30289 +
30290 +static const struct super_operations aufs_sop = {
30291 +       .alloc_inode    = aufs_alloc_inode,
30292 +       .destroy_inode  = aufs_destroy_inode,
30293 +       .free_inode     = aufs_free_inode,
30294 +       /* always deleting, no clearing */
30295 +       .drop_inode     = generic_delete_inode,
30296 +       .show_options   = aufs_show_options,
30297 +       .statfs         = aufs_statfs,
30298 +       .put_super      = aufs_put_super,
30299 +       .sync_fs        = aufs_sync_fs,
30300 +       .remount_fs     = aufs_remount_fs
30301 +};
30302 +
30303 +/* ---------------------------------------------------------------------- */
30304 +
30305 +static int alloc_root(struct super_block *sb)
30306 +{
30307 +       int err;
30308 +       struct inode *inode;
30309 +       struct dentry *root;
30310 +
30311 +       err = -ENOMEM;
30312 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30313 +       err = PTR_ERR(inode);
30314 +       if (IS_ERR(inode))
30315 +               goto out;
30316 +
30317 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30318 +       inode->i_fop = &aufs_dir_fop;
30319 +       inode->i_mode = S_IFDIR;
30320 +       set_nlink(inode, 2);
30321 +       unlock_new_inode(inode);
30322 +
30323 +       root = d_make_root(inode);
30324 +       if (unlikely(!root))
30325 +               goto out;
30326 +       err = PTR_ERR(root);
30327 +       if (IS_ERR(root))
30328 +               goto out;
30329 +
30330 +       err = au_di_init(root);
30331 +       if (!err) {
30332 +               sb->s_root = root;
30333 +               return 0; /* success */
30334 +       }
30335 +       dput(root);
30336 +
30337 +out:
30338 +       return err;
30339 +}
30340 +
30341 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30342 +                          int silent __maybe_unused)
30343 +{
30344 +       int err;
30345 +       struct au_opts opts = {
30346 +               .opt = NULL
30347 +       };
30348 +       struct au_sbinfo *sbinfo;
30349 +       struct dentry *root;
30350 +       struct inode *inode;
30351 +       char *arg = raw_data;
30352 +
30353 +       if (unlikely(!arg || !*arg)) {
30354 +               err = -EINVAL;
30355 +               pr_err("no arg\n");
30356 +               goto out;
30357 +       }
30358 +
30359 +       err = -ENOMEM;
30360 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30361 +       if (unlikely(!opts.opt))
30362 +               goto out;
30363 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30364 +       opts.sb_flags = sb->s_flags;
30365 +
30366 +       err = au_si_alloc(sb);
30367 +       if (unlikely(err))
30368 +               goto out_opts;
30369 +       sbinfo = au_sbi(sb);
30370 +
30371 +       /* all timestamps always follow the ones on the branch */
30372 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30373 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30374 +       sb->s_op = &aufs_sop;
30375 +       sb->s_d_op = &aufs_dop;
30376 +       sb->s_magic = AUFS_SUPER_MAGIC;
30377 +       sb->s_maxbytes = 0;
30378 +       sb->s_stack_depth = 1;
30379 +       au_export_init(sb);
30380 +       au_xattr_init(sb);
30381 +
30382 +       err = alloc_root(sb);
30383 +       if (unlikely(err)) {
30384 +               si_write_unlock(sb);
30385 +               goto out_info;
30386 +       }
30387 +       root = sb->s_root;
30388 +       inode = d_inode(root);
30389 +
30390 +       /*
30391 +        * actually we can parse options regardless aufs lock here.
30392 +        * but at remount time, parsing must be done before aufs lock.
30393 +        * so we follow the same rule.
30394 +        */
30395 +       ii_write_lock_parent(inode);
30396 +       aufs_write_unlock(root);
30397 +       err = au_opts_parse(sb, arg, &opts);
30398 +       if (unlikely(err))
30399 +               goto out_root;
30400 +
30401 +       /* lock vfs_inode first, then aufs. */
30402 +       inode_lock(inode);
30403 +       aufs_write_lock(root);
30404 +       err = au_opts_mount(sb, &opts);
30405 +       au_opts_free(&opts);
30406 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30407 +               sb->s_d_op = &aufs_dop_noreval;
30408 +               pr_info("%ps\n", sb->s_d_op);
30409 +               au_refresh_dop(root, /*force_reval*/0);
30410 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30411 +               au_refresh_iop(inode, /*force_getattr*/0);
30412 +       }
30413 +       aufs_write_unlock(root);
30414 +       inode_unlock(inode);
30415 +       if (!err)
30416 +               goto out_opts; /* success */
30417 +
30418 +out_root:
30419 +       dput(root);
30420 +       sb->s_root = NULL;
30421 +out_info:
30422 +       kobject_put(&sbinfo->si_kobj);
30423 +       sb->s_fs_info = NULL;
30424 +out_opts:
30425 +       free_page((unsigned long)opts.opt);
30426 +out:
30427 +       AuTraceErr(err);
30428 +       err = cvt_err(err);
30429 +       AuTraceErr(err);
30430 +       return err;
30431 +}
30432 +
30433 +/* ---------------------------------------------------------------------- */
30434 +
30435 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30436 +                                const char *dev_name __maybe_unused,
30437 +                                void *raw_data)
30438 +{
30439 +       struct dentry *root;
30440 +
30441 +       /* all timestamps always follow the ones on the branch */
30442 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30443 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30444 +       if (IS_ERR(root))
30445 +               goto out;
30446 +
30447 +       au_sbilist_add(root->d_sb);
30448 +
30449 +out:
30450 +       return root;
30451 +}
30452 +
30453 +static void aufs_kill_sb(struct super_block *sb)
30454 +{
30455 +       struct au_sbinfo *sbinfo;
30456 +
30457 +       sbinfo = au_sbi(sb);
30458 +       if (sbinfo) {
30459 +               au_sbilist_del(sb);
30460 +               aufs_write_lock(sb->s_root);
30461 +               au_fhsm_fin(sb);
30462 +               if (sbinfo->si_wbr_create_ops->fin)
30463 +                       sbinfo->si_wbr_create_ops->fin(sb);
30464 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30465 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30466 +                       au_remount_refresh(sb, /*do_idop*/0);
30467 +               }
30468 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30469 +                       au_plink_put(sb, /*verbose*/1);
30470 +               au_xino_clr(sb);
30471 +               au_dr_opt_flush(sb);
30472 +               sbinfo->si_sb = NULL;
30473 +               aufs_write_unlock(sb->s_root);
30474 +               au_nwt_flush(&sbinfo->si_nowait);
30475 +       }
30476 +       kill_anon_super(sb);
30477 +}
30478 +
30479 +struct file_system_type aufs_fs_type = {
30480 +       .name           = AUFS_FSTYPE,
30481 +       /* a race between rename and others */
30482 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30483 +       .mount          = aufs_mount,
30484 +       .kill_sb        = aufs_kill_sb,
30485 +       /* no need to __module_get() and module_put(). */
30486 +       .owner          = THIS_MODULE,
30487 +};
30488 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30489 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30490 +++ linux/fs/aufs/super.h       2020-01-27 10:57:18.178871751 +0100
30491 @@ -0,0 +1,589 @@
30492 +/* SPDX-License-Identifier: GPL-2.0 */
30493 +/*
30494 + * Copyright (C) 2005-2020 Junjiro R. Okajima
30495 + *
30496 + * This program, aufs is free software; you can redistribute it and/or modify
30497 + * it under the terms of the GNU General Public License as published by
30498 + * the Free Software Foundation; either version 2 of the License, or
30499 + * (at your option) any later version.
30500 + *
30501 + * This program is distributed in the hope that it will be useful,
30502 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30503 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30504 + * GNU General Public License for more details.
30505 + *
30506 + * You should have received a copy of the GNU General Public License
30507 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30508 + */
30509 +
30510 +/*
30511 + * super_block operations
30512 + */
30513 +
30514 +#ifndef __AUFS_SUPER_H__
30515 +#define __AUFS_SUPER_H__
30516 +
30517 +#ifdef __KERNEL__
30518 +
30519 +#include <linux/fs.h>
30520 +#include <linux/kobject.h>
30521 +#include "hbl.h"
30522 +#include "lcnt.h"
30523 +#include "rwsem.h"
30524 +#include "wkq.h"
30525 +
30526 +/* policies to select one among multiple writable branches */
30527 +struct au_wbr_copyup_operations {
30528 +       int (*copyup)(struct dentry *dentry);
30529 +};
30530 +
30531 +#define AuWbr_DIR      1               /* target is a dir */
30532 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30533 +
30534 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30535 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30536 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30537 +
30538 +struct au_wbr_create_operations {
30539 +       int (*create)(struct dentry *dentry, unsigned int flags);
30540 +       int (*init)(struct super_block *sb);
30541 +       int (*fin)(struct super_block *sb);
30542 +};
30543 +
30544 +struct au_wbr_mfs {
30545 +       struct mutex    mfs_lock; /* protect this structure */
30546 +       unsigned long   mfs_jiffy;
30547 +       unsigned long   mfs_expire;
30548 +       aufs_bindex_t   mfs_bindex;
30549 +
30550 +       unsigned long long      mfsrr_bytes;
30551 +       unsigned long long      mfsrr_watermark;
30552 +};
30553 +
30554 +#define AuPlink_NHASH 100
30555 +static inline int au_plink_hash(ino_t ino)
30556 +{
30557 +       return ino % AuPlink_NHASH;
30558 +}
30559 +
30560 +/* File-based Hierarchical Storage Management */
30561 +struct au_fhsm {
30562 +#ifdef CONFIG_AUFS_FHSM
30563 +       /* allow only one process who can receive the notification */
30564 +       spinlock_t              fhsm_spin;
30565 +       pid_t                   fhsm_pid;
30566 +       wait_queue_head_t       fhsm_wqh;
30567 +       atomic_t                fhsm_readable;
30568 +
30569 +       /* these are protected by si_rwsem */
30570 +       unsigned long           fhsm_expire;
30571 +       aufs_bindex_t           fhsm_bottom;
30572 +#endif
30573 +};
30574 +
30575 +struct au_branch;
30576 +struct au_sbinfo {
30577 +       /* nowait tasks in the system-wide workqueue */
30578 +       struct au_nowait_tasks  si_nowait;
30579 +
30580 +       /*
30581 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30582 +        * rwsem for au_sbinfo is necessary.
30583 +        */
30584 +       struct au_rwsem         si_rwsem;
30585 +
30586 +       /*
30587 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30588 +        * remount.
30589 +        */
30590 +       au_lcnt_t               si_ninodes, si_nfiles;
30591 +
30592 +       /* branch management */
30593 +       unsigned int            si_generation;
30594 +
30595 +       /* see AuSi_ flags */
30596 +       unsigned char           au_si_status;
30597 +
30598 +       aufs_bindex_t           si_bbot;
30599 +
30600 +       /* dirty trick to keep br_id plus */
30601 +       unsigned int            si_last_br_id :
30602 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30603 +       struct au_branch        **si_branch;
30604 +
30605 +       /* policy to select a writable branch */
30606 +       unsigned char           si_wbr_copyup;
30607 +       unsigned char           si_wbr_create;
30608 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30609 +       struct au_wbr_create_operations *si_wbr_create_ops;
30610 +
30611 +       /* round robin */
30612 +       atomic_t                si_wbr_rr_next;
30613 +
30614 +       /* most free space */
30615 +       struct au_wbr_mfs       si_wbr_mfs;
30616 +
30617 +       /* File-based Hierarchical Storage Management */
30618 +       struct au_fhsm          si_fhsm;
30619 +
30620 +       /* mount flags */
30621 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30622 +       unsigned int            si_mntflags;
30623 +
30624 +       /* external inode number (bitmap and translation table) */
30625 +       vfs_readf_t             si_xread;
30626 +       vfs_writef_t            si_xwrite;
30627 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30628 +
30629 +       struct file             *si_xib;
30630 +       struct mutex            si_xib_mtx; /* protect xib members */
30631 +       unsigned long           *si_xib_buf;
30632 +       unsigned long           si_xib_last_pindex;
30633 +       int                     si_xib_next_bit;
30634 +
30635 +       unsigned long           si_xino_jiffy;
30636 +       unsigned long           si_xino_expire;
30637 +       /* reserved for future use */
30638 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30639 +
30640 +#ifdef CONFIG_AUFS_EXPORT
30641 +       /* i_generation */
30642 +       /* todo: make xigen file an array to support many inode numbers */
30643 +       struct file             *si_xigen;
30644 +       atomic_t                si_xigen_next;
30645 +#endif
30646 +
30647 +       /* dirty trick to support atomic_open */
30648 +       struct hlist_bl_head    si_aopen;
30649 +
30650 +       /* vdir parameters */
30651 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30652 +       unsigned int            si_rdblk;       /* deblk size */
30653 +       unsigned int            si_rdhash;      /* hash size */
30654 +
30655 +       /*
30656 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30657 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30658 +        * future fsck.aufs or kernel thread will remove them later.
30659 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30660 +        */
30661 +       unsigned int            si_dirwh;
30662 +
30663 +       /* pseudo_link list */
30664 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30665 +       wait_queue_head_t       si_plink_wq;
30666 +       spinlock_t              si_plink_maint_lock;
30667 +       pid_t                   si_plink_maint_pid;
30668 +
30669 +       /* file list */
30670 +       struct hlist_bl_head    si_files;
30671 +
30672 +       /* with/without getattr, brother of sb->s_d_op */
30673 +       const struct inode_operations *si_iop_array;
30674 +
30675 +       /*
30676 +        * sysfs and lifetime management.
30677 +        * this is not a small structure and it may be a waste of memory in case
30678 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30679 +        * but using sysfs is majority.
30680 +        */
30681 +       struct kobject          si_kobj;
30682 +#ifdef CONFIG_DEBUG_FS
30683 +       struct dentry            *si_dbgaufs;
30684 +       struct dentry            *si_dbgaufs_plink;
30685 +       struct dentry            *si_dbgaufs_xib;
30686 +#ifdef CONFIG_AUFS_EXPORT
30687 +       struct dentry            *si_dbgaufs_xigen;
30688 +#endif
30689 +#endif
30690 +
30691 +#ifdef CONFIG_AUFS_SBILIST
30692 +       struct hlist_bl_node    si_list;
30693 +#endif
30694 +
30695 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30696 +       struct super_block      *si_sb;
30697 +};
30698 +
30699 +/* sbinfo status flags */
30700 +/*
30701 + * set true when refresh_dirs() failed at remount time.
30702 + * then try refreshing dirs at access time again.
30703 + * if it is false, refreshing dirs at access time is unnecessary
30704 + */
30705 +#define AuSi_FAILED_REFRESH_DIR        1
30706 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30707 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30708 +
30709 +#ifndef CONFIG_AUFS_FHSM
30710 +#undef AuSi_FHSM
30711 +#define AuSi_FHSM              0
30712 +#endif
30713 +
30714 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30715 +                                          unsigned int flag)
30716 +{
30717 +       AuRwMustAnyLock(&sbi->si_rwsem);
30718 +       return sbi->au_si_status & flag;
30719 +}
30720 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30721 +#define au_fset_si(sbinfo, name) do { \
30722 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30723 +       (sbinfo)->au_si_status |= AuSi_##name; \
30724 +} while (0)
30725 +#define au_fclr_si(sbinfo, name) do { \
30726 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30727 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30728 +} while (0)
30729 +
30730 +/* ---------------------------------------------------------------------- */
30731 +
30732 +/* policy to select one among writable branches */
30733 +#define AuWbrCopyup(sbinfo, ...) \
30734 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30735 +#define AuWbrCreate(sbinfo, ...) \
30736 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30737 +
30738 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30739 +#define AuLock_DW              1               /* write-lock dentry */
30740 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30741 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30742 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30743 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30744 +                                               /* except RENAME_EXCHANGE */
30745 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30746 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30747 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30748 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30749 +#define au_fset_lock(flags, name) \
30750 +       do { (flags) |= AuLock_##name; } while (0)
30751 +#define au_fclr_lock(flags, name) \
30752 +       do { (flags) &= ~AuLock_##name; } while (0)
30753 +
30754 +/* ---------------------------------------------------------------------- */
30755 +
30756 +/* super.c */
30757 +extern struct file_system_type aufs_fs_type;
30758 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30759 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30760 +                                          unsigned long long max, void *arg);
30761 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30762 +                    struct super_block *sb, void *arg);
30763 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30764 +void au_iarray_free(struct inode **a, unsigned long long max);
30765 +
30766 +/* sbinfo.c */
30767 +void au_si_free(struct kobject *kobj);
30768 +int au_si_alloc(struct super_block *sb);
30769 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30770 +
30771 +unsigned int au_sigen_inc(struct super_block *sb);
30772 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30773 +
30774 +int si_read_lock(struct super_block *sb, int flags);
30775 +int si_write_lock(struct super_block *sb, int flags);
30776 +int aufs_read_lock(struct dentry *dentry, int flags);
30777 +void aufs_read_unlock(struct dentry *dentry, int flags);
30778 +void aufs_write_lock(struct dentry *dentry);
30779 +void aufs_write_unlock(struct dentry *dentry);
30780 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30781 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30782 +
30783 +/* wbr_policy.c */
30784 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30785 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30786 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30787 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30788 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30789 +
30790 +/* mvdown.c */
30791 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30792 +
30793 +#ifdef CONFIG_AUFS_FHSM
30794 +/* fhsm.c */
30795 +
30796 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30797 +{
30798 +       pid_t pid;
30799 +
30800 +       spin_lock(&fhsm->fhsm_spin);
30801 +       pid = fhsm->fhsm_pid;
30802 +       spin_unlock(&fhsm->fhsm_spin);
30803 +
30804 +       return pid;
30805 +}
30806 +
30807 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30808 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30809 +int au_fhsm_fd(struct super_block *sb, int oflags);
30810 +int au_fhsm_br_alloc(struct au_branch *br);
30811 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30812 +void au_fhsm_fin(struct super_block *sb);
30813 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30814 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30815 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30816 +#else
30817 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30818 +          int force)
30819 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30820 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30821 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30822 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30823 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30824 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30825 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30826 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30827 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30828 +#endif
30829 +
30830 +/* ---------------------------------------------------------------------- */
30831 +
30832 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30833 +{
30834 +       return sb->s_fs_info;
30835 +}
30836 +
30837 +/* ---------------------------------------------------------------------- */
30838 +
30839 +#ifdef CONFIG_AUFS_EXPORT
30840 +int au_test_nfsd(void);
30841 +void au_export_init(struct super_block *sb);
30842 +void au_xigen_inc(struct inode *inode);
30843 +int au_xigen_new(struct inode *inode);
30844 +int au_xigen_set(struct super_block *sb, struct path *path);
30845 +void au_xigen_clr(struct super_block *sb);
30846 +
30847 +static inline int au_busy_or_stale(void)
30848 +{
30849 +       if (!au_test_nfsd())
30850 +               return -EBUSY;
30851 +       return -ESTALE;
30852 +}
30853 +#else
30854 +AuStubInt0(au_test_nfsd, void)
30855 +AuStubVoid(au_export_init, struct super_block *sb)
30856 +AuStubVoid(au_xigen_inc, struct inode *inode)
30857 +AuStubInt0(au_xigen_new, struct inode *inode)
30858 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30859 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30860 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30861 +#endif /* CONFIG_AUFS_EXPORT */
30862 +
30863 +/* ---------------------------------------------------------------------- */
30864 +
30865 +#ifdef CONFIG_AUFS_SBILIST
30866 +/* module.c */
30867 +extern struct hlist_bl_head au_sbilist;
30868 +
30869 +static inline void au_sbilist_init(void)
30870 +{
30871 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30872 +}
30873 +
30874 +static inline void au_sbilist_add(struct super_block *sb)
30875 +{
30876 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30877 +}
30878 +
30879 +static inline void au_sbilist_del(struct super_block *sb)
30880 +{
30881 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30882 +}
30883 +
30884 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30885 +static inline void au_sbilist_lock(void)
30886 +{
30887 +       hlist_bl_lock(&au_sbilist);
30888 +}
30889 +
30890 +static inline void au_sbilist_unlock(void)
30891 +{
30892 +       hlist_bl_unlock(&au_sbilist);
30893 +}
30894 +#define AuGFP_SBILIST  GFP_ATOMIC
30895 +#else
30896 +AuStubVoid(au_sbilist_lock, void)
30897 +AuStubVoid(au_sbilist_unlock, void)
30898 +#define AuGFP_SBILIST  GFP_NOFS
30899 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30900 +#else
30901 +AuStubVoid(au_sbilist_init, void)
30902 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30903 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30904 +AuStubVoid(au_sbilist_lock, void)
30905 +AuStubVoid(au_sbilist_unlock, void)
30906 +#define AuGFP_SBILIST  GFP_NOFS
30907 +#endif
30908 +
30909 +/* ---------------------------------------------------------------------- */
30910 +
30911 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30912 +{
30913 +       /*
30914 +        * This function is a dynamic '__init' function actually,
30915 +        * so the tiny check for si_rwsem is unnecessary.
30916 +        */
30917 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30918 +#ifdef CONFIG_DEBUG_FS
30919 +       sbinfo->si_dbgaufs = NULL;
30920 +       sbinfo->si_dbgaufs_plink = NULL;
30921 +       sbinfo->si_dbgaufs_xib = NULL;
30922 +#ifdef CONFIG_AUFS_EXPORT
30923 +       sbinfo->si_dbgaufs_xigen = NULL;
30924 +#endif
30925 +#endif
30926 +}
30927 +
30928 +/* ---------------------------------------------------------------------- */
30929 +
30930 +/* current->atomic_flags */
30931 +/* this value should never corrupt the ones defined in linux/sched.h */
30932 +#define PFA_AUFS       0x10
30933 +
30934 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30935 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30936 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30937 +
30938 +static inline int si_pid_test(struct super_block *sb)
30939 +{
30940 +       return !!task_test_aufs(current);
30941 +}
30942 +
30943 +static inline void si_pid_clr(struct super_block *sb)
30944 +{
30945 +       AuDebugOn(!task_test_aufs(current));
30946 +       task_clear_aufs(current);
30947 +}
30948 +
30949 +static inline void si_pid_set(struct super_block *sb)
30950 +{
30951 +       AuDebugOn(task_test_aufs(current));
30952 +       task_set_aufs(current);
30953 +}
30954 +
30955 +/* ---------------------------------------------------------------------- */
30956 +
30957 +/* lock superblock. mainly for entry point functions */
30958 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30959 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30960 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30961 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30962 +/*
30963 +#define __si_read_trylock_nested(sb) \
30964 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30965 +#define __si_write_trylock_nested(sb) \
30966 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30967 +*/
30968 +
30969 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30970 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30971 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30972 +
30973 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30974 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30975 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30976 +
30977 +static inline void si_noflush_read_lock(struct super_block *sb)
30978 +{
30979 +       __si_read_lock(sb);
30980 +       si_pid_set(sb);
30981 +}
30982 +
30983 +static inline int si_noflush_read_trylock(struct super_block *sb)
30984 +{
30985 +       int locked;
30986 +
30987 +       locked = __si_read_trylock(sb);
30988 +       if (locked)
30989 +               si_pid_set(sb);
30990 +       return locked;
30991 +}
30992 +
30993 +static inline void si_noflush_write_lock(struct super_block *sb)
30994 +{
30995 +       __si_write_lock(sb);
30996 +       si_pid_set(sb);
30997 +}
30998 +
30999 +static inline int si_noflush_write_trylock(struct super_block *sb)
31000 +{
31001 +       int locked;
31002 +
31003 +       locked = __si_write_trylock(sb);
31004 +       if (locked)
31005 +               si_pid_set(sb);
31006 +       return locked;
31007 +}
31008 +
31009 +#if 0 /* reserved */
31010 +static inline int si_read_trylock(struct super_block *sb, int flags)
31011 +{
31012 +       if (au_ftest_lock(flags, FLUSH))
31013 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31014 +       return si_noflush_read_trylock(sb);
31015 +}
31016 +#endif
31017 +
31018 +static inline void si_read_unlock(struct super_block *sb)
31019 +{
31020 +       si_pid_clr(sb);
31021 +       __si_read_unlock(sb);
31022 +}
31023 +
31024 +#if 0 /* reserved */
31025 +static inline int si_write_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_write_trylock(sb);
31030 +}
31031 +#endif
31032 +
31033 +static inline void si_write_unlock(struct super_block *sb)
31034 +{
31035 +       si_pid_clr(sb);
31036 +       __si_write_unlock(sb);
31037 +}
31038 +
31039 +#if 0 /* reserved */
31040 +static inline void si_downgrade_lock(struct super_block *sb)
31041 +{
31042 +       __si_downgrade_lock(sb);
31043 +}
31044 +#endif
31045 +
31046 +/* ---------------------------------------------------------------------- */
31047 +
31048 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31049 +{
31050 +       SiMustAnyLock(sb);
31051 +       return au_sbi(sb)->si_bbot;
31052 +}
31053 +
31054 +static inline unsigned int au_mntflags(struct super_block *sb)
31055 +{
31056 +       SiMustAnyLock(sb);
31057 +       return au_sbi(sb)->si_mntflags;
31058 +}
31059 +
31060 +static inline unsigned int au_sigen(struct super_block *sb)
31061 +{
31062 +       SiMustAnyLock(sb);
31063 +       return au_sbi(sb)->si_generation;
31064 +}
31065 +
31066 +static inline struct au_branch *au_sbr(struct super_block *sb,
31067 +                                      aufs_bindex_t bindex)
31068 +{
31069 +       SiMustAnyLock(sb);
31070 +       return au_sbi(sb)->si_branch[0 + bindex];
31071 +}
31072 +
31073 +static inline loff_t au_xi_maxent(struct super_block *sb)
31074 +{
31075 +       SiMustAnyLock(sb);
31076 +       return au_sbi(sb)->si_ximaxent;
31077 +}
31078 +
31079 +#endif /* __KERNEL__ */
31080 +#endif /* __AUFS_SUPER_H__ */
31081 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31082 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31083 +++ linux/fs/aufs/sysaufs.c     2020-01-27 10:57:18.178871751 +0100
31084 @@ -0,0 +1,93 @@
31085 +// SPDX-License-Identifier: GPL-2.0
31086 +/*
31087 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31088 + *
31089 + * This program, aufs is free software; you can redistribute it and/or modify
31090 + * it under the terms of the GNU General Public License as published by
31091 + * the Free Software Foundation; either version 2 of the License, or
31092 + * (at your option) any later version.
31093 + *
31094 + * This program is distributed in the hope that it will be useful,
31095 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31096 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31097 + * GNU General Public License for more details.
31098 + *
31099 + * You should have received a copy of the GNU General Public License
31100 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31101 + */
31102 +
31103 +/*
31104 + * sysfs interface and lifetime management
31105 + * they are necessary regardless sysfs is disabled.
31106 + */
31107 +
31108 +#include <linux/random.h>
31109 +#include "aufs.h"
31110 +
31111 +unsigned long sysaufs_si_mask;
31112 +struct kset *sysaufs_kset;
31113 +
31114 +#define AuSiAttr(_name) { \
31115 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31116 +       .show   = sysaufs_si_##_name,                           \
31117 +}
31118 +
31119 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31120 +struct attribute *sysaufs_si_attrs[] = {
31121 +       &sysaufs_si_attr_xi_path.attr,
31122 +       NULL,
31123 +};
31124 +
31125 +static const struct sysfs_ops au_sbi_ops = {
31126 +       .show   = sysaufs_si_show
31127 +};
31128 +
31129 +static struct kobj_type au_sbi_ktype = {
31130 +       .release        = au_si_free,
31131 +       .sysfs_ops      = &au_sbi_ops,
31132 +       .default_attrs  = sysaufs_si_attrs
31133 +};
31134 +
31135 +/* ---------------------------------------------------------------------- */
31136 +
31137 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31138 +{
31139 +       int err;
31140 +
31141 +       sbinfo->si_kobj.kset = sysaufs_kset;
31142 +       /* cf. sysaufs_name() */
31143 +       err = kobject_init_and_add
31144 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31145 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31146 +
31147 +       return err;
31148 +}
31149 +
31150 +void sysaufs_fin(void)
31151 +{
31152 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31153 +       kset_unregister(sysaufs_kset);
31154 +}
31155 +
31156 +int __init sysaufs_init(void)
31157 +{
31158 +       int err;
31159 +
31160 +       do {
31161 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31162 +       } while (!sysaufs_si_mask);
31163 +
31164 +       err = -EINVAL;
31165 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31166 +       if (unlikely(!sysaufs_kset))
31167 +               goto out;
31168 +       err = PTR_ERR(sysaufs_kset);
31169 +       if (IS_ERR(sysaufs_kset))
31170 +               goto out;
31171 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31172 +       if (unlikely(err))
31173 +               kset_unregister(sysaufs_kset);
31174 +
31175 +out:
31176 +       return err;
31177 +}
31178 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31179 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31180 +++ linux/fs/aufs/sysaufs.h     2020-01-27 10:57:18.178871751 +0100
31181 @@ -0,0 +1,102 @@
31182 +/* SPDX-License-Identifier: GPL-2.0 */
31183 +/*
31184 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31185 + *
31186 + * This program, aufs is free software; you can redistribute it and/or modify
31187 + * it under the terms of the GNU General Public License as published by
31188 + * the Free Software Foundation; either version 2 of the License, or
31189 + * (at your option) any later version.
31190 + *
31191 + * This program is distributed in the hope that it will be useful,
31192 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31193 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31194 + * GNU General Public License for more details.
31195 + *
31196 + * You should have received a copy of the GNU General Public License
31197 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31198 + */
31199 +
31200 +/*
31201 + * sysfs interface and mount lifetime management
31202 + */
31203 +
31204 +#ifndef __SYSAUFS_H__
31205 +#define __SYSAUFS_H__
31206 +
31207 +#ifdef __KERNEL__
31208 +
31209 +#include <linux/sysfs.h>
31210 +#include "module.h"
31211 +
31212 +struct super_block;
31213 +struct au_sbinfo;
31214 +
31215 +struct sysaufs_si_attr {
31216 +       struct attribute attr;
31217 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31218 +};
31219 +
31220 +/* ---------------------------------------------------------------------- */
31221 +
31222 +/* sysaufs.c */
31223 +extern unsigned long sysaufs_si_mask;
31224 +extern struct kset *sysaufs_kset;
31225 +extern struct attribute *sysaufs_si_attrs[];
31226 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31227 +int __init sysaufs_init(void);
31228 +void sysaufs_fin(void);
31229 +
31230 +/* ---------------------------------------------------------------------- */
31231 +
31232 +/* some people doesn't like to show a pointer in kernel */
31233 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31234 +{
31235 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31236 +}
31237 +
31238 +#define SysaufsSiNamePrefix    "si_"
31239 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31240 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31241 +{
31242 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31243 +                sysaufs_si_id(sbinfo));
31244 +}
31245 +
31246 +struct au_branch;
31247 +#ifdef CONFIG_SYSFS
31248 +/* sysfs.c */
31249 +extern struct attribute_group *sysaufs_attr_group;
31250 +
31251 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31252 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31253 +                        char *buf);
31254 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31255 +#ifdef CONFIG_COMPAT
31256 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31257 +#endif
31258 +
31259 +void sysaufs_br_init(struct au_branch *br);
31260 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31261 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31262 +
31263 +#define sysaufs_brs_init()     do {} while (0)
31264 +
31265 +#else
31266 +#define sysaufs_attr_group     NULL
31267 +
31268 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31269 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31270 +       struct attribute *attr, char *buf)
31271 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31272 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31273 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31274 +
31275 +static inline void sysaufs_brs_init(void)
31276 +{
31277 +       sysaufs_brs = 0;
31278 +}
31279 +
31280 +#endif /* CONFIG_SYSFS */
31281 +
31282 +#endif /* __KERNEL__ */
31283 +#endif /* __SYSAUFS_H__ */
31284 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31285 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31286 +++ linux/fs/aufs/sysfs.c       2020-01-27 10:57:18.178871751 +0100
31287 @@ -0,0 +1,374 @@
31288 +// SPDX-License-Identifier: GPL-2.0
31289 +/*
31290 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31291 + *
31292 + * This program, aufs is free software; you can redistribute it and/or modify
31293 + * it under the terms of the GNU General Public License as published by
31294 + * the Free Software Foundation; either version 2 of the License, or
31295 + * (at your option) any later version.
31296 + *
31297 + * This program is distributed in the hope that it will be useful,
31298 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31299 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31300 + * GNU General Public License for more details.
31301 + *
31302 + * You should have received a copy of the GNU General Public License
31303 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31304 + */
31305 +
31306 +/*
31307 + * sysfs interface
31308 + */
31309 +
31310 +#include <linux/compat.h>
31311 +#include <linux/seq_file.h>
31312 +#include "aufs.h"
31313 +
31314 +#ifdef CONFIG_AUFS_FS_MODULE
31315 +/* this entry violates the "one line per file" policy of sysfs */
31316 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31317 +                          char *buf)
31318 +{
31319 +       ssize_t err;
31320 +       static char *conf =
31321 +/* this file is generated at compiling */
31322 +#include "conf.str"
31323 +               ;
31324 +
31325 +       err = snprintf(buf, PAGE_SIZE, conf);
31326 +       if (unlikely(err >= PAGE_SIZE))
31327 +               err = -EFBIG;
31328 +       return err;
31329 +}
31330 +
31331 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31332 +#endif
31333 +
31334 +static struct attribute *au_attr[] = {
31335 +#ifdef CONFIG_AUFS_FS_MODULE
31336 +       &au_config_attr.attr,
31337 +#endif
31338 +       NULL,   /* need to NULL terminate the list of attributes */
31339 +};
31340 +
31341 +static struct attribute_group sysaufs_attr_group_body = {
31342 +       .attrs = au_attr
31343 +};
31344 +
31345 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31346 +
31347 +/* ---------------------------------------------------------------------- */
31348 +
31349 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31350 +{
31351 +       int err;
31352 +
31353 +       SiMustAnyLock(sb);
31354 +
31355 +       err = 0;
31356 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31357 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31358 +               seq_putc(seq, '\n');
31359 +       }
31360 +       return err;
31361 +}
31362 +
31363 +/*
31364 + * the lifetime of branch is independent from the entry under sysfs.
31365 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31366 + * unlinked.
31367 + */
31368 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31369 +                        aufs_bindex_t bindex, int idx)
31370 +{
31371 +       int err;
31372 +       struct path path;
31373 +       struct dentry *root;
31374 +       struct au_branch *br;
31375 +       au_br_perm_str_t perm;
31376 +
31377 +       AuDbg("b%d\n", bindex);
31378 +
31379 +       err = 0;
31380 +       root = sb->s_root;
31381 +       di_read_lock_parent(root, !AuLock_IR);
31382 +       br = au_sbr(sb, bindex);
31383 +
31384 +       switch (idx) {
31385 +       case AuBrSysfs_BR:
31386 +               path.mnt = au_br_mnt(br);
31387 +               path.dentry = au_h_dptr(root, bindex);
31388 +               err = au_seq_path(seq, &path);
31389 +               if (!err) {
31390 +                       au_optstr_br_perm(&perm, br->br_perm);
31391 +                       seq_printf(seq, "=%s\n", perm.a);
31392 +               }
31393 +               break;
31394 +       case AuBrSysfs_BRID:
31395 +               seq_printf(seq, "%d\n", br->br_id);
31396 +               break;
31397 +       }
31398 +       di_read_unlock(root, !AuLock_IR);
31399 +       if (unlikely(err || seq_has_overflowed(seq)))
31400 +               err = -E2BIG;
31401 +
31402 +       return err;
31403 +}
31404 +
31405 +/* ---------------------------------------------------------------------- */
31406 +
31407 +static struct seq_file *au_seq(char *p, ssize_t len)
31408 +{
31409 +       struct seq_file *seq;
31410 +
31411 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31412 +       if (seq) {
31413 +               /* mutex_init(&seq.lock); */
31414 +               seq->buf = p;
31415 +               seq->size = len;
31416 +               return seq; /* success */
31417 +       }
31418 +
31419 +       seq = ERR_PTR(-ENOMEM);
31420 +       return seq;
31421 +}
31422 +
31423 +#define SysaufsBr_PREFIX       "br"
31424 +#define SysaufsBrid_PREFIX     "brid"
31425 +
31426 +/* todo: file size may exceed PAGE_SIZE */
31427 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31428 +                       char *buf)
31429 +{
31430 +       ssize_t err;
31431 +       int idx;
31432 +       long l;
31433 +       aufs_bindex_t bbot;
31434 +       struct au_sbinfo *sbinfo;
31435 +       struct super_block *sb;
31436 +       struct seq_file *seq;
31437 +       char *name;
31438 +       struct attribute **cattr;
31439 +
31440 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31441 +       sb = sbinfo->si_sb;
31442 +
31443 +       /*
31444 +        * prevent a race condition between sysfs and aufs.
31445 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31446 +        * prohibits maintaining the sysfs entries.
31447 +        * hew we acquire read lock after sysfs_get_active_two().
31448 +        * on the other hand, the remount process may maintain the sysfs/aufs
31449 +        * entries after acquiring write lock.
31450 +        * it can cause a deadlock.
31451 +        * simply we gave up processing read here.
31452 +        */
31453 +       err = -EBUSY;
31454 +       if (unlikely(!si_noflush_read_trylock(sb)))
31455 +               goto out;
31456 +
31457 +       seq = au_seq(buf, PAGE_SIZE);
31458 +       err = PTR_ERR(seq);
31459 +       if (IS_ERR(seq))
31460 +               goto out_unlock;
31461 +
31462 +       name = (void *)attr->name;
31463 +       cattr = sysaufs_si_attrs;
31464 +       while (*cattr) {
31465 +               if (!strcmp(name, (*cattr)->name)) {
31466 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31467 +                               ->show(seq, sb);
31468 +                       goto out_seq;
31469 +               }
31470 +               cattr++;
31471 +       }
31472 +
31473 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31474 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31475 +               idx = AuBrSysfs_BRID;
31476 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31477 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31478 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31479 +               idx = AuBrSysfs_BR;
31480 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31481 +       } else
31482 +                 BUG();
31483 +
31484 +       err = kstrtol(name, 10, &l);
31485 +       if (!err) {
31486 +               bbot = au_sbbot(sb);
31487 +               if (l <= bbot)
31488 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31489 +               else
31490 +                       err = -ENOENT;
31491 +       }
31492 +
31493 +out_seq:
31494 +       if (!err) {
31495 +               err = seq->count;
31496 +               /* sysfs limit */
31497 +               if (unlikely(err == PAGE_SIZE))
31498 +                       err = -EFBIG;
31499 +       }
31500 +       au_kfree_rcu(seq);
31501 +out_unlock:
31502 +       si_read_unlock(sb);
31503 +out:
31504 +       return err;
31505 +}
31506 +
31507 +/* ---------------------------------------------------------------------- */
31508 +
31509 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31510 +{
31511 +       int err;
31512 +       int16_t brid;
31513 +       aufs_bindex_t bindex, bbot;
31514 +       size_t sz;
31515 +       char *buf;
31516 +       struct seq_file *seq;
31517 +       struct au_branch *br;
31518 +
31519 +       si_read_lock(sb, AuLock_FLUSH);
31520 +       bbot = au_sbbot(sb);
31521 +       err = bbot + 1;
31522 +       if (!arg)
31523 +               goto out;
31524 +
31525 +       err = -ENOMEM;
31526 +       buf = (void *)__get_free_page(GFP_NOFS);
31527 +       if (unlikely(!buf))
31528 +               goto out;
31529 +
31530 +       seq = au_seq(buf, PAGE_SIZE);
31531 +       err = PTR_ERR(seq);
31532 +       if (IS_ERR(seq))
31533 +               goto out_buf;
31534 +
31535 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31536 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31537 +               /* VERIFY_WRITE */
31538 +               err = !access_ok(arg, sizeof(*arg));
31539 +               if (unlikely(err))
31540 +                       break;
31541 +
31542 +               br = au_sbr(sb, bindex);
31543 +               brid = br->br_id;
31544 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31545 +               err = __put_user(brid, &arg->id);
31546 +               if (unlikely(err))
31547 +                       break;
31548 +
31549 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31550 +               err = __put_user(br->br_perm, &arg->perm);
31551 +               if (unlikely(err))
31552 +                       break;
31553 +
31554 +               err = au_seq_path(seq, &br->br_path);
31555 +               if (unlikely(err))
31556 +                       break;
31557 +               seq_putc(seq, '\0');
31558 +               if (!seq_has_overflowed(seq)) {
31559 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31560 +                       seq->count = 0;
31561 +                       if (unlikely(err))
31562 +                               break;
31563 +               } else {
31564 +                       err = -E2BIG;
31565 +                       goto out_seq;
31566 +               }
31567 +       }
31568 +       if (unlikely(err))
31569 +               err = -EFAULT;
31570 +
31571 +out_seq:
31572 +       au_kfree_rcu(seq);
31573 +out_buf:
31574 +       free_page((unsigned long)buf);
31575 +out:
31576 +       si_read_unlock(sb);
31577 +       return err;
31578 +}
31579 +
31580 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31581 +{
31582 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31583 +}
31584 +
31585 +#ifdef CONFIG_COMPAT
31586 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31587 +{
31588 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31589 +}
31590 +#endif
31591 +
31592 +/* ---------------------------------------------------------------------- */
31593 +
31594 +void sysaufs_br_init(struct au_branch *br)
31595 +{
31596 +       int i;
31597 +       struct au_brsysfs *br_sysfs;
31598 +       struct attribute *attr;
31599 +
31600 +       br_sysfs = br->br_sysfs;
31601 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31602 +               attr = &br_sysfs->attr;
31603 +               sysfs_attr_init(attr);
31604 +               attr->name = br_sysfs->name;
31605 +               attr->mode = 0444;
31606 +               br_sysfs++;
31607 +       }
31608 +}
31609 +
31610 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31611 +{
31612 +       struct au_branch *br;
31613 +       struct kobject *kobj;
31614 +       struct au_brsysfs *br_sysfs;
31615 +       int i;
31616 +       aufs_bindex_t bbot;
31617 +
31618 +       if (!sysaufs_brs)
31619 +               return;
31620 +
31621 +       kobj = &au_sbi(sb)->si_kobj;
31622 +       bbot = au_sbbot(sb);
31623 +       for (; bindex <= bbot; bindex++) {
31624 +               br = au_sbr(sb, bindex);
31625 +               br_sysfs = br->br_sysfs;
31626 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31627 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31628 +                       br_sysfs++;
31629 +               }
31630 +       }
31631 +}
31632 +
31633 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31634 +{
31635 +       int err, i;
31636 +       aufs_bindex_t bbot;
31637 +       struct kobject *kobj;
31638 +       struct au_branch *br;
31639 +       struct au_brsysfs *br_sysfs;
31640 +
31641 +       if (!sysaufs_brs)
31642 +               return;
31643 +
31644 +       kobj = &au_sbi(sb)->si_kobj;
31645 +       bbot = au_sbbot(sb);
31646 +       for (; bindex <= bbot; bindex++) {
31647 +               br = au_sbr(sb, bindex);
31648 +               br_sysfs = br->br_sysfs;
31649 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31650 +                        SysaufsBr_PREFIX "%d", bindex);
31651 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31652 +                        SysaufsBrid_PREFIX "%d", bindex);
31653 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31654 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31655 +                       if (unlikely(err))
31656 +                               pr_warn("failed %s under sysfs(%d)\n",
31657 +                                       br_sysfs->name, err);
31658 +                       br_sysfs++;
31659 +               }
31660 +       }
31661 +}
31662 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31663 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31664 +++ linux/fs/aufs/sysrq.c       2020-01-27 10:57:18.178871751 +0100
31665 @@ -0,0 +1,149 @@
31666 +// SPDX-License-Identifier: GPL-2.0
31667 +/*
31668 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31669 + *
31670 + * This program, aufs is free software; you can redistribute it and/or modify
31671 + * it under the terms of the GNU General Public License as published by
31672 + * the Free Software Foundation; either version 2 of the License, or
31673 + * (at your option) any later version.
31674 + *
31675 + * This program is distributed in the hope that it will be useful,
31676 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31677 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31678 + * GNU General Public License for more details.
31679 + *
31680 + * You should have received a copy of the GNU General Public License
31681 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31682 + */
31683 +
31684 +/*
31685 + * magic sysrq handler
31686 + */
31687 +
31688 +/* #include <linux/sysrq.h> */
31689 +#include <linux/writeback.h>
31690 +#include "aufs.h"
31691 +
31692 +/* ---------------------------------------------------------------------- */
31693 +
31694 +static void sysrq_sb(struct super_block *sb)
31695 +{
31696 +       char *plevel;
31697 +       struct au_sbinfo *sbinfo;
31698 +       struct file *file;
31699 +       struct hlist_bl_head *files;
31700 +       struct hlist_bl_node *pos;
31701 +       struct au_finfo *finfo;
31702 +       struct inode *i;
31703 +
31704 +       plevel = au_plevel;
31705 +       au_plevel = KERN_WARNING;
31706 +
31707 +       /* since we define pr_fmt, call printk directly */
31708 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31709 +
31710 +       sbinfo = au_sbi(sb);
31711 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31712 +       pr("superblock\n");
31713 +       au_dpri_sb(sb);
31714 +
31715 +#if 0 /* reserved */
31716 +       do {
31717 +               int err, i, j, ndentry;
31718 +               struct au_dcsub_pages dpages;
31719 +               struct au_dpage *dpage;
31720 +
31721 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31722 +               if (unlikely(err))
31723 +                       break;
31724 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31725 +               if (!err)
31726 +                       for (i = 0; i < dpages.ndpage; i++) {
31727 +                               dpage = dpages.dpages + i;
31728 +                               ndentry = dpage->ndentry;
31729 +                               for (j = 0; j < ndentry; j++)
31730 +                                       au_dpri_dentry(dpage->dentries[j]);
31731 +                       }
31732 +               au_dpages_free(&dpages);
31733 +       } while (0);
31734 +#endif
31735 +
31736 +       pr("isolated inode\n");
31737 +       spin_lock(&sb->s_inode_list_lock);
31738 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31739 +               spin_lock(&i->i_lock);
31740 +               if (hlist_empty(&i->i_dentry))
31741 +                       au_dpri_inode(i);
31742 +               spin_unlock(&i->i_lock);
31743 +       }
31744 +       spin_unlock(&sb->s_inode_list_lock);
31745 +
31746 +       pr("files\n");
31747 +       files = &au_sbi(sb)->si_files;
31748 +       hlist_bl_lock(files);
31749 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31750 +               umode_t mode;
31751 +
31752 +               file = finfo->fi_file;
31753 +               mode = file_inode(file)->i_mode;
31754 +               if (!special_file(mode))
31755 +                       au_dpri_file(file);
31756 +       }
31757 +       hlist_bl_unlock(files);
31758 +       pr("done\n");
31759 +
31760 +#undef pr
31761 +       au_plevel = plevel;
31762 +}
31763 +
31764 +/* ---------------------------------------------------------------------- */
31765 +
31766 +/* module parameter */
31767 +static char *aufs_sysrq_key = "a";
31768 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31769 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31770 +
31771 +static void au_sysrq(int key __maybe_unused)
31772 +{
31773 +       struct au_sbinfo *sbinfo;
31774 +       struct hlist_bl_node *pos;
31775 +
31776 +       lockdep_off();
31777 +       au_sbilist_lock();
31778 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31779 +               sysrq_sb(sbinfo->si_sb);
31780 +       au_sbilist_unlock();
31781 +       lockdep_on();
31782 +}
31783 +
31784 +static struct sysrq_key_op au_sysrq_op = {
31785 +       .handler        = au_sysrq,
31786 +       .help_msg       = "Aufs",
31787 +       .action_msg     = "Aufs",
31788 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31789 +};
31790 +
31791 +/* ---------------------------------------------------------------------- */
31792 +
31793 +int __init au_sysrq_init(void)
31794 +{
31795 +       int err;
31796 +       char key;
31797 +
31798 +       err = -1;
31799 +       key = *aufs_sysrq_key;
31800 +       if ('a' <= key && key <= 'z')
31801 +               err = register_sysrq_key(key, &au_sysrq_op);
31802 +       if (unlikely(err))
31803 +               pr_err("err %d, sysrq=%c\n", err, key);
31804 +       return err;
31805 +}
31806 +
31807 +void au_sysrq_fin(void)
31808 +{
31809 +       int err;
31810 +
31811 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31812 +       if (unlikely(err))
31813 +               pr_err("err %d (ignored)\n", err);
31814 +}
31815 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31816 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31817 +++ linux/fs/aufs/vdir.c        2020-01-27 10:57:18.178871751 +0100
31818 @@ -0,0 +1,896 @@
31819 +// SPDX-License-Identifier: GPL-2.0
31820 +/*
31821 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31822 + *
31823 + * This program, aufs is free software; you can redistribute it and/or modify
31824 + * it under the terms of the GNU General Public License as published by
31825 + * the Free Software Foundation; either version 2 of the License, or
31826 + * (at your option) any later version.
31827 + *
31828 + * This program is distributed in the hope that it will be useful,
31829 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31830 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31831 + * GNU General Public License for more details.
31832 + *
31833 + * You should have received a copy of the GNU General Public License
31834 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31835 + */
31836 +
31837 +/*
31838 + * virtual or vertical directory
31839 + */
31840 +
31841 +#include <linux/iversion.h>
31842 +#include "aufs.h"
31843 +
31844 +static unsigned int calc_size(int nlen)
31845 +{
31846 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31847 +}
31848 +
31849 +static int set_deblk_end(union au_vdir_deblk_p *p,
31850 +                        union au_vdir_deblk_p *deblk_end)
31851 +{
31852 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31853 +               p->de->de_str.len = 0;
31854 +               /* smp_mb(); */
31855 +               return 0;
31856 +       }
31857 +       return -1; /* error */
31858 +}
31859 +
31860 +/* returns true or false */
31861 +static int is_deblk_end(union au_vdir_deblk_p *p,
31862 +                       union au_vdir_deblk_p *deblk_end)
31863 +{
31864 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31865 +               return !p->de->de_str.len;
31866 +       return 1;
31867 +}
31868 +
31869 +static unsigned char *last_deblk(struct au_vdir *vdir)
31870 +{
31871 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31872 +}
31873 +
31874 +/* ---------------------------------------------------------------------- */
31875 +
31876 +/* estimate the appropriate size for name hash table */
31877 +unsigned int au_rdhash_est(loff_t sz)
31878 +{
31879 +       unsigned int n;
31880 +
31881 +       n = UINT_MAX;
31882 +       sz >>= 10;
31883 +       if (sz < n)
31884 +               n = sz;
31885 +       if (sz < AUFS_RDHASH_DEF)
31886 +               n = AUFS_RDHASH_DEF;
31887 +       /* pr_info("n %u\n", n); */
31888 +       return n;
31889 +}
31890 +
31891 +/*
31892 + * the allocated memory has to be freed by
31893 + * au_nhash_wh_free() or au_nhash_de_free().
31894 + */
31895 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31896 +{
31897 +       struct hlist_head *head;
31898 +       unsigned int u;
31899 +       size_t sz;
31900 +
31901 +       sz = sizeof(*nhash->nh_head) * num_hash;
31902 +       head = kmalloc(sz, gfp);
31903 +       if (head) {
31904 +               nhash->nh_num = num_hash;
31905 +               nhash->nh_head = head;
31906 +               for (u = 0; u < num_hash; u++)
31907 +                       INIT_HLIST_HEAD(head++);
31908 +               return 0; /* success */
31909 +       }
31910 +
31911 +       return -ENOMEM;
31912 +}
31913 +
31914 +static void nhash_count(struct hlist_head *head)
31915 +{
31916 +#if 0 /* debugging */
31917 +       unsigned long n;
31918 +       struct hlist_node *pos;
31919 +
31920 +       n = 0;
31921 +       hlist_for_each(pos, head)
31922 +               n++;
31923 +       pr_info("%lu\n", n);
31924 +#endif
31925 +}
31926 +
31927 +static void au_nhash_wh_do_free(struct hlist_head *head)
31928 +{
31929 +       struct au_vdir_wh *pos;
31930 +       struct hlist_node *node;
31931 +
31932 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31933 +               au_kfree_rcu(pos);
31934 +}
31935 +
31936 +static void au_nhash_de_do_free(struct hlist_head *head)
31937 +{
31938 +       struct au_vdir_dehstr *pos;
31939 +       struct hlist_node *node;
31940 +
31941 +       hlist_for_each_entry_safe(pos, node, head, hash)
31942 +               au_cache_free_vdir_dehstr(pos);
31943 +}
31944 +
31945 +static void au_nhash_do_free(struct au_nhash *nhash,
31946 +                            void (*free)(struct hlist_head *head))
31947 +{
31948 +       unsigned int n;
31949 +       struct hlist_head *head;
31950 +
31951 +       n = nhash->nh_num;
31952 +       if (!n)
31953 +               return;
31954 +
31955 +       head = nhash->nh_head;
31956 +       while (n-- > 0) {
31957 +               nhash_count(head);
31958 +               free(head++);
31959 +       }
31960 +       au_kfree_try_rcu(nhash->nh_head);
31961 +}
31962 +
31963 +void au_nhash_wh_free(struct au_nhash *whlist)
31964 +{
31965 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31966 +}
31967 +
31968 +static void au_nhash_de_free(struct au_nhash *delist)
31969 +{
31970 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31971 +}
31972 +
31973 +/* ---------------------------------------------------------------------- */
31974 +
31975 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31976 +                           int limit)
31977 +{
31978 +       int num;
31979 +       unsigned int u, n;
31980 +       struct hlist_head *head;
31981 +       struct au_vdir_wh *pos;
31982 +
31983 +       num = 0;
31984 +       n = whlist->nh_num;
31985 +       head = whlist->nh_head;
31986 +       for (u = 0; u < n; u++, head++)
31987 +               hlist_for_each_entry(pos, head, wh_hash)
31988 +                       if (pos->wh_bindex == btgt && ++num > limit)
31989 +                               return 1;
31990 +       return 0;
31991 +}
31992 +
31993 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31994 +                                      unsigned char *name,
31995 +                                      unsigned int len)
31996 +{
31997 +       unsigned int v;
31998 +       /* const unsigned int magic_bit = 12; */
31999 +
32000 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32001 +
32002 +       v = 0;
32003 +       if (len > 8)
32004 +               len = 8;
32005 +       while (len--)
32006 +               v += *name++;
32007 +       /* v = hash_long(v, magic_bit); */
32008 +       v %= nhash->nh_num;
32009 +       return nhash->nh_head + v;
32010 +}
32011 +
32012 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32013 +                             int nlen)
32014 +{
32015 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32016 +}
32017 +
32018 +/* returns found or not */
32019 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32020 +{
32021 +       struct hlist_head *head;
32022 +       struct au_vdir_wh *pos;
32023 +       struct au_vdir_destr *str;
32024 +
32025 +       head = au_name_hash(whlist, name, nlen);
32026 +       hlist_for_each_entry(pos, head, wh_hash) {
32027 +               str = &pos->wh_str;
32028 +               AuDbg("%.*s\n", str->len, str->name);
32029 +               if (au_nhash_test_name(str, name, nlen))
32030 +                       return 1;
32031 +       }
32032 +       return 0;
32033 +}
32034 +
32035 +/* returns found(true) or not */
32036 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32037 +{
32038 +       struct hlist_head *head;
32039 +       struct au_vdir_dehstr *pos;
32040 +       struct au_vdir_destr *str;
32041 +
32042 +       head = au_name_hash(delist, name, nlen);
32043 +       hlist_for_each_entry(pos, head, hash) {
32044 +               str = pos->str;
32045 +               AuDbg("%.*s\n", str->len, str->name);
32046 +               if (au_nhash_test_name(str, name, nlen))
32047 +                       return 1;
32048 +       }
32049 +       return 0;
32050 +}
32051 +
32052 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32053 +                           unsigned char d_type)
32054 +{
32055 +#ifdef CONFIG_AUFS_SHWH
32056 +       wh->wh_ino = ino;
32057 +       wh->wh_type = d_type;
32058 +#endif
32059 +}
32060 +
32061 +/* ---------------------------------------------------------------------- */
32062 +
32063 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32064 +                      unsigned int d_type, aufs_bindex_t bindex,
32065 +                      unsigned char shwh)
32066 +{
32067 +       int err;
32068 +       struct au_vdir_destr *str;
32069 +       struct au_vdir_wh *wh;
32070 +
32071 +       AuDbg("%.*s\n", nlen, name);
32072 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32073 +
32074 +       err = -ENOMEM;
32075 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32076 +       if (unlikely(!wh))
32077 +               goto out;
32078 +
32079 +       err = 0;
32080 +       wh->wh_bindex = bindex;
32081 +       if (shwh)
32082 +               au_shwh_init_wh(wh, ino, d_type);
32083 +       str = &wh->wh_str;
32084 +       str->len = nlen;
32085 +       memcpy(str->name, name, nlen);
32086 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32087 +       /* smp_mb(); */
32088 +
32089 +out:
32090 +       return err;
32091 +}
32092 +
32093 +static int append_deblk(struct au_vdir *vdir)
32094 +{
32095 +       int err;
32096 +       unsigned long ul;
32097 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32098 +       union au_vdir_deblk_p p, deblk_end;
32099 +       unsigned char **o;
32100 +
32101 +       err = -ENOMEM;
32102 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32103 +                       GFP_NOFS, /*may_shrink*/0);
32104 +       if (unlikely(!o))
32105 +               goto out;
32106 +
32107 +       vdir->vd_deblk = o;
32108 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32109 +       if (p.deblk) {
32110 +               ul = vdir->vd_nblk++;
32111 +               vdir->vd_deblk[ul] = p.deblk;
32112 +               vdir->vd_last.ul = ul;
32113 +               vdir->vd_last.p.deblk = p.deblk;
32114 +               deblk_end.deblk = p.deblk + deblk_sz;
32115 +               err = set_deblk_end(&p, &deblk_end);
32116 +       }
32117 +
32118 +out:
32119 +       return err;
32120 +}
32121 +
32122 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32123 +                    unsigned int d_type, struct au_nhash *delist)
32124 +{
32125 +       int err;
32126 +       unsigned int sz;
32127 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32128 +       union au_vdir_deblk_p p, *room, deblk_end;
32129 +       struct au_vdir_dehstr *dehstr;
32130 +
32131 +       p.deblk = last_deblk(vdir);
32132 +       deblk_end.deblk = p.deblk + deblk_sz;
32133 +       room = &vdir->vd_last.p;
32134 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32135 +                 || !is_deblk_end(room, &deblk_end));
32136 +
32137 +       sz = calc_size(nlen);
32138 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32139 +               err = append_deblk(vdir);
32140 +               if (unlikely(err))
32141 +                       goto out;
32142 +
32143 +               p.deblk = last_deblk(vdir);
32144 +               deblk_end.deblk = p.deblk + deblk_sz;
32145 +               /* smp_mb(); */
32146 +               AuDebugOn(room->deblk != p.deblk);
32147 +       }
32148 +
32149 +       err = -ENOMEM;
32150 +       dehstr = au_cache_alloc_vdir_dehstr();
32151 +       if (unlikely(!dehstr))
32152 +               goto out;
32153 +
32154 +       dehstr->str = &room->de->de_str;
32155 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32156 +       room->de->de_ino = ino;
32157 +       room->de->de_type = d_type;
32158 +       room->de->de_str.len = nlen;
32159 +       memcpy(room->de->de_str.name, name, nlen);
32160 +
32161 +       err = 0;
32162 +       room->deblk += sz;
32163 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32164 +               err = append_deblk(vdir);
32165 +       /* smp_mb(); */
32166 +
32167 +out:
32168 +       return err;
32169 +}
32170 +
32171 +/* ---------------------------------------------------------------------- */
32172 +
32173 +void au_vdir_free(struct au_vdir *vdir)
32174 +{
32175 +       unsigned char **deblk;
32176 +
32177 +       deblk = vdir->vd_deblk;
32178 +       while (vdir->vd_nblk--)
32179 +               au_kfree_try_rcu(*deblk++);
32180 +       au_kfree_try_rcu(vdir->vd_deblk);
32181 +       au_cache_free_vdir(vdir);
32182 +}
32183 +
32184 +static struct au_vdir *alloc_vdir(struct file *file)
32185 +{
32186 +       struct au_vdir *vdir;
32187 +       struct super_block *sb;
32188 +       int err;
32189 +
32190 +       sb = file->f_path.dentry->d_sb;
32191 +       SiMustAnyLock(sb);
32192 +
32193 +       err = -ENOMEM;
32194 +       vdir = au_cache_alloc_vdir();
32195 +       if (unlikely(!vdir))
32196 +               goto out;
32197 +
32198 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32199 +       if (unlikely(!vdir->vd_deblk))
32200 +               goto out_free;
32201 +
32202 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32203 +       if (!vdir->vd_deblk_sz) {
32204 +               /* estimate the appropriate size for deblk */
32205 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32206 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32207 +       }
32208 +       vdir->vd_nblk = 0;
32209 +       vdir->vd_version = 0;
32210 +       vdir->vd_jiffy = 0;
32211 +       err = append_deblk(vdir);
32212 +       if (!err)
32213 +               return vdir; /* success */
32214 +
32215 +       au_kfree_try_rcu(vdir->vd_deblk);
32216 +
32217 +out_free:
32218 +       au_cache_free_vdir(vdir);
32219 +out:
32220 +       vdir = ERR_PTR(err);
32221 +       return vdir;
32222 +}
32223 +
32224 +static int reinit_vdir(struct au_vdir *vdir)
32225 +{
32226 +       int err;
32227 +       union au_vdir_deblk_p p, deblk_end;
32228 +
32229 +       while (vdir->vd_nblk > 1) {
32230 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32231 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32232 +               vdir->vd_nblk--;
32233 +       }
32234 +       p.deblk = vdir->vd_deblk[0];
32235 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32236 +       err = set_deblk_end(&p, &deblk_end);
32237 +       /* keep vd_dblk_sz */
32238 +       vdir->vd_last.ul = 0;
32239 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32240 +       vdir->vd_version = 0;
32241 +       vdir->vd_jiffy = 0;
32242 +       /* smp_mb(); */
32243 +       return err;
32244 +}
32245 +
32246 +/* ---------------------------------------------------------------------- */
32247 +
32248 +#define AuFillVdir_CALLED      1
32249 +#define AuFillVdir_WHABLE      (1 << 1)
32250 +#define AuFillVdir_SHWH                (1 << 2)
32251 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32252 +#define au_fset_fillvdir(flags, name) \
32253 +       do { (flags) |= AuFillVdir_##name; } while (0)
32254 +#define au_fclr_fillvdir(flags, name) \
32255 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32256 +
32257 +#ifndef CONFIG_AUFS_SHWH
32258 +#undef AuFillVdir_SHWH
32259 +#define AuFillVdir_SHWH                0
32260 +#endif
32261 +
32262 +struct fillvdir_arg {
32263 +       struct dir_context      ctx;
32264 +       struct file             *file;
32265 +       struct au_vdir          *vdir;
32266 +       struct au_nhash         delist;
32267 +       struct au_nhash         whlist;
32268 +       aufs_bindex_t           bindex;
32269 +       unsigned int            flags;
32270 +       int                     err;
32271 +};
32272 +
32273 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32274 +                   loff_t offset __maybe_unused, u64 h_ino,
32275 +                   unsigned int d_type)
32276 +{
32277 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32278 +       char *name = (void *)__name;
32279 +       struct super_block *sb;
32280 +       ino_t ino;
32281 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32282 +
32283 +       arg->err = 0;
32284 +       sb = arg->file->f_path.dentry->d_sb;
32285 +       au_fset_fillvdir(arg->flags, CALLED);
32286 +       /* smp_mb(); */
32287 +       if (nlen <= AUFS_WH_PFX_LEN
32288 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32289 +               if (test_known(&arg->delist, name, nlen)
32290 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32291 +                       goto out; /* already exists or whiteouted */
32292 +
32293 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32294 +               if (!arg->err) {
32295 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32296 +                               d_type = DT_UNKNOWN;
32297 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32298 +                                            d_type, &arg->delist);
32299 +               }
32300 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32301 +               name += AUFS_WH_PFX_LEN;
32302 +               nlen -= AUFS_WH_PFX_LEN;
32303 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32304 +                       goto out; /* already whiteouted */
32305 +
32306 +               ino = 0; /* just to suppress a warning */
32307 +               if (shwh)
32308 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32309 +                                            &ino);
32310 +               if (!arg->err) {
32311 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32312 +                               d_type = DT_UNKNOWN;
32313 +                       arg->err = au_nhash_append_wh
32314 +                               (&arg->whlist, name, nlen, ino, d_type,
32315 +                                arg->bindex, shwh);
32316 +               }
32317 +       }
32318 +
32319 +out:
32320 +       if (!arg->err)
32321 +               arg->vdir->vd_jiffy = jiffies;
32322 +       /* smp_mb(); */
32323 +       AuTraceErr(arg->err);
32324 +       return arg->err;
32325 +}
32326 +
32327 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32328 +                         struct au_nhash *whlist, struct au_nhash *delist)
32329 +{
32330 +#ifdef CONFIG_AUFS_SHWH
32331 +       int err;
32332 +       unsigned int nh, u;
32333 +       struct hlist_head *head;
32334 +       struct au_vdir_wh *pos;
32335 +       struct hlist_node *n;
32336 +       char *p, *o;
32337 +       struct au_vdir_destr *destr;
32338 +
32339 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32340 +
32341 +       err = -ENOMEM;
32342 +       o = p = (void *)__get_free_page(GFP_NOFS);
32343 +       if (unlikely(!p))
32344 +               goto out;
32345 +
32346 +       err = 0;
32347 +       nh = whlist->nh_num;
32348 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32349 +       p += AUFS_WH_PFX_LEN;
32350 +       for (u = 0; u < nh; u++) {
32351 +               head = whlist->nh_head + u;
32352 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32353 +                       destr = &pos->wh_str;
32354 +                       memcpy(p, destr->name, destr->len);
32355 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32356 +                                       pos->wh_ino, pos->wh_type, delist);
32357 +                       if (unlikely(err))
32358 +                               break;
32359 +               }
32360 +       }
32361 +
32362 +       free_page((unsigned long)o);
32363 +
32364 +out:
32365 +       AuTraceErr(err);
32366 +       return err;
32367 +#else
32368 +       return 0;
32369 +#endif
32370 +}
32371 +
32372 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32373 +{
32374 +       int err;
32375 +       unsigned int rdhash;
32376 +       loff_t offset;
32377 +       aufs_bindex_t bbot, bindex, btop;
32378 +       unsigned char shwh;
32379 +       struct file *hf, *file;
32380 +       struct super_block *sb;
32381 +
32382 +       file = arg->file;
32383 +       sb = file->f_path.dentry->d_sb;
32384 +       SiMustAnyLock(sb);
32385 +
32386 +       rdhash = au_sbi(sb)->si_rdhash;
32387 +       if (!rdhash)
32388 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32389 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32390 +       if (unlikely(err))
32391 +               goto out;
32392 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32393 +       if (unlikely(err))
32394 +               goto out_delist;
32395 +
32396 +       err = 0;
32397 +       arg->flags = 0;
32398 +       shwh = 0;
32399 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32400 +               shwh = 1;
32401 +               au_fset_fillvdir(arg->flags, SHWH);
32402 +       }
32403 +       btop = au_fbtop(file);
32404 +       bbot = au_fbbot_dir(file);
32405 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32406 +               hf = au_hf_dir(file, bindex);
32407 +               if (!hf)
32408 +                       continue;
32409 +
32410 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32411 +               err = offset;
32412 +               if (unlikely(offset))
32413 +                       break;
32414 +
32415 +               arg->bindex = bindex;
32416 +               au_fclr_fillvdir(arg->flags, WHABLE);
32417 +               if (shwh
32418 +                   || (bindex != bbot
32419 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32420 +                       au_fset_fillvdir(arg->flags, WHABLE);
32421 +               do {
32422 +                       arg->err = 0;
32423 +                       au_fclr_fillvdir(arg->flags, CALLED);
32424 +                       /* smp_mb(); */
32425 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32426 +                       if (err >= 0)
32427 +                               err = arg->err;
32428 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32429 +
32430 +               /*
32431 +                * dir_relax() may be good for concurrency, but aufs should not
32432 +                * use it since it will cause a lockdep problem.
32433 +                */
32434 +       }
32435 +
32436 +       if (!err && shwh)
32437 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32438 +
32439 +       au_nhash_wh_free(&arg->whlist);
32440 +
32441 +out_delist:
32442 +       au_nhash_de_free(&arg->delist);
32443 +out:
32444 +       return err;
32445 +}
32446 +
32447 +static int read_vdir(struct file *file, int may_read)
32448 +{
32449 +       int err;
32450 +       unsigned long expire;
32451 +       unsigned char do_read;
32452 +       struct fillvdir_arg arg = {
32453 +               .ctx = {
32454 +                       .actor = fillvdir
32455 +               }
32456 +       };
32457 +       struct inode *inode;
32458 +       struct au_vdir *vdir, *allocated;
32459 +
32460 +       err = 0;
32461 +       inode = file_inode(file);
32462 +       IMustLock(inode);
32463 +       IiMustWriteLock(inode);
32464 +       SiMustAnyLock(inode->i_sb);
32465 +
32466 +       allocated = NULL;
32467 +       do_read = 0;
32468 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32469 +       vdir = au_ivdir(inode);
32470 +       if (!vdir) {
32471 +               do_read = 1;
32472 +               vdir = alloc_vdir(file);
32473 +               err = PTR_ERR(vdir);
32474 +               if (IS_ERR(vdir))
32475 +                       goto out;
32476 +               err = 0;
32477 +               allocated = vdir;
32478 +       } else if (may_read
32479 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32480 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32481 +               do_read = 1;
32482 +               err = reinit_vdir(vdir);
32483 +               if (unlikely(err))
32484 +                       goto out;
32485 +       }
32486 +
32487 +       if (!do_read)
32488 +               return 0; /* success */
32489 +
32490 +       arg.file = file;
32491 +       arg.vdir = vdir;
32492 +       err = au_do_read_vdir(&arg);
32493 +       if (!err) {
32494 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32495 +               vdir->vd_version = inode_query_iversion(inode);
32496 +               vdir->vd_last.ul = 0;
32497 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32498 +               if (allocated)
32499 +                       au_set_ivdir(inode, allocated);
32500 +       } else if (allocated)
32501 +               au_vdir_free(allocated);
32502 +
32503 +out:
32504 +       return err;
32505 +}
32506 +
32507 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32508 +{
32509 +       int err, rerr;
32510 +       unsigned long ul, n;
32511 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32512 +
32513 +       AuDebugOn(tgt->vd_nblk != 1);
32514 +
32515 +       err = -ENOMEM;
32516 +       if (tgt->vd_nblk < src->vd_nblk) {
32517 +               unsigned char **p;
32518 +
32519 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32520 +                               GFP_NOFS, /*may_shrink*/0);
32521 +               if (unlikely(!p))
32522 +                       goto out;
32523 +               tgt->vd_deblk = p;
32524 +       }
32525 +
32526 +       if (tgt->vd_deblk_sz != deblk_sz) {
32527 +               unsigned char *p;
32528 +
32529 +               tgt->vd_deblk_sz = deblk_sz;
32530 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32531 +                               /*may_shrink*/1);
32532 +               if (unlikely(!p))
32533 +                       goto out;
32534 +               tgt->vd_deblk[0] = p;
32535 +       }
32536 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32537 +       tgt->vd_version = src->vd_version;
32538 +       tgt->vd_jiffy = src->vd_jiffy;
32539 +
32540 +       n = src->vd_nblk;
32541 +       for (ul = 1; ul < n; ul++) {
32542 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32543 +                                           GFP_NOFS);
32544 +               if (unlikely(!tgt->vd_deblk[ul]))
32545 +                       goto out;
32546 +               tgt->vd_nblk++;
32547 +       }
32548 +       tgt->vd_nblk = n;
32549 +       tgt->vd_last.ul = tgt->vd_last.ul;
32550 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32551 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32552 +               - src->vd_deblk[src->vd_last.ul];
32553 +       /* smp_mb(); */
32554 +       return 0; /* success */
32555 +
32556 +out:
32557 +       rerr = reinit_vdir(tgt);
32558 +       BUG_ON(rerr);
32559 +       return err;
32560 +}
32561 +
32562 +int au_vdir_init(struct file *file)
32563 +{
32564 +       int err;
32565 +       struct inode *inode;
32566 +       struct au_vdir *vdir_cache, *allocated;
32567 +
32568 +       /* test file->f_pos here instead of ctx->pos */
32569 +       err = read_vdir(file, !file->f_pos);
32570 +       if (unlikely(err))
32571 +               goto out;
32572 +
32573 +       allocated = NULL;
32574 +       vdir_cache = au_fvdir_cache(file);
32575 +       if (!vdir_cache) {
32576 +               vdir_cache = alloc_vdir(file);
32577 +               err = PTR_ERR(vdir_cache);
32578 +               if (IS_ERR(vdir_cache))
32579 +                       goto out;
32580 +               allocated = vdir_cache;
32581 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32582 +               /* test file->f_pos here instead of ctx->pos */
32583 +               err = reinit_vdir(vdir_cache);
32584 +               if (unlikely(err))
32585 +                       goto out;
32586 +       } else
32587 +               return 0; /* success */
32588 +
32589 +       inode = file_inode(file);
32590 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32591 +       if (!err) {
32592 +               file->f_version = inode_query_iversion(inode);
32593 +               if (allocated)
32594 +                       au_set_fvdir_cache(file, allocated);
32595 +       } else if (allocated)
32596 +               au_vdir_free(allocated);
32597 +
32598 +out:
32599 +       return err;
32600 +}
32601 +
32602 +static loff_t calc_offset(struct au_vdir *vdir)
32603 +{
32604 +       loff_t offset;
32605 +       union au_vdir_deblk_p p;
32606 +
32607 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32608 +       offset = vdir->vd_last.p.deblk - p.deblk;
32609 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32610 +       return offset;
32611 +}
32612 +
32613 +/* returns true or false */
32614 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32615 +{
32616 +       int valid;
32617 +       unsigned int deblk_sz;
32618 +       unsigned long ul, n;
32619 +       loff_t offset;
32620 +       union au_vdir_deblk_p p, deblk_end;
32621 +       struct au_vdir *vdir_cache;
32622 +
32623 +       valid = 1;
32624 +       vdir_cache = au_fvdir_cache(file);
32625 +       offset = calc_offset(vdir_cache);
32626 +       AuDbg("offset %lld\n", offset);
32627 +       if (ctx->pos == offset)
32628 +               goto out;
32629 +
32630 +       vdir_cache->vd_last.ul = 0;
32631 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32632 +       if (!ctx->pos)
32633 +               goto out;
32634 +
32635 +       valid = 0;
32636 +       deblk_sz = vdir_cache->vd_deblk_sz;
32637 +       ul = div64_u64(ctx->pos, deblk_sz);
32638 +       AuDbg("ul %lu\n", ul);
32639 +       if (ul >= vdir_cache->vd_nblk)
32640 +               goto out;
32641 +
32642 +       n = vdir_cache->vd_nblk;
32643 +       for (; ul < n; ul++) {
32644 +               p.deblk = vdir_cache->vd_deblk[ul];
32645 +               deblk_end.deblk = p.deblk + deblk_sz;
32646 +               offset = ul;
32647 +               offset *= deblk_sz;
32648 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32649 +                       unsigned int l;
32650 +
32651 +                       l = calc_size(p.de->de_str.len);
32652 +                       offset += l;
32653 +                       p.deblk += l;
32654 +               }
32655 +               if (!is_deblk_end(&p, &deblk_end)) {
32656 +                       valid = 1;
32657 +                       vdir_cache->vd_last.ul = ul;
32658 +                       vdir_cache->vd_last.p = p;
32659 +                       break;
32660 +               }
32661 +       }
32662 +
32663 +out:
32664 +       /* smp_mb(); */
32665 +       if (!valid)
32666 +               AuDbg("valid %d\n", !valid);
32667 +       return valid;
32668 +}
32669 +
32670 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32671 +{
32672 +       unsigned int l, deblk_sz;
32673 +       union au_vdir_deblk_p deblk_end;
32674 +       struct au_vdir *vdir_cache;
32675 +       struct au_vdir_de *de;
32676 +
32677 +       if (!seek_vdir(file, ctx))
32678 +               return 0;
32679 +
32680 +       vdir_cache = au_fvdir_cache(file);
32681 +       deblk_sz = vdir_cache->vd_deblk_sz;
32682 +       while (1) {
32683 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32684 +               deblk_end.deblk += deblk_sz;
32685 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32686 +                       de = vdir_cache->vd_last.p.de;
32687 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32688 +                             de->de_str.len, de->de_str.name, ctx->pos,
32689 +                             (unsigned long)de->de_ino, de->de_type);
32690 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32691 +                                              de->de_str.len, de->de_ino,
32692 +                                              de->de_type))) {
32693 +                               /* todo: ignore the error caused by udba? */
32694 +                               /* return err; */
32695 +                               return 0;
32696 +                       }
32697 +
32698 +                       l = calc_size(de->de_str.len);
32699 +                       vdir_cache->vd_last.p.deblk += l;
32700 +                       ctx->pos += l;
32701 +               }
32702 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32703 +                       vdir_cache->vd_last.ul++;
32704 +                       vdir_cache->vd_last.p.deblk
32705 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32706 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32707 +                       continue;
32708 +               }
32709 +               break;
32710 +       }
32711 +
32712 +       /* smp_mb(); */
32713 +       return 0;
32714 +}
32715 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32716 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32717 +++ linux/fs/aufs/vfsub.c       2020-08-03 09:14:46.095748745 +0200
32718 @@ -0,0 +1,895 @@
32719 +// SPDX-License-Identifier: GPL-2.0
32720 +/*
32721 + * Copyright (C) 2005-2020 Junjiro R. Okajima
32722 + *
32723 + * This program, aufs is free software; you can redistribute it and/or modify
32724 + * it under the terms of the GNU General Public License as published by
32725 + * the Free Software Foundation; either version 2 of the License, or
32726 + * (at your option) any later version.
32727 + *
32728 + * This program is distributed in the hope that it will be useful,
32729 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32730 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32731 + * GNU General Public License for more details.
32732 + *
32733 + * You should have received a copy of the GNU General Public License
32734 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32735 + */
32736 +
32737 +/*
32738 + * sub-routines for VFS
32739 + */
32740 +
32741 +#include <linux/mnt_namespace.h>
32742 +#include <linux/namei.h>
32743 +#include <linux/nsproxy.h>
32744 +#include <linux/security.h>
32745 +#include <linux/splice.h>
32746 +#include "aufs.h"
32747 +
32748 +#ifdef CONFIG_AUFS_BR_FUSE
32749 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32750 +{
32751 +       if (!au_test_fuse(h_sb) || !au_userns)
32752 +               return 0;
32753 +
32754 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32755 +}
32756 +#endif
32757 +
32758 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32759 +{
32760 +       int err;
32761 +
32762 +       lockdep_off();
32763 +       down_read(&h_sb->s_umount);
32764 +       err = __sync_filesystem(h_sb, wait);
32765 +       up_read(&h_sb->s_umount);
32766 +       lockdep_on();
32767 +
32768 +       return err;
32769 +}
32770 +
32771 +/* ---------------------------------------------------------------------- */
32772 +
32773 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32774 +{
32775 +       int err;
32776 +       struct kstat st;
32777 +       struct super_block *h_sb;
32778 +
32779 +       /* for remote fs, leave work for its getattr or d_revalidate */
32780 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32781 +       /* still some fs may acquire i_mutex. we need to skip them */
32782 +       err = 0;
32783 +       if (!did)
32784 +               did = &err;
32785 +       h_sb = h_path->dentry->d_sb;
32786 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32787 +       if (*did)
32788 +               err = vfsub_getattr(h_path, &st);
32789 +
32790 +       return err;
32791 +}
32792 +
32793 +/* ---------------------------------------------------------------------- */
32794 +
32795 +struct file *vfsub_dentry_open(struct path *path, int flags)
32796 +{
32797 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32798 +                          current_cred());
32799 +}
32800 +
32801 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32802 +{
32803 +       struct file *file;
32804 +
32805 +       lockdep_off();
32806 +       file = filp_open(path,
32807 +                        oflags /* | __FMODE_NONOTIFY */,
32808 +                        mode);
32809 +       lockdep_on();
32810 +       if (IS_ERR(file))
32811 +               goto out;
32812 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32813 +
32814 +out:
32815 +       return file;
32816 +}
32817 +
32818 +/*
32819 + * Ideally this function should call VFS:do_last() in order to keep all its
32820 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32821 + * structure such as nameidata. This is a second (or third) best approach.
32822 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32823 + */
32824 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32825 +                     struct vfsub_aopen_args *args)
32826 +{
32827 +       int err;
32828 +       struct au_branch *br = args->br;
32829 +       struct file *file = args->file;
32830 +       /* copied from linux/fs/namei.c:atomic_open() */
32831 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32832 +
32833 +       IMustLock(dir);
32834 +       AuDebugOn(!dir->i_op->atomic_open);
32835 +
32836 +       err = au_br_test_oflag(args->open_flag, br);
32837 +       if (unlikely(err))
32838 +               goto out;
32839 +
32840 +       au_lcnt_inc(&br->br_nfiles);
32841 +       file->f_path.dentry = DENTRY_NOT_SET;
32842 +       file->f_path.mnt = au_br_mnt(br);
32843 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32844 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32845 +                                    args->create_mode);
32846 +       if (unlikely(err < 0)) {
32847 +               au_lcnt_dec(&br->br_nfiles);
32848 +               goto out;
32849 +       }
32850 +
32851 +       /* temporary workaround for nfsv4 branch */
32852 +       if (au_test_nfs(dir->i_sb))
32853 +               nfs_mark_for_revalidate(dir);
32854 +
32855 +       if (file->f_mode & FMODE_CREATED)
32856 +               fsnotify_create(dir, dentry);
32857 +       if (!(file->f_mode & FMODE_OPENED)) {
32858 +               au_lcnt_dec(&br->br_nfiles);
32859 +               goto out;
32860 +       }
32861 +
32862 +       /* todo: call VFS:may_open() here */
32863 +       /* todo: ima_file_check() too? */
32864 +       if (!err && (args->open_flag & __FMODE_EXEC))
32865 +               err = deny_write_access(file);
32866 +       if (!err)
32867 +               fsnotify_open(file);
32868 +       else
32869 +               au_lcnt_dec(&br->br_nfiles);
32870 +       /* note that the file is created and still opened */
32871 +
32872 +out:
32873 +       return err;
32874 +}
32875 +
32876 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32877 +{
32878 +       int err;
32879 +
32880 +       err = kern_path(name, flags, path);
32881 +       if (!err && d_is_positive(path->dentry))
32882 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32883 +       return err;
32884 +}
32885 +
32886 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32887 +                                            struct dentry *parent, int len)
32888 +{
32889 +       struct path path = {
32890 +               .mnt = NULL
32891 +       };
32892 +
32893 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32894 +       if (IS_ERR(path.dentry))
32895 +               goto out;
32896 +       if (d_is_positive(path.dentry))
32897 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32898 +
32899 +out:
32900 +       AuTraceErrPtr(path.dentry);
32901 +       return path.dentry;
32902 +}
32903 +
32904 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32905 +                                   int len)
32906 +{
32907 +       struct path path = {
32908 +               .mnt = NULL
32909 +       };
32910 +
32911 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32912 +       IMustLock(d_inode(parent));
32913 +
32914 +       path.dentry = lookup_one_len(name, parent, len);
32915 +       if (IS_ERR(path.dentry))
32916 +               goto out;
32917 +       if (d_is_positive(path.dentry))
32918 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32919 +
32920 +out:
32921 +       AuTraceErrPtr(path.dentry);
32922 +       return path.dentry;
32923 +}
32924 +
32925 +void vfsub_call_lkup_one(void *args)
32926 +{
32927 +       struct vfsub_lkup_one_args *a = args;
32928 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32929 +}
32930 +
32931 +/* ---------------------------------------------------------------------- */
32932 +
32933 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32934 +                                struct dentry *d2, struct au_hinode *hdir2)
32935 +{
32936 +       struct dentry *d;
32937 +
32938 +       lockdep_off();
32939 +       d = lock_rename(d1, d2);
32940 +       lockdep_on();
32941 +       au_hn_suspend(hdir1);
32942 +       if (hdir1 != hdir2)
32943 +               au_hn_suspend(hdir2);
32944 +
32945 +       return d;
32946 +}
32947 +
32948 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32949 +                        struct dentry *d2, struct au_hinode *hdir2)
32950 +{
32951 +       au_hn_resume(hdir1);
32952 +       if (hdir1 != hdir2)
32953 +               au_hn_resume(hdir2);
32954 +       lockdep_off();
32955 +       unlock_rename(d1, d2);
32956 +       lockdep_on();
32957 +}
32958 +
32959 +/* ---------------------------------------------------------------------- */
32960 +
32961 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32962 +{
32963 +       int err;
32964 +       struct dentry *d;
32965 +
32966 +       IMustLock(dir);
32967 +
32968 +       d = path->dentry;
32969 +       path->dentry = d->d_parent;
32970 +       err = security_path_mknod(path, d, mode, 0);
32971 +       path->dentry = d;
32972 +       if (unlikely(err))
32973 +               goto out;
32974 +
32975 +       lockdep_off();
32976 +       err = vfs_create(dir, path->dentry, mode, want_excl);
32977 +       lockdep_on();
32978 +       if (!err) {
32979 +               struct path tmp = *path;
32980 +               int did;
32981 +
32982 +               vfsub_update_h_iattr(&tmp, &did);
32983 +               if (did) {
32984 +                       tmp.dentry = path->dentry->d_parent;
32985 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32986 +               }
32987 +               /*ignore*/
32988 +       }
32989 +
32990 +out:
32991 +       return err;
32992 +}
32993 +
32994 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
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_symlink(path, d, symname);
33004 +       path->dentry = d;
33005 +       if (unlikely(err))
33006 +               goto out;
33007 +
33008 +       lockdep_off();
33009 +       err = vfs_symlink(dir, path->dentry, symname);
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_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
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_mknod(path, d, mode, new_encode_dev(dev));
33037 +       path->dentry = d;
33038 +       if (unlikely(err))
33039 +               goto out;
33040 +
33041 +       lockdep_off();
33042 +       err = vfs_mknod(dir, path->dentry, mode, dev);
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 +static int au_test_nlink(struct inode *inode)
33061 +{
33062 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33063 +
33064 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33065 +           || inode->i_nlink < link_max)
33066 +               return 0;
33067 +       return -EMLINK;
33068 +}
33069 +
33070 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33071 +              struct inode **delegated_inode)
33072 +{
33073 +       int err;
33074 +       struct dentry *d;
33075 +
33076 +       IMustLock(dir);
33077 +
33078 +       err = au_test_nlink(d_inode(src_dentry));
33079 +       if (unlikely(err))
33080 +               return err;
33081 +
33082 +       /* we don't call may_linkat() */
33083 +       d = path->dentry;
33084 +       path->dentry = d->d_parent;
33085 +       err = security_path_link(src_dentry, path, d);
33086 +       path->dentry = d;
33087 +       if (unlikely(err))
33088 +               goto out;
33089 +
33090 +       lockdep_off();
33091 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
33092 +       lockdep_on();
33093 +       if (!err) {
33094 +               struct path tmp = *path;
33095 +               int did;
33096 +
33097 +               /* fuse has different memory inode for the same inumber */
33098 +               vfsub_update_h_iattr(&tmp, &did);
33099 +               if (did) {
33100 +                       tmp.dentry = path->dentry->d_parent;
33101 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33102 +                       tmp.dentry = src_dentry;
33103 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33104 +               }
33105 +               /*ignore*/
33106 +       }
33107 +
33108 +out:
33109 +       return err;
33110 +}
33111 +
33112 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33113 +                struct inode *dir, struct path *path,
33114 +                struct inode **delegated_inode, unsigned int flags)
33115 +{
33116 +       int err;
33117 +       struct path tmp = {
33118 +               .mnt    = path->mnt
33119 +       };
33120 +       struct dentry *d;
33121 +
33122 +       IMustLock(dir);
33123 +       IMustLock(src_dir);
33124 +
33125 +       d = path->dentry;
33126 +       path->dentry = d->d_parent;
33127 +       tmp.dentry = src_dentry->d_parent;
33128 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33129 +       path->dentry = d;
33130 +       if (unlikely(err))
33131 +               goto out;
33132 +
33133 +       lockdep_off();
33134 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33135 +                        delegated_inode, flags);
33136 +       lockdep_on();
33137 +       if (!err) {
33138 +               int did;
33139 +
33140 +               tmp.dentry = d->d_parent;
33141 +               vfsub_update_h_iattr(&tmp, &did);
33142 +               if (did) {
33143 +                       tmp.dentry = src_dentry;
33144 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33145 +                       tmp.dentry = src_dentry->d_parent;
33146 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33147 +               }
33148 +               /*ignore*/
33149 +       }
33150 +
33151 +out:
33152 +       return err;
33153 +}
33154 +
33155 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33156 +{
33157 +       int err;
33158 +       struct dentry *d;
33159 +
33160 +       IMustLock(dir);
33161 +
33162 +       d = path->dentry;
33163 +       path->dentry = d->d_parent;
33164 +       err = security_path_mkdir(path, d, mode);
33165 +       path->dentry = d;
33166 +       if (unlikely(err))
33167 +               goto out;
33168 +
33169 +       lockdep_off();
33170 +       err = vfs_mkdir(dir, path->dentry, mode);
33171 +       lockdep_on();
33172 +       if (!err) {
33173 +               struct path tmp = *path;
33174 +               int did;
33175 +
33176 +               vfsub_update_h_iattr(&tmp, &did);
33177 +               if (did) {
33178 +                       tmp.dentry = path->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_rmdir(struct inode *dir, struct path *path)
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_rmdir(path, d);
33198 +       path->dentry = d;
33199 +       if (unlikely(err))
33200 +               goto out;
33201 +
33202 +       lockdep_off();
33203 +       err = vfs_rmdir(dir, path->dentry);
33204 +       lockdep_on();
33205 +       if (!err) {
33206 +               struct path tmp = {
33207 +                       .dentry = path->dentry->d_parent,
33208 +                       .mnt    = path->mnt
33209 +               };
33210 +
33211 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33212 +       }
33213 +
33214 +out:
33215 +       return err;
33216 +}
33217 +
33218 +/* ---------------------------------------------------------------------- */
33219 +
33220 +/* todo: support mmap_sem? */
33221 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33222 +                    loff_t *ppos)
33223 +{
33224 +       ssize_t err;
33225 +
33226 +       lockdep_off();
33227 +       err = vfs_read(file, ubuf, count, ppos);
33228 +       lockdep_on();
33229 +       if (err >= 0)
33230 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33231 +       return err;
33232 +}
33233 +
33234 +/* todo: kernel_read()? */
33235 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33236 +                    loff_t *ppos)
33237 +{
33238 +       ssize_t err;
33239 +       mm_segment_t oldfs;
33240 +       union {
33241 +               void *k;
33242 +               char __user *u;
33243 +       } buf;
33244 +
33245 +       buf.k = kbuf;
33246 +       oldfs = get_fs();
33247 +       set_fs(KERNEL_DS);
33248 +       err = vfsub_read_u(file, buf.u, count, ppos);
33249 +       set_fs(oldfs);
33250 +       return err;
33251 +}
33252 +
33253 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33254 +                     loff_t *ppos)
33255 +{
33256 +       ssize_t err;
33257 +
33258 +       lockdep_off();
33259 +       err = vfs_write(file, ubuf, count, ppos);
33260 +       lockdep_on();
33261 +       if (err >= 0)
33262 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33263 +       return err;
33264 +}
33265 +
33266 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33267 +{
33268 +       ssize_t err;
33269 +       mm_segment_t oldfs;
33270 +       union {
33271 +               void *k;
33272 +               const char __user *u;
33273 +       } buf;
33274 +
33275 +       buf.k = kbuf;
33276 +       oldfs = get_fs();
33277 +       set_fs(KERNEL_DS);
33278 +       err = vfsub_write_u(file, buf.u, count, ppos);
33279 +       set_fs(oldfs);
33280 +       return err;
33281 +}
33282 +
33283 +int vfsub_flush(struct file *file, fl_owner_t id)
33284 +{
33285 +       int err;
33286 +
33287 +       err = 0;
33288 +       if (file->f_op->flush) {
33289 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33290 +                       err = file->f_op->flush(file, id);
33291 +               else {
33292 +                       lockdep_off();
33293 +                       err = file->f_op->flush(file, id);
33294 +                       lockdep_on();
33295 +               }
33296 +               if (!err)
33297 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33298 +               /*ignore*/
33299 +       }
33300 +       return err;
33301 +}
33302 +
33303 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33304 +{
33305 +       int err;
33306 +
33307 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33308 +
33309 +       lockdep_off();
33310 +       err = iterate_dir(file, ctx);
33311 +       lockdep_on();
33312 +       if (err >= 0)
33313 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33314 +
33315 +       return err;
33316 +}
33317 +
33318 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33319 +                    struct pipe_inode_info *pipe, size_t len,
33320 +                    unsigned int flags)
33321 +{
33322 +       long err;
33323 +
33324 +       lockdep_off();
33325 +       err = do_splice_to(in, ppos, pipe, len, flags);
33326 +       lockdep_on();
33327 +       file_accessed(in);
33328 +       if (err >= 0)
33329 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33330 +       return err;
33331 +}
33332 +
33333 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33334 +                      loff_t *ppos, size_t len, unsigned int flags)
33335 +{
33336 +       long err;
33337 +
33338 +       lockdep_off();
33339 +       err = do_splice_from(pipe, out, ppos, len, flags);
33340 +       lockdep_on();
33341 +       if (err >= 0)
33342 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33343 +       return err;
33344 +}
33345 +
33346 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33347 +{
33348 +       int err;
33349 +
33350 +       /* file can be NULL */
33351 +       lockdep_off();
33352 +       err = vfs_fsync(file, datasync);
33353 +       lockdep_on();
33354 +       if (!err) {
33355 +               if (!path) {
33356 +                       AuDebugOn(!file);
33357 +                       path = &file->f_path;
33358 +               }
33359 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33360 +       }
33361 +       return err;
33362 +}
33363 +
33364 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33365 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33366 +               struct file *h_file)
33367 +{
33368 +       int err;
33369 +       struct inode *h_inode;
33370 +       struct super_block *h_sb;
33371 +
33372 +       if (!h_file) {
33373 +               err = vfsub_truncate(h_path, length);
33374 +               goto out;
33375 +       }
33376 +
33377 +       h_inode = d_inode(h_path->dentry);
33378 +       h_sb = h_inode->i_sb;
33379 +       lockdep_off();
33380 +       sb_start_write(h_sb);
33381 +       lockdep_on();
33382 +       err = locks_verify_truncate(h_inode, h_file, length);
33383 +       if (!err)
33384 +               err = security_path_truncate(h_path);
33385 +       if (!err) {
33386 +               lockdep_off();
33387 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33388 +               lockdep_on();
33389 +       }
33390 +       lockdep_off();
33391 +       sb_end_write(h_sb);
33392 +       lockdep_on();
33393 +
33394 +out:
33395 +       return err;
33396 +}
33397 +
33398 +/* ---------------------------------------------------------------------- */
33399 +
33400 +struct au_vfsub_mkdir_args {
33401 +       int *errp;
33402 +       struct inode *dir;
33403 +       struct path *path;
33404 +       int mode;
33405 +};
33406 +
33407 +static void au_call_vfsub_mkdir(void *args)
33408 +{
33409 +       struct au_vfsub_mkdir_args *a = args;
33410 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33411 +}
33412 +
33413 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33414 +{
33415 +       int err, do_sio, wkq_err;
33416 +
33417 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33418 +       if (!do_sio) {
33419 +               lockdep_off();
33420 +               err = vfsub_mkdir(dir, path, mode);
33421 +               lockdep_on();
33422 +       } else {
33423 +               struct au_vfsub_mkdir_args args = {
33424 +                       .errp   = &err,
33425 +                       .dir    = dir,
33426 +                       .path   = path,
33427 +                       .mode   = mode
33428 +               };
33429 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33430 +               if (unlikely(wkq_err))
33431 +                       err = wkq_err;
33432 +       }
33433 +
33434 +       return err;
33435 +}
33436 +
33437 +struct au_vfsub_rmdir_args {
33438 +       int *errp;
33439 +       struct inode *dir;
33440 +       struct path *path;
33441 +};
33442 +
33443 +static void au_call_vfsub_rmdir(void *args)
33444 +{
33445 +       struct au_vfsub_rmdir_args *a = args;
33446 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33447 +}
33448 +
33449 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33450 +{
33451 +       int err, do_sio, wkq_err;
33452 +
33453 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33454 +       if (!do_sio) {
33455 +               lockdep_off();
33456 +               err = vfsub_rmdir(dir, path);
33457 +               lockdep_on();
33458 +       } else {
33459 +               struct au_vfsub_rmdir_args args = {
33460 +                       .errp   = &err,
33461 +                       .dir    = dir,
33462 +                       .path   = path
33463 +               };
33464 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33465 +               if (unlikely(wkq_err))
33466 +                       err = wkq_err;
33467 +       }
33468 +
33469 +       return err;
33470 +}
33471 +
33472 +/* ---------------------------------------------------------------------- */
33473 +
33474 +struct notify_change_args {
33475 +       int *errp;
33476 +       struct path *path;
33477 +       struct iattr *ia;
33478 +       struct inode **delegated_inode;
33479 +};
33480 +
33481 +static void call_notify_change(void *args)
33482 +{
33483 +       struct notify_change_args *a = args;
33484 +       struct inode *h_inode;
33485 +
33486 +       h_inode = d_inode(a->path->dentry);
33487 +       IMustLock(h_inode);
33488 +
33489 +       *a->errp = -EPERM;
33490 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33491 +               lockdep_off();
33492 +               *a->errp = notify_change(a->path->dentry, a->ia,
33493 +                                        a->delegated_inode);
33494 +               lockdep_on();
33495 +               if (!*a->errp)
33496 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33497 +       }
33498 +       AuTraceErr(*a->errp);
33499 +}
33500 +
33501 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33502 +                       struct inode **delegated_inode)
33503 +{
33504 +       int err;
33505 +       struct notify_change_args args = {
33506 +               .errp                   = &err,
33507 +               .path                   = path,
33508 +               .ia                     = ia,
33509 +               .delegated_inode        = delegated_inode
33510 +       };
33511 +
33512 +       call_notify_change(&args);
33513 +
33514 +       return err;
33515 +}
33516 +
33517 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33518 +                           struct inode **delegated_inode)
33519 +{
33520 +       int err, wkq_err;
33521 +       struct notify_change_args args = {
33522 +               .errp                   = &err,
33523 +               .path                   = path,
33524 +               .ia                     = ia,
33525 +               .delegated_inode        = delegated_inode
33526 +       };
33527 +
33528 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33529 +       if (unlikely(wkq_err))
33530 +               err = wkq_err;
33531 +
33532 +       return err;
33533 +}
33534 +
33535 +/* ---------------------------------------------------------------------- */
33536 +
33537 +struct unlink_args {
33538 +       int *errp;
33539 +       struct inode *dir;
33540 +       struct path *path;
33541 +       struct inode **delegated_inode;
33542 +};
33543 +
33544 +static void call_unlink(void *args)
33545 +{
33546 +       struct unlink_args *a = args;
33547 +       struct dentry *d = a->path->dentry;
33548 +       struct inode *h_inode;
33549 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33550 +                                     && au_dcount(d) == 1);
33551 +
33552 +       IMustLock(a->dir);
33553 +
33554 +       a->path->dentry = d->d_parent;
33555 +       *a->errp = security_path_unlink(a->path, d);
33556 +       a->path->dentry = d;
33557 +       if (unlikely(*a->errp))
33558 +               return;
33559 +
33560 +       if (!stop_sillyrename)
33561 +               dget(d);
33562 +       h_inode = NULL;
33563 +       if (d_is_positive(d)) {
33564 +               h_inode = d_inode(d);
33565 +               ihold(h_inode);
33566 +       }
33567 +
33568 +       lockdep_off();
33569 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33570 +       lockdep_on();
33571 +       if (!*a->errp) {
33572 +               struct path tmp = {
33573 +                       .dentry = d->d_parent,
33574 +                       .mnt    = a->path->mnt
33575 +               };
33576 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33577 +       }
33578 +
33579 +       if (!stop_sillyrename)
33580 +               dput(d);
33581 +       if (h_inode)
33582 +               iput(h_inode);
33583 +
33584 +       AuTraceErr(*a->errp);
33585 +}
33586 +
33587 +/*
33588 + * @dir: must be locked.
33589 + * @dentry: target dentry.
33590 + */
33591 +int vfsub_unlink(struct inode *dir, struct path *path,
33592 +                struct inode **delegated_inode, int force)
33593 +{
33594 +       int err;
33595 +       struct unlink_args args = {
33596 +               .errp                   = &err,
33597 +               .dir                    = dir,
33598 +               .path                   = path,
33599 +               .delegated_inode        = delegated_inode
33600 +       };
33601 +
33602 +       if (!force)
33603 +               call_unlink(&args);
33604 +       else {
33605 +               int wkq_err;
33606 +
33607 +               wkq_err = au_wkq_wait(call_unlink, &args);
33608 +               if (unlikely(wkq_err))
33609 +                       err = wkq_err;
33610 +       }
33611 +
33612 +       return err;
33613 +}
33614 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33615 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33616 +++ linux/fs/aufs/vfsub.h       2020-01-27 10:57:18.178871751 +0100
33617 @@ -0,0 +1,354 @@
33618 +/* SPDX-License-Identifier: GPL-2.0 */
33619 +/*
33620 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33621 + *
33622 + * This program, aufs is free software; you can redistribute it and/or modify
33623 + * it under the terms of the GNU General Public License as published by
33624 + * the Free Software Foundation; either version 2 of the License, or
33625 + * (at your option) any later version.
33626 + *
33627 + * This program is distributed in the hope that it will be useful,
33628 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33629 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33630 + * GNU General Public License for more details.
33631 + *
33632 + * You should have received a copy of the GNU General Public License
33633 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33634 + */
33635 +
33636 +/*
33637 + * sub-routines for VFS
33638 + */
33639 +
33640 +#ifndef __AUFS_VFSUB_H__
33641 +#define __AUFS_VFSUB_H__
33642 +
33643 +#ifdef __KERNEL__
33644 +
33645 +#include <linux/fs.h>
33646 +#include <linux/mount.h>
33647 +#include <linux/posix_acl.h>
33648 +#include <linux/xattr.h>
33649 +#include "debug.h"
33650 +
33651 +/* copied from linux/fs/internal.h */
33652 +/* todo: BAD approach!! */
33653 +extern void __mnt_drop_write(struct vfsmount *);
33654 +extern struct file *alloc_empty_file(int, const struct cred *);
33655 +
33656 +/* ---------------------------------------------------------------------- */
33657 +
33658 +/* lock subclass for lower inode */
33659 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33660 +/* reduce? gave up. */
33661 +enum {
33662 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33663 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33664 +       AuLsc_I_PARENT2,        /* copyup dirs */
33665 +       AuLsc_I_PARENT3,        /* copyup wh */
33666 +       AuLsc_I_CHILD,
33667 +       AuLsc_I_CHILD2,
33668 +       AuLsc_I_End
33669 +};
33670 +
33671 +/* to debug easier, do not make them inlined functions */
33672 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33673 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33674 +
33675 +/* ---------------------------------------------------------------------- */
33676 +
33677 +static inline void vfsub_drop_nlink(struct inode *inode)
33678 +{
33679 +       AuDebugOn(!inode->i_nlink);
33680 +       drop_nlink(inode);
33681 +}
33682 +
33683 +static inline void vfsub_dead_dir(struct inode *inode)
33684 +{
33685 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33686 +       inode->i_flags |= S_DEAD;
33687 +       clear_nlink(inode);
33688 +}
33689 +
33690 +static inline int vfsub_native_ro(struct inode *inode)
33691 +{
33692 +       return sb_rdonly(inode->i_sb)
33693 +               || IS_RDONLY(inode)
33694 +               /* || IS_APPEND(inode) */
33695 +               || IS_IMMUTABLE(inode);
33696 +}
33697 +
33698 +#ifdef CONFIG_AUFS_BR_FUSE
33699 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33700 +#else
33701 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33702 +#endif
33703 +
33704 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33705 +
33706 +/* ---------------------------------------------------------------------- */
33707 +
33708 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33709 +struct file *vfsub_dentry_open(struct path *path, int flags);
33710 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33711 +struct au_branch;
33712 +struct vfsub_aopen_args {
33713 +       struct file             *file;
33714 +       unsigned int            open_flag;
33715 +       umode_t                 create_mode;
33716 +       struct au_branch        *br;
33717 +};
33718 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33719 +                     struct vfsub_aopen_args *args);
33720 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33721 +
33722 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33723 +                                            struct dentry *parent, int len);
33724 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33725 +                                   int len);
33726 +
33727 +struct vfsub_lkup_one_args {
33728 +       struct dentry **errp;
33729 +       struct qstr *name;
33730 +       struct dentry *parent;
33731 +};
33732 +
33733 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33734 +                                           struct dentry *parent)
33735 +{
33736 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33737 +}
33738 +
33739 +void vfsub_call_lkup_one(void *args);
33740 +
33741 +/* ---------------------------------------------------------------------- */
33742 +
33743 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33744 +{
33745 +       int err;
33746 +
33747 +       lockdep_off();
33748 +       err = mnt_want_write(mnt);
33749 +       lockdep_on();
33750 +       return err;
33751 +}
33752 +
33753 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33754 +{
33755 +       lockdep_off();
33756 +       mnt_drop_write(mnt);
33757 +       lockdep_on();
33758 +}
33759 +
33760 +#if 0 /* reserved */
33761 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33762 +{
33763 +       lockdep_off();
33764 +       mnt_drop_write_file(file);
33765 +       lockdep_on();
33766 +}
33767 +#endif
33768 +
33769 +/* ---------------------------------------------------------------------- */
33770 +
33771 +struct au_hinode;
33772 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33773 +                                struct dentry *d2, struct au_hinode *hdir2);
33774 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33775 +                        struct dentry *d2, struct au_hinode *hdir2);
33776 +
33777 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33778 +                bool want_excl);
33779 +int vfsub_symlink(struct inode *dir, struct path *path,
33780 +                 const char *symname);
33781 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33782 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33783 +              struct path *path, struct inode **delegated_inode);
33784 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33785 +                struct inode *hdir, struct path *path,
33786 +                struct inode **delegated_inode, unsigned int flags);
33787 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33788 +int vfsub_rmdir(struct inode *dir, struct path *path);
33789 +
33790 +/* ---------------------------------------------------------------------- */
33791 +
33792 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33793 +                    loff_t *ppos);
33794 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33795 +                       loff_t *ppos);
33796 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33797 +                     loff_t *ppos);
33798 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33799 +                     loff_t *ppos);
33800 +int vfsub_flush(struct file *file, fl_owner_t id);
33801 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33802 +
33803 +static inline loff_t vfsub_f_size_read(struct file *file)
33804 +{
33805 +       return i_size_read(file_inode(file));
33806 +}
33807 +
33808 +static inline unsigned int vfsub_file_flags(struct file *file)
33809 +{
33810 +       unsigned int flags;
33811 +
33812 +       spin_lock(&file->f_lock);
33813 +       flags = file->f_flags;
33814 +       spin_unlock(&file->f_lock);
33815 +
33816 +       return flags;
33817 +}
33818 +
33819 +static inline int vfsub_file_execed(struct file *file)
33820 +{
33821 +       /* todo: direct access f_flags */
33822 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33823 +}
33824 +
33825 +#if 0 /* reserved */
33826 +static inline void vfsub_file_accessed(struct file *h_file)
33827 +{
33828 +       file_accessed(h_file);
33829 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33830 +}
33831 +#endif
33832 +
33833 +#if 0 /* reserved */
33834 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33835 +                                    struct dentry *h_dentry)
33836 +{
33837 +       struct path h_path = {
33838 +               .dentry = h_dentry,
33839 +               .mnt    = h_mnt
33840 +       };
33841 +       touch_atime(&h_path);
33842 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33843 +}
33844 +#endif
33845 +
33846 +static inline int vfsub_update_time(struct inode *h_inode,
33847 +                                   struct timespec64 *ts, int flags)
33848 +{
33849 +       return update_time(h_inode, ts, flags);
33850 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33851 +}
33852 +
33853 +#ifdef CONFIG_FS_POSIX_ACL
33854 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33855 +{
33856 +       int err;
33857 +
33858 +       err = posix_acl_chmod(h_inode, h_mode);
33859 +       if (err == -EOPNOTSUPP)
33860 +               err = 0;
33861 +       return err;
33862 +}
33863 +#else
33864 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33865 +#endif
33866 +
33867 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33868 +                    struct pipe_inode_info *pipe, size_t len,
33869 +                    unsigned int flags);
33870 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33871 +                      loff_t *ppos, size_t len, unsigned int flags);
33872 +
33873 +static inline long vfsub_truncate(struct path *path, loff_t length)
33874 +{
33875 +       long err;
33876 +
33877 +       lockdep_off();
33878 +       err = vfs_truncate(path, length);
33879 +       lockdep_on();
33880 +       return err;
33881 +}
33882 +
33883 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33884 +               struct file *h_file);
33885 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33886 +
33887 +/*
33888 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33889 + * ioctl.
33890 + */
33891 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33892 +                                           loff_t len)
33893 +{
33894 +       loff_t err;
33895 +
33896 +       lockdep_off();
33897 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33898 +       lockdep_on();
33899 +
33900 +       return err;
33901 +}
33902 +
33903 +/* copy_file_range(2) is a systemcall */
33904 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33905 +                                           struct file *dst, loff_t dst_pos,
33906 +                                           size_t len, unsigned int flags)
33907 +{
33908 +       ssize_t ssz;
33909 +
33910 +       lockdep_off();
33911 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33912 +       lockdep_on();
33913 +
33914 +       return ssz;
33915 +}
33916 +
33917 +/* ---------------------------------------------------------------------- */
33918 +
33919 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33920 +{
33921 +       loff_t err;
33922 +
33923 +       lockdep_off();
33924 +       err = vfs_llseek(file, offset, origin);
33925 +       lockdep_on();
33926 +       return err;
33927 +}
33928 +
33929 +/* ---------------------------------------------------------------------- */
33930 +
33931 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33932 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33933 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33934 +                           struct inode **delegated_inode);
33935 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33936 +                       struct inode **delegated_inode);
33937 +int vfsub_unlink(struct inode *dir, struct path *path,
33938 +                struct inode **delegated_inode, int force);
33939 +
33940 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33941 +{
33942 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33943 +}
33944 +
33945 +/* ---------------------------------------------------------------------- */
33946 +
33947 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33948 +                                const void *value, size_t size, int flags)
33949 +{
33950 +       int err;
33951 +
33952 +       lockdep_off();
33953 +       err = vfs_setxattr(dentry, name, value, size, flags);
33954 +       lockdep_on();
33955 +
33956 +       return err;
33957 +}
33958 +
33959 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33960 +{
33961 +       int err;
33962 +
33963 +       lockdep_off();
33964 +       err = vfs_removexattr(dentry, name);
33965 +       lockdep_on();
33966 +
33967 +       return err;
33968 +}
33969 +
33970 +#endif /* __KERNEL__ */
33971 +#endif /* __AUFS_VFSUB_H__ */
33972 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33973 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33974 +++ linux/fs/aufs/wbr_policy.c  2020-01-27 10:57:18.178871751 +0100
33975 @@ -0,0 +1,830 @@
33976 +// SPDX-License-Identifier: GPL-2.0
33977 +/*
33978 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33979 + *
33980 + * This program, aufs is free software; you can redistribute it and/or modify
33981 + * it under the terms of the GNU General Public License as published by
33982 + * the Free Software Foundation; either version 2 of the License, or
33983 + * (at your option) any later version.
33984 + *
33985 + * This program is distributed in the hope that it will be useful,
33986 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33987 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33988 + * GNU General Public License for more details.
33989 + *
33990 + * You should have received a copy of the GNU General Public License
33991 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33992 + */
33993 +
33994 +/*
33995 + * policies for selecting one among multiple writable branches
33996 + */
33997 +
33998 +#include <linux/statfs.h>
33999 +#include "aufs.h"
34000 +
34001 +/* subset of cpup_attr() */
34002 +static noinline_for_stack
34003 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34004 +{
34005 +       int err, sbits;
34006 +       struct iattr ia;
34007 +       struct inode *h_isrc;
34008 +
34009 +       h_isrc = d_inode(h_src);
34010 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34011 +       ia.ia_mode = h_isrc->i_mode;
34012 +       ia.ia_uid = h_isrc->i_uid;
34013 +       ia.ia_gid = h_isrc->i_gid;
34014 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34015 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34016 +       /* no delegation since it is just created */
34017 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34018 +
34019 +       /* is this nfs only? */
34020 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34021 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34022 +               ia.ia_mode = h_isrc->i_mode;
34023 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34024 +       }
34025 +
34026 +       return err;
34027 +}
34028 +
34029 +#define AuCpdown_PARENT_OPQ    1
34030 +#define AuCpdown_WHED          (1 << 1)
34031 +#define AuCpdown_MADE_DIR      (1 << 2)
34032 +#define AuCpdown_DIROPQ                (1 << 3)
34033 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34034 +#define au_fset_cpdown(flags, name) \
34035 +       do { (flags) |= AuCpdown_##name; } while (0)
34036 +#define au_fclr_cpdown(flags, name) \
34037 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34038 +
34039 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34040 +                            unsigned int *flags)
34041 +{
34042 +       int err;
34043 +       struct dentry *opq_dentry;
34044 +
34045 +       opq_dentry = au_diropq_create(dentry, bdst);
34046 +       err = PTR_ERR(opq_dentry);
34047 +       if (IS_ERR(opq_dentry))
34048 +               goto out;
34049 +       dput(opq_dentry);
34050 +       au_fset_cpdown(*flags, DIROPQ);
34051 +
34052 +out:
34053 +       return err;
34054 +}
34055 +
34056 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34057 +                           struct inode *dir, aufs_bindex_t bdst)
34058 +{
34059 +       int err;
34060 +       struct path h_path;
34061 +       struct au_branch *br;
34062 +
34063 +       br = au_sbr(dentry->d_sb, bdst);
34064 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34065 +       err = PTR_ERR(h_path.dentry);
34066 +       if (IS_ERR(h_path.dentry))
34067 +               goto out;
34068 +
34069 +       err = 0;
34070 +       if (d_is_positive(h_path.dentry)) {
34071 +               h_path.mnt = au_br_mnt(br);
34072 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34073 +                                         dentry);
34074 +       }
34075 +       dput(h_path.dentry);
34076 +
34077 +out:
34078 +       return err;
34079 +}
34080 +
34081 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34082 +                        struct au_pin *pin,
34083 +                        struct dentry *h_parent, void *arg)
34084 +{
34085 +       int err, rerr;
34086 +       aufs_bindex_t bopq, btop;
34087 +       struct path h_path;
34088 +       struct dentry *parent;
34089 +       struct inode *h_dir, *h_inode, *inode, *dir;
34090 +       unsigned int *flags = arg;
34091 +
34092 +       btop = au_dbtop(dentry);
34093 +       /* dentry is di-locked */
34094 +       parent = dget_parent(dentry);
34095 +       dir = d_inode(parent);
34096 +       h_dir = d_inode(h_parent);
34097 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34098 +       IMustLock(h_dir);
34099 +
34100 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34101 +       if (unlikely(err < 0))
34102 +               goto out;
34103 +       h_path.dentry = au_h_dptr(dentry, bdst);
34104 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34105 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34106 +       if (unlikely(err))
34107 +               goto out_put;
34108 +       au_fset_cpdown(*flags, MADE_DIR);
34109 +
34110 +       bopq = au_dbdiropq(dentry);
34111 +       au_fclr_cpdown(*flags, WHED);
34112 +       au_fclr_cpdown(*flags, DIROPQ);
34113 +       if (au_dbwh(dentry) == bdst)
34114 +               au_fset_cpdown(*flags, WHED);
34115 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34116 +               au_fset_cpdown(*flags, PARENT_OPQ);
34117 +       h_inode = d_inode(h_path.dentry);
34118 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34119 +       if (au_ftest_cpdown(*flags, WHED)) {
34120 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34121 +               if (unlikely(err)) {
34122 +                       inode_unlock(h_inode);
34123 +                       goto out_dir;
34124 +               }
34125 +       }
34126 +
34127 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34128 +       inode_unlock(h_inode);
34129 +       if (unlikely(err))
34130 +               goto out_opq;
34131 +
34132 +       if (au_ftest_cpdown(*flags, WHED)) {
34133 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34134 +               if (unlikely(err))
34135 +                       goto out_opq;
34136 +       }
34137 +
34138 +       inode = d_inode(dentry);
34139 +       if (au_ibbot(inode) < bdst)
34140 +               au_set_ibbot(inode, bdst);
34141 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34142 +                     au_hi_flags(inode, /*isdir*/1));
34143 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34144 +       goto out; /* success */
34145 +
34146 +       /* revert */
34147 +out_opq:
34148 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34149 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34150 +               rerr = au_diropq_remove(dentry, bdst);
34151 +               inode_unlock(h_inode);
34152 +               if (unlikely(rerr)) {
34153 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34154 +                               dentry, bdst, rerr);
34155 +                       err = -EIO;
34156 +                       goto out;
34157 +               }
34158 +       }
34159 +out_dir:
34160 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34161 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34162 +               if (unlikely(rerr)) {
34163 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34164 +                               dentry, bdst, rerr);
34165 +                       err = -EIO;
34166 +               }
34167 +       }
34168 +out_put:
34169 +       au_set_h_dptr(dentry, bdst, NULL);
34170 +       if (au_dbbot(dentry) == bdst)
34171 +               au_update_dbbot(dentry);
34172 +out:
34173 +       dput(parent);
34174 +       return err;
34175 +}
34176 +
34177 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34178 +{
34179 +       int err;
34180 +       unsigned int flags;
34181 +
34182 +       flags = 0;
34183 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34184 +
34185 +       return err;
34186 +}
34187 +
34188 +/* ---------------------------------------------------------------------- */
34189 +
34190 +/* policies for create */
34191 +
34192 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34193 +{
34194 +       int err, i, j, ndentry;
34195 +       aufs_bindex_t bopq;
34196 +       struct au_dcsub_pages dpages;
34197 +       struct au_dpage *dpage;
34198 +       struct dentry **dentries, *parent, *d;
34199 +
34200 +       err = au_dpages_init(&dpages, GFP_NOFS);
34201 +       if (unlikely(err))
34202 +               goto out;
34203 +       parent = dget_parent(dentry);
34204 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34205 +       if (unlikely(err))
34206 +               goto out_free;
34207 +
34208 +       err = bindex;
34209 +       for (i = 0; i < dpages.ndpage; i++) {
34210 +               dpage = dpages.dpages + i;
34211 +               dentries = dpage->dentries;
34212 +               ndentry = dpage->ndentry;
34213 +               for (j = 0; j < ndentry; j++) {
34214 +                       d = dentries[j];
34215 +                       di_read_lock_parent2(d, !AuLock_IR);
34216 +                       bopq = au_dbdiropq(d);
34217 +                       di_read_unlock(d, !AuLock_IR);
34218 +                       if (bopq >= 0 && bopq < err)
34219 +                               err = bopq;
34220 +               }
34221 +       }
34222 +
34223 +out_free:
34224 +       dput(parent);
34225 +       au_dpages_free(&dpages);
34226 +out:
34227 +       return err;
34228 +}
34229 +
34230 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34231 +{
34232 +       for (; bindex >= 0; bindex--)
34233 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34234 +                       return bindex;
34235 +       return -EROFS;
34236 +}
34237 +
34238 +/* top down parent */
34239 +static int au_wbr_create_tdp(struct dentry *dentry,
34240 +                            unsigned int flags __maybe_unused)
34241 +{
34242 +       int err;
34243 +       aufs_bindex_t btop, bindex;
34244 +       struct super_block *sb;
34245 +       struct dentry *parent, *h_parent;
34246 +
34247 +       sb = dentry->d_sb;
34248 +       btop = au_dbtop(dentry);
34249 +       err = btop;
34250 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34251 +               goto out;
34252 +
34253 +       err = -EROFS;
34254 +       parent = dget_parent(dentry);
34255 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34256 +               h_parent = au_h_dptr(parent, bindex);
34257 +               if (!h_parent || d_is_negative(h_parent))
34258 +                       continue;
34259 +
34260 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34261 +                       err = bindex;
34262 +                       break;
34263 +               }
34264 +       }
34265 +       dput(parent);
34266 +
34267 +       /* bottom up here */
34268 +       if (unlikely(err < 0)) {
34269 +               err = au_wbr_bu(sb, btop - 1);
34270 +               if (err >= 0)
34271 +                       err = au_wbr_nonopq(dentry, err);
34272 +       }
34273 +
34274 +out:
34275 +       AuDbg("b%d\n", err);
34276 +       return err;
34277 +}
34278 +
34279 +/* ---------------------------------------------------------------------- */
34280 +
34281 +/* an exception for the policy other than tdp */
34282 +static int au_wbr_create_exp(struct dentry *dentry)
34283 +{
34284 +       int err;
34285 +       aufs_bindex_t bwh, bdiropq;
34286 +       struct dentry *parent;
34287 +
34288 +       err = -1;
34289 +       bwh = au_dbwh(dentry);
34290 +       parent = dget_parent(dentry);
34291 +       bdiropq = au_dbdiropq(parent);
34292 +       if (bwh >= 0) {
34293 +               if (bdiropq >= 0)
34294 +                       err = min(bdiropq, bwh);
34295 +               else
34296 +                       err = bwh;
34297 +               AuDbg("%d\n", err);
34298 +       } else if (bdiropq >= 0) {
34299 +               err = bdiropq;
34300 +               AuDbg("%d\n", err);
34301 +       }
34302 +       dput(parent);
34303 +
34304 +       if (err >= 0)
34305 +               err = au_wbr_nonopq(dentry, err);
34306 +
34307 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34308 +               err = -1;
34309 +
34310 +       AuDbg("%d\n", err);
34311 +       return err;
34312 +}
34313 +
34314 +/* ---------------------------------------------------------------------- */
34315 +
34316 +/* round robin */
34317 +static int au_wbr_create_init_rr(struct super_block *sb)
34318 +{
34319 +       int err;
34320 +
34321 +       err = au_wbr_bu(sb, au_sbbot(sb));
34322 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34323 +       /* smp_mb(); */
34324 +
34325 +       AuDbg("b%d\n", err);
34326 +       return err;
34327 +}
34328 +
34329 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34330 +{
34331 +       int err, nbr;
34332 +       unsigned int u;
34333 +       aufs_bindex_t bindex, bbot;
34334 +       struct super_block *sb;
34335 +       atomic_t *next;
34336 +
34337 +       err = au_wbr_create_exp(dentry);
34338 +       if (err >= 0)
34339 +               goto out;
34340 +
34341 +       sb = dentry->d_sb;
34342 +       next = &au_sbi(sb)->si_wbr_rr_next;
34343 +       bbot = au_sbbot(sb);
34344 +       nbr = bbot + 1;
34345 +       for (bindex = 0; bindex <= bbot; bindex++) {
34346 +               if (!au_ftest_wbr(flags, DIR)) {
34347 +                       err = atomic_dec_return(next) + 1;
34348 +                       /* modulo for 0 is meaningless */
34349 +                       if (unlikely(!err))
34350 +                               err = atomic_dec_return(next) + 1;
34351 +               } else
34352 +                       err = atomic_read(next);
34353 +               AuDbg("%d\n", err);
34354 +               u = err;
34355 +               err = u % nbr;
34356 +               AuDbg("%d\n", err);
34357 +               if (!au_br_rdonly(au_sbr(sb, err)))
34358 +                       break;
34359 +               err = -EROFS;
34360 +       }
34361 +
34362 +       if (err >= 0)
34363 +               err = au_wbr_nonopq(dentry, err);
34364 +
34365 +out:
34366 +       AuDbg("%d\n", err);
34367 +       return err;
34368 +}
34369 +
34370 +/* ---------------------------------------------------------------------- */
34371 +
34372 +/* most free space */
34373 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34374 +{
34375 +       struct super_block *sb;
34376 +       struct au_branch *br;
34377 +       struct au_wbr_mfs *mfs;
34378 +       struct dentry *h_parent;
34379 +       aufs_bindex_t bindex, bbot;
34380 +       int err;
34381 +       unsigned long long b, bavail;
34382 +       struct path h_path;
34383 +       /* reduce the stack usage */
34384 +       struct kstatfs *st;
34385 +
34386 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34387 +       if (unlikely(!st)) {
34388 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34389 +               return;
34390 +       }
34391 +
34392 +       bavail = 0;
34393 +       sb = dentry->d_sb;
34394 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34395 +       MtxMustLock(&mfs->mfs_lock);
34396 +       mfs->mfs_bindex = -EROFS;
34397 +       mfs->mfsrr_bytes = 0;
34398 +       if (!parent) {
34399 +               bindex = 0;
34400 +               bbot = au_sbbot(sb);
34401 +       } else {
34402 +               bindex = au_dbtop(parent);
34403 +               bbot = au_dbtaildir(parent);
34404 +       }
34405 +
34406 +       for (; bindex <= bbot; bindex++) {
34407 +               if (parent) {
34408 +                       h_parent = au_h_dptr(parent, bindex);
34409 +                       if (!h_parent || d_is_negative(h_parent))
34410 +                               continue;
34411 +               }
34412 +               br = au_sbr(sb, bindex);
34413 +               if (au_br_rdonly(br))
34414 +                       continue;
34415 +
34416 +               /* sb->s_root for NFS is unreliable */
34417 +               h_path.mnt = au_br_mnt(br);
34418 +               h_path.dentry = h_path.mnt->mnt_root;
34419 +               err = vfs_statfs(&h_path, st);
34420 +               if (unlikely(err)) {
34421 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34422 +                       continue;
34423 +               }
34424 +
34425 +               /* when the available size is equal, select the lower one */
34426 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34427 +                            || sizeof(b) < sizeof(st->f_bsize));
34428 +               b = st->f_bavail * st->f_bsize;
34429 +               br->br_wbr->wbr_bytes = b;
34430 +               if (b >= bavail) {
34431 +                       bavail = b;
34432 +                       mfs->mfs_bindex = bindex;
34433 +                       mfs->mfs_jiffy = jiffies;
34434 +               }
34435 +       }
34436 +
34437 +       mfs->mfsrr_bytes = bavail;
34438 +       AuDbg("b%d\n", mfs->mfs_bindex);
34439 +       au_kfree_rcu(st);
34440 +}
34441 +
34442 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34443 +{
34444 +       int err;
34445 +       struct dentry *parent;
34446 +       struct super_block *sb;
34447 +       struct au_wbr_mfs *mfs;
34448 +
34449 +       err = au_wbr_create_exp(dentry);
34450 +       if (err >= 0)
34451 +               goto out;
34452 +
34453 +       sb = dentry->d_sb;
34454 +       parent = NULL;
34455 +       if (au_ftest_wbr(flags, PARENT))
34456 +               parent = dget_parent(dentry);
34457 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34458 +       mutex_lock(&mfs->mfs_lock);
34459 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34460 +           || mfs->mfs_bindex < 0
34461 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34462 +               au_mfs(dentry, parent);
34463 +       mutex_unlock(&mfs->mfs_lock);
34464 +       err = mfs->mfs_bindex;
34465 +       dput(parent);
34466 +
34467 +       if (err >= 0)
34468 +               err = au_wbr_nonopq(dentry, err);
34469 +
34470 +out:
34471 +       AuDbg("b%d\n", err);
34472 +       return err;
34473 +}
34474 +
34475 +static int au_wbr_create_init_mfs(struct super_block *sb)
34476 +{
34477 +       struct au_wbr_mfs *mfs;
34478 +
34479 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34480 +       mutex_init(&mfs->mfs_lock);
34481 +       mfs->mfs_jiffy = 0;
34482 +       mfs->mfs_bindex = -EROFS;
34483 +
34484 +       return 0;
34485 +}
34486 +
34487 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34488 +{
34489 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34490 +       return 0;
34491 +}
34492 +
34493 +/* ---------------------------------------------------------------------- */
34494 +
34495 +/* top down regardless parent, and then mfs */
34496 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34497 +                              unsigned int flags __maybe_unused)
34498 +{
34499 +       int err;
34500 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34501 +       unsigned long long watermark;
34502 +       struct super_block *sb;
34503 +       struct au_wbr_mfs *mfs;
34504 +       struct au_branch *br;
34505 +       struct dentry *parent;
34506 +
34507 +       sb = dentry->d_sb;
34508 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34509 +       mutex_lock(&mfs->mfs_lock);
34510 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34511 +           || mfs->mfs_bindex < 0)
34512 +               au_mfs(dentry, /*parent*/NULL);
34513 +       watermark = mfs->mfsrr_watermark;
34514 +       bmfs = mfs->mfs_bindex;
34515 +       mutex_unlock(&mfs->mfs_lock);
34516 +
34517 +       /* another style of au_wbr_create_exp() */
34518 +       bwh = au_dbwh(dentry);
34519 +       parent = dget_parent(dentry);
34520 +       btail = au_dbtaildir(parent);
34521 +       if (bwh >= 0 && bwh < btail)
34522 +               btail = bwh;
34523 +
34524 +       err = au_wbr_nonopq(dentry, btail);
34525 +       if (unlikely(err < 0))
34526 +               goto out;
34527 +       btail = err;
34528 +       bfound = -1;
34529 +       for (bindex = 0; bindex <= btail; bindex++) {
34530 +               br = au_sbr(sb, bindex);
34531 +               if (au_br_rdonly(br))
34532 +                       continue;
34533 +               if (br->br_wbr->wbr_bytes > watermark) {
34534 +                       bfound = bindex;
34535 +                       break;
34536 +               }
34537 +       }
34538 +       err = bfound;
34539 +       if (err < 0)
34540 +               err = bmfs;
34541 +
34542 +out:
34543 +       dput(parent);
34544 +       AuDbg("b%d\n", err);
34545 +       return err;
34546 +}
34547 +
34548 +/* ---------------------------------------------------------------------- */
34549 +
34550 +/* most free space and then round robin */
34551 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34552 +{
34553 +       int err;
34554 +       struct au_wbr_mfs *mfs;
34555 +
34556 +       err = au_wbr_create_mfs(dentry, flags);
34557 +       if (err >= 0) {
34558 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34559 +               mutex_lock(&mfs->mfs_lock);
34560 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34561 +                       err = au_wbr_create_rr(dentry, flags);
34562 +               mutex_unlock(&mfs->mfs_lock);
34563 +       }
34564 +
34565 +       AuDbg("b%d\n", err);
34566 +       return err;
34567 +}
34568 +
34569 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34570 +{
34571 +       int err;
34572 +
34573 +       au_wbr_create_init_mfs(sb); /* ignore */
34574 +       err = au_wbr_create_init_rr(sb);
34575 +
34576 +       return err;
34577 +}
34578 +
34579 +/* ---------------------------------------------------------------------- */
34580 +
34581 +/* top down parent and most free space */
34582 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34583 +{
34584 +       int err, e2;
34585 +       unsigned long long b;
34586 +       aufs_bindex_t bindex, btop, bbot;
34587 +       struct super_block *sb;
34588 +       struct dentry *parent, *h_parent;
34589 +       struct au_branch *br;
34590 +
34591 +       err = au_wbr_create_tdp(dentry, flags);
34592 +       if (unlikely(err < 0))
34593 +               goto out;
34594 +       parent = dget_parent(dentry);
34595 +       btop = au_dbtop(parent);
34596 +       bbot = au_dbtaildir(parent);
34597 +       if (btop == bbot)
34598 +               goto out_parent; /* success */
34599 +
34600 +       e2 = au_wbr_create_mfs(dentry, flags);
34601 +       if (e2 < 0)
34602 +               goto out_parent; /* success */
34603 +
34604 +       /* when the available size is equal, select upper one */
34605 +       sb = dentry->d_sb;
34606 +       br = au_sbr(sb, err);
34607 +       b = br->br_wbr->wbr_bytes;
34608 +       AuDbg("b%d, %llu\n", err, b);
34609 +
34610 +       for (bindex = btop; bindex <= bbot; bindex++) {
34611 +               h_parent = au_h_dptr(parent, bindex);
34612 +               if (!h_parent || d_is_negative(h_parent))
34613 +                       continue;
34614 +
34615 +               br = au_sbr(sb, bindex);
34616 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34617 +                       b = br->br_wbr->wbr_bytes;
34618 +                       err = bindex;
34619 +                       AuDbg("b%d, %llu\n", err, b);
34620 +               }
34621 +       }
34622 +
34623 +       if (err >= 0)
34624 +               err = au_wbr_nonopq(dentry, err);
34625 +
34626 +out_parent:
34627 +       dput(parent);
34628 +out:
34629 +       AuDbg("b%d\n", err);
34630 +       return err;
34631 +}
34632 +
34633 +/* ---------------------------------------------------------------------- */
34634 +
34635 +/*
34636 + * - top down parent
34637 + * - most free space with parent
34638 + * - most free space round-robin regardless parent
34639 + */
34640 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34641 +{
34642 +       int err;
34643 +       unsigned long long watermark;
34644 +       struct super_block *sb;
34645 +       struct au_branch *br;
34646 +       struct au_wbr_mfs *mfs;
34647 +
34648 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34649 +       if (unlikely(err < 0))
34650 +               goto out;
34651 +
34652 +       sb = dentry->d_sb;
34653 +       br = au_sbr(sb, err);
34654 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34655 +       mutex_lock(&mfs->mfs_lock);
34656 +       watermark = mfs->mfsrr_watermark;
34657 +       mutex_unlock(&mfs->mfs_lock);
34658 +       if (br->br_wbr->wbr_bytes < watermark)
34659 +               /* regardless the parent dir */
34660 +               err = au_wbr_create_mfsrr(dentry, flags);
34661 +
34662 +out:
34663 +       AuDbg("b%d\n", err);
34664 +       return err;
34665 +}
34666 +
34667 +/* ---------------------------------------------------------------------- */
34668 +
34669 +/* policies for copyup */
34670 +
34671 +/* top down parent */
34672 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34673 +{
34674 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34675 +}
34676 +
34677 +/* bottom up parent */
34678 +static int au_wbr_copyup_bup(struct dentry *dentry)
34679 +{
34680 +       int err;
34681 +       aufs_bindex_t bindex, btop;
34682 +       struct dentry *parent, *h_parent;
34683 +       struct super_block *sb;
34684 +
34685 +       err = -EROFS;
34686 +       sb = dentry->d_sb;
34687 +       parent = dget_parent(dentry);
34688 +       btop = au_dbtop(parent);
34689 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34690 +               h_parent = au_h_dptr(parent, bindex);
34691 +               if (!h_parent || d_is_negative(h_parent))
34692 +                       continue;
34693 +
34694 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34695 +                       err = bindex;
34696 +                       break;
34697 +               }
34698 +       }
34699 +       dput(parent);
34700 +
34701 +       /* bottom up here */
34702 +       if (unlikely(err < 0))
34703 +               err = au_wbr_bu(sb, btop - 1);
34704 +
34705 +       AuDbg("b%d\n", err);
34706 +       return err;
34707 +}
34708 +
34709 +/* bottom up */
34710 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34711 +{
34712 +       int err;
34713 +
34714 +       err = au_wbr_bu(dentry->d_sb, btop);
34715 +       AuDbg("b%d\n", err);
34716 +       if (err > btop)
34717 +               err = au_wbr_nonopq(dentry, err);
34718 +
34719 +       AuDbg("b%d\n", err);
34720 +       return err;
34721 +}
34722 +
34723 +static int au_wbr_copyup_bu(struct dentry *dentry)
34724 +{
34725 +       int err;
34726 +       aufs_bindex_t btop;
34727 +
34728 +       btop = au_dbtop(dentry);
34729 +       err = au_wbr_do_copyup_bu(dentry, btop);
34730 +       return err;
34731 +}
34732 +
34733 +/* ---------------------------------------------------------------------- */
34734 +
34735 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34736 +       [AuWbrCopyup_TDP] = {
34737 +               .copyup = au_wbr_copyup_tdp
34738 +       },
34739 +       [AuWbrCopyup_BUP] = {
34740 +               .copyup = au_wbr_copyup_bup
34741 +       },
34742 +       [AuWbrCopyup_BU] = {
34743 +               .copyup = au_wbr_copyup_bu
34744 +       }
34745 +};
34746 +
34747 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34748 +       [AuWbrCreate_TDP] = {
34749 +               .create = au_wbr_create_tdp
34750 +       },
34751 +       [AuWbrCreate_RR] = {
34752 +               .create = au_wbr_create_rr,
34753 +               .init   = au_wbr_create_init_rr
34754 +       },
34755 +       [AuWbrCreate_MFS] = {
34756 +               .create = au_wbr_create_mfs,
34757 +               .init   = au_wbr_create_init_mfs,
34758 +               .fin    = au_wbr_create_fin_mfs
34759 +       },
34760 +       [AuWbrCreate_MFSV] = {
34761 +               .create = au_wbr_create_mfs,
34762 +               .init   = au_wbr_create_init_mfs,
34763 +               .fin    = au_wbr_create_fin_mfs
34764 +       },
34765 +       [AuWbrCreate_MFSRR] = {
34766 +               .create = au_wbr_create_mfsrr,
34767 +               .init   = au_wbr_create_init_mfsrr,
34768 +               .fin    = au_wbr_create_fin_mfs
34769 +       },
34770 +       [AuWbrCreate_MFSRRV] = {
34771 +               .create = au_wbr_create_mfsrr,
34772 +               .init   = au_wbr_create_init_mfsrr,
34773 +               .fin    = au_wbr_create_fin_mfs
34774 +       },
34775 +       [AuWbrCreate_TDMFS] = {
34776 +               .create = au_wbr_create_tdmfs,
34777 +               .init   = au_wbr_create_init_mfs,
34778 +               .fin    = au_wbr_create_fin_mfs
34779 +       },
34780 +       [AuWbrCreate_TDMFSV] = {
34781 +               .create = au_wbr_create_tdmfs,
34782 +               .init   = au_wbr_create_init_mfs,
34783 +               .fin    = au_wbr_create_fin_mfs
34784 +       },
34785 +       [AuWbrCreate_PMFS] = {
34786 +               .create = au_wbr_create_pmfs,
34787 +               .init   = au_wbr_create_init_mfs,
34788 +               .fin    = au_wbr_create_fin_mfs
34789 +       },
34790 +       [AuWbrCreate_PMFSV] = {
34791 +               .create = au_wbr_create_pmfs,
34792 +               .init   = au_wbr_create_init_mfs,
34793 +               .fin    = au_wbr_create_fin_mfs
34794 +       },
34795 +       [AuWbrCreate_PMFSRR] = {
34796 +               .create = au_wbr_create_pmfsrr,
34797 +               .init   = au_wbr_create_init_mfsrr,
34798 +               .fin    = au_wbr_create_fin_mfs
34799 +       },
34800 +       [AuWbrCreate_PMFSRRV] = {
34801 +               .create = au_wbr_create_pmfsrr,
34802 +               .init   = au_wbr_create_init_mfsrr,
34803 +               .fin    = au_wbr_create_fin_mfs
34804 +       }
34805 +};
34806 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34807 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34808 +++ linux/fs/aufs/whout.c       2020-01-27 10:57:18.178871751 +0100
34809 @@ -0,0 +1,1062 @@
34810 +// SPDX-License-Identifier: GPL-2.0
34811 +/*
34812 + * Copyright (C) 2005-2020 Junjiro R. Okajima
34813 + *
34814 + * This program, aufs is free software; you can redistribute it and/or modify
34815 + * it under the terms of the GNU General Public License as published by
34816 + * the Free Software Foundation; either version 2 of the License, or
34817 + * (at your option) any later version.
34818 + *
34819 + * This program is distributed in the hope that it will be useful,
34820 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34821 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34822 + * GNU General Public License for more details.
34823 + *
34824 + * You should have received a copy of the GNU General Public License
34825 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34826 + */
34827 +
34828 +/*
34829 + * whiteout for logical deletion and opaque directory
34830 + */
34831 +
34832 +#include "aufs.h"
34833 +
34834 +#define WH_MASK                        0444
34835 +
34836 +/*
34837 + * If a directory contains this file, then it is opaque.  We start with the
34838 + * .wh. flag so that it is blocked by lookup.
34839 + */
34840 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34841 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34842 +
34843 +/*
34844 + * generate whiteout name, which is NOT terminated by NULL.
34845 + * @name: original d_name.name
34846 + * @len: original d_name.len
34847 + * @wh: whiteout qstr
34848 + * returns zero when succeeds, otherwise error.
34849 + * succeeded value as wh->name should be freed by kfree().
34850 + */
34851 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34852 +{
34853 +       char *p;
34854 +
34855 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34856 +               return -ENAMETOOLONG;
34857 +
34858 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34859 +       p = kmalloc(wh->len, GFP_NOFS);
34860 +       wh->name = p;
34861 +       if (p) {
34862 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34863 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34864 +               /* smp_mb(); */
34865 +               return 0;
34866 +       }
34867 +       return -ENOMEM;
34868 +}
34869 +
34870 +/* ---------------------------------------------------------------------- */
34871 +
34872 +/*
34873 + * test if the @wh_name exists under @h_parent.
34874 + * @try_sio specifies the necessary of super-io.
34875 + */
34876 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34877 +{
34878 +       int err;
34879 +       struct dentry *wh_dentry;
34880 +
34881 +       if (!try_sio)
34882 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34883 +       else
34884 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34885 +       err = PTR_ERR(wh_dentry);
34886 +       if (IS_ERR(wh_dentry)) {
34887 +               if (err == -ENAMETOOLONG)
34888 +                       err = 0;
34889 +               goto out;
34890 +       }
34891 +
34892 +       err = 0;
34893 +       if (d_is_negative(wh_dentry))
34894 +               goto out_wh; /* success */
34895 +
34896 +       err = 1;
34897 +       if (d_is_reg(wh_dentry))
34898 +               goto out_wh; /* success */
34899 +
34900 +       err = -EIO;
34901 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34902 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34903 +
34904 +out_wh:
34905 +       dput(wh_dentry);
34906 +out:
34907 +       return err;
34908 +}
34909 +
34910 +/*
34911 + * test if the @h_dentry sets opaque or not.
34912 + */
34913 +int au_diropq_test(struct dentry *h_dentry)
34914 +{
34915 +       int err;
34916 +       struct inode *h_dir;
34917 +
34918 +       h_dir = d_inode(h_dentry);
34919 +       err = au_wh_test(h_dentry, &diropq_name,
34920 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34921 +       return err;
34922 +}
34923 +
34924 +/*
34925 + * returns a negative dentry whose name is unique and temporary.
34926 + */
34927 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34928 +                            struct qstr *prefix)
34929 +{
34930 +       struct dentry *dentry;
34931 +       int i;
34932 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34933 +               *name, *p;
34934 +       /* strict atomic_t is unnecessary here */
34935 +       static unsigned short cnt;
34936 +       struct qstr qs;
34937 +
34938 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34939 +
34940 +       name = defname;
34941 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34942 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34943 +               dentry = ERR_PTR(-ENAMETOOLONG);
34944 +               if (unlikely(qs.len > NAME_MAX))
34945 +                       goto out;
34946 +               dentry = ERR_PTR(-ENOMEM);
34947 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34948 +               if (unlikely(!name))
34949 +                       goto out;
34950 +       }
34951 +
34952 +       /* doubly whiteout-ed */
34953 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34954 +       p = name + AUFS_WH_PFX_LEN * 2;
34955 +       memcpy(p, prefix->name, prefix->len);
34956 +       p += prefix->len;
34957 +       *p++ = '.';
34958 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34959 +
34960 +       qs.name = name;
34961 +       for (i = 0; i < 3; i++) {
34962 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34963 +               dentry = au_sio_lkup_one(&qs, h_parent);
34964 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34965 +                       goto out_name;
34966 +               dput(dentry);
34967 +       }
34968 +       /* pr_warn("could not get random name\n"); */
34969 +       dentry = ERR_PTR(-EEXIST);
34970 +       AuDbg("%.*s\n", AuLNPair(&qs));
34971 +       BUG();
34972 +
34973 +out_name:
34974 +       if (name != defname)
34975 +               au_kfree_try_rcu(name);
34976 +out:
34977 +       AuTraceErrPtr(dentry);
34978 +       return dentry;
34979 +}
34980 +
34981 +/*
34982 + * rename the @h_dentry on @br to the whiteouted temporary name.
34983 + */
34984 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34985 +{
34986 +       int err;
34987 +       struct path h_path = {
34988 +               .mnt = au_br_mnt(br)
34989 +       };
34990 +       struct inode *h_dir, *delegated;
34991 +       struct dentry *h_parent;
34992 +
34993 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34994 +       h_dir = d_inode(h_parent);
34995 +       IMustLock(h_dir);
34996 +
34997 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34998 +       err = PTR_ERR(h_path.dentry);
34999 +       if (IS_ERR(h_path.dentry))
35000 +               goto out;
35001 +
35002 +       /* under the same dir, no need to lock_rename() */
35003 +       delegated = NULL;
35004 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35005 +                          /*flags*/0);
35006 +       AuTraceErr(err);
35007 +       if (unlikely(err == -EWOULDBLOCK)) {
35008 +               pr_warn("cannot retry for NFSv4 delegation"
35009 +                       " for an internal rename\n");
35010 +               iput(delegated);
35011 +       }
35012 +       dput(h_path.dentry);
35013 +
35014 +out:
35015 +       AuTraceErr(err);
35016 +       return err;
35017 +}
35018 +
35019 +/* ---------------------------------------------------------------------- */
35020 +/*
35021 + * functions for removing a whiteout
35022 + */
35023 +
35024 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35025 +{
35026 +       int err, force;
35027 +       struct inode *delegated;
35028 +
35029 +       /*
35030 +        * forces superio when the dir has a sticky bit.
35031 +        * this may be a violation of unix fs semantics.
35032 +        */
35033 +       force = (h_dir->i_mode & S_ISVTX)
35034 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35035 +       delegated = NULL;
35036 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35037 +       if (unlikely(err == -EWOULDBLOCK)) {
35038 +               pr_warn("cannot retry for NFSv4 delegation"
35039 +                       " for an internal unlink\n");
35040 +               iput(delegated);
35041 +       }
35042 +       return err;
35043 +}
35044 +
35045 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35046 +                       struct dentry *dentry)
35047 +{
35048 +       int err;
35049 +
35050 +       err = do_unlink_wh(h_dir, h_path);
35051 +       if (!err && dentry)
35052 +               au_set_dbwh(dentry, -1);
35053 +
35054 +       return err;
35055 +}
35056 +
35057 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35058 +                         struct au_branch *br)
35059 +{
35060 +       int err;
35061 +       struct path h_path = {
35062 +               .mnt = au_br_mnt(br)
35063 +       };
35064 +
35065 +       err = 0;
35066 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
35067 +       if (IS_ERR(h_path.dentry))
35068 +               err = PTR_ERR(h_path.dentry);
35069 +       else {
35070 +               if (d_is_reg(h_path.dentry))
35071 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
35072 +               dput(h_path.dentry);
35073 +       }
35074 +
35075 +       return err;
35076 +}
35077 +
35078 +/* ---------------------------------------------------------------------- */
35079 +/*
35080 + * initialize/clean whiteout for a branch
35081 + */
35082 +
35083 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35084 +                       const int isdir)
35085 +{
35086 +       int err;
35087 +       struct inode *delegated;
35088 +
35089 +       if (d_is_negative(whpath->dentry))
35090 +               return;
35091 +
35092 +       if (isdir)
35093 +               err = vfsub_rmdir(h_dir, whpath);
35094 +       else {
35095 +               delegated = NULL;
35096 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35097 +               if (unlikely(err == -EWOULDBLOCK)) {
35098 +                       pr_warn("cannot retry for NFSv4 delegation"
35099 +                               " for an internal unlink\n");
35100 +                       iput(delegated);
35101 +               }
35102 +       }
35103 +       if (unlikely(err))
35104 +               pr_warn("failed removing %pd (%d), ignored.\n",
35105 +                       whpath->dentry, err);
35106 +}
35107 +
35108 +static int test_linkable(struct dentry *h_root)
35109 +{
35110 +       struct inode *h_dir = d_inode(h_root);
35111 +
35112 +       if (h_dir->i_op->link)
35113 +               return 0;
35114 +
35115 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35116 +              h_root, au_sbtype(h_root->d_sb));
35117 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35118 +}
35119 +
35120 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35121 +static int au_whdir(struct inode *h_dir, struct path *path)
35122 +{
35123 +       int err;
35124 +
35125 +       err = -EEXIST;
35126 +       if (d_is_negative(path->dentry)) {
35127 +               int mode = 0700;
35128 +
35129 +               if (au_test_nfs(path->dentry->d_sb))
35130 +                       mode |= 0111;
35131 +               err = vfsub_mkdir(h_dir, path, mode);
35132 +       } else if (d_is_dir(path->dentry))
35133 +               err = 0;
35134 +       else
35135 +               pr_err("unknown %pd exists\n", path->dentry);
35136 +
35137 +       return err;
35138 +}
35139 +
35140 +struct au_wh_base {
35141 +       const struct qstr *name;
35142 +       struct dentry *dentry;
35143 +};
35144 +
35145 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35146 +                         struct path *h_path)
35147 +{
35148 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35149 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35150 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35151 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35152 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35153 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35154 +}
35155 +
35156 +/*
35157 + * returns tri-state,
35158 + * minus: error, caller should print the message
35159 + * zero: success
35160 + * plus: error, caller should NOT print the message
35161 + */
35162 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35163 +                               int do_plink, struct au_wh_base base[],
35164 +                               struct path *h_path)
35165 +{
35166 +       int err;
35167 +       struct inode *h_dir;
35168 +
35169 +       h_dir = d_inode(h_root);
35170 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35171 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35172 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35173 +       if (do_plink) {
35174 +               err = test_linkable(h_root);
35175 +               if (unlikely(err)) {
35176 +                       err = 1;
35177 +                       goto out;
35178 +               }
35179 +
35180 +               err = au_whdir(h_dir, h_path);
35181 +               if (unlikely(err))
35182 +                       goto out;
35183 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35184 +       } else
35185 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35186 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35187 +       err = au_whdir(h_dir, h_path);
35188 +       if (unlikely(err))
35189 +               goto out;
35190 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35191 +
35192 +out:
35193 +       return err;
35194 +}
35195 +
35196 +/*
35197 + * for the moment, aufs supports the branch filesystem which does not support
35198 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35199 + * copyup failed. finally, such filesystem will not be used as the writable
35200 + * branch.
35201 + *
35202 + * returns tri-state, see above.
35203 + */
35204 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35205 +                        int do_plink, struct au_wh_base base[],
35206 +                        struct path *h_path)
35207 +{
35208 +       int err;
35209 +       struct inode *h_dir;
35210 +
35211 +       WbrWhMustWriteLock(wbr);
35212 +
35213 +       err = test_linkable(h_root);
35214 +       if (unlikely(err)) {
35215 +               err = 1;
35216 +               goto out;
35217 +       }
35218 +
35219 +       /*
35220 +        * todo: should this create be done in /sbin/mount.aufs helper?
35221 +        */
35222 +       err = -EEXIST;
35223 +       h_dir = d_inode(h_root);
35224 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35225 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35226 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35227 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35228 +               err = 0;
35229 +       else
35230 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35231 +       if (unlikely(err))
35232 +               goto out;
35233 +
35234 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35235 +       if (do_plink) {
35236 +               err = au_whdir(h_dir, h_path);
35237 +               if (unlikely(err))
35238 +                       goto out;
35239 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35240 +       } else
35241 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35242 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35243 +
35244 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35245 +       err = au_whdir(h_dir, h_path);
35246 +       if (unlikely(err))
35247 +               goto out;
35248 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35249 +
35250 +out:
35251 +       return err;
35252 +}
35253 +
35254 +/*
35255 + * initialize the whiteout base file/dir for @br.
35256 + */
35257 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35258 +{
35259 +       int err, i;
35260 +       const unsigned char do_plink
35261 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35262 +       struct inode *h_dir;
35263 +       struct path path = br->br_path;
35264 +       struct dentry *h_root = path.dentry;
35265 +       struct au_wbr *wbr = br->br_wbr;
35266 +       static const struct qstr base_name[] = {
35267 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35268 +                                         sizeof(AUFS_BASE_NAME) - 1),
35269 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35270 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35271 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35272 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35273 +       };
35274 +       struct au_wh_base base[] = {
35275 +               [AuBrWh_BASE] = {
35276 +                       .name   = base_name + AuBrWh_BASE,
35277 +                       .dentry = NULL
35278 +               },
35279 +               [AuBrWh_PLINK] = {
35280 +                       .name   = base_name + AuBrWh_PLINK,
35281 +                       .dentry = NULL
35282 +               },
35283 +               [AuBrWh_ORPH] = {
35284 +                       .name   = base_name + AuBrWh_ORPH,
35285 +                       .dentry = NULL
35286 +               }
35287 +       };
35288 +
35289 +       if (wbr)
35290 +               WbrWhMustWriteLock(wbr);
35291 +
35292 +       for (i = 0; i < AuBrWh_Last; i++) {
35293 +               /* doubly whiteouted */
35294 +               struct dentry *d;
35295 +
35296 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35297 +               err = PTR_ERR(d);
35298 +               if (IS_ERR(d))
35299 +                       goto out;
35300 +
35301 +               base[i].dentry = d;
35302 +               AuDebugOn(wbr
35303 +                         && wbr->wbr_wh[i]
35304 +                         && wbr->wbr_wh[i] != base[i].dentry);
35305 +       }
35306 +
35307 +       if (wbr)
35308 +               for (i = 0; i < AuBrWh_Last; i++) {
35309 +                       dput(wbr->wbr_wh[i]);
35310 +                       wbr->wbr_wh[i] = NULL;
35311 +               }
35312 +
35313 +       err = 0;
35314 +       if (!au_br_writable(br->br_perm)) {
35315 +               h_dir = d_inode(h_root);
35316 +               au_wh_init_ro(h_dir, base, &path);
35317 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35318 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35319 +               if (err > 0)
35320 +                       goto out;
35321 +               else if (err)
35322 +                       goto out_err;
35323 +       } else {
35324 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35325 +               if (err > 0)
35326 +                       goto out;
35327 +               else if (err)
35328 +                       goto out_err;
35329 +       }
35330 +       goto out; /* success */
35331 +
35332 +out_err:
35333 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35334 +              err, h_root, au_sbtype(h_root->d_sb));
35335 +out:
35336 +       for (i = 0; i < AuBrWh_Last; i++)
35337 +               dput(base[i].dentry);
35338 +       return err;
35339 +}
35340 +
35341 +/* ---------------------------------------------------------------------- */
35342 +/*
35343 + * whiteouts are all hard-linked usually.
35344 + * when its link count reaches a ceiling, we create a new whiteout base
35345 + * asynchronously.
35346 + */
35347 +
35348 +struct reinit_br_wh {
35349 +       struct super_block *sb;
35350 +       struct au_branch *br;
35351 +};
35352 +
35353 +static void reinit_br_wh(void *arg)
35354 +{
35355 +       int err;
35356 +       aufs_bindex_t bindex;
35357 +       struct path h_path;
35358 +       struct reinit_br_wh *a = arg;
35359 +       struct au_wbr *wbr;
35360 +       struct inode *dir, *delegated;
35361 +       struct dentry *h_root;
35362 +       struct au_hinode *hdir;
35363 +
35364 +       err = 0;
35365 +       wbr = a->br->br_wbr;
35366 +       /* big aufs lock */
35367 +       si_noflush_write_lock(a->sb);
35368 +       if (!au_br_writable(a->br->br_perm))
35369 +               goto out;
35370 +       bindex = au_br_index(a->sb, a->br->br_id);
35371 +       if (unlikely(bindex < 0))
35372 +               goto out;
35373 +
35374 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35375 +       dir = d_inode(a->sb->s_root);
35376 +       hdir = au_hi(dir, bindex);
35377 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35378 +       AuDebugOn(h_root != au_br_dentry(a->br));
35379 +
35380 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35381 +       wbr_wh_write_lock(wbr);
35382 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35383 +                         h_root, a->br);
35384 +       if (!err) {
35385 +               h_path.dentry = wbr->wbr_whbase;
35386 +               h_path.mnt = au_br_mnt(a->br);
35387 +               delegated = NULL;
35388 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35389 +                                  /*force*/0);
35390 +               if (unlikely(err == -EWOULDBLOCK)) {
35391 +                       pr_warn("cannot retry for NFSv4 delegation"
35392 +                               " for an internal unlink\n");
35393 +                       iput(delegated);
35394 +               }
35395 +       } else {
35396 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35397 +               err = 0;
35398 +       }
35399 +       dput(wbr->wbr_whbase);
35400 +       wbr->wbr_whbase = NULL;
35401 +       if (!err)
35402 +               err = au_wh_init(a->br, a->sb);
35403 +       wbr_wh_write_unlock(wbr);
35404 +       au_hn_inode_unlock(hdir);
35405 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35406 +       if (!err)
35407 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35408 +
35409 +out:
35410 +       if (wbr)
35411 +               atomic_dec(&wbr->wbr_wh_running);
35412 +       au_lcnt_dec(&a->br->br_count);
35413 +       si_write_unlock(a->sb);
35414 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35415 +       au_kfree_rcu(a);
35416 +       if (unlikely(err))
35417 +               AuIOErr("err %d\n", err);
35418 +}
35419 +
35420 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35421 +{
35422 +       int do_dec, wkq_err;
35423 +       struct reinit_br_wh *arg;
35424 +
35425 +       do_dec = 1;
35426 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35427 +               goto out;
35428 +
35429 +       /* ignore ENOMEM */
35430 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35431 +       if (arg) {
35432 +               /*
35433 +                * dec(wh_running), kfree(arg) and dec(br_count)
35434 +                * in reinit function
35435 +                */
35436 +               arg->sb = sb;
35437 +               arg->br = br;
35438 +               au_lcnt_inc(&br->br_count);
35439 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35440 +               if (unlikely(wkq_err)) {
35441 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35442 +                       au_lcnt_dec(&br->br_count);
35443 +                       au_kfree_rcu(arg);
35444 +               }
35445 +               do_dec = 0;
35446 +       }
35447 +
35448 +out:
35449 +       if (do_dec)
35450 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35451 +}
35452 +
35453 +/* ---------------------------------------------------------------------- */
35454 +
35455 +/*
35456 + * create the whiteout @wh.
35457 + */
35458 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35459 +                            struct dentry *wh)
35460 +{
35461 +       int err;
35462 +       struct path h_path = {
35463 +               .dentry = wh
35464 +       };
35465 +       struct au_branch *br;
35466 +       struct au_wbr *wbr;
35467 +       struct dentry *h_parent;
35468 +       struct inode *h_dir, *delegated;
35469 +
35470 +       h_parent = wh->d_parent; /* dir inode is locked */
35471 +       h_dir = d_inode(h_parent);
35472 +       IMustLock(h_dir);
35473 +
35474 +       br = au_sbr(sb, bindex);
35475 +       h_path.mnt = au_br_mnt(br);
35476 +       wbr = br->br_wbr;
35477 +       wbr_wh_read_lock(wbr);
35478 +       if (wbr->wbr_whbase) {
35479 +               delegated = NULL;
35480 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35481 +               if (unlikely(err == -EWOULDBLOCK)) {
35482 +                       pr_warn("cannot retry for NFSv4 delegation"
35483 +                               " for an internal link\n");
35484 +                       iput(delegated);
35485 +               }
35486 +               if (!err || err != -EMLINK)
35487 +                       goto out;
35488 +
35489 +               /* link count full. re-initialize br_whbase. */
35490 +               kick_reinit_br_wh(sb, br);
35491 +       }
35492 +
35493 +       /* return this error in this context */
35494 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35495 +       if (!err)
35496 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35497 +
35498 +out:
35499 +       wbr_wh_read_unlock(wbr);
35500 +       return err;
35501 +}
35502 +
35503 +/* ---------------------------------------------------------------------- */
35504 +
35505 +/*
35506 + * create or remove the diropq.
35507 + */
35508 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35509 +                               unsigned int flags)
35510 +{
35511 +       struct dentry *opq_dentry, *h_dentry;
35512 +       struct super_block *sb;
35513 +       struct au_branch *br;
35514 +       int err;
35515 +
35516 +       sb = dentry->d_sb;
35517 +       br = au_sbr(sb, bindex);
35518 +       h_dentry = au_h_dptr(dentry, bindex);
35519 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35520 +       if (IS_ERR(opq_dentry))
35521 +               goto out;
35522 +
35523 +       if (au_ftest_diropq(flags, CREATE)) {
35524 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35525 +               if (!err) {
35526 +                       au_set_dbdiropq(dentry, bindex);
35527 +                       goto out; /* success */
35528 +               }
35529 +       } else {
35530 +               struct path tmp = {
35531 +                       .dentry = opq_dentry,
35532 +                       .mnt    = au_br_mnt(br)
35533 +               };
35534 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35535 +               if (!err)
35536 +                       au_set_dbdiropq(dentry, -1);
35537 +       }
35538 +       dput(opq_dentry);
35539 +       opq_dentry = ERR_PTR(err);
35540 +
35541 +out:
35542 +       return opq_dentry;
35543 +}
35544 +
35545 +struct do_diropq_args {
35546 +       struct dentry **errp;
35547 +       struct dentry *dentry;
35548 +       aufs_bindex_t bindex;
35549 +       unsigned int flags;
35550 +};
35551 +
35552 +static void call_do_diropq(void *args)
35553 +{
35554 +       struct do_diropq_args *a = args;
35555 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35556 +}
35557 +
35558 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35559 +                            unsigned int flags)
35560 +{
35561 +       struct dentry *diropq, *h_dentry;
35562 +
35563 +       h_dentry = au_h_dptr(dentry, bindex);
35564 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35565 +               diropq = do_diropq(dentry, bindex, flags);
35566 +       else {
35567 +               int wkq_err;
35568 +               struct do_diropq_args args = {
35569 +                       .errp           = &diropq,
35570 +                       .dentry         = dentry,
35571 +                       .bindex         = bindex,
35572 +                       .flags          = flags
35573 +               };
35574 +
35575 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35576 +               if (unlikely(wkq_err))
35577 +                       diropq = ERR_PTR(wkq_err);
35578 +       }
35579 +
35580 +       return diropq;
35581 +}
35582 +
35583 +/* ---------------------------------------------------------------------- */
35584 +
35585 +/*
35586 + * lookup whiteout dentry.
35587 + * @h_parent: lower parent dentry which must exist and be locked
35588 + * @base_name: name of dentry which will be whiteouted
35589 + * returns dentry for whiteout.
35590 + */
35591 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35592 +                         struct au_branch *br)
35593 +{
35594 +       int err;
35595 +       struct qstr wh_name;
35596 +       struct dentry *wh_dentry;
35597 +
35598 +       err = au_wh_name_alloc(&wh_name, base_name);
35599 +       wh_dentry = ERR_PTR(err);
35600 +       if (!err) {
35601 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35602 +               au_kfree_try_rcu(wh_name.name);
35603 +       }
35604 +       return wh_dentry;
35605 +}
35606 +
35607 +/*
35608 + * link/create a whiteout for @dentry on @bindex.
35609 + */
35610 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35611 +                           struct dentry *h_parent)
35612 +{
35613 +       struct dentry *wh_dentry;
35614 +       struct super_block *sb;
35615 +       int err;
35616 +
35617 +       sb = dentry->d_sb;
35618 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35619 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35620 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35621 +               if (!err) {
35622 +                       au_set_dbwh(dentry, bindex);
35623 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35624 +               } else {
35625 +                       dput(wh_dentry);
35626 +                       wh_dentry = ERR_PTR(err);
35627 +               }
35628 +       }
35629 +
35630 +       return wh_dentry;
35631 +}
35632 +
35633 +/* ---------------------------------------------------------------------- */
35634 +
35635 +/* Delete all whiteouts in this directory on branch bindex. */
35636 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35637 +                          aufs_bindex_t bindex, struct au_branch *br)
35638 +{
35639 +       int err;
35640 +       unsigned long ul, n;
35641 +       struct qstr wh_name;
35642 +       char *p;
35643 +       struct hlist_head *head;
35644 +       struct au_vdir_wh *pos;
35645 +       struct au_vdir_destr *str;
35646 +
35647 +       err = -ENOMEM;
35648 +       p = (void *)__get_free_page(GFP_NOFS);
35649 +       wh_name.name = p;
35650 +       if (unlikely(!wh_name.name))
35651 +               goto out;
35652 +
35653 +       err = 0;
35654 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35655 +       p += AUFS_WH_PFX_LEN;
35656 +       n = whlist->nh_num;
35657 +       head = whlist->nh_head;
35658 +       for (ul = 0; !err && ul < n; ul++, head++) {
35659 +               hlist_for_each_entry(pos, head, wh_hash) {
35660 +                       if (pos->wh_bindex != bindex)
35661 +                               continue;
35662 +
35663 +                       str = &pos->wh_str;
35664 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35665 +                               memcpy(p, str->name, str->len);
35666 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35667 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35668 +                               if (!err)
35669 +                                       continue;
35670 +                               break;
35671 +                       }
35672 +                       AuIOErr("whiteout name too long %.*s\n",
35673 +                               str->len, str->name);
35674 +                       err = -EIO;
35675 +                       break;
35676 +               }
35677 +       }
35678 +       free_page((unsigned long)wh_name.name);
35679 +
35680 +out:
35681 +       return err;
35682 +}
35683 +
35684 +struct del_wh_children_args {
35685 +       int *errp;
35686 +       struct dentry *h_dentry;
35687 +       struct au_nhash *whlist;
35688 +       aufs_bindex_t bindex;
35689 +       struct au_branch *br;
35690 +};
35691 +
35692 +static void call_del_wh_children(void *args)
35693 +{
35694 +       struct del_wh_children_args *a = args;
35695 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35696 +}
35697 +
35698 +/* ---------------------------------------------------------------------- */
35699 +
35700 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35701 +{
35702 +       struct au_whtmp_rmdir *whtmp;
35703 +       int err;
35704 +       unsigned int rdhash;
35705 +
35706 +       SiMustAnyLock(sb);
35707 +
35708 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35709 +       if (unlikely(!whtmp)) {
35710 +               whtmp = ERR_PTR(-ENOMEM);
35711 +               goto out;
35712 +       }
35713 +
35714 +       /* no estimation for dir size */
35715 +       rdhash = au_sbi(sb)->si_rdhash;
35716 +       if (!rdhash)
35717 +               rdhash = AUFS_RDHASH_DEF;
35718 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35719 +       if (unlikely(err)) {
35720 +               au_kfree_rcu(whtmp);
35721 +               whtmp = ERR_PTR(err);
35722 +       }
35723 +
35724 +out:
35725 +       return whtmp;
35726 +}
35727 +
35728 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35729 +{
35730 +       if (whtmp->br)
35731 +               au_lcnt_dec(&whtmp->br->br_count);
35732 +       dput(whtmp->wh_dentry);
35733 +       iput(whtmp->dir);
35734 +       au_nhash_wh_free(&whtmp->whlist);
35735 +       au_kfree_rcu(whtmp);
35736 +}
35737 +
35738 +/*
35739 + * rmdir the whiteouted temporary named dir @h_dentry.
35740 + * @whlist: whiteouted children.
35741 + */
35742 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35743 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35744 +{
35745 +       int err;
35746 +       unsigned int h_nlink;
35747 +       struct path h_tmp;
35748 +       struct inode *wh_inode, *h_dir;
35749 +       struct au_branch *br;
35750 +
35751 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35752 +       IMustLock(h_dir);
35753 +
35754 +       br = au_sbr(dir->i_sb, bindex);
35755 +       wh_inode = d_inode(wh_dentry);
35756 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35757 +
35758 +       /*
35759 +        * someone else might change some whiteouts while we were sleeping.
35760 +        * it means this whlist may have an obsoleted entry.
35761 +        */
35762 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35763 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35764 +       else {
35765 +               int wkq_err;
35766 +               struct del_wh_children_args args = {
35767 +                       .errp           = &err,
35768 +                       .h_dentry       = wh_dentry,
35769 +                       .whlist         = whlist,
35770 +                       .bindex         = bindex,
35771 +                       .br             = br
35772 +               };
35773 +
35774 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35775 +               if (unlikely(wkq_err))
35776 +                       err = wkq_err;
35777 +       }
35778 +       inode_unlock(wh_inode);
35779 +
35780 +       if (!err) {
35781 +               h_tmp.dentry = wh_dentry;
35782 +               h_tmp.mnt = au_br_mnt(br);
35783 +               h_nlink = h_dir->i_nlink;
35784 +               err = vfsub_rmdir(h_dir, &h_tmp);
35785 +               /* some fs doesn't change the parent nlink in some cases */
35786 +               h_nlink -= h_dir->i_nlink;
35787 +       }
35788 +
35789 +       if (!err) {
35790 +               if (au_ibtop(dir) == bindex) {
35791 +                       /* todo: dir->i_mutex is necessary */
35792 +                       au_cpup_attr_timesizes(dir);
35793 +                       if (h_nlink)
35794 +                               vfsub_drop_nlink(dir);
35795 +               }
35796 +               return 0; /* success */
35797 +       }
35798 +
35799 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35800 +       return err;
35801 +}
35802 +
35803 +static void call_rmdir_whtmp(void *args)
35804 +{
35805 +       int err;
35806 +       aufs_bindex_t bindex;
35807 +       struct au_whtmp_rmdir *a = args;
35808 +       struct super_block *sb;
35809 +       struct dentry *h_parent;
35810 +       struct inode *h_dir;
35811 +       struct au_hinode *hdir;
35812 +
35813 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35814 +       /* inode_lock(a->dir); */
35815 +       err = -EROFS;
35816 +       sb = a->dir->i_sb;
35817 +       si_read_lock(sb, !AuLock_FLUSH);
35818 +       if (!au_br_writable(a->br->br_perm))
35819 +               goto out;
35820 +       bindex = au_br_index(sb, a->br->br_id);
35821 +       if (unlikely(bindex < 0))
35822 +               goto out;
35823 +
35824 +       err = -EIO;
35825 +       ii_write_lock_parent(a->dir);
35826 +       h_parent = dget_parent(a->wh_dentry);
35827 +       h_dir = d_inode(h_parent);
35828 +       hdir = au_hi(a->dir, bindex);
35829 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35830 +       if (unlikely(err))
35831 +               goto out_mnt;
35832 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35833 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35834 +                         a->br);
35835 +       if (!err)
35836 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35837 +       au_hn_inode_unlock(hdir);
35838 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35839 +
35840 +out_mnt:
35841 +       dput(h_parent);
35842 +       ii_write_unlock(a->dir);
35843 +out:
35844 +       /* inode_unlock(a->dir); */
35845 +       au_whtmp_rmdir_free(a);
35846 +       si_read_unlock(sb);
35847 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35848 +       if (unlikely(err))
35849 +               AuIOErr("err %d\n", err);
35850 +}
35851 +
35852 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35853 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35854 +{
35855 +       int wkq_err;
35856 +       struct super_block *sb;
35857 +
35858 +       IMustLock(dir);
35859 +
35860 +       /* all post-process will be done in do_rmdir_whtmp(). */
35861 +       sb = dir->i_sb;
35862 +       args->dir = au_igrab(dir);
35863 +       args->br = au_sbr(sb, bindex);
35864 +       au_lcnt_inc(&args->br->br_count);
35865 +       args->wh_dentry = dget(wh_dentry);
35866 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35867 +       if (unlikely(wkq_err)) {
35868 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35869 +               au_whtmp_rmdir_free(args);
35870 +       }
35871 +}
35872 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35873 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35874 +++ linux/fs/aufs/whout.h       2020-01-27 10:57:18.178871751 +0100
35875 @@ -0,0 +1,86 @@
35876 +/* SPDX-License-Identifier: GPL-2.0 */
35877 +/*
35878 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35879 + *
35880 + * This program, aufs is free software; you can redistribute it and/or modify
35881 + * it under the terms of the GNU General Public License as published by
35882 + * the Free Software Foundation; either version 2 of the License, or
35883 + * (at your option) any later version.
35884 + *
35885 + * This program is distributed in the hope that it will be useful,
35886 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35887 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35888 + * GNU General Public License for more details.
35889 + *
35890 + * You should have received a copy of the GNU General Public License
35891 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35892 + */
35893 +
35894 +/*
35895 + * whiteout for logical deletion and opaque directory
35896 + */
35897 +
35898 +#ifndef __AUFS_WHOUT_H__
35899 +#define __AUFS_WHOUT_H__
35900 +
35901 +#ifdef __KERNEL__
35902 +
35903 +#include "dir.h"
35904 +
35905 +/* whout.c */
35906 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35907 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35908 +int au_diropq_test(struct dentry *h_dentry);
35909 +struct au_branch;
35910 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35911 +                            struct qstr *prefix);
35912 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35913 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35914 +                       struct dentry *dentry);
35915 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35916 +
35917 +/* diropq flags */
35918 +#define AuDiropq_CREATE        1
35919 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35920 +#define au_fset_diropq(flags, name) \
35921 +       do { (flags) |= AuDiropq_##name; } while (0)
35922 +#define au_fclr_diropq(flags, name) \
35923 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35924 +
35925 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35926 +                            unsigned int flags);
35927 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35928 +                         struct au_branch *br);
35929 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35930 +                           struct dentry *h_parent);
35931 +
35932 +/* real rmdir for the whiteout-ed dir */
35933 +struct au_whtmp_rmdir {
35934 +       struct inode *dir;
35935 +       struct au_branch *br;
35936 +       struct dentry *wh_dentry;
35937 +       struct au_nhash whlist;
35938 +};
35939 +
35940 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35941 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35942 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35943 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35944 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35945 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35946 +
35947 +/* ---------------------------------------------------------------------- */
35948 +
35949 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35950 +                                             aufs_bindex_t bindex)
35951 +{
35952 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35953 +}
35954 +
35955 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35956 +{
35957 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35958 +}
35959 +
35960 +#endif /* __KERNEL__ */
35961 +#endif /* __AUFS_WHOUT_H__ */
35962 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35963 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35964 +++ linux/fs/aufs/wkq.c 2020-08-03 09:12:33.764880472 +0200
35965 @@ -0,0 +1,372 @@
35966 +// SPDX-License-Identifier: GPL-2.0
35967 +/*
35968 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35969 + *
35970 + * This program, aufs is free software; you can redistribute it and/or modify
35971 + * it under the terms of the GNU General Public License as published by
35972 + * the Free Software Foundation; either version 2 of the License, or
35973 + * (at your option) any later version.
35974 + *
35975 + * This program is distributed in the hope that it will be useful,
35976 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35977 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35978 + * GNU General Public License for more details.
35979 + *
35980 + * You should have received a copy of the GNU General Public License
35981 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35982 + */
35983 +
35984 +/*
35985 + * workqueue for asynchronous/super-io operations
35986 + * todo: try new credential scheme
35987 + */
35988 +
35989 +#include <linux/module.h>
35990 +#include "aufs.h"
35991 +
35992 +/* internal workqueue named AUFS_WKQ_NAME */
35993 +
35994 +static struct workqueue_struct *au_wkq;
35995 +
35996 +struct au_wkinfo {
35997 +       struct work_struct wk;
35998 +       struct kobject *kobj;
35999 +
36000 +       unsigned int flags; /* see wkq.h */
36001 +
36002 +       au_wkq_func_t func;
36003 +       void *args;
36004 +
36005 +#ifdef CONFIG_LOCKDEP
36006 +       int dont_check;
36007 +       struct held_lock **hlock;
36008 +#endif
36009 +
36010 +       struct completion *comp;
36011 +};
36012 +
36013 +/* ---------------------------------------------------------------------- */
36014 +/*
36015 + * Aufs passes some operations to the workqueue such as the internal copyup.
36016 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36017 + * job run by workqueue depends upon the locks acquired in the other task.
36018 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36019 + * information too. And the job in the workqueue restores the info in order to
36020 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36021 + * correctly and expectedly.
36022 + */
36023 +
36024 +#ifndef CONFIG_LOCKDEP
36025 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36026 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36027 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36028 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36029 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36030 +#else
36031 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36032 +{
36033 +       wkinfo->hlock = NULL;
36034 +       wkinfo->dont_check = 0;
36035 +}
36036 +
36037 +/*
36038 + * 1: matched
36039 + * 0: unmatched
36040 + */
36041 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36042 +{
36043 +       static DEFINE_SPINLOCK(spin);
36044 +       static struct {
36045 +               char *name;
36046 +               struct lock_class_key *key;
36047 +       } a[] = {
36048 +               { .name = "&sbinfo->si_rwsem" },
36049 +               { .name = "&finfo->fi_rwsem" },
36050 +               { .name = "&dinfo->di_rwsem" },
36051 +               { .name = "&iinfo->ii_rwsem" }
36052 +       };
36053 +       static int set;
36054 +       int i;
36055 +
36056 +       /* lockless read from 'set.' see below */
36057 +       if (set == ARRAY_SIZE(a)) {
36058 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36059 +                       if (a[i].key == key)
36060 +                               goto match;
36061 +               goto unmatch;
36062 +       }
36063 +
36064 +       spin_lock(&spin);
36065 +       if (set)
36066 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36067 +                       if (a[i].key == key) {
36068 +                               spin_unlock(&spin);
36069 +                               goto match;
36070 +                       }
36071 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36072 +               if (a[i].key) {
36073 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36074 +                               spin_unlock(&spin);
36075 +                               goto match;
36076 +                       } else
36077 +                               continue;
36078 +               }
36079 +               if (strstr(a[i].name, name)) {
36080 +                       /*
36081 +                        * the order of these three lines is important for the
36082 +                        * lockless read above.
36083 +                        */
36084 +                       a[i].key = key;
36085 +                       spin_unlock(&spin);
36086 +                       set++;
36087 +                       /* AuDbg("%d, %s\n", set, name); */
36088 +                       goto match;
36089 +               }
36090 +       }
36091 +       spin_unlock(&spin);
36092 +       goto unmatch;
36093 +
36094 +match:
36095 +       return 1;
36096 +unmatch:
36097 +       return 0;
36098 +}
36099 +
36100 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36101 +{
36102 +       int err, n;
36103 +       struct task_struct *curr;
36104 +       struct held_lock **hl, *held_locks, *p;
36105 +
36106 +       err = 0;
36107 +       curr = current;
36108 +       wkinfo->dont_check = lockdep_recursing(curr);
36109 +       if (wkinfo->dont_check)
36110 +               goto out;
36111 +       n = curr->lockdep_depth;
36112 +       if (!n)
36113 +               goto out;
36114 +
36115 +       err = -ENOMEM;
36116 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36117 +       if (unlikely(!wkinfo->hlock))
36118 +               goto out;
36119 +
36120 +       err = 0;
36121 +#if 0 /* left for debugging */
36122 +       if (0 && au_debug_test())
36123 +               lockdep_print_held_locks(curr);
36124 +#endif
36125 +       held_locks = curr->held_locks;
36126 +       hl = wkinfo->hlock;
36127 +       while (n--) {
36128 +               p = held_locks++;
36129 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36130 +                       *hl++ = p;
36131 +       }
36132 +       *hl = NULL;
36133 +
36134 +out:
36135 +       return err;
36136 +}
36137 +
36138 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36139 +{
36140 +       au_kfree_try_rcu(wkinfo->hlock);
36141 +}
36142 +
36143 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36144 +{
36145 +       struct held_lock *p, **hl = wkinfo->hlock;
36146 +       int subclass;
36147 +
36148 +       if (wkinfo->dont_check)
36149 +               lockdep_off();
36150 +       if (!hl)
36151 +               return;
36152 +       while ((p = *hl++)) { /* assignment */
36153 +               subclass = lockdep_hlock_class(p)->subclass;
36154 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36155 +               if (p->read)
36156 +                       rwsem_acquire_read(p->instance, subclass, 0,
36157 +                                          /*p->acquire_ip*/_RET_IP_);
36158 +               else
36159 +                       rwsem_acquire(p->instance, subclass, 0,
36160 +                                     /*p->acquire_ip*/_RET_IP_);
36161 +       }
36162 +}
36163 +
36164 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36165 +{
36166 +       struct held_lock *p, **hl = wkinfo->hlock;
36167 +
36168 +       if (wkinfo->dont_check)
36169 +               lockdep_on();
36170 +       if (!hl)
36171 +               return;
36172 +       while ((p = *hl++)) /* assignment */
36173 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36174 +}
36175 +#endif
36176 +
36177 +static void wkq_func(struct work_struct *wk)
36178 +{
36179 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36180 +
36181 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36182 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36183 +
36184 +       au_wkq_lockdep_pre(wkinfo);
36185 +       wkinfo->func(wkinfo->args);
36186 +       au_wkq_lockdep_post(wkinfo);
36187 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36188 +               complete(wkinfo->comp);
36189 +       else {
36190 +               kobject_put(wkinfo->kobj);
36191 +               module_put(THIS_MODULE); /* todo: ?? */
36192 +               au_kfree_rcu(wkinfo);
36193 +       }
36194 +}
36195 +
36196 +/*
36197 + * Since struct completion is large, try allocating it dynamically.
36198 + */
36199 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36200 +
36201 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36202 +{
36203 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36204 +       if (*comp) {
36205 +               init_completion(*comp);
36206 +               wkinfo->comp = *comp;
36207 +               return 0;
36208 +       }
36209 +       return -ENOMEM;
36210 +}
36211 +
36212 +static void au_wkq_comp_free(struct completion *comp)
36213 +{
36214 +       au_kfree_rcu(comp);
36215 +}
36216 +
36217 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36218 +{
36219 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36220 +               if (au_wkq_test()) {
36221 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36222 +                               " due to a dead dir by UDBA,"
36223 +                               " or async xino write?\n");
36224 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36225 +               }
36226 +       } else
36227 +               au_dbg_verify_kthread();
36228 +
36229 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36230 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36231 +               queue_work(au_wkq, &wkinfo->wk);
36232 +       } else {
36233 +               INIT_WORK(&wkinfo->wk, wkq_func);
36234 +               schedule_work(&wkinfo->wk);
36235 +       }
36236 +}
36237 +
36238 +/*
36239 + * Be careful. It is easy to make deadlock happen.
36240 + * processA: lock, wkq and wait
36241 + * processB: wkq and wait, lock in wkq
36242 + * --> deadlock
36243 + */
36244 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36245 +{
36246 +       int err;
36247 +       AuWkqCompDeclare(comp);
36248 +       struct au_wkinfo wkinfo = {
36249 +               .flags  = flags,
36250 +               .func   = func,
36251 +               .args   = args
36252 +       };
36253 +
36254 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36255 +       if (unlikely(err))
36256 +               goto out;
36257 +       err = au_wkq_lockdep_alloc(&wkinfo);
36258 +       if (unlikely(err))
36259 +               goto out_comp;
36260 +       if (!err) {
36261 +               au_wkq_run(&wkinfo);
36262 +               /* no timeout, no interrupt */
36263 +               wait_for_completion(wkinfo.comp);
36264 +       }
36265 +       au_wkq_lockdep_free(&wkinfo);
36266 +
36267 +out_comp:
36268 +       au_wkq_comp_free(comp);
36269 +out:
36270 +       destroy_work_on_stack(&wkinfo.wk);
36271 +       return err;
36272 +}
36273 +
36274 +/*
36275 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36276 + * problem in a concurrent umounting.
36277 + */
36278 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36279 +                 unsigned int flags)
36280 +{
36281 +       int err;
36282 +       struct au_wkinfo *wkinfo;
36283 +
36284 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36285 +
36286 +       /*
36287 +        * wkq_func() must free this wkinfo.
36288 +        * it highly depends upon the implementation of workqueue.
36289 +        */
36290 +       err = 0;
36291 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36292 +       if (wkinfo) {
36293 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36294 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36295 +               wkinfo->func = func;
36296 +               wkinfo->args = args;
36297 +               wkinfo->comp = NULL;
36298 +               au_wkq_lockdep_init(wkinfo);
36299 +               kobject_get(wkinfo->kobj);
36300 +               __module_get(THIS_MODULE); /* todo: ?? */
36301 +
36302 +               au_wkq_run(wkinfo);
36303 +       } else {
36304 +               err = -ENOMEM;
36305 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36306 +       }
36307 +
36308 +       return err;
36309 +}
36310 +
36311 +/* ---------------------------------------------------------------------- */
36312 +
36313 +void au_nwt_init(struct au_nowait_tasks *nwt)
36314 +{
36315 +       atomic_set(&nwt->nw_len, 0);
36316 +       /* smp_mb(); */ /* atomic_set */
36317 +       init_waitqueue_head(&nwt->nw_wq);
36318 +}
36319 +
36320 +void au_wkq_fin(void)
36321 +{
36322 +       destroy_workqueue(au_wkq);
36323 +}
36324 +
36325 +int __init au_wkq_init(void)
36326 +{
36327 +       int err;
36328 +
36329 +       err = 0;
36330 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36331 +       if (IS_ERR(au_wkq))
36332 +               err = PTR_ERR(au_wkq);
36333 +       else if (!au_wkq)
36334 +               err = -ENOMEM;
36335 +
36336 +       return err;
36337 +}
36338 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36339 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36340 +++ linux/fs/aufs/wkq.h 2020-01-27 10:57:18.182205184 +0100
36341 @@ -0,0 +1,89 @@
36342 +/* SPDX-License-Identifier: GPL-2.0 */
36343 +/*
36344 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36345 + *
36346 + * This program, aufs is free software; you can redistribute it and/or modify
36347 + * it under the terms of the GNU General Public License as published by
36348 + * the Free Software Foundation; either version 2 of the License, or
36349 + * (at your option) any later version.
36350 + *
36351 + * This program is distributed in the hope that it will be useful,
36352 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36353 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36354 + * GNU General Public License for more details.
36355 + *
36356 + * You should have received a copy of the GNU General Public License
36357 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36358 + */
36359 +
36360 +/*
36361 + * workqueue for asynchronous/super-io operations
36362 + * todo: try new credentials management scheme
36363 + */
36364 +
36365 +#ifndef __AUFS_WKQ_H__
36366 +#define __AUFS_WKQ_H__
36367 +
36368 +#ifdef __KERNEL__
36369 +
36370 +#include <linux/wait.h>
36371 +
36372 +struct super_block;
36373 +
36374 +/* ---------------------------------------------------------------------- */
36375 +
36376 +/*
36377 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36378 + */
36379 +struct au_nowait_tasks {
36380 +       atomic_t                nw_len;
36381 +       wait_queue_head_t       nw_wq;
36382 +};
36383 +
36384 +/* ---------------------------------------------------------------------- */
36385 +
36386 +typedef void (*au_wkq_func_t)(void *args);
36387 +
36388 +/* wkq flags */
36389 +#define AuWkq_WAIT     1
36390 +#define AuWkq_NEST     (1 << 1)
36391 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36392 +#define au_fset_wkq(flags, name) \
36393 +       do { (flags) |= AuWkq_##name; } while (0)
36394 +#define au_fclr_wkq(flags, name) \
36395 +       do { (flags) &= ~AuWkq_##name; } while (0)
36396 +
36397 +/* wkq.c */
36398 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36399 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36400 +                 unsigned int flags);
36401 +void au_nwt_init(struct au_nowait_tasks *nwt);
36402 +int __init au_wkq_init(void);
36403 +void au_wkq_fin(void);
36404 +
36405 +/* ---------------------------------------------------------------------- */
36406 +
36407 +static inline int au_wkq_test(void)
36408 +{
36409 +       return current->flags & PF_WQ_WORKER;
36410 +}
36411 +
36412 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36413 +{
36414 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36415 +}
36416 +
36417 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36418 +{
36419 +       if (atomic_dec_and_test(&nwt->nw_len))
36420 +               wake_up_all(&nwt->nw_wq);
36421 +}
36422 +
36423 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36424 +{
36425 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36426 +       return 0;
36427 +}
36428 +
36429 +#endif /* __KERNEL__ */
36430 +#endif /* __AUFS_WKQ_H__ */
36431 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36432 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36433 +++ linux/fs/aufs/xattr.c       2020-08-03 09:14:46.095748745 +0200
36434 @@ -0,0 +1,356 @@
36435 +// SPDX-License-Identifier: GPL-2.0
36436 +/*
36437 + * Copyright (C) 2014-2020 Junjiro R. Okajima
36438 + *
36439 + * This program, aufs is free software; you can redistribute it and/or modify
36440 + * it under the terms of the GNU General Public License as published by
36441 + * the Free Software Foundation; either version 2 of the License, or
36442 + * (at your option) any later version.
36443 + *
36444 + * This program is distributed in the hope that it will be useful,
36445 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36446 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36447 + * GNU General Public License for more details.
36448 + *
36449 + * You should have received a copy of the GNU General Public License
36450 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36451 + */
36452 +
36453 +/*
36454 + * handling xattr functions
36455 + */
36456 +
36457 +#include <linux/fs.h>
36458 +#include <linux/posix_acl_xattr.h>
36459 +#include <linux/xattr.h>
36460 +#include "aufs.h"
36461 +
36462 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36463 +{
36464 +       if (!ignore_flags)
36465 +               goto out;
36466 +       switch (err) {
36467 +       case -ENOMEM:
36468 +       case -EDQUOT:
36469 +               goto out;
36470 +       }
36471 +
36472 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36473 +               err = 0;
36474 +               goto out;
36475 +       }
36476 +
36477 +#define cmp(brattr, prefix) do {                                       \
36478 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36479 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36480 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36481 +                               err = 0;                                \
36482 +                       goto out;                                       \
36483 +               }                                                       \
36484 +       } while (0)
36485 +
36486 +       cmp(SEC, SECURITY);
36487 +       cmp(SYS, SYSTEM);
36488 +       cmp(TR, TRUSTED);
36489 +       cmp(USR, USER);
36490 +#undef cmp
36491 +
36492 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36493 +               err = 0;
36494 +
36495 +out:
36496 +       return err;
36497 +}
36498 +
36499 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36500 +
36501 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36502 +                           char *name, char **buf, unsigned int ignore_flags,
36503 +                           unsigned int verbose)
36504 +{
36505 +       int err;
36506 +       ssize_t ssz;
36507 +       struct inode *h_idst;
36508 +
36509 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36510 +       err = ssz;
36511 +       if (unlikely(err <= 0)) {
36512 +               if (err == -ENODATA
36513 +                   || (err == -EOPNOTSUPP
36514 +                       && ((ignore_flags & au_xattr_out_of_list)
36515 +                           || (au_test_nfs_noacl(d_inode(h_src))
36516 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36517 +                                   || !strcmp(name,
36518 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36519 +                           ))
36520 +                       err = 0;
36521 +               if (err && (verbose || au_debug_test()))
36522 +                       pr_err("%s, err %d\n", name, err);
36523 +               goto out;
36524 +       }
36525 +
36526 +       /* unlock it temporary */
36527 +       h_idst = d_inode(h_dst);
36528 +       inode_unlock(h_idst);
36529 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36530 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36531 +       if (unlikely(err)) {
36532 +               if (verbose || au_debug_test())
36533 +                       pr_err("%s, err %d\n", name, err);
36534 +               err = au_xattr_ignore(err, name, ignore_flags);
36535 +       }
36536 +
36537 +out:
36538 +       return err;
36539 +}
36540 +
36541 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36542 +                 unsigned int verbose)
36543 +{
36544 +       int err, unlocked, acl_access, acl_default;
36545 +       ssize_t ssz;
36546 +       struct inode *h_isrc, *h_idst;
36547 +       char *value, *p, *o, *e;
36548 +
36549 +       /* try stopping to update the source inode while we are referencing */
36550 +       /* there should not be the parent-child relationship between them */
36551 +       h_isrc = d_inode(h_src);
36552 +       h_idst = d_inode(h_dst);
36553 +       inode_unlock(h_idst);
36554 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36555 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36556 +       unlocked = 0;
36557 +
36558 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36559 +       ssz = vfs_listxattr(h_src, NULL, 0);
36560 +       err = ssz;
36561 +       if (unlikely(err < 0)) {
36562 +               AuTraceErr(err);
36563 +               if (err == -ENODATA
36564 +                   || err == -EOPNOTSUPP)
36565 +                       err = 0;        /* ignore */
36566 +               goto out;
36567 +       }
36568 +
36569 +       err = 0;
36570 +       p = NULL;
36571 +       o = NULL;
36572 +       if (ssz) {
36573 +               err = -ENOMEM;
36574 +               p = kmalloc(ssz, GFP_NOFS);
36575 +               o = p;
36576 +               if (unlikely(!p))
36577 +                       goto out;
36578 +               err = vfs_listxattr(h_src, p, ssz);
36579 +       }
36580 +       inode_unlock_shared(h_isrc);
36581 +       unlocked = 1;
36582 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36583 +       if (unlikely(err < 0))
36584 +               goto out_free;
36585 +
36586 +       err = 0;
36587 +       e = p + ssz;
36588 +       value = NULL;
36589 +       acl_access = 0;
36590 +       acl_default = 0;
36591 +       while (!err && p < e) {
36592 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36593 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36594 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36595 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36596 +                                       - 1);
36597 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36598 +                                      verbose);
36599 +               p += strlen(p) + 1;
36600 +       }
36601 +       AuTraceErr(err);
36602 +       ignore_flags |= au_xattr_out_of_list;
36603 +       if (!err && !acl_access) {
36604 +               err = au_do_cpup_xattr(h_dst, h_src,
36605 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36606 +                                      ignore_flags, verbose);
36607 +               AuTraceErr(err);
36608 +       }
36609 +       if (!err && !acl_default) {
36610 +               err = au_do_cpup_xattr(h_dst, h_src,
36611 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36612 +                                      ignore_flags, verbose);
36613 +               AuTraceErr(err);
36614 +       }
36615 +
36616 +       au_kfree_try_rcu(value);
36617 +
36618 +out_free:
36619 +       au_kfree_try_rcu(o);
36620 +out:
36621 +       if (!unlocked)
36622 +               inode_unlock_shared(h_isrc);
36623 +       AuTraceErr(err);
36624 +       return err;
36625 +}
36626 +
36627 +/* ---------------------------------------------------------------------- */
36628 +
36629 +static int au_smack_reentering(struct super_block *sb)
36630 +{
36631 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36632 +       /*
36633 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36634 +        * i_op->getxattr(). ouch.
36635 +        */
36636 +       return si_pid_test(sb);
36637 +#else
36638 +       return 0;
36639 +#endif
36640 +}
36641 +
36642 +enum {
36643 +       AU_XATTR_LIST,
36644 +       AU_XATTR_GET
36645 +};
36646 +
36647 +struct au_lgxattr {
36648 +       int type;
36649 +       union {
36650 +               struct {
36651 +                       char    *list;
36652 +                       size_t  size;
36653 +               } list;
36654 +               struct {
36655 +                       const char      *name;
36656 +                       void            *value;
36657 +                       size_t          size;
36658 +               } get;
36659 +       } u;
36660 +};
36661 +
36662 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36663 +                         struct au_lgxattr *arg)
36664 +{
36665 +       ssize_t err;
36666 +       int reenter;
36667 +       struct path h_path;
36668 +       struct super_block *sb;
36669 +
36670 +       sb = dentry->d_sb;
36671 +       reenter = au_smack_reentering(sb);
36672 +       if (!reenter) {
36673 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36674 +               if (unlikely(err))
36675 +                       goto out;
36676 +       }
36677 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36678 +       if (unlikely(err))
36679 +               goto out_si;
36680 +       if (unlikely(!h_path.dentry))
36681 +               /* illegally overlapped or something */
36682 +               goto out_di; /* pretending success */
36683 +
36684 +       /* always topmost entry only */
36685 +       switch (arg->type) {
36686 +       case AU_XATTR_LIST:
36687 +               err = vfs_listxattr(h_path.dentry,
36688 +                                   arg->u.list.list, arg->u.list.size);
36689 +               break;
36690 +       case AU_XATTR_GET:
36691 +               AuDebugOn(d_is_negative(h_path.dentry));
36692 +               err = vfs_getxattr(h_path.dentry,
36693 +                                  arg->u.get.name, arg->u.get.value,
36694 +                                  arg->u.get.size);
36695 +               break;
36696 +       }
36697 +
36698 +out_di:
36699 +       if (!reenter)
36700 +               di_read_unlock(dentry, AuLock_IR);
36701 +out_si:
36702 +       if (!reenter)
36703 +               si_read_unlock(sb);
36704 +out:
36705 +       AuTraceErr(err);
36706 +       return err;
36707 +}
36708 +
36709 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36710 +{
36711 +       struct au_lgxattr arg = {
36712 +               .type = AU_XATTR_LIST,
36713 +               .u.list = {
36714 +                       .list   = list,
36715 +                       .size   = size
36716 +               },
36717 +       };
36718 +
36719 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36720 +}
36721 +
36722 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36723 +                          const char *name, void *value, size_t size)
36724 +{
36725 +       struct au_lgxattr arg = {
36726 +               .type = AU_XATTR_GET,
36727 +               .u.get = {
36728 +                       .name   = name,
36729 +                       .value  = value,
36730 +                       .size   = size
36731 +               },
36732 +       };
36733 +
36734 +       return au_lgxattr(dentry, inode, &arg);
36735 +}
36736 +
36737 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36738 +                      const char *name, const void *value, size_t size,
36739 +                      int flags)
36740 +{
36741 +       struct au_sxattr arg = {
36742 +               .type = AU_XATTR_SET,
36743 +               .u.set = {
36744 +                       .name   = name,
36745 +                       .value  = value,
36746 +                       .size   = size,
36747 +                       .flags  = flags
36748 +               },
36749 +       };
36750 +
36751 +       return au_sxattr(dentry, inode, &arg);
36752 +}
36753 +
36754 +/* ---------------------------------------------------------------------- */
36755 +
36756 +static int au_xattr_get(const struct xattr_handler *handler,
36757 +                       struct dentry *dentry, struct inode *inode,
36758 +                       const char *name, void *buffer, size_t size)
36759 +{
36760 +       return au_getxattr(dentry, inode, name, buffer, size);
36761 +}
36762 +
36763 +static int au_xattr_set(const struct xattr_handler *handler,
36764 +                       struct dentry *dentry, struct inode *inode,
36765 +                       const char *name, const void *value, size_t size,
36766 +                       int flags)
36767 +{
36768 +       return au_setxattr(dentry, inode, name, value, size, flags);
36769 +}
36770 +
36771 +static const struct xattr_handler au_xattr_handler = {
36772 +       .name   = "",
36773 +       .prefix = "",
36774 +       .get    = au_xattr_get,
36775 +       .set    = au_xattr_set
36776 +};
36777 +
36778 +static const struct xattr_handler *au_xattr_handlers[] = {
36779 +#ifdef CONFIG_FS_POSIX_ACL
36780 +       &posix_acl_access_xattr_handler,
36781 +       &posix_acl_default_xattr_handler,
36782 +#endif
36783 +       &au_xattr_handler, /* must be last */
36784 +       NULL
36785 +};
36786 +
36787 +void au_xattr_init(struct super_block *sb)
36788 +{
36789 +       sb->s_xattr = au_xattr_handlers;
36790 +}
36791 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36792 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36793 +++ linux/fs/aufs/xino.c        2020-04-03 08:16:49.834195677 +0200
36794 @@ -0,0 +1,1966 @@
36795 +// SPDX-License-Identifier: GPL-2.0
36796 +/*
36797 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36798 + *
36799 + * This program, aufs is free software; you can redistribute it and/or modify
36800 + * it under the terms of the GNU General Public License as published by
36801 + * the Free Software Foundation; either version 2 of the License, or
36802 + * (at your option) any later version.
36803 + *
36804 + * This program is distributed in the hope that it will be useful,
36805 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36806 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36807 + * GNU General Public License for more details.
36808 + *
36809 + * You should have received a copy of the GNU General Public License
36810 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36811 + */
36812 +
36813 +/*
36814 + * external inode number translation table and bitmap
36815 + *
36816 + * things to consider
36817 + * - the lifetime
36818 + *   + au_xino object
36819 + *   + XINO files (xino, xib, xigen)
36820 + *   + dynamic debugfs entries (xiN)
36821 + *   + static debugfs entries (xib, xigen)
36822 + *   + static sysfs entry (xi_path)
36823 + * - several entry points to handle them.
36824 + *   + mount(2) without xino option (default)
36825 + *   + mount(2) with xino option
36826 + *   + mount(2) with noxino option
36827 + *   + umount(2)
36828 + *   + remount with add/del branches
36829 + *   + remount with xino/noxino options
36830 + */
36831 +
36832 +#include <linux/seq_file.h>
36833 +#include <linux/statfs.h>
36834 +#include "aufs.h"
36835 +
36836 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36837 +                                    aufs_bindex_t bbot,
36838 +                                    struct super_block *h_sb)
36839 +{
36840 +       /* todo: try binary-search if the branches are many */
36841 +       for (; btop <= bbot; btop++)
36842 +               if (h_sb == au_sbr_sb(sb, btop))
36843 +                       return btop;
36844 +       return -1;
36845 +}
36846 +
36847 +/*
36848 + * find another branch who is on the same filesystem of the specified
36849 + * branch{@btgt}. search until @bbot.
36850 + */
36851 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36852 +                                 aufs_bindex_t bbot)
36853 +{
36854 +       aufs_bindex_t bindex;
36855 +       struct super_block *tgt_sb;
36856 +
36857 +       tgt_sb = au_sbr_sb(sb, btgt);
36858 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36859 +       if (bindex < 0)
36860 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36861 +
36862 +       return bindex;
36863 +}
36864 +
36865 +/* ---------------------------------------------------------------------- */
36866 +
36867 +/*
36868 + * stop unnecessary notify events at creating xino files
36869 + */
36870 +
36871 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36872 +{
36873 +       aufs_bindex_t bfound, bindex, bbot;
36874 +       struct dentry *parent;
36875 +       struct au_branch *br;
36876 +
36877 +       bfound = -1;
36878 +       parent = dentry->d_parent; /* safe d_parent access */
36879 +       bbot = au_sbbot(sb);
36880 +       for (bindex = 0; bindex <= bbot; bindex++) {
36881 +               br = au_sbr(sb, bindex);
36882 +               if (au_br_dentry(br) == parent) {
36883 +                       bfound = bindex;
36884 +                       break;
36885 +               }
36886 +       }
36887 +
36888 +       AuDbg("bfound b%d\n", bfound);
36889 +       return bfound;
36890 +}
36891 +
36892 +struct au_xino_lock_dir {
36893 +       struct au_hinode *hdir;
36894 +       struct dentry *parent;
36895 +       struct inode *dir;
36896 +};
36897 +
36898 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36899 +                                         unsigned int lsc)
36900 +{
36901 +       struct dentry *parent;
36902 +       struct inode *dir;
36903 +
36904 +       parent = dget_parent(dentry);
36905 +       dir = d_inode(parent);
36906 +       inode_lock_nested(dir, lsc);
36907 +#if 0 /* it should not happen */
36908 +       spin_lock(&dentry->d_lock);
36909 +       if (unlikely(dentry->d_parent != parent)) {
36910 +               spin_unlock(&dentry->d_lock);
36911 +               inode_unlock(dir);
36912 +               dput(parent);
36913 +               parent = NULL;
36914 +               goto out;
36915 +       }
36916 +       spin_unlock(&dentry->d_lock);
36917 +
36918 +out:
36919 +#endif
36920 +       return parent;
36921 +}
36922 +
36923 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36924 +                            struct au_xino_lock_dir *ldir)
36925 +{
36926 +       aufs_bindex_t bindex;
36927 +
36928 +       ldir->hdir = NULL;
36929 +       bindex = au_xi_root(sb, xipath->dentry);
36930 +       if (bindex >= 0) {
36931 +               /* rw branch root */
36932 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36933 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36934 +       } else {
36935 +               /* other */
36936 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36937 +                                                  AuLsc_I_PARENT);
36938 +               ldir->dir = d_inode(ldir->parent);
36939 +       }
36940 +}
36941 +
36942 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36943 +{
36944 +       if (ldir->hdir)
36945 +               au_hn_inode_unlock(ldir->hdir);
36946 +       else {
36947 +               inode_unlock(ldir->dir);
36948 +               dput(ldir->parent);
36949 +       }
36950 +}
36951 +
36952 +/* ---------------------------------------------------------------------- */
36953 +
36954 +/*
36955 + * create and set a new xino file
36956 + */
36957 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36958 +                           int wbrtop)
36959 +{
36960 +       struct file *file;
36961 +       struct dentry *h_parent, *d;
36962 +       struct inode *h_dir, *inode;
36963 +       int err;
36964 +       static DEFINE_MUTEX(mtx);
36965 +
36966 +       /*
36967 +        * at mount-time, and the xino file is the default path,
36968 +        * hnotify is disabled so we have no notify events to ignore.
36969 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36970 +        */
36971 +       if (!wbrtop)
36972 +               mutex_lock(&mtx);
36973 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36974 +                              /* | __FMODE_NONOTIFY */,
36975 +                              0666);
36976 +       if (IS_ERR(file)) {
36977 +               if (!wbrtop)
36978 +                       mutex_unlock(&mtx);
36979 +               if (!silent)
36980 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36981 +               return file;
36982 +       }
36983 +
36984 +       /* keep file count */
36985 +       err = 0;
36986 +       d = file->f_path.dentry;
36987 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36988 +       if (!wbrtop)
36989 +               mutex_unlock(&mtx);
36990 +       /* mnt_want_write() is unnecessary here */
36991 +       h_dir = d_inode(h_parent);
36992 +       inode = file_inode(file);
36993 +       /* no delegation since it is just created */
36994 +       if (inode->i_nlink)
36995 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36996 +                                  /*force*/0);
36997 +       inode_unlock(h_dir);
36998 +       dput(h_parent);
36999 +       if (unlikely(err)) {
37000 +               if (!silent)
37001 +                       pr_err("unlink %s(%d)\n", fpath, err);
37002 +               goto out;
37003 +       }
37004 +
37005 +       err = -EINVAL;
37006 +       if (unlikely(sb == d->d_sb)) {
37007 +               if (!silent)
37008 +                       pr_err("%s must be outside\n", fpath);
37009 +               goto out;
37010 +       }
37011 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37012 +               if (!silent)
37013 +                       pr_err("xino doesn't support %s(%s)\n",
37014 +                              fpath, au_sbtype(d->d_sb));
37015 +               goto out;
37016 +       }
37017 +       return file; /* success */
37018 +
37019 +out:
37020 +       fput(file);
37021 +       file = ERR_PTR(err);
37022 +       return file;
37023 +}
37024 +
37025 +/*
37026 + * create a new xinofile at the same place/path as @base.
37027 + */
37028 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37029 +                            struct file *copy_src)
37030 +{
37031 +       struct file *file;
37032 +       struct dentry *dentry, *parent;
37033 +       struct inode *dir, *delegated;
37034 +       struct qstr *name;
37035 +       struct path path;
37036 +       int err, do_unlock;
37037 +       struct au_xino_lock_dir ldir;
37038 +
37039 +       do_unlock = 1;
37040 +       au_xino_lock_dir(sb, base, &ldir);
37041 +       dentry = base->dentry;
37042 +       parent = dentry->d_parent; /* dir inode is locked */
37043 +       dir = d_inode(parent);
37044 +       IMustLock(dir);
37045 +
37046 +       name = &dentry->d_name;
37047 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37048 +       if (IS_ERR(path.dentry)) {
37049 +               file = (void *)path.dentry;
37050 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37051 +               goto out;
37052 +       }
37053 +
37054 +       /* no need to mnt_want_write() since we call dentry_open() later */
37055 +       err = vfs_create(dir, path.dentry, 0666, NULL);
37056 +       if (unlikely(err)) {
37057 +               file = ERR_PTR(err);
37058 +               pr_err("%pd create err %d\n", dentry, err);
37059 +               goto out_dput;
37060 +       }
37061 +
37062 +       path.mnt = base->mnt;
37063 +       file = vfsub_dentry_open(&path,
37064 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37065 +                                /* | __FMODE_NONOTIFY */);
37066 +       if (IS_ERR(file)) {
37067 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37068 +               goto out_dput;
37069 +       }
37070 +
37071 +       delegated = NULL;
37072 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37073 +       au_xino_unlock_dir(&ldir);
37074 +       do_unlock = 0;
37075 +       if (unlikely(err == -EWOULDBLOCK)) {
37076 +               pr_warn("cannot retry for NFSv4 delegation"
37077 +                       " for an internal unlink\n");
37078 +               iput(delegated);
37079 +       }
37080 +       if (unlikely(err)) {
37081 +               pr_err("%pd unlink err %d\n", dentry, err);
37082 +               goto out_fput;
37083 +       }
37084 +
37085 +       if (copy_src) {
37086 +               /* no one can touch copy_src xino */
37087 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37088 +               if (unlikely(err)) {
37089 +                       pr_err("%pd copy err %d\n", dentry, err);
37090 +                       goto out_fput;
37091 +               }
37092 +       }
37093 +       goto out_dput; /* success */
37094 +
37095 +out_fput:
37096 +       fput(file);
37097 +       file = ERR_PTR(err);
37098 +out_dput:
37099 +       dput(path.dentry);
37100 +out:
37101 +       if (do_unlock)
37102 +               au_xino_unlock_dir(&ldir);
37103 +       return file;
37104 +}
37105 +
37106 +struct file *au_xino_file1(struct au_xino *xi)
37107 +{
37108 +       struct file *file;
37109 +       unsigned int u, nfile;
37110 +
37111 +       file = NULL;
37112 +       nfile = xi->xi_nfile;
37113 +       for (u = 0; u < nfile; u++) {
37114 +               file = xi->xi_file[u];
37115 +               if (file)
37116 +                       break;
37117 +       }
37118 +
37119 +       return file;
37120 +}
37121 +
37122 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37123 +{
37124 +       int err;
37125 +       struct file *f;
37126 +       void *p;
37127 +
37128 +       if (file)
37129 +               get_file(file);
37130 +
37131 +       err = 0;
37132 +       f = NULL;
37133 +       if (idx < xi->xi_nfile) {
37134 +               f = xi->xi_file[idx];
37135 +               if (f)
37136 +                       fput(f);
37137 +       } else {
37138 +               p = au_kzrealloc(xi->xi_file,
37139 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37140 +                                sizeof(*xi->xi_file) * (idx + 1),
37141 +                                GFP_NOFS, /*may_shrink*/0);
37142 +               if (p) {
37143 +                       MtxMustLock(&xi->xi_mtx);
37144 +                       xi->xi_file = p;
37145 +                       xi->xi_nfile = idx + 1;
37146 +               } else {
37147 +                       err = -ENOMEM;
37148 +                       if (file)
37149 +                               fput(file);
37150 +                       goto out;
37151 +               }
37152 +       }
37153 +       xi->xi_file[idx] = file;
37154 +
37155 +out:
37156 +       return err;
37157 +}
37158 +
37159 +/*
37160 + * if @xinew->xi is not set, then create new xigen file.
37161 + */
37162 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37163 +{
37164 +       struct file *file;
37165 +       int err;
37166 +
37167 +       SiMustAnyLock(sb);
37168 +
37169 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37170 +       if (IS_ERR(file)) {
37171 +               err = PTR_ERR(file);
37172 +               pr_err("%s[%d], err %d\n",
37173 +                      xinew->xi ? "xino" : "xigen",
37174 +                      xinew->idx, err);
37175 +               goto out;
37176 +       }
37177 +
37178 +       if (xinew->xi)
37179 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37180 +       else {
37181 +               BUG();
37182 +               /* todo: make xigen file an array */
37183 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37184 +       }
37185 +       fput(file);
37186 +       if (unlikely(err))
37187 +               file = ERR_PTR(err);
37188 +
37189 +out:
37190 +       return file;
37191 +}
37192 +
37193 +/* ---------------------------------------------------------------------- */
37194 +
37195 +/*
37196 + * truncate xino files
37197 + */
37198 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37199 +                           int idx, struct kstatfs *st)
37200 +{
37201 +       int err;
37202 +       blkcnt_t blocks;
37203 +       struct file *file, *new_xino;
37204 +       struct au_xi_new xinew = {
37205 +               .idx = idx
37206 +       };
37207 +
37208 +       err = 0;
37209 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37210 +       file = au_xino_file(xinew.xi, idx);
37211 +       if (!file)
37212 +               goto out;
37213 +
37214 +       xinew.base = &file->f_path;
37215 +       err = vfs_statfs(xinew.base, st);
37216 +       if (unlikely(err)) {
37217 +               AuErr1("statfs err %d, ignored\n", err);
37218 +               err = 0;
37219 +               goto out;
37220 +       }
37221 +
37222 +       blocks = file_inode(file)->i_blocks;
37223 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37224 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37225 +
37226 +       xinew.copy_src = file;
37227 +       new_xino = au_xi_new(sb, &xinew);
37228 +       if (IS_ERR(new_xino)) {
37229 +               err = PTR_ERR(new_xino);
37230 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37231 +               goto out;
37232 +       }
37233 +
37234 +       err = vfs_statfs(&new_xino->f_path, st);
37235 +       if (!err)
37236 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37237 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37238 +                       st->f_bfree, st->f_blocks);
37239 +       else {
37240 +               AuErr1("statfs err %d, ignored\n", err);
37241 +               err = 0;
37242 +       }
37243 +
37244 +out:
37245 +       return err;
37246 +}
37247 +
37248 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37249 +{
37250 +       int err, i;
37251 +       unsigned long jiffy;
37252 +       aufs_bindex_t bbot;
37253 +       struct kstatfs *st;
37254 +       struct au_branch *br;
37255 +       struct au_xino *xi;
37256 +
37257 +       err = -ENOMEM;
37258 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37259 +       if (unlikely(!st))
37260 +               goto out;
37261 +
37262 +       err = -EINVAL;
37263 +       bbot = au_sbbot(sb);
37264 +       if (unlikely(bindex < 0 || bbot < bindex))
37265 +               goto out_st;
37266 +
37267 +       err = 0;
37268 +       jiffy = jiffies;
37269 +       br = au_sbr(sb, bindex);
37270 +       xi = br->br_xino;
37271 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37272 +               err = au_xino_do_trunc(sb, bindex, i, st);
37273 +       if (!err)
37274 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37275 +
37276 +out_st:
37277 +       au_kfree_rcu(st);
37278 +out:
37279 +       return err;
37280 +}
37281 +
37282 +struct xino_do_trunc_args {
37283 +       struct super_block *sb;
37284 +       struct au_branch *br;
37285 +       int idx;
37286 +};
37287 +
37288 +static void xino_do_trunc(void *_args)
37289 +{
37290 +       struct xino_do_trunc_args *args = _args;
37291 +       struct super_block *sb;
37292 +       struct au_branch *br;
37293 +       struct inode *dir;
37294 +       int err, idx;
37295 +       aufs_bindex_t bindex;
37296 +
37297 +       err = 0;
37298 +       sb = args->sb;
37299 +       dir = d_inode(sb->s_root);
37300 +       br = args->br;
37301 +       idx = args->idx;
37302 +
37303 +       si_noflush_write_lock(sb);
37304 +       ii_read_lock_parent(dir);
37305 +       bindex = au_br_index(sb, br->br_id);
37306 +       err = au_xino_trunc(sb, bindex, idx);
37307 +       ii_read_unlock(dir);
37308 +       if (unlikely(err))
37309 +               pr_warn("err b%d, (%d)\n", bindex, err);
37310 +       atomic_dec(&br->br_xino->xi_truncating);
37311 +       au_lcnt_dec(&br->br_count);
37312 +       si_write_unlock(sb);
37313 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37314 +       au_kfree_rcu(args);
37315 +}
37316 +
37317 +/*
37318 + * returns the index in the xi_file array whose corresponding file is necessary
37319 + * to truncate, or -1 which means no need to truncate.
37320 + */
37321 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37322 +{
37323 +       int err;
37324 +       unsigned int u;
37325 +       struct kstatfs st;
37326 +       struct au_sbinfo *sbinfo;
37327 +       struct au_xino *xi;
37328 +       struct file *file;
37329 +
37330 +       /* todo: si_xino_expire and the ratio should be customizable */
37331 +       sbinfo = au_sbi(sb);
37332 +       if (time_before(jiffies,
37333 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37334 +               return -1;
37335 +
37336 +       /* truncation border */
37337 +       xi = br->br_xino;
37338 +       for (u = 0; u < xi->xi_nfile; u++) {
37339 +               file = au_xino_file(xi, u);
37340 +               if (!file)
37341 +                       continue;
37342 +
37343 +               err = vfs_statfs(&file->f_path, &st);
37344 +               if (unlikely(err)) {
37345 +                       AuErr1("statfs err %d, ignored\n", err);
37346 +                       return -1;
37347 +               }
37348 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37349 +                   >= AUFS_XINO_DEF_TRUNC)
37350 +                       return u;
37351 +       }
37352 +
37353 +       return -1;
37354 +}
37355 +
37356 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37357 +{
37358 +       int idx;
37359 +       struct xino_do_trunc_args *args;
37360 +       int wkq_err;
37361 +
37362 +       idx = xino_trunc_test(sb, br);
37363 +       if (idx < 0)
37364 +               return;
37365 +
37366 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37367 +               goto out;
37368 +
37369 +       /* lock and kfree() will be called in trunc_xino() */
37370 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37371 +       if (unlikely(!args)) {
37372 +               AuErr1("no memory\n");
37373 +               goto out;
37374 +       }
37375 +
37376 +       au_lcnt_inc(&br->br_count);
37377 +       args->sb = sb;
37378 +       args->br = br;
37379 +       args->idx = idx;
37380 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37381 +       if (!wkq_err)
37382 +               return; /* success */
37383 +
37384 +       pr_err("wkq %d\n", wkq_err);
37385 +       au_lcnt_dec(&br->br_count);
37386 +       au_kfree_rcu(args);
37387 +
37388 +out:
37389 +       atomic_dec(&br->br_xino->xi_truncating);
37390 +}
37391 +
37392 +/* ---------------------------------------------------------------------- */
37393 +
37394 +struct au_xi_calc {
37395 +       int idx;
37396 +       loff_t pos;
37397 +};
37398 +
37399 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37400 +                      struct au_xi_calc *calc)
37401 +{
37402 +       loff_t maxent;
37403 +
37404 +       maxent = au_xi_maxent(sb);
37405 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37406 +       calc->pos *= sizeof(ino_t);
37407 +}
37408 +
37409 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37410 +                               struct au_xi_calc *calc)
37411 +{
37412 +       int err;
37413 +       struct file *file;
37414 +       struct au_xino *xi = br->br_xino;
37415 +       struct au_xi_new xinew = {
37416 +               .xi = xi
37417 +       };
37418 +
37419 +       SiMustAnyLock(sb);
37420 +
37421 +       err = 0;
37422 +       if (!xi)
37423 +               goto out;
37424 +
37425 +       mutex_lock(&xi->xi_mtx);
37426 +       file = au_xino_file(xi, calc->idx);
37427 +       if (file)
37428 +               goto out_mtx;
37429 +
37430 +       file = au_xino_file(xi, /*idx*/-1);
37431 +       AuDebugOn(!file);
37432 +       xinew.idx = calc->idx;
37433 +       xinew.base = &file->f_path;
37434 +       /* xinew.copy_src = NULL; */
37435 +       file = au_xi_new(sb, &xinew);
37436 +       if (IS_ERR(file))
37437 +               err = PTR_ERR(file);
37438 +
37439 +out_mtx:
37440 +       mutex_unlock(&xi->xi_mtx);
37441 +out:
37442 +       return err;
37443 +}
37444 +
37445 +struct au_xino_do_new_async_args {
37446 +       struct super_block *sb;
37447 +       struct au_branch *br;
37448 +       struct au_xi_calc calc;
37449 +       ino_t ino;
37450 +};
37451 +
37452 +struct au_xi_writing {
37453 +       struct hlist_bl_node node;
37454 +       ino_t h_ino, ino;
37455 +};
37456 +
37457 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37458 +                           struct au_xi_calc *calc, ino_t ino);
37459 +
37460 +static void au_xino_call_do_new_async(void *args)
37461 +{
37462 +       struct au_xino_do_new_async_args *a = args;
37463 +       struct au_branch *br;
37464 +       struct super_block *sb;
37465 +       struct au_sbinfo *sbi;
37466 +       struct inode *root;
37467 +       struct file *file;
37468 +       struct au_xi_writing *del, *p;
37469 +       struct hlist_bl_head *hbl;
37470 +       struct hlist_bl_node *pos;
37471 +       int err;
37472 +
37473 +       br = a->br;
37474 +       sb = a->sb;
37475 +       sbi = au_sbi(sb);
37476 +       si_noflush_read_lock(sb);
37477 +       root = d_inode(sb->s_root);
37478 +       ii_read_lock_child(root);
37479 +       err = au_xino_do_new_async(sb, br, &a->calc);
37480 +       if (unlikely(err)) {
37481 +               AuIOErr("err %d\n", err);
37482 +               goto out;
37483 +       }
37484 +
37485 +       file = au_xino_file(br->br_xino, a->calc.idx);
37486 +       AuDebugOn(!file);
37487 +       err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37488 +       if (unlikely(err)) {
37489 +               AuIOErr("err %d\n", err);
37490 +               goto out;
37491 +       }
37492 +
37493 +       del = NULL;
37494 +       hbl = &br->br_xino->xi_writing;
37495 +       hlist_bl_lock(hbl);
37496 +       au_hbl_for_each(pos, hbl) {
37497 +               p = container_of(pos, struct au_xi_writing, node);
37498 +               if (p->ino == a->ino) {
37499 +                       del = p;
37500 +                       hlist_bl_del(&p->node);
37501 +                       break;
37502 +               }
37503 +       }
37504 +       hlist_bl_unlock(hbl);
37505 +       au_kfree_rcu(del);
37506 +
37507 +out:
37508 +       au_lcnt_dec(&br->br_count);
37509 +       ii_read_unlock(root);
37510 +       si_read_unlock(sb);
37511 +       au_nwt_done(&sbi->si_nowait);
37512 +       au_kfree_rcu(a);
37513 +}
37514 +
37515 +/*
37516 + * create a new xino file asynchronously
37517 + */
37518 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37519 +                            struct au_xi_calc *calc, ino_t ino)
37520 +{
37521 +       int err;
37522 +       struct au_xino_do_new_async_args *arg;
37523 +
37524 +       err = -ENOMEM;
37525 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37526 +       if (unlikely(!arg))
37527 +               goto out;
37528 +
37529 +       arg->sb = sb;
37530 +       arg->br = br;
37531 +       arg->calc = *calc;
37532 +       arg->ino = ino;
37533 +       au_lcnt_inc(&br->br_count);
37534 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37535 +       if (unlikely(err)) {
37536 +               pr_err("wkq %d\n", err);
37537 +               au_lcnt_dec(&br->br_count);
37538 +               au_kfree_rcu(arg);
37539 +       }
37540 +
37541 +out:
37542 +       return err;
37543 +}
37544 +
37545 +/*
37546 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37547 + * at the position of @h_ino.
37548 + */
37549 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37550 +                ino_t *ino)
37551 +{
37552 +       int err;
37553 +       ssize_t sz;
37554 +       struct au_xi_calc calc;
37555 +       struct au_sbinfo *sbinfo;
37556 +       struct file *file;
37557 +       struct au_xino *xi;
37558 +       struct hlist_bl_head *hbl;
37559 +       struct hlist_bl_node *pos;
37560 +       struct au_xi_writing *p;
37561 +
37562 +       *ino = 0;
37563 +       if (!au_opt_test(au_mntflags(sb), XINO))
37564 +               return 0; /* no xino */
37565 +
37566 +       err = 0;
37567 +       au_xi_calc(sb, h_ino, &calc);
37568 +       xi = au_sbr(sb, bindex)->br_xino;
37569 +       file = au_xino_file(xi, calc.idx);
37570 +       if (!file) {
37571 +               hbl = &xi->xi_writing;
37572 +               hlist_bl_lock(hbl);
37573 +               au_hbl_for_each(pos, hbl) {
37574 +                       p = container_of(pos, struct au_xi_writing, node);
37575 +                       if (p->h_ino == h_ino) {
37576 +                               AuDbg("hi%llu, i%llu, found\n",
37577 +                                     (u64)p->h_ino, (u64)p->ino);
37578 +                               *ino = p->ino;
37579 +                               break;
37580 +                       }
37581 +               }
37582 +               hlist_bl_unlock(hbl);
37583 +               return 0;
37584 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37585 +               return 0; /* no xino */
37586 +
37587 +       sbinfo = au_sbi(sb);
37588 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
37589 +       if (sz == sizeof(*ino))
37590 +               return 0; /* success */
37591 +
37592 +       err = sz;
37593 +       if (unlikely(sz >= 0)) {
37594 +               err = -EIO;
37595 +               AuIOErr("xino read error (%zd)\n", sz);
37596 +       }
37597 +       return err;
37598 +}
37599 +
37600 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37601 +                           struct au_xi_calc *calc, ino_t ino)
37602 +{
37603 +       ssize_t sz;
37604 +
37605 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
37606 +       if (sz == sizeof(ino))
37607 +               return 0; /* success */
37608 +
37609 +       AuIOErr("write failed (%zd)\n", sz);
37610 +       return -EIO;
37611 +}
37612 +
37613 +/*
37614 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37615 + * at the position of @h_ino.
37616 + * even if @ino is zero, it is written to the xinofile and means no entry.
37617 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37618 + * try truncating it.
37619 + */
37620 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37621 +                 ino_t ino)
37622 +{
37623 +       int err;
37624 +       unsigned int mnt_flags;
37625 +       struct au_xi_calc calc;
37626 +       struct file *file;
37627 +       struct au_branch *br;
37628 +       struct au_xino *xi;
37629 +       struct au_xi_writing *p;
37630 +
37631 +       SiMustAnyLock(sb);
37632 +
37633 +       mnt_flags = au_mntflags(sb);
37634 +       if (!au_opt_test(mnt_flags, XINO))
37635 +               return 0;
37636 +
37637 +       au_xi_calc(sb, h_ino, &calc);
37638 +       br = au_sbr(sb, bindex);
37639 +       xi = br->br_xino;
37640 +       file = au_xino_file(xi, calc.idx);
37641 +       if (!file) {
37642 +               /* store the inum pair into the list */
37643 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37644 +               p->h_ino = h_ino;
37645 +               p->ino = ino;
37646 +               au_hbl_add(&p->node, &xi->xi_writing);
37647 +
37648 +               /* create and write a new xino file asynchronously */
37649 +               err = au_xino_new_async(sb, br, &calc, ino);
37650 +               if (!err)
37651 +                       return 0; /* success */
37652 +               goto out;
37653 +       }
37654 +
37655 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
37656 +       if (!err) {
37657 +               br = au_sbr(sb, bindex);
37658 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37659 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37660 +                       xino_try_trunc(sb, br);
37661 +               return 0; /* success */
37662 +       }
37663 +
37664 +out:
37665 +       AuIOErr("write failed (%d)\n", err);
37666 +       return -EIO;
37667 +}
37668 +
37669 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37670 +                             size_t size, loff_t *pos);
37671 +
37672 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37673 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37674 +                  loff_t *pos)
37675 +{
37676 +       ssize_t err;
37677 +       mm_segment_t oldfs;
37678 +       union {
37679 +               void *k;
37680 +               char __user *u;
37681 +       } buf;
37682 +       int i;
37683 +       const int prevent_endless = 10;
37684 +
37685 +       i = 0;
37686 +       buf.k = kbuf;
37687 +       oldfs = get_fs();
37688 +       set_fs(KERNEL_DS);
37689 +       do {
37690 +               err = func(file, buf.u, size, pos);
37691 +               if (err == -EINTR
37692 +                   && !au_wkq_test()
37693 +                   && fatal_signal_pending(current)) {
37694 +                       set_fs(oldfs);
37695 +                       err = xino_fread_wkq(func, file, kbuf, size, pos);
37696 +                       BUG_ON(err == -EINTR);
37697 +                       oldfs = get_fs();
37698 +                       set_fs(KERNEL_DS);
37699 +               }
37700 +       } while (i++ < prevent_endless
37701 +                && (err == -EAGAIN || err == -EINTR));
37702 +       set_fs(oldfs);
37703 +
37704 +#if 0 /* reserved for future use */
37705 +       if (err > 0)
37706 +               fsnotify_access(file->f_path.dentry);
37707 +#endif
37708 +
37709 +       return err;
37710 +}
37711 +
37712 +struct xino_fread_args {
37713 +       ssize_t *errp;
37714 +       vfs_readf_t func;
37715 +       struct file *file;
37716 +       void *buf;
37717 +       size_t size;
37718 +       loff_t *pos;
37719 +};
37720 +
37721 +static void call_xino_fread(void *args)
37722 +{
37723 +       struct xino_fread_args *a = args;
37724 +       *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37725 +}
37726 +
37727 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37728 +                             size_t size, loff_t *pos)
37729 +{
37730 +       ssize_t err;
37731 +       int wkq_err;
37732 +       struct xino_fread_args args = {
37733 +               .errp   = &err,
37734 +               .func   = func,
37735 +               .file   = file,
37736 +               .buf    = buf,
37737 +               .size   = size,
37738 +               .pos    = pos
37739 +       };
37740 +
37741 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37742 +       if (unlikely(wkq_err))
37743 +               err = wkq_err;
37744 +
37745 +       return err;
37746 +}
37747 +
37748 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37749 +                              size_t size, loff_t *pos);
37750 +
37751 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37752 +                             size_t size, loff_t *pos)
37753 +{
37754 +       ssize_t err;
37755 +       mm_segment_t oldfs;
37756 +       union {
37757 +               void *k;
37758 +               const char __user *u;
37759 +       } buf;
37760 +       int i;
37761 +       const int prevent_endless = 10;
37762 +
37763 +       i = 0;
37764 +       buf.k = kbuf;
37765 +       oldfs = get_fs();
37766 +       set_fs(KERNEL_DS);
37767 +       do {
37768 +               err = func(file, buf.u, size, pos);
37769 +               if (err == -EINTR
37770 +                   && !au_wkq_test()
37771 +                   && fatal_signal_pending(current)) {
37772 +                       set_fs(oldfs);
37773 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37774 +                       BUG_ON(err == -EINTR);
37775 +                       oldfs = get_fs();
37776 +                       set_fs(KERNEL_DS);
37777 +               }
37778 +       } while (i++ < prevent_endless
37779 +                && (err == -EAGAIN || err == -EINTR));
37780 +       set_fs(oldfs);
37781 +
37782 +#if 0 /* reserved for future use */
37783 +       if (err > 0)
37784 +               fsnotify_modify(file->f_path.dentry);
37785 +#endif
37786 +
37787 +       return err;
37788 +}
37789 +
37790 +struct do_xino_fwrite_args {
37791 +       ssize_t *errp;
37792 +       vfs_writef_t func;
37793 +       struct file *file;
37794 +       void *buf;
37795 +       size_t size;
37796 +       loff_t *pos;
37797 +};
37798 +
37799 +static void call_do_xino_fwrite(void *args)
37800 +{
37801 +       struct do_xino_fwrite_args *a = args;
37802 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37803 +}
37804 +
37805 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37806 +                              size_t size, loff_t *pos)
37807 +{
37808 +       ssize_t err;
37809 +       int wkq_err;
37810 +       struct do_xino_fwrite_args args = {
37811 +               .errp   = &err,
37812 +               .func   = func,
37813 +               .file   = file,
37814 +               .buf    = buf,
37815 +               .size   = size,
37816 +               .pos    = pos
37817 +       };
37818 +
37819 +       /*
37820 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37821 +        * users should care about quota and real 'filesystem full.'
37822 +        */
37823 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37824 +       if (unlikely(wkq_err))
37825 +               err = wkq_err;
37826 +
37827 +       return err;
37828 +}
37829 +
37830 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37831 +                   size_t size, loff_t *pos)
37832 +{
37833 +       ssize_t err;
37834 +
37835 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37836 +               lockdep_off();
37837 +               err = do_xino_fwrite(func, file, buf, size, pos);
37838 +               lockdep_on();
37839 +       } else {
37840 +               lockdep_off();
37841 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
37842 +               lockdep_on();
37843 +       }
37844 +
37845 +       return err;
37846 +}
37847 +
37848 +/* ---------------------------------------------------------------------- */
37849 +
37850 +/*
37851 + * inode number bitmap
37852 + */
37853 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37854 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37855 +{
37856 +       ino_t ino;
37857 +
37858 +       AuDebugOn(bit < 0 || page_bits <= bit);
37859 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37860 +       return ino;
37861 +}
37862 +
37863 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37864 +{
37865 +       AuDebugOn(ino < AUFS_FIRST_INO);
37866 +       ino -= AUFS_FIRST_INO;
37867 +       *pindex = ino / page_bits;
37868 +       *bit = ino % page_bits;
37869 +}
37870 +
37871 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37872 +{
37873 +       int err;
37874 +       loff_t pos;
37875 +       ssize_t sz;
37876 +       struct au_sbinfo *sbinfo;
37877 +       struct file *xib;
37878 +       unsigned long *p;
37879 +
37880 +       sbinfo = au_sbi(sb);
37881 +       MtxMustLock(&sbinfo->si_xib_mtx);
37882 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37883 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37884 +
37885 +       if (pindex == sbinfo->si_xib_last_pindex)
37886 +               return 0;
37887 +
37888 +       xib = sbinfo->si_xib;
37889 +       p = sbinfo->si_xib_buf;
37890 +       pos = sbinfo->si_xib_last_pindex;
37891 +       pos *= PAGE_SIZE;
37892 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37893 +       if (unlikely(sz != PAGE_SIZE))
37894 +               goto out;
37895 +
37896 +       pos = pindex;
37897 +       pos *= PAGE_SIZE;
37898 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37899 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37900 +       else {
37901 +               memset(p, 0, PAGE_SIZE);
37902 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37903 +       }
37904 +       if (sz == PAGE_SIZE) {
37905 +               sbinfo->si_xib_last_pindex = pindex;
37906 +               return 0; /* success */
37907 +       }
37908 +
37909 +out:
37910 +       AuIOErr1("write failed (%zd)\n", sz);
37911 +       err = sz;
37912 +       if (sz >= 0)
37913 +               err = -EIO;
37914 +       return err;
37915 +}
37916 +
37917 +static void au_xib_clear_bit(struct inode *inode)
37918 +{
37919 +       int err, bit;
37920 +       unsigned long pindex;
37921 +       struct super_block *sb;
37922 +       struct au_sbinfo *sbinfo;
37923 +
37924 +       AuDebugOn(inode->i_nlink);
37925 +
37926 +       sb = inode->i_sb;
37927 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37928 +       AuDebugOn(page_bits <= bit);
37929 +       sbinfo = au_sbi(sb);
37930 +       mutex_lock(&sbinfo->si_xib_mtx);
37931 +       err = xib_pindex(sb, pindex);
37932 +       if (!err) {
37933 +               clear_bit(bit, sbinfo->si_xib_buf);
37934 +               sbinfo->si_xib_next_bit = bit;
37935 +       }
37936 +       mutex_unlock(&sbinfo->si_xib_mtx);
37937 +}
37938 +
37939 +/* ---------------------------------------------------------------------- */
37940 +
37941 +/*
37942 + * truncate a xino bitmap file
37943 + */
37944 +
37945 +/* todo: slow */
37946 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37947 +{
37948 +       int err, bit;
37949 +       ssize_t sz;
37950 +       unsigned long pindex;
37951 +       loff_t pos, pend;
37952 +       struct au_sbinfo *sbinfo;
37953 +       vfs_readf_t func;
37954 +       ino_t *ino;
37955 +       unsigned long *p;
37956 +
37957 +       err = 0;
37958 +       sbinfo = au_sbi(sb);
37959 +       MtxMustLock(&sbinfo->si_xib_mtx);
37960 +       p = sbinfo->si_xib_buf;
37961 +       func = sbinfo->si_xread;
37962 +       pend = vfsub_f_size_read(file);
37963 +       pos = 0;
37964 +       while (pos < pend) {
37965 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37966 +               err = sz;
37967 +               if (unlikely(sz <= 0))
37968 +                       goto out;
37969 +
37970 +               err = 0;
37971 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37972 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37973 +                               continue;
37974 +
37975 +                       xib_calc_bit(*ino, &pindex, &bit);
37976 +                       AuDebugOn(page_bits <= bit);
37977 +                       err = xib_pindex(sb, pindex);
37978 +                       if (!err)
37979 +                               set_bit(bit, p);
37980 +                       else
37981 +                               goto out;
37982 +               }
37983 +       }
37984 +
37985 +out:
37986 +       return err;
37987 +}
37988 +
37989 +static int xib_restore(struct super_block *sb)
37990 +{
37991 +       int err, i;
37992 +       unsigned int nfile;
37993 +       aufs_bindex_t bindex, bbot;
37994 +       void *page;
37995 +       struct au_branch *br;
37996 +       struct au_xino *xi;
37997 +       struct file *file;
37998 +
37999 +       err = -ENOMEM;
38000 +       page = (void *)__get_free_page(GFP_NOFS);
38001 +       if (unlikely(!page))
38002 +               goto out;
38003 +
38004 +       err = 0;
38005 +       bbot = au_sbbot(sb);
38006 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38007 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38008 +                       br = au_sbr(sb, bindex);
38009 +                       xi = br->br_xino;
38010 +                       nfile = xi->xi_nfile;
38011 +                       for (i = 0; i < nfile; i++) {
38012 +                               file = au_xino_file(xi, i);
38013 +                               if (file)
38014 +                                       err = do_xib_restore(sb, file, page);
38015 +                       }
38016 +               } else
38017 +                       AuDbg("skip shared b%d\n", bindex);
38018 +       free_page((unsigned long)page);
38019 +
38020 +out:
38021 +       return err;
38022 +}
38023 +
38024 +int au_xib_trunc(struct super_block *sb)
38025 +{
38026 +       int err;
38027 +       ssize_t sz;
38028 +       loff_t pos;
38029 +       struct au_sbinfo *sbinfo;
38030 +       unsigned long *p;
38031 +       struct file *file;
38032 +
38033 +       SiMustWriteLock(sb);
38034 +
38035 +       err = 0;
38036 +       sbinfo = au_sbi(sb);
38037 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38038 +               goto out;
38039 +
38040 +       file = sbinfo->si_xib;
38041 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38042 +               goto out;
38043 +
38044 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38045 +       err = PTR_ERR(file);
38046 +       if (IS_ERR(file))
38047 +               goto out;
38048 +       fput(sbinfo->si_xib);
38049 +       sbinfo->si_xib = file;
38050 +
38051 +       p = sbinfo->si_xib_buf;
38052 +       memset(p, 0, PAGE_SIZE);
38053 +       pos = 0;
38054 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38055 +       if (unlikely(sz != PAGE_SIZE)) {
38056 +               err = sz;
38057 +               AuIOErr("err %d\n", err);
38058 +               if (sz >= 0)
38059 +                       err = -EIO;
38060 +               goto out;
38061 +       }
38062 +
38063 +       mutex_lock(&sbinfo->si_xib_mtx);
38064 +       /* mnt_want_write() is unnecessary here */
38065 +       err = xib_restore(sb);
38066 +       mutex_unlock(&sbinfo->si_xib_mtx);
38067 +
38068 +out:
38069 +       return err;
38070 +}
38071 +
38072 +/* ---------------------------------------------------------------------- */
38073 +
38074 +struct au_xino *au_xino_alloc(unsigned int nfile)
38075 +{
38076 +       struct au_xino *xi;
38077 +
38078 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38079 +       if (unlikely(!xi))
38080 +               goto out;
38081 +       xi->xi_nfile = nfile;
38082 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38083 +       if (unlikely(!xi->xi_file))
38084 +               goto out_free;
38085 +
38086 +       xi->xi_nondir.total = 8; /* initial size */
38087 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38088 +                                     GFP_NOFS);
38089 +       if (unlikely(!xi->xi_nondir.array))
38090 +               goto out_file;
38091 +
38092 +       spin_lock_init(&xi->xi_nondir.spin);
38093 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38094 +       mutex_init(&xi->xi_mtx);
38095 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38096 +       atomic_set(&xi->xi_truncating, 0);
38097 +       kref_init(&xi->xi_kref);
38098 +       goto out; /* success */
38099 +
38100 +out_file:
38101 +       au_kfree_try_rcu(xi->xi_file);
38102 +out_free:
38103 +       au_kfree_rcu(xi);
38104 +       xi = NULL;
38105 +out:
38106 +       return xi;
38107 +}
38108 +
38109 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38110 +{
38111 +       int err;
38112 +       struct au_xino *xi;
38113 +
38114 +       err = 0;
38115 +       xi = au_xino_alloc(idx + 1);
38116 +       if (unlikely(!xi)) {
38117 +               err = -ENOMEM;
38118 +               goto out;
38119 +       }
38120 +
38121 +       if (file)
38122 +               get_file(file);
38123 +       xi->xi_file[idx] = file;
38124 +       AuDebugOn(br->br_xino);
38125 +       br->br_xino = xi;
38126 +
38127 +out:
38128 +       return err;
38129 +}
38130 +
38131 +static void au_xino_release(struct kref *kref)
38132 +{
38133 +       struct au_xino *xi;
38134 +       int i;
38135 +       unsigned long ul;
38136 +       struct hlist_bl_head *hbl;
38137 +       struct hlist_bl_node *pos, *n;
38138 +       struct au_xi_writing *p;
38139 +
38140 +       xi = container_of(kref, struct au_xino, xi_kref);
38141 +       for (i = 0; i < xi->xi_nfile; i++)
38142 +               if (xi->xi_file[i])
38143 +                       fput(xi->xi_file[i]);
38144 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38145 +               AuDebugOn(xi->xi_nondir.array[i]);
38146 +       mutex_destroy(&xi->xi_mtx);
38147 +       hbl = &xi->xi_writing;
38148 +       ul = au_hbl_count(hbl);
38149 +       if (unlikely(ul)) {
38150 +               pr_warn("xi_writing %lu\n", ul);
38151 +               hlist_bl_lock(hbl);
38152 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38153 +                       hlist_bl_del(&p->node);
38154 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38155 +                       kfree(p);
38156 +               }
38157 +               hlist_bl_unlock(hbl);
38158 +       }
38159 +       au_kfree_try_rcu(xi->xi_file);
38160 +       au_kfree_try_rcu(xi->xi_nondir.array);
38161 +       au_kfree_rcu(xi);
38162 +}
38163 +
38164 +int au_xino_put(struct au_branch *br)
38165 +{
38166 +       int ret;
38167 +       struct au_xino *xi;
38168 +
38169 +       ret = 0;
38170 +       xi = br->br_xino;
38171 +       if (xi) {
38172 +               br->br_xino = NULL;
38173 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38174 +       }
38175 +
38176 +       return ret;
38177 +}
38178 +
38179 +/* ---------------------------------------------------------------------- */
38180 +
38181 +/*
38182 + * xino mount option handlers
38183 + */
38184 +
38185 +/* xino bitmap */
38186 +static void xino_clear_xib(struct super_block *sb)
38187 +{
38188 +       struct au_sbinfo *sbinfo;
38189 +
38190 +       SiMustWriteLock(sb);
38191 +
38192 +       sbinfo = au_sbi(sb);
38193 +       /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
38194 +       if (sbinfo->si_xib)
38195 +               fput(sbinfo->si_xib);
38196 +       sbinfo->si_xib = NULL;
38197 +       if (sbinfo->si_xib_buf)
38198 +               free_page((unsigned long)sbinfo->si_xib_buf);
38199 +       sbinfo->si_xib_buf = NULL;
38200 +}
38201 +
38202 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38203 +{
38204 +       int err;
38205 +       loff_t pos;
38206 +       struct au_sbinfo *sbinfo;
38207 +       struct file *file;
38208 +       struct super_block *xi_sb;
38209 +
38210 +       SiMustWriteLock(sb);
38211 +
38212 +       sbinfo = au_sbi(sb);
38213 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38214 +       err = PTR_ERR(file);
38215 +       if (IS_ERR(file))
38216 +               goto out;
38217 +       if (sbinfo->si_xib)
38218 +               fput(sbinfo->si_xib);
38219 +       sbinfo->si_xib = file;
38220 +       sbinfo->si_xread = vfs_readf(file);
38221 +       sbinfo->si_xwrite = vfs_writef(file);
38222 +       xi_sb = file_inode(file)->i_sb;
38223 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38224 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38225 +               err = -EIO;
38226 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38227 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38228 +               goto out_unset;
38229 +       }
38230 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38231 +
38232 +       err = -ENOMEM;
38233 +       if (!sbinfo->si_xib_buf)
38234 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38235 +       if (unlikely(!sbinfo->si_xib_buf))
38236 +               goto out_unset;
38237 +
38238 +       sbinfo->si_xib_last_pindex = 0;
38239 +       sbinfo->si_xib_next_bit = 0;
38240 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38241 +               pos = 0;
38242 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38243 +                                 PAGE_SIZE, &pos);
38244 +               if (unlikely(err != PAGE_SIZE))
38245 +                       goto out_free;
38246 +       }
38247 +       err = 0;
38248 +       goto out; /* success */
38249 +
38250 +out_free:
38251 +       if (sbinfo->si_xib_buf)
38252 +               free_page((unsigned long)sbinfo->si_xib_buf);
38253 +       sbinfo->si_xib_buf = NULL;
38254 +       if (err >= 0)
38255 +               err = -EIO;
38256 +out_unset:
38257 +       fput(sbinfo->si_xib);
38258 +       sbinfo->si_xib = NULL;
38259 +out:
38260 +       AuTraceErr(err);
38261 +       return err;
38262 +}
38263 +
38264 +/* xino for each branch */
38265 +static void xino_clear_br(struct super_block *sb)
38266 +{
38267 +       aufs_bindex_t bindex, bbot;
38268 +       struct au_branch *br;
38269 +
38270 +       bbot = au_sbbot(sb);
38271 +       for (bindex = 0; bindex <= bbot; bindex++) {
38272 +               br = au_sbr(sb, bindex);
38273 +               AuDebugOn(!br);
38274 +               au_xino_put(br);
38275 +       }
38276 +}
38277 +
38278 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38279 +                                 aufs_bindex_t bshared)
38280 +{
38281 +       struct au_branch *brshared;
38282 +
38283 +       brshared = au_sbr(sb, bshared);
38284 +       AuDebugOn(!brshared->br_xino);
38285 +       AuDebugOn(!brshared->br_xino->xi_file);
38286 +       if (br->br_xino != brshared->br_xino) {
38287 +               au_xino_get(brshared);
38288 +               au_xino_put(br);
38289 +               br->br_xino = brshared->br_xino;
38290 +       }
38291 +}
38292 +
38293 +struct au_xino_do_set_br {
38294 +       vfs_writef_t writef;
38295 +       struct au_branch *br;
38296 +       ino_t h_ino;
38297 +       aufs_bindex_t bshared;
38298 +};
38299 +
38300 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38301 +                            struct au_xino_do_set_br *args)
38302 +{
38303 +       int err;
38304 +       struct au_xi_calc calc;
38305 +       struct file *file;
38306 +       struct au_branch *br;
38307 +       struct au_xi_new xinew = {
38308 +               .base = path
38309 +       };
38310 +
38311 +       br = args->br;
38312 +       xinew.xi = br->br_xino;
38313 +       au_xi_calc(sb, args->h_ino, &calc);
38314 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38315 +       if (args->bshared >= 0)
38316 +               /* shared xino */
38317 +               au_xino_set_br_shared(sb, br, args->bshared);
38318 +       else if (!xinew.xi) {
38319 +               /* new xino */
38320 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38321 +               if (unlikely(err))
38322 +                       goto out;
38323 +       }
38324 +
38325 +       /* force re-creating */
38326 +       xinew.xi = br->br_xino;
38327 +       xinew.idx = calc.idx;
38328 +       mutex_lock(&xinew.xi->xi_mtx);
38329 +       file = au_xi_new(sb, &xinew);
38330 +       mutex_unlock(&xinew.xi->xi_mtx);
38331 +       err = PTR_ERR(file);
38332 +       if (IS_ERR(file))
38333 +               goto out;
38334 +       AuDebugOn(!file);
38335 +
38336 +       err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38337 +       if (unlikely(err))
38338 +               au_xino_put(br);
38339 +
38340 +out:
38341 +       AuTraceErr(err);
38342 +       return err;
38343 +}
38344 +
38345 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38346 +{
38347 +       int err;
38348 +       aufs_bindex_t bindex, bbot;
38349 +       struct au_xino_do_set_br args;
38350 +       struct inode *inode;
38351 +
38352 +       SiMustWriteLock(sb);
38353 +
38354 +       bbot = au_sbbot(sb);
38355 +       inode = d_inode(sb->s_root);
38356 +       args.writef = au_sbi(sb)->si_xwrite;
38357 +       for (bindex = 0; bindex <= bbot; bindex++) {
38358 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38359 +               args.br = au_sbr(sb, bindex);
38360 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38361 +               err = au_xino_do_set_br(sb, path, &args);
38362 +               if (unlikely(err))
38363 +                       break;
38364 +       }
38365 +
38366 +       AuTraceErr(err);
38367 +       return err;
38368 +}
38369 +
38370 +void au_xino_clr(struct super_block *sb)
38371 +{
38372 +       struct au_sbinfo *sbinfo;
38373 +
38374 +       au_xigen_clr(sb);
38375 +       xino_clear_xib(sb);
38376 +       xino_clear_br(sb);
38377 +       dbgaufs_brs_del(sb, 0);
38378 +       sbinfo = au_sbi(sb);
38379 +       /* lvalue, do not call au_mntflags() */
38380 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38381 +}
38382 +
38383 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38384 +{
38385 +       int err, skip;
38386 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38387 +       struct qstr *dname, *cur_name;
38388 +       struct file *cur_xino;
38389 +       struct au_sbinfo *sbinfo;
38390 +       struct path *path, *cur_path;
38391 +
38392 +       SiMustWriteLock(sb);
38393 +
38394 +       err = 0;
38395 +       sbinfo = au_sbi(sb);
38396 +       path = &xiopt->file->f_path;
38397 +       dentry = path->dentry;
38398 +       parent = dget_parent(dentry);
38399 +       if (remount) {
38400 +               skip = 0;
38401 +               cur_xino = sbinfo->si_xib;
38402 +               if (cur_xino) {
38403 +                       cur_path = &cur_xino->f_path;
38404 +                       cur_dentry = cur_path->dentry;
38405 +                       cur_parent = dget_parent(cur_dentry);
38406 +                       cur_name = &cur_dentry->d_name;
38407 +                       dname = &dentry->d_name;
38408 +                       skip = (cur_parent == parent
38409 +                               && au_qstreq(dname, cur_name));
38410 +                       dput(cur_parent);
38411 +               }
38412 +               if (skip)
38413 +                       goto out;
38414 +       }
38415 +
38416 +       au_opt_set(sbinfo->si_mntflags, XINO);
38417 +       err = au_xino_set_xib(sb, path);
38418 +       /* si_x{read,write} are set */
38419 +       if (!err)
38420 +               err = au_xigen_set(sb, path);
38421 +       if (!err)
38422 +               err = au_xino_set_br(sb, path);
38423 +       if (!err) {
38424 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38425 +               goto out; /* success */
38426 +       }
38427 +
38428 +       /* reset all */
38429 +       AuIOErr("failed setting xino(%d).\n", err);
38430 +       au_xino_clr(sb);
38431 +
38432 +out:
38433 +       dput(parent);
38434 +       return err;
38435 +}
38436 +
38437 +/*
38438 + * create a xinofile at the default place/path.
38439 + */
38440 +struct file *au_xino_def(struct super_block *sb)
38441 +{
38442 +       struct file *file;
38443 +       char *page, *p;
38444 +       struct au_branch *br;
38445 +       struct super_block *h_sb;
38446 +       struct path path;
38447 +       aufs_bindex_t bbot, bindex, bwr;
38448 +
38449 +       br = NULL;
38450 +       bbot = au_sbbot(sb);
38451 +       bwr = -1;
38452 +       for (bindex = 0; bindex <= bbot; bindex++) {
38453 +               br = au_sbr(sb, bindex);
38454 +               if (au_br_writable(br->br_perm)
38455 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38456 +                       bwr = bindex;
38457 +                       break;
38458 +               }
38459 +       }
38460 +
38461 +       if (bwr >= 0) {
38462 +               file = ERR_PTR(-ENOMEM);
38463 +               page = (void *)__get_free_page(GFP_NOFS);
38464 +               if (unlikely(!page))
38465 +                       goto out;
38466 +               path.mnt = au_br_mnt(br);
38467 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38468 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38469 +               file = (void *)p;
38470 +               if (!IS_ERR(p)) {
38471 +                       strcat(p, "/" AUFS_XINO_FNAME);
38472 +                       AuDbg("%s\n", p);
38473 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38474 +               }
38475 +               free_page((unsigned long)page);
38476 +       } else {
38477 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38478 +                                     /*wbrtop*/0);
38479 +               if (IS_ERR(file))
38480 +                       goto out;
38481 +               h_sb = file->f_path.dentry->d_sb;
38482 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38483 +                       pr_err("xino doesn't support %s(%s)\n",
38484 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38485 +                       fput(file);
38486 +                       file = ERR_PTR(-EINVAL);
38487 +               }
38488 +       }
38489 +
38490 +out:
38491 +       return file;
38492 +}
38493 +
38494 +/* ---------------------------------------------------------------------- */
38495 +
38496 +/*
38497 + * initialize the xinofile for the specified branch @br
38498 + * at the place/path where @base_file indicates.
38499 + * test whether another branch is on the same filesystem or not,
38500 + * if found then share the xinofile with another branch.
38501 + */
38502 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38503 +                   struct path *base)
38504 +{
38505 +       int err;
38506 +       struct au_xino_do_set_br args = {
38507 +               .h_ino  = h_ino,
38508 +               .br     = br
38509 +       };
38510 +
38511 +       args.writef = au_sbi(sb)->si_xwrite;
38512 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38513 +                                      au_br_sb(br));
38514 +       err = au_xino_do_set_br(sb, base, &args);
38515 +       if (unlikely(err))
38516 +               au_xino_put(br);
38517 +
38518 +       return err;
38519 +}
38520 +
38521 +/* ---------------------------------------------------------------------- */
38522 +
38523 +/*
38524 + * get an unused inode number from bitmap
38525 + */
38526 +ino_t au_xino_new_ino(struct super_block *sb)
38527 +{
38528 +       ino_t ino;
38529 +       unsigned long *p, pindex, ul, pend;
38530 +       struct au_sbinfo *sbinfo;
38531 +       struct file *file;
38532 +       int free_bit, err;
38533 +
38534 +       if (!au_opt_test(au_mntflags(sb), XINO))
38535 +               return iunique(sb, AUFS_FIRST_INO);
38536 +
38537 +       sbinfo = au_sbi(sb);
38538 +       mutex_lock(&sbinfo->si_xib_mtx);
38539 +       p = sbinfo->si_xib_buf;
38540 +       free_bit = sbinfo->si_xib_next_bit;
38541 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38542 +               goto out; /* success */
38543 +       free_bit = find_first_zero_bit(p, page_bits);
38544 +       if (free_bit < page_bits)
38545 +               goto out; /* success */
38546 +
38547 +       pindex = sbinfo->si_xib_last_pindex;
38548 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38549 +               err = xib_pindex(sb, ul);
38550 +               if (unlikely(err))
38551 +                       goto out_err;
38552 +               free_bit = find_first_zero_bit(p, page_bits);
38553 +               if (free_bit < page_bits)
38554 +                       goto out; /* success */
38555 +       }
38556 +
38557 +       file = sbinfo->si_xib;
38558 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38559 +       for (ul = pindex + 1; ul <= pend; ul++) {
38560 +               err = xib_pindex(sb, ul);
38561 +               if (unlikely(err))
38562 +                       goto out_err;
38563 +               free_bit = find_first_zero_bit(p, page_bits);
38564 +               if (free_bit < page_bits)
38565 +                       goto out; /* success */
38566 +       }
38567 +       BUG();
38568 +
38569 +out:
38570 +       set_bit(free_bit, p);
38571 +       sbinfo->si_xib_next_bit = free_bit + 1;
38572 +       pindex = sbinfo->si_xib_last_pindex;
38573 +       mutex_unlock(&sbinfo->si_xib_mtx);
38574 +       ino = xib_calc_ino(pindex, free_bit);
38575 +       AuDbg("i%lu\n", (unsigned long)ino);
38576 +       return ino;
38577 +out_err:
38578 +       mutex_unlock(&sbinfo->si_xib_mtx);
38579 +       AuDbg("i0\n");
38580 +       return 0;
38581 +}
38582 +
38583 +/* for s_op->delete_inode() */
38584 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38585 +{
38586 +       int err;
38587 +       unsigned int mnt_flags;
38588 +       aufs_bindex_t bindex, bbot, bi;
38589 +       unsigned char try_trunc;
38590 +       struct au_iinfo *iinfo;
38591 +       struct super_block *sb;
38592 +       struct au_hinode *hi;
38593 +       struct inode *h_inode;
38594 +       struct au_branch *br;
38595 +       vfs_writef_t xwrite;
38596 +       struct au_xi_calc calc;
38597 +       struct file *file;
38598 +
38599 +       AuDebugOn(au_is_bad_inode(inode));
38600 +
38601 +       sb = inode->i_sb;
38602 +       mnt_flags = au_mntflags(sb);
38603 +       if (!au_opt_test(mnt_flags, XINO)
38604 +           || inode->i_ino == AUFS_ROOT_INO)
38605 +               return;
38606 +
38607 +       if (unlinked) {
38608 +               au_xigen_inc(inode);
38609 +               au_xib_clear_bit(inode);
38610 +       }
38611 +
38612 +       iinfo = au_ii(inode);
38613 +       bindex = iinfo->ii_btop;
38614 +       if (bindex < 0)
38615 +               return;
38616 +
38617 +       xwrite = au_sbi(sb)->si_xwrite;
38618 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38619 +       hi = au_hinode(iinfo, bindex);
38620 +       bbot = iinfo->ii_bbot;
38621 +       for (; bindex <= bbot; bindex++, hi++) {
38622 +               h_inode = hi->hi_inode;
38623 +               if (!h_inode
38624 +                   || (!unlinked && h_inode->i_nlink))
38625 +                       continue;
38626 +
38627 +               /* inode may not be revalidated */
38628 +               bi = au_br_index(sb, hi->hi_id);
38629 +               if (bi < 0)
38630 +                       continue;
38631 +
38632 +               br = au_sbr(sb, bi);
38633 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38634 +               file = au_xino_file(br->br_xino, calc.idx);
38635 +               if (IS_ERR_OR_NULL(file))
38636 +                       continue;
38637 +
38638 +               err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
38639 +               if (!err && try_trunc
38640 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38641 +                       xino_try_trunc(sb, br);
38642 +       }
38643 +}
38644 +
38645 +/* ---------------------------------------------------------------------- */
38646 +
38647 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38648 +{
38649 +       int found, total, i;
38650 +
38651 +       found = -1;
38652 +       total = xi->xi_nondir.total;
38653 +       for (i = 0; i < total; i++) {
38654 +               if (xi->xi_nondir.array[i] != h_ino)
38655 +                       continue;
38656 +               found = i;
38657 +               break;
38658 +       }
38659 +
38660 +       return found;
38661 +}
38662 +
38663 +static int au_xinondir_expand(struct au_xino *xi)
38664 +{
38665 +       int err, sz;
38666 +       ino_t *p;
38667 +
38668 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38669 +
38670 +       err = -ENOMEM;
38671 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38672 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38673 +               goto out;
38674 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38675 +                        /*may_shrink*/0);
38676 +       if (p) {
38677 +               xi->xi_nondir.array = p;
38678 +               xi->xi_nondir.total <<= 1;
38679 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38680 +               err = 0;
38681 +       }
38682 +
38683 +out:
38684 +       return err;
38685 +}
38686 +
38687 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38688 +                      ino_t h_ino, int idx)
38689 +{
38690 +       struct au_xino *xi;
38691 +
38692 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38693 +       xi = au_sbr(sb, bindex)->br_xino;
38694 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38695 +
38696 +       spin_lock(&xi->xi_nondir.spin);
38697 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38698 +       xi->xi_nondir.array[idx] = 0;
38699 +       spin_unlock(&xi->xi_nondir.spin);
38700 +       wake_up_all(&xi->xi_nondir.wqh);
38701 +}
38702 +
38703 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38704 +                     int *idx)
38705 +{
38706 +       int err, found, empty;
38707 +       struct au_xino *xi;
38708 +
38709 +       err = 0;
38710 +       *idx = -1;
38711 +       if (!au_opt_test(au_mntflags(sb), XINO))
38712 +               goto out; /* no xino */
38713 +
38714 +       xi = au_sbr(sb, bindex)->br_xino;
38715 +
38716 +again:
38717 +       spin_lock(&xi->xi_nondir.spin);
38718 +       found = au_xinondir_find(xi, h_ino);
38719 +       if (found == -1) {
38720 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38721 +               if (empty == -1) {
38722 +                       empty = xi->xi_nondir.total;
38723 +                       err = au_xinondir_expand(xi);
38724 +                       if (unlikely(err))
38725 +                               goto out_unlock;
38726 +               }
38727 +               xi->xi_nondir.array[empty] = h_ino;
38728 +               *idx = empty;
38729 +       } else {
38730 +               spin_unlock(&xi->xi_nondir.spin);
38731 +               wait_event(xi->xi_nondir.wqh,
38732 +                          xi->xi_nondir.array[found] != h_ino);
38733 +               goto again;
38734 +       }
38735 +
38736 +out_unlock:
38737 +       spin_unlock(&xi->xi_nondir.spin);
38738 +out:
38739 +       return err;
38740 +}
38741 +
38742 +/* ---------------------------------------------------------------------- */
38743 +
38744 +int au_xino_path(struct seq_file *seq, struct file *file)
38745 +{
38746 +       int err;
38747 +
38748 +       err = au_seq_path(seq, &file->f_path);
38749 +       if (unlikely(err))
38750 +               goto out;
38751 +
38752 +#define Deleted "\\040(deleted)"
38753 +       seq->count -= sizeof(Deleted) - 1;
38754 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38755 +                        sizeof(Deleted) - 1));
38756 +#undef Deleted
38757 +
38758 +out:
38759 +       return err;
38760 +}
38761 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38762 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38763 +++ linux/include/uapi/linux/aufs_type.h        2020-08-03 09:14:46.095748745 +0200
38764 @@ -0,0 +1,452 @@
38765 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38766 +/*
38767 + * Copyright (C) 2005-2020 Junjiro R. Okajima
38768 + *
38769 + * This program, aufs is free software; you can redistribute it and/or modify
38770 + * it under the terms of the GNU General Public License as published by
38771 + * the Free Software Foundation; either version 2 of the License, or
38772 + * (at your option) any later version.
38773 + *
38774 + * This program is distributed in the hope that it will be useful,
38775 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38776 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38777 + * GNU General Public License for more details.
38778 + *
38779 + * You should have received a copy of the GNU General Public License
38780 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38781 + */
38782 +
38783 +#ifndef __AUFS_TYPE_H__
38784 +#define __AUFS_TYPE_H__
38785 +
38786 +#define AUFS_NAME      "aufs"
38787 +
38788 +#ifdef __KERNEL__
38789 +/*
38790 + * define it before including all other headers.
38791 + * sched.h may use pr_* macros before defining "current", so define the
38792 + * no-current version first, and re-define later.
38793 + */
38794 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38795 +#include <linux/sched.h>
38796 +#undef pr_fmt
38797 +#define pr_fmt(fmt) \
38798 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38799 +               (int)sizeof(current->comm), current->comm, current->pid
38800 +#include <linux/limits.h>
38801 +#else
38802 +#include <stdint.h>
38803 +#include <sys/types.h>
38804 +#include <limits.h>
38805 +#endif /* __KERNEL__ */
38806 +
38807 +#define AUFS_VERSION   "5.x-rcN-20200622"
38808 +
38809 +/* todo? move this to linux-2.6.19/include/magic.h */
38810 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38811 +
38812 +/* ---------------------------------------------------------------------- */
38813 +
38814 +#ifdef __KERNEL__
38815 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38816 +typedef int8_t aufs_bindex_t;
38817 +#define AUFS_BRANCH_MAX 127
38818 +#else
38819 +typedef int16_t aufs_bindex_t;
38820 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38821 +#define AUFS_BRANCH_MAX 511
38822 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38823 +#define AUFS_BRANCH_MAX 1023
38824 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38825 +#define AUFS_BRANCH_MAX 32767
38826 +#endif
38827 +#endif
38828 +
38829 +#ifndef AUFS_BRANCH_MAX
38830 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38831 +#endif
38832 +#endif /* __KERNEL__ */
38833 +
38834 +/* ---------------------------------------------------------------------- */
38835 +
38836 +#define AUFS_FSTYPE            AUFS_NAME
38837 +
38838 +#define AUFS_ROOT_INO          2
38839 +#define AUFS_FIRST_INO         11
38840 +
38841 +#define AUFS_WH_PFX            ".wh."
38842 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38843 +#define AUFS_WH_TMP_LEN                4
38844 +/* a limit for rmdir/rename a dir and copyup */
38845 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38846 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38847 +                               - 1                     /* dot */\
38848 +                               - AUFS_WH_TMP_LEN)      /* hex */
38849 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38850 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38851 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38852 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38853 +#define AUFS_DIRWH_DEF         3
38854 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38855 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38856 +#define AUFS_RDBLK_DEF         512 /* bytes */
38857 +#define AUFS_RDHASH_DEF                32
38858 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38859 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38860 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38861 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38862 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38863 +
38864 +/* pseudo-link maintenace under /proc */
38865 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38866 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38867 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38868 +
38869 +/* dirren, renamed dir */
38870 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38871 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38872 +/* whiteouted doubly */
38873 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38874 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38875 +
38876 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38877 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38878 +
38879 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38880 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38881 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38882 +
38883 +/* doubly whiteouted */
38884 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38885 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38886 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38887 +
38888 +/* branch permissions and attributes */
38889 +#define AUFS_BRPERM_RW         "rw"
38890 +#define AUFS_BRPERM_RO         "ro"
38891 +#define AUFS_BRPERM_RR         "rr"
38892 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38893 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38894 +#define AUFS_BRATTR_FHSM       "fhsm"
38895 +#define AUFS_BRATTR_UNPIN      "unpin"
38896 +#define AUFS_BRATTR_ICEX       "icex"
38897 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38898 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38899 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38900 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38901 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38902 +#define AUFS_BRRATTR_WH                "wh"
38903 +#define AUFS_BRWATTR_NLWH      "nolwh"
38904 +#define AUFS_BRWATTR_MOO       "moo"
38905 +
38906 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38907 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38908 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38909 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38910 +
38911 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38912 +#define AuBrAttr_COO_ALL       (1 << 4)
38913 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38914 +
38915 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38916 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38917 +                                                  branch. meaningless since
38918 +                                                  linux-3.18-rc1 */
38919 +
38920 +/* ignore error in copying XATTR */
38921 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38922 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38923 +#define AuBrAttr_ICEX_TR       (1 << 9)
38924 +#define AuBrAttr_ICEX_USR      (1 << 10)
38925 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38926 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38927 +                                | AuBrAttr_ICEX_SYS    \
38928 +                                | AuBrAttr_ICEX_TR     \
38929 +                                | AuBrAttr_ICEX_USR    \
38930 +                                | AuBrAttr_ICEX_OTH)
38931 +
38932 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38933 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38934 +
38935 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38936 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38937 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38938 +
38939 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38940 +
38941 +/* #warning test userspace */
38942 +#ifdef __KERNEL__
38943 +#ifndef CONFIG_AUFS_FHSM
38944 +#undef AuBrAttr_FHSM
38945 +#define AuBrAttr_FHSM          0
38946 +#endif
38947 +#ifndef CONFIG_AUFS_XATTR
38948 +#undef AuBrAttr_ICEX
38949 +#define AuBrAttr_ICEX          0
38950 +#undef AuBrAttr_ICEX_SEC
38951 +#define AuBrAttr_ICEX_SEC      0
38952 +#undef AuBrAttr_ICEX_SYS
38953 +#define AuBrAttr_ICEX_SYS      0
38954 +#undef AuBrAttr_ICEX_TR
38955 +#define AuBrAttr_ICEX_TR       0
38956 +#undef AuBrAttr_ICEX_USR
38957 +#define AuBrAttr_ICEX_USR      0
38958 +#undef AuBrAttr_ICEX_OTH
38959 +#define AuBrAttr_ICEX_OTH      0
38960 +#endif
38961 +#endif
38962 +
38963 +/* the longest combination */
38964 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38965 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38966 +                              "+" AUFS_BRATTR_COO_REG          \
38967 +                              "+" AUFS_BRATTR_FHSM             \
38968 +                              "+" AUFS_BRATTR_UNPIN            \
38969 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38970 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38971 +                              "+" AUFS_BRATTR_ICEX_USR         \
38972 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38973 +                              "+" AUFS_BRWATTR_NLWH)
38974 +
38975 +typedef struct {
38976 +       char a[AuBrPermStrSz];
38977 +} au_br_perm_str_t;
38978 +
38979 +static inline int au_br_writable(int brperm)
38980 +{
38981 +       return brperm & AuBrPerm_RW;
38982 +}
38983 +
38984 +static inline int au_br_whable(int brperm)
38985 +{
38986 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38987 +}
38988 +
38989 +static inline int au_br_wh_linkable(int brperm)
38990 +{
38991 +       return !(brperm & AuBrWAttr_NoLinkWH);
38992 +}
38993 +
38994 +static inline int au_br_cmoo(int brperm)
38995 +{
38996 +       return brperm & AuBrAttr_CMOO_Mask;
38997 +}
38998 +
38999 +static inline int au_br_fhsm(int brperm)
39000 +{
39001 +       return brperm & AuBrAttr_FHSM;
39002 +}
39003 +
39004 +/* ---------------------------------------------------------------------- */
39005 +
39006 +/* ioctl */
39007 +enum {
39008 +       /* readdir in userspace */
39009 +       AuCtl_RDU,
39010 +       AuCtl_RDU_INO,
39011 +
39012 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39013 +       AuCtl_IBUSY,    /* busy inode */
39014 +       AuCtl_MVDOWN,   /* move-down */
39015 +       AuCtl_BR,       /* info about branches */
39016 +       AuCtl_FHSM_FD   /* connection for fhsm */
39017 +};
39018 +
39019 +/* borrowed from linux/include/linux/kernel.h */
39020 +#ifndef ALIGN
39021 +#ifdef _GNU_SOURCE
39022 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39023 +#else
39024 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39025 +#endif
39026 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39027 +#endif
39028 +
39029 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39030 +#ifndef __aligned
39031 +#define __aligned(x)                   __attribute__((aligned(x)))
39032 +#endif
39033 +
39034 +#ifdef __KERNEL__
39035 +#ifndef __packed
39036 +#define __packed                       __attribute__((packed))
39037 +#endif
39038 +#endif
39039 +
39040 +struct au_rdu_cookie {
39041 +       uint64_t        h_pos;
39042 +       int16_t         bindex;
39043 +       uint8_t         flags;
39044 +       uint8_t         pad;
39045 +       uint32_t        generation;
39046 +} __aligned(8);
39047 +
39048 +struct au_rdu_ent {
39049 +       uint64_t        ino;
39050 +       int16_t         bindex;
39051 +       uint8_t         type;
39052 +       uint8_t         nlen;
39053 +       uint8_t         wh;
39054 +       char            name[];
39055 +} __aligned(8);
39056 +
39057 +static inline int au_rdu_len(int nlen)
39058 +{
39059 +       /* include the terminating NULL */
39060 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39061 +                    sizeof(uint64_t));
39062 +}
39063 +
39064 +union au_rdu_ent_ul {
39065 +       struct au_rdu_ent __user        *e;
39066 +       uint64_t                        ul;
39067 +};
39068 +
39069 +enum {
39070 +       AufsCtlRduV_SZ,
39071 +       AufsCtlRduV_End
39072 +};
39073 +
39074 +struct aufs_rdu {
39075 +       /* input */
39076 +       union {
39077 +               uint64_t        sz;     /* AuCtl_RDU */
39078 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39079 +       };
39080 +       union au_rdu_ent_ul     ent;
39081 +       uint16_t                verify[AufsCtlRduV_End];
39082 +
39083 +       /* input/output */
39084 +       uint32_t                blk;
39085 +
39086 +       /* output */
39087 +       union au_rdu_ent_ul     tail;
39088 +       /* number of entries which were added in a single call */
39089 +       uint64_t                rent;
39090 +       uint8_t                 full;
39091 +       uint8_t                 shwh;
39092 +
39093 +       struct au_rdu_cookie    cookie;
39094 +} __aligned(8);
39095 +
39096 +/* ---------------------------------------------------------------------- */
39097 +
39098 +/* dirren. the branch is identified by the filename who contains this */
39099 +struct au_drinfo {
39100 +       uint64_t ino;
39101 +       union {
39102 +               uint8_t oldnamelen;
39103 +               uint64_t _padding;
39104 +       };
39105 +       uint8_t oldname[];
39106 +} __aligned(8);
39107 +
39108 +struct au_drinfo_fdata {
39109 +       uint32_t magic;
39110 +       struct au_drinfo drinfo;
39111 +} __aligned(8);
39112 +
39113 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39114 +/* future */
39115 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39116 +
39117 +/* ---------------------------------------------------------------------- */
39118 +
39119 +struct aufs_wbr_fd {
39120 +       uint32_t        oflags;
39121 +       int16_t         brid;
39122 +} __aligned(8);
39123 +
39124 +/* ---------------------------------------------------------------------- */
39125 +
39126 +struct aufs_ibusy {
39127 +       uint64_t        ino, h_ino;
39128 +       int16_t         bindex;
39129 +} __aligned(8);
39130 +
39131 +/* ---------------------------------------------------------------------- */
39132 +
39133 +/* error code for move-down */
39134 +/* the actual message strings are implemented in aufs-util.git */
39135 +enum {
39136 +       EAU_MVDOWN_OPAQUE = 1,
39137 +       EAU_MVDOWN_WHITEOUT,
39138 +       EAU_MVDOWN_UPPER,
39139 +       EAU_MVDOWN_BOTTOM,
39140 +       EAU_MVDOWN_NOUPPER,
39141 +       EAU_MVDOWN_NOLOWERBR,
39142 +       EAU_Last
39143 +};
39144 +
39145 +/* flags for move-down */
39146 +#define AUFS_MVDOWN_DMSG       1
39147 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39148 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39149 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39150 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39151 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39152 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39153 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39154 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39155 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39156 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39157 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39158 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39159 +
39160 +/* index for move-down */
39161 +enum {
39162 +       AUFS_MVDOWN_UPPER,
39163 +       AUFS_MVDOWN_LOWER,
39164 +       AUFS_MVDOWN_NARRAY
39165 +};
39166 +
39167 +/*
39168 + * additional info of move-down
39169 + * number of free blocks and inodes.
39170 + * subset of struct kstatfs, but smaller and always 64bit.
39171 + */
39172 +struct aufs_stfs {
39173 +       uint64_t        f_blocks;
39174 +       uint64_t        f_bavail;
39175 +       uint64_t        f_files;
39176 +       uint64_t        f_ffree;
39177 +};
39178 +
39179 +struct aufs_stbr {
39180 +       int16_t                 brid;   /* optional input */
39181 +       int16_t                 bindex; /* output */
39182 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39183 +} __aligned(8);
39184 +
39185 +struct aufs_mvdown {
39186 +       uint32_t                flags;                  /* input/output */
39187 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39188 +       int8_t                  au_errno;               /* output */
39189 +} __aligned(8);
39190 +
39191 +/* ---------------------------------------------------------------------- */
39192 +
39193 +union aufs_brinfo {
39194 +       /* PATH_MAX may differ between kernel-space and user-space */
39195 +       char    _spacer[4096];
39196 +       struct {
39197 +               int16_t id;
39198 +               int     perm;
39199 +               char    path[];
39200 +       };
39201 +} __aligned(8);
39202 +
39203 +/* ---------------------------------------------------------------------- */
39204 +
39205 +#define AuCtlType              'A'
39206 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39207 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39208 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39209 +                                    struct aufs_wbr_fd)
39210 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39211 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39212 +                                     struct aufs_mvdown)
39213 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39214 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39215 +
39216 +#endif /* __AUFS_TYPE_H__ */
39217 SPDX-License-Identifier: GPL-2.0
39218 aufs5.x-rcN loopback patch
39219
39220 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39221 index 0479902a0cd50..758c8ee4d4e6b 100644
39222 --- a/drivers/block/loop.c
39223 +++ b/drivers/block/loop.c
39224 @@ -638,6 +638,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39225                         lo->use_dio);
39226  }
39227  
39228 +static struct file *loop_real_file(struct file *file)
39229 +{
39230 +       struct file *f = NULL;
39231 +
39232 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39233 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39234 +       return f;
39235 +}
39236 +
39237  static void loop_reread_partitions(struct loop_device *lo,
39238                                    struct block_device *bdev)
39239  {
39240 @@ -693,6 +702,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39241                           unsigned int arg)
39242  {
39243         struct file     *file = NULL, *old_file;
39244 +       struct file     *f, *virt_file = NULL, *old_virt_file;
39245         int             error;
39246         bool            partscan;
39247  
39248 @@ -712,12 +722,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39249         file = fget(arg);
39250         if (!file)
39251                 goto out_err;
39252 +       f = loop_real_file(file);
39253 +       if (f) {
39254 +               virt_file = file;
39255 +               file = f;
39256 +               get_file(file);
39257 +       }
39258  
39259         error = loop_validate_file(file, bdev);
39260         if (error)
39261                 goto out_err;
39262  
39263         old_file = lo->lo_backing_file;
39264 +       old_virt_file = lo->lo_backing_virt_file;
39265  
39266         error = -EINVAL;
39267  
39268 @@ -729,6 +746,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39269         blk_mq_freeze_queue(lo->lo_queue);
39270         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39271         lo->lo_backing_file = file;
39272 +       lo->lo_backing_virt_file = virt_file;
39273         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39274         mapping_set_gfp_mask(file->f_mapping,
39275                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39276 @@ -742,6 +760,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39277          * dependency.
39278          */
39279         fput(old_file);
39280 +       if (old_virt_file)
39281 +               fput(old_virt_file);
39282         if (partscan)
39283                 loop_reread_partitions(lo, bdev);
39284         return 0;
39285 @@ -750,6 +770,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39286         mutex_unlock(&loop_ctl_mutex);
39287         if (file)
39288                 fput(file);
39289 +       if (virt_file)
39290 +               fput(virt_file);
39291         return error;
39292  }
39293  
39294 @@ -973,7 +995,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode
39295                           struct block_device *bdev,
39296                           const struct loop_config *config)
39297  {
39298 -       struct file     *file;
39299 +       struct file     *file, *f, *virt_file = NULL;
39300         struct inode    *inode;
39301         struct address_space *mapping;
39302         struct block_device *claimed_bdev = NULL;
39303 @@ -989,6 +1011,12 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39304         file = fget(arg);
39305         if (!file)
39306                 goto out;
39307 +       f = loop_real_file(file);
39308 +       if (f) {
39309 +               virt_file = file;
39310 +               file = f;
39311 +               get_file(file);
39312 +       }
39313  
39314         /*
39315          * If we don't hold exclusive handle for the device, upgrade to it
39316 @@ -1037,6 +1065,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode
39317         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39318         lo->lo_device = bdev;
39319         lo->lo_backing_file = file;
39320 +       lo->lo_backing_virt_file = virt_file;
39321         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39322         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39323  
39324 @@ -1090,6 +1119,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode
39325                 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
39326  out_putf:
39327         fput(file);
39328 +       if (virt_file)
39329 +               fput(virt_file);
39330  out:
39331         /* This is safe: open() is still holding a reference. */
39332         module_put(THIS_MODULE);
39333 @@ -1136,6 +1167,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
39334  static int __loop_clr_fd(struct loop_device *lo, bool release)
39335  {
39336         struct file *filp = NULL;
39337 +       struct file *virt_filp = lo->lo_backing_virt_file;
39338         gfp_t gfp = lo->old_gfp_mask;
39339         struct block_device *bdev = lo->lo_device;
39340         int err = 0;
39341 @@ -1159,6 +1191,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39342  
39343         spin_lock_irq(&lo->lo_lock);
39344         lo->lo_backing_file = NULL;
39345 +       lo->lo_backing_virt_file = NULL;
39346         spin_unlock_irq(&lo->lo_lock);
39347  
39348         loop_release_xfer(lo);
39349 @@ -1242,6 +1275,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39350          */
39351         if (filp)
39352                 fput(filp);
39353 +       if (virt_filp)
39354 +               fput(virt_filp);
39355         return err;
39356  }
39357  
39358 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39359 index af75a5ee40944..1d847cb194ff6 100644
39360 --- a/drivers/block/loop.h
39361 +++ b/drivers/block/loop.h
39362 @@ -46,7 +46,7 @@ struct loop_device {
39363         int             (*ioctl)(struct loop_device *, int cmd, 
39364                                  unsigned long arg); 
39365  
39366 -       struct file *   lo_backing_file;
39367 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39368         struct block_device *lo_device;
39369         void            *key_data; 
39370  
39371 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39372 index 6fb4a4ed8cc7f..ba9a959f2db27 100644
39373 --- a/fs/aufs/f_op.c
39374 +++ b/fs/aufs/f_op.c
39375 @@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39376         if (IS_ERR(h_file))
39377                 goto out;
39378  
39379 -       if (au_test_loopback_kthread()) {
39380 +       if (0 && au_test_loopback_kthread()) {
39381                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39382                 if (file->f_mapping != h_file->f_mapping) {
39383                         file->f_mapping = h_file->f_mapping;
39384 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39385 index a8b63acc62045..9d97c3af5686a 100644
39386 --- a/fs/aufs/loop.c
39387 +++ b/fs/aufs/loop.c
39388 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39389                 symbol_put(loop_backing_file);
39390         au_kfree_try_rcu(au_warn_loopback_array);
39391  }
39392 +
39393 +/* ---------------------------------------------------------------------- */
39394 +
39395 +/* support the loopback block device insude aufs */
39396 +
39397 +struct file *aufs_real_loop(struct file *file)
39398 +{
39399 +       struct file *f;
39400 +
39401 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39402 +       fi_read_lock(file);
39403 +       f = au_hf_top(file);
39404 +       fi_read_unlock(file);
39405 +       AuDebugOn(!f);
39406 +       return f;
39407 +}
39408 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39409 index 94f4f80ae33bf..ca1194354aff4 100644
39410 --- a/fs/aufs/loop.h
39411 +++ b/fs/aufs/loop.h
39412 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39413  
39414  int au_loopback_init(void);
39415  void au_loopback_fin(void);
39416 +
39417 +struct file *aufs_real_loop(struct file *file);
39418  #else
39419  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39420  
39421 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39422  
39423  AuStubInt0(au_loopback_init, void)
39424  AuStubVoid(au_loopback_fin, void)
39425 +
39426 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39427  #endif /* BLK_DEV_LOOP */
39428  
39429  #endif /* __KERNEL__ */
39430 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39431 index 589dd01220201..801e0a7faec59 100644
39432 --- a/fs/aufs/super.c
39433 +++ b/fs/aufs/super.c
39434 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39435         .statfs         = aufs_statfs,
39436         .put_super      = aufs_put_super,
39437         .sync_fs        = aufs_sync_fs,
39438 -       .remount_fs     = aufs_remount_fs
39439 +       .remount_fs     = aufs_remount_fs,
39440 +#ifdef CONFIG_AUFS_BDEV_LOOP
39441 +       .real_loop      = aufs_real_loop
39442 +#endif
39443  };
39444  
39445  /* ---------------------------------------------------------------------- */
39446 diff --git a/include/linux/fs.h b/include/linux/fs.h
39447 index 916efd7e612b3..68f536f42b2a0 100644
39448 --- a/include/linux/fs.h
39449 +++ b/include/linux/fs.h
39450 @@ -1985,6 +1985,10 @@ struct super_operations {
39451                                   struct shrink_control *);
39452         long (*free_cached_objects)(struct super_block *,
39453                                     struct shrink_control *);
39454 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39455 +       /* and aufs */
39456 +       struct file *(*real_loop)(struct file *);
39457 +#endif
39458  };
39459  
39460  /*
This page took 3.631708 seconds and 3 git commands to generate.