]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- up to 4.18.5
[packages/kernel.git] / kernel-aufs4.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs4.x-rcN kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index ac474a6..284cee9 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -255,6 +255,7 @@ source "fs/pstore/Kconfig"
9  source "fs/sysv/Kconfig"
10  source "fs/ufs/Kconfig"
11  source "fs/exofs/Kconfig"
12 +source "fs/aufs/Kconfig"
13  
14  endif # MISC_FILESYSTEMS
15  
16 diff --git a/fs/Makefile b/fs/Makefile
17 index 293733f..12d19d0 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -128,3 +128,4 @@ obj-y                               += exofs/ # Multiple modules
21  obj-$(CONFIG_CEPH_FS)          += ceph/
22  obj-$(CONFIG_PSTORE)           += pstore/
23  obj-$(CONFIG_EFIVAR_FS)                += efivarfs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs4.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 0fe4228..52cedab 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2541,6 +2541,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:     linux-unionfs@vger.kernel.org
39 +L:     aufs-users@lists.sourceforge.net (members only)
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 4cb1d1b..5678bf8 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -741,6 +741,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 0e8e5de..88de3b6 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1240,7 +1240,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 12273b6..545e686 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 8c86c80..af894e7 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1649,7 +1649,7 @@ EXPORT_SYMBOL(generic_update_time);
121   * This does the actual work of updating an inodes time or version.  Must have
122   * had called mnt_want_write() before calling this.
123   */
124 -static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125 +int update_time(struct inode *inode, struct timespec64 *time, int flags)
126  {
127         int (*update_time)(struct inode *, struct timespec64 *, int);
128  
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index 8ddd148..ee649e0 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -846,6 +846,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 153f8f6..ccc5bc8 100644
148 --- a/fs/read_write.c
149 +++ b/fs/read_write.c
150 @@ -489,6 +489,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
151                 return -EINVAL;
152  }
153  
154 +vfs_readf_t vfs_readf(struct file *file)
155 +{
156 +       const struct file_operations *fop = file->f_op;
157 +
158 +       if (fop->read)
159 +               return fop->read;
160 +       if (fop->read_iter)
161 +               return new_sync_read;
162 +       return ERR_PTR(-ENOSYS);
163 +}
164 +
165 +vfs_writef_t vfs_writef(struct file *file)
166 +{
167 +       const struct file_operations *fop = file->f_op;
168 +
169 +       if (fop->write)
170 +               return fop->write;
171 +       if (fop->write_iter)
172 +               return new_sync_write;
173 +       return ERR_PTR(-ENOSYS);
174 +}
175 +
176  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
177  {
178         mm_segment_t old_fs;
179 diff --git a/fs/splice.c b/fs/splice.c
180 index b3daa97..1dd7f96 100644
181 --- a/fs/splice.c
182 +++ b/fs/splice.c
183 @@ -838,8 +838,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 @@ -855,9 +855,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 b54e054..2860782 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/file.h b/include/linux/file.h
221 index 279720d..76e38ea 100644
222 --- a/include/linux/file.h
223 +++ b/include/linux/file.h
224 @@ -20,6 +20,7 @@ struct dentry;
225  struct path;
226  extern struct file *alloc_file(const struct path *, fmode_t mode,
227         const struct file_operations *fop);
228 +extern struct file *get_empty_filp(void);
229  
230  static inline void fput_light(struct file *file, int fput_needed)
231  {
232 diff --git a/include/linux/fs.h b/include/linux/fs.h
233 index 805bf22..0d0d247f 100644
234 --- a/include/linux/fs.h
235 +++ b/include/linux/fs.h
236 @@ -1273,6 +1273,7 @@ extern void fasync_free(struct fasync_struct *);
237  /* can be called from interrupts */
238  extern void kill_fasync(struct fasync_struct **, int, int);
239  
240 +extern int setfl(int fd, struct file * filp, unsigned long arg);
241  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
242  extern int f_setown(struct file *filp, unsigned long arg, int force);
243  extern void f_delown(struct file *filp);
244 @@ -1733,6 +1734,7 @@ struct file_operations {
245         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
246         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
247         int (*check_flags)(int);
248 +       int (*setfl)(struct file *, unsigned long);
249         int (*flock) (struct file *, int, struct file_lock *);
250         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
251         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
252 @@ -1803,6 +1805,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
253                               struct iovec *fast_pointer,
254                               struct iovec **ret_pointer);
255  
256 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
257 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
258 +                               loff_t *);
259 +vfs_readf_t vfs_readf(struct file *file);
260 +vfs_writef_t vfs_writef(struct file *file);
261 +
262  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
263  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
264  extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
265 @@ -2218,6 +2226,7 @@ extern int current_umask(void);
266  extern void ihold(struct inode * inode);
267  extern void iput(struct inode *);
268  extern int generic_update_time(struct inode *, struct timespec64 *, int);
269 +extern int update_time(struct inode *, struct timespec64 *, int);
270  
271  /* /sys/fs */
272  extern struct kobject *fs_kobj;
273 @@ -2505,6 +2514,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
274         return false;
275  }
276  #endif
277 +extern int __sync_filesystem(struct super_block *, int);
278  extern int sync_filesystem(struct super_block *);
279  extern const struct file_operations def_blk_fops;
280  extern const struct file_operations def_chr_fops;
281 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
282 index 6fc77d4..27e76f0 100644
283 --- a/include/linux/lockdep.h
284 +++ b/include/linux/lockdep.h
285 @@ -313,6 +313,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
286         return lock->key == key;
287  }
288  
289 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
290 +
291  /*
292   * Acquire a lock.
293   *
294 @@ -439,6 +441,7 @@ struct lockdep_map { };
295  
296  #define lockdep_depth(tsk)     (0)
297  
298 +#define lockdep_is_held(lock)                  (1)
299  #define lockdep_is_held_type(l, r)             (1)
300  
301  #define lockdep_assert_held(l)                 do { (void)(l); } while (0)
302 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
303 index 3594208..24f5fd1 100644
304 --- a/include/linux/mnt_namespace.h
305 +++ b/include/linux/mnt_namespace.h
306 @@ -6,11 +6,14 @@
307  struct mnt_namespace;
308  struct fs_struct;
309  struct user_namespace;
310 +struct vfsmount;
311  
312  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
313                 struct user_namespace *, struct fs_struct *);
314  extern void put_mnt_ns(struct mnt_namespace *ns);
315  
316 +extern int is_current_mnt_ns(struct vfsmount *mnt);
317 +
318  extern const struct file_operations proc_mounts_operations;
319  extern const struct file_operations proc_mountinfo_operations;
320  extern const struct file_operations proc_mountstats_operations;
321 diff --git a/include/linux/splice.h b/include/linux/splice.h
322 index 74b4911..19789fb 100644
323 --- a/include/linux/splice.h
324 +++ b/include/linux/splice.h
325 @@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
326  
327  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
328  extern const struct pipe_buf_operations default_pipe_buf_ops;
329 +
330 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
331 +                          loff_t *ppos, size_t len, unsigned int flags);
332 +extern long do_splice_to(struct file *in, loff_t *ppos,
333 +                        struct pipe_inode_info *pipe, size_t len,
334 +                        unsigned int flags);
335  #endif
336 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
337 index 5fa4d31..720f070 100644
338 --- a/kernel/locking/lockdep.c
339 +++ b/kernel/locking/lockdep.c
340 @@ -140,7 +140,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
341  unsigned long nr_lock_classes;
342  static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
343  
344 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
345 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
346  {
347         if (!hlock->class_idx) {
348                 /*
349 @@ -151,6 +151,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
350         }
351         return lock_classes + hlock->class_idx - 1;
352  }
353 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
354  
355  #ifdef CONFIG_LOCK_STAT
356  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
357 SPDX-License-Identifier: GPL-2.0
358 aufs4.x-rcN mmap patch
359
360 diff --git a/fs/proc/base.c b/fs/proc/base.c
361 index aaffc0c..ab72941 100644
362 --- a/fs/proc/base.c
363 +++ b/fs/proc/base.c
364 @@ -2004,7 +2004,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
365         down_read(&mm->mmap_sem);
366         vma = find_exact_vma(mm, vm_start, vm_end);
367         if (vma && vma->vm_file) {
368 -               *path = vma->vm_file->f_path;
369 +               *path = vma_pr_or_file(vma)->f_path;
370                 path_get(path);
371                 rc = 0;
372         }
373 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
374 index 3b63be6..fb9913b 100644
375 --- a/fs/proc/nommu.c
376 +++ b/fs/proc/nommu.c
377 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
378         file = region->vm_file;
379  
380         if (file) {
381 -               struct inode *inode = file_inode(region->vm_file);
382 +               struct inode *inode;
383 +
384 +               file = vmr_pr_or_file(region);
385 +               inode = file_inode(file);
386                 dev = inode->i_sb->s_dev;
387                 ino = inode->i_ino;
388         }
389 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
390 index dfd73a4..ed2ce10 100644
391 --- a/fs/proc/task_mmu.c
392 +++ b/fs/proc/task_mmu.c
393 @@ -306,7 +306,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
394         const char *name = NULL;
395  
396         if (file) {
397 -               struct inode *inode = file_inode(vma->vm_file);
398 +               struct inode *inode;
399 +
400 +               file = vma_pr_or_file(vma);
401 +               inode = file_inode(file);
402                 dev = inode->i_sb->s_dev;
403                 ino = inode->i_ino;
404                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
405 @@ -1734,7 +1737,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
406         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
407         struct vm_area_struct *vma = v;
408         struct numa_maps *md = &numa_priv->md;
409 -       struct file *file = vma->vm_file;
410 +       struct file *file = vma_pr_or_file(vma);
411         struct mm_struct *mm = vma->vm_mm;
412         struct mm_walk walk = {
413                 .hugetlb_entry = gather_hugetlb_stats,
414 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
415 index 5b62f57..dfb4a3b 100644
416 --- a/fs/proc/task_nommu.c
417 +++ b/fs/proc/task_nommu.c
418 @@ -156,7 +156,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
419         file = vma->vm_file;
420  
421         if (file) {
422 -               struct inode *inode = file_inode(vma->vm_file);
423 +               struct inode *inode;
424 +
425 +               file = vma_pr_or_file(vma);
426 +               inode = file_inode(file);
427                 dev = inode->i_sb->s_dev;
428                 ino = inode->i_ino;
429                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
430 diff --git a/include/linux/mm.h b/include/linux/mm.h
431 index d3a3842..c4664f2 100644
432 --- a/include/linux/mm.h
433 +++ b/include/linux/mm.h
434 @@ -1431,6 +1431,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
435         unmap_mapping_range(mapping, holebegin, holelen, 0);
436  }
437  
438 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
439 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
440 +                                     int);
441 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
442 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
443 +
444 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
445 +                                                               __LINE__)
446 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
447 +                                                         __LINE__)
448 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
449 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
450 +
451 +#ifndef CONFIG_MMU
452 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
453 +extern void vmr_do_fput(struct vm_region *, const char[], int);
454 +
455 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
456 +                                                         __LINE__)
457 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
458 +#endif /* !CONFIG_MMU */
459 +
460  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
461                 void *buf, int len, unsigned int gup_flags);
462  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
463 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
464 index 99ce070..8babe35 100644
465 --- a/include/linux/mm_types.h
466 +++ b/include/linux/mm_types.h
467 @@ -236,6 +236,7 @@ struct vm_region {
468         unsigned long   vm_top;         /* region allocated to here */
469         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
470         struct file     *vm_file;       /* the backing file or NULL */
471 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
472  
473         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
474         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
475 @@ -310,6 +311,7 @@ struct vm_area_struct {
476         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
477                                            units */
478         struct file * vm_file;          /* File we map to (can be NULL). */
479 +       struct file *vm_prfile;         /* shadow of vm_file */
480         void * vm_private_data;         /* was vm_pte (shared mem) */
481  
482         atomic_long_t swap_readahead_info;
483 diff --git a/kernel/fork.c b/kernel/fork.c
484 index a191c05..d88ee5a 100644
485 --- a/kernel/fork.c
486 +++ b/kernel/fork.c
487 @@ -506,7 +506,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
488                         struct inode *inode = file_inode(file);
489                         struct address_space *mapping = file->f_mapping;
490  
491 -                       get_file(file);
492 +                       vma_get_file(tmp);
493                         if (tmp->vm_flags & VM_DENYWRITE)
494                                 atomic_dec(&inode->i_writecount);
495                         i_mmap_lock_write(mapping);
496 diff --git a/mm/Makefile b/mm/Makefile
497 index 8716bda..68afd6d 100644
498 --- a/mm/Makefile
499 +++ b/mm/Makefile
500 @@ -39,7 +39,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
501                            mm_init.o mmu_context.o percpu.o slab_common.o \
502                            compaction.o vmacache.o \
503                            interval_tree.o list_lru.o workingset.o \
504 -                          debug.o $(mmu-y)
505 +                          prfile.o debug.o $(mmu-y)
506  
507  obj-y += init-mm.o
508  
509 diff --git a/mm/filemap.c b/mm/filemap.c
510 index 52517f2..250f675 100644
511 --- a/mm/filemap.c
512 +++ b/mm/filemap.c
513 @@ -2700,7 +2700,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
514         vm_fault_t ret = VM_FAULT_LOCKED;
515  
516         sb_start_pagefault(inode->i_sb);
517 -       file_update_time(vmf->vma->vm_file);
518 +       vma_file_update_time(vmf->vma);
519         lock_page(page);
520         if (page->mapping != inode->i_mapping) {
521                 unlock_page(page);
522 diff --git a/mm/mmap.c b/mm/mmap.c
523 index ff1944d..88abe2d 100644
524 --- a/mm/mmap.c
525 +++ b/mm/mmap.c
526 @@ -180,7 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
527         if (vma->vm_ops && vma->vm_ops->close)
528                 vma->vm_ops->close(vma);
529         if (vma->vm_file)
530 -               fput(vma->vm_file);
531 +               vma_fput(vma);
532         mpol_put(vma_policy(vma));
533         vm_area_free(vma);
534         return next;
535 @@ -905,7 +905,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
536         if (remove_next) {
537                 if (file) {
538                         uprobe_munmap(next, next->vm_start, next->vm_end);
539 -                       fput(file);
540 +                       vma_fput(vma);
541                 }
542                 if (next->anon_vma)
543                         anon_vma_merge(vma, next);
544 @@ -1818,8 +1818,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
545         return addr;
546  
547  unmap_and_free_vma:
548 +       vma_fput(vma);
549         vma->vm_file = NULL;
550 -       fput(file);
551  
552         /* Undo any partial mapping done by a device driver. */
553         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
554 @@ -2638,7 +2638,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
555                 goto out_free_mpol;
556  
557         if (new->vm_file)
558 -               get_file(new->vm_file);
559 +               vma_get_file(new);
560  
561         if (new->vm_ops && new->vm_ops->open)
562                 new->vm_ops->open(new);
563 @@ -2657,7 +2657,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
564         if (new->vm_ops && new->vm_ops->close)
565                 new->vm_ops->close(new);
566         if (new->vm_file)
567 -               fput(new->vm_file);
568 +               vma_fput(new);
569         unlink_anon_vmas(new);
570   out_free_mpol:
571         mpol_put(vma_policy(new));
572 @@ -2819,7 +2819,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
573         struct vm_area_struct *vma;
574         unsigned long populate = 0;
575         unsigned long ret = -EINVAL;
576 -       struct file *file;
577 +       struct file *file, *prfile;
578  
579         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
580                      current->comm, current->pid);
581 @@ -2894,10 +2894,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
582                 }
583         }
584  
585 -       file = get_file(vma->vm_file);
586 +       vma_get_file(vma);
587 +       file = vma->vm_file;
588 +       prfile = vma->vm_prfile;
589         ret = do_mmap_pgoff(vma->vm_file, start, size,
590                         prot, flags, pgoff, &populate, NULL);
591 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
592 +               struct vm_area_struct *new_vma;
593 +
594 +               new_vma = find_vma(mm, ret);
595 +               if (!new_vma->vm_prfile)
596 +                       new_vma->vm_prfile = prfile;
597 +               if (new_vma != vma)
598 +                       get_file(prfile);
599 +       }
600 +       /*
601 +        * two fput()s instead of vma_fput(vma),
602 +        * coz vma may not be available anymore.
603 +        */
604         fput(file);
605 +       if (prfile)
606 +               fput(prfile);
607  out:
608         up_write(&mm->mmap_sem);
609         if (populate)
610 @@ -3204,7 +3221,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
611                 if (anon_vma_clone(new_vma, vma))
612                         goto out_free_mempol;
613                 if (new_vma->vm_file)
614 -                       get_file(new_vma->vm_file);
615 +                       vma_get_file(new_vma);
616                 if (new_vma->vm_ops && new_vma->vm_ops->open)
617                         new_vma->vm_ops->open(new_vma);
618                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
619 diff --git a/mm/nommu.c b/mm/nommu.c
620 index 1d22fdb..2b608be 100644
621 --- a/mm/nommu.c
622 +++ b/mm/nommu.c
623 @@ -629,7 +629,7 @@ static void __put_nommu_region(struct vm_region *region)
624                 up_write(&nommu_region_sem);
625  
626                 if (region->vm_file)
627 -                       fput(region->vm_file);
628 +                       vmr_fput(region);
629  
630                 /* IO memory and memory shared directly out of the pagecache
631                  * from ramfs/tmpfs mustn't be released here */
632 @@ -767,7 +767,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
633         if (vma->vm_ops && vma->vm_ops->close)
634                 vma->vm_ops->close(vma);
635         if (vma->vm_file)
636 -               fput(vma->vm_file);
637 +               vma_fput(vma);
638         put_nommu_region(vma->vm_region);
639         vm_area_free(vma);
640  }
641 @@ -1288,7 +1288,7 @@ unsigned long do_mmap(struct file *file,
642                                         goto error_just_free;
643                                 }
644                         }
645 -                       fput(region->vm_file);
646 +                       vmr_fput(region);
647                         kmem_cache_free(vm_region_jar, region);
648                         region = pregion;
649                         result = start;
650 @@ -1363,7 +1363,7 @@ unsigned long do_mmap(struct file *file,
651         up_write(&nommu_region_sem);
652  error:
653         if (region->vm_file)
654 -               fput(region->vm_file);
655 +               vmr_fput(region);
656         kmem_cache_free(vm_region_jar, region);
657         if (vma->vm_file)
658                 fput(vma->vm_file);
659 diff --git a/mm/prfile.c b/mm/prfile.c
660 new file mode 100644
661 index 0000000..a27ac36
662 --- /dev/null
663 +++ b/mm/prfile.c
664 @@ -0,0 +1,86 @@
665 +// SPDX-License-Identifier: GPL-2.0
666 +/*
667 + * Mainly for aufs which mmap(2) different file and wants to print different
668 + * path in /proc/PID/maps.
669 + * Call these functions via macros defined in linux/mm.h.
670 + *
671 + * See Documentation/filesystems/aufs/design/06mmap.txt
672 + *
673 + * Copyright (c) 2014-2018 Junjro R. Okajima
674 + * Copyright (c) 2014 Ian Campbell
675 + */
676 +
677 +#include <linux/mm.h>
678 +#include <linux/file.h>
679 +#include <linux/fs.h>
680 +
681 +/* #define PRFILE_TRACE */
682 +static inline void prfile_trace(struct file *f, struct file *pr,
683 +                             const char func[], int line, const char func2[])
684 +{
685 +#ifdef PRFILE_TRACE
686 +       if (pr)
687 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
688 +#endif
689 +}
690 +
691 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
692 +                            int line)
693 +{
694 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
695 +
696 +       prfile_trace(f, pr, func, line, __func__);
697 +       file_update_time(f);
698 +       if (f && pr)
699 +               file_update_time(pr);
700 +}
701 +
702 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
703 +                              int line)
704 +{
705 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
706 +
707 +       prfile_trace(f, pr, func, line, __func__);
708 +       return (f && pr) ? pr : f;
709 +}
710 +
711 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
712 +{
713 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
714 +
715 +       prfile_trace(f, pr, func, line, __func__);
716 +       get_file(f);
717 +       if (f && pr)
718 +               get_file(pr);
719 +}
720 +
721 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
722 +{
723 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
724 +
725 +       prfile_trace(f, pr, func, line, __func__);
726 +       fput(f);
727 +       if (f && pr)
728 +               fput(pr);
729 +}
730 +
731 +#ifndef CONFIG_MMU
732 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
733 +                              int line)
734 +{
735 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
736 +
737 +       prfile_trace(f, pr, func, line, __func__);
738 +       return (f && pr) ? pr : f;
739 +}
740 +
741 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
742 +{
743 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
744 +
745 +       prfile_trace(f, pr, func, line, __func__);
746 +       fput(f);
747 +       if (f && pr)
748 +               fput(pr);
749 +}
750 +#endif /* !CONFIG_MMU */
751 SPDX-License-Identifier: GPL-2.0
752 aufs4.x-rcN standalone patch
753
754 diff --git a/fs/dcache.c b/fs/dcache.c
755 index 88de3b6..de0091a 100644
756 --- a/fs/dcache.c
757 +++ b/fs/dcache.c
758 @@ -1345,6 +1345,7 @@ void d_walk(struct dentry *parent, void *data,
759         seq = 1;
760         goto again;
761  }
762 +EXPORT_SYMBOL_GPL(d_walk);
763  
764  struct check_mount {
765         struct vfsmount *mnt;
766 @@ -2891,6 +2892,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
767  
768         write_sequnlock(&rename_lock);
769  }
770 +EXPORT_SYMBOL_GPL(d_exchange);
771  
772  /**
773   * d_ancestor - search for an ancestor
774 diff --git a/fs/exec.c b/fs/exec.c
775 index 72e961a..adc7329 100644
776 --- a/fs/exec.c
777 +++ b/fs/exec.c
778 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
779         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
780                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
781  }
782 +EXPORT_SYMBOL_GPL(path_noexec);
783  
784  #ifdef CONFIG_USELIB
785  /*
786 diff --git a/fs/fcntl.c b/fs/fcntl.c
787 index 545e686..0237ad7 100644
788 --- a/fs/fcntl.c
789 +++ b/fs/fcntl.c
790 @@ -85,6 +85,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
791   out:
792         return error;
793  }
794 +EXPORT_SYMBOL_GPL(setfl);
795  
796  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
797                       int force)
798 diff --git a/fs/file_table.c b/fs/file_table.c
799 index 7ec0b3e..819ee07 100644
800 --- a/fs/file_table.c
801 +++ b/fs/file_table.c
802 @@ -147,6 +147,7 @@ struct file *get_empty_filp(void)
803         }
804         return ERR_PTR(-ENFILE);
805  }
806 +EXPORT_SYMBOL_GPL(get_empty_filp);
807  
808  /**
809   * alloc_file - allocate and initialize a 'struct file'
810 @@ -257,6 +258,7 @@ void flush_delayed_fput(void)
811  {
812         delayed_fput(NULL);
813  }
814 +EXPORT_SYMBOL_GPL(flush_delayed_fput);
815  
816  static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
817  
818 @@ -299,6 +301,7 @@ void __fput_sync(struct file *file)
819  }
820  
821  EXPORT_SYMBOL(fput);
822 +EXPORT_SYMBOL_GPL(__fput_sync);
823  
824  void put_filp(struct file *file)
825  {
826 @@ -307,6 +310,7 @@ void put_filp(struct file *file)
827                 file_free(file);
828         }
829  }
830 +EXPORT_SYMBOL_GPL(put_filp);
831  
832  void __init files_init(void)
833  {
834 diff --git a/fs/inode.c b/fs/inode.c
835 index af894e7..169811b 100644
836 --- a/fs/inode.c
837 +++ b/fs/inode.c
838 @@ -1658,6 +1658,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
839  
840         return update_time(inode, time, flags);
841  }
842 +EXPORT_SYMBOL_GPL(update_time);
843  
844  /**
845   *     touch_atime     -       update the access time
846 diff --git a/fs/namespace.c b/fs/namespace.c
847 index ee649e0..10923cb 100644
848 --- a/fs/namespace.c
849 +++ b/fs/namespace.c
850 @@ -517,6 +517,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
851         mnt_dec_writers(real_mount(mnt));
852         preempt_enable();
853  }
854 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
855  
856  /**
857   * mnt_drop_write - give up write access to a mount
858 @@ -851,6 +852,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
859  {
860         return check_mnt(real_mount(mnt));
861  }
862 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
863  
864  /*
865   * vfsmount lock must be held for write
866 @@ -1893,6 +1895,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
867         }
868         return 0;
869  }
870 +EXPORT_SYMBOL_GPL(iterate_mounts);
871  
872  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
873  {
874 diff --git a/fs/notify/group.c b/fs/notify/group.c
875 index aa5468f2..b38d224 100644
876 --- a/fs/notify/group.c
877 +++ b/fs/notify/group.c
878 @@ -22,6 +22,7 @@
879  #include <linux/srcu.h>
880  #include <linux/rculist.h>
881  #include <linux/wait.h>
882 +#include <linux/module.h>
883  
884  #include <linux/fsnotify_backend.h>
885  #include "fsnotify.h"
886 @@ -109,6 +110,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
887  {
888         refcount_inc(&group->refcnt);
889  }
890 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
891  
892  /*
893   * Drop a reference to a group.  Free it if it's through.
894 @@ -118,6 +120,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
895         if (refcount_dec_and_test(&group->refcnt))
896                 fsnotify_final_destroy_group(group);
897  }
898 +EXPORT_SYMBOL_GPL(fsnotify_put_group);
899  
900  /*
901   * Create a new fsnotify_group and hold a reference for the group returned.
902 @@ -147,6 +150,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
903  
904         return group;
905  }
906 +EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
907  
908  int fsnotify_fasync(int fd, struct file *file, int on)
909  {
910 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
911 index 61f4c5f..855c655 100644
912 --- a/fs/notify/mark.c
913 +++ b/fs/notify/mark.c
914 @@ -245,6 +245,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
915         queue_delayed_work(system_unbound_wq, &reaper_work,
916                            FSNOTIFY_REAPER_DELAY);
917  }
918 +EXPORT_SYMBOL_GPL(fsnotify_put_mark);
919  
920  /*
921   * Get mark reference when we found the mark via lockless traversal of object
922 @@ -399,6 +400,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
923         mutex_unlock(&group->mark_mutex);
924         fsnotify_free_mark(mark);
925  }
926 +EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
927  
928  /*
929   * Sorting function for lists of fsnotify marks.
930 @@ -624,6 +626,7 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
931         mutex_unlock(&group->mark_mutex);
932         return ret;
933  }
934 +EXPORT_SYMBOL_GPL(fsnotify_add_mark);
935  
936  /*
937   * Given a list of marks, find the mark associated with given group. If found
938 @@ -747,6 +750,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
939         fsnotify_get_group(group);
940         mark->group = group;
941  }
942 +EXPORT_SYMBOL_GPL(fsnotify_init_mark);
943  
944  /*
945   * Destroy all marks in destroy_list, waits for SRCU period to finish before
946 diff --git a/fs/open.c b/fs/open.c
947 index d0e955b..527bc1a 100644
948 --- a/fs/open.c
949 +++ b/fs/open.c
950 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
951         inode_unlock(dentry->d_inode);
952         return ret;
953  }
954 +EXPORT_SYMBOL_GPL(do_truncate);
955  
956  long vfs_truncate(const struct path *path, loff_t length)
957  {
958 @@ -723,6 +724,7 @@ SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
959  {
960         return ksys_fchown(fd, user, group);
961  }
962 +EXPORT_SYMBOL_GPL(open_check_o_direct);
963  
964  int open_check_o_direct(struct file *f)
965  {
966 diff --git a/fs/read_write.c b/fs/read_write.c
967 index ccc5bc8..36f52f4 100644
968 --- a/fs/read_write.c
969 +++ b/fs/read_write.c
970 @@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
971  
972         return ret;
973  }
974 +EXPORT_SYMBOL_GPL(vfs_read);
975  
976  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
977  {
978 @@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
979                 return new_sync_read;
980         return ERR_PTR(-ENOSYS);
981  }
982 +EXPORT_SYMBOL_GPL(vfs_readf);
983  
984  vfs_writef_t vfs_writef(struct file *file)
985  {
986 @@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
987                 return new_sync_write;
988         return ERR_PTR(-ENOSYS);
989  }
990 +EXPORT_SYMBOL_GPL(vfs_writef);
991  
992  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
993  {
994 @@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
995  
996         return ret;
997  }
998 +EXPORT_SYMBOL_GPL(vfs_write);
999  
1000  static inline loff_t file_pos_read(struct file *file)
1001  {
1002 diff --git a/fs/splice.c b/fs/splice.c
1003 index 1dd7f96..a5e3bcb 100644
1004 --- a/fs/splice.c
1005 +++ b/fs/splice.c
1006 @@ -851,6 +851,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
1007  
1008         return splice_write(pipe, out, ppos, len, flags);
1009  }
1010 +EXPORT_SYMBOL_GPL(do_splice_from);
1011  
1012  /*
1013   * Attempt to initiate a splice from a file to a pipe.
1014 @@ -880,6 +881,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
1015  
1016         return splice_read(in, ppos, pipe, len, flags);
1017  }
1018 +EXPORT_SYMBOL_GPL(do_splice_to);
1019  
1020  /**
1021   * splice_direct_to_actor - splices data directly between two non-pipes
1022 diff --git a/fs/sync.c b/fs/sync.c
1023 index 2860782..ffd7ea4 100644
1024 --- a/fs/sync.c
1025 +++ b/fs/sync.c
1026 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
1027                 sb->s_op->sync_fs(sb, wait);
1028         return __sync_blockdev(sb->s_bdev, wait);
1029  }
1030 +EXPORT_SYMBOL_GPL(__sync_filesystem);
1031  
1032  /*
1033   * Write out and wait upon all dirty data associated with this
1034 diff --git a/fs/xattr.c b/fs/xattr.c
1035 index f9cb1db..000b62b 100644
1036 --- a/fs/xattr.c
1037 +++ b/fs/xattr.c
1038 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
1039         *xattr_value = value;
1040         return error;
1041  }
1042 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
1043  
1044  ssize_t
1045  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
1046 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
1047 index 720f070..4f58215 100644
1048 --- a/kernel/locking/lockdep.c
1049 +++ b/kernel/locking/lockdep.c
1050 @@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
1051         }
1052         return lock_classes + hlock->class_idx - 1;
1053  }
1054 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1055  #define hlock_class(hlock) lockdep_hlock_class(hlock)
1056  
1057  #ifdef CONFIG_LOCK_STAT
1058 diff --git a/kernel/task_work.c b/kernel/task_work.c
1059 index 0fef395..83fb1ec 100644
1060 --- a/kernel/task_work.c
1061 +++ b/kernel/task_work.c
1062 @@ -116,3 +116,4 @@ void task_work_run(void)
1063                 } while (work);
1064         }
1065  }
1066 +EXPORT_SYMBOL_GPL(task_work_run);
1067 diff --git a/security/commoncap.c b/security/commoncap.c
1068 index f4c33ab..70563f1 100644
1069 --- a/security/commoncap.c
1070 +++ b/security/commoncap.c
1071 @@ -1336,12 +1336,14 @@ int cap_mmap_addr(unsigned long addr)
1072         }
1073         return ret;
1074  }
1075 +EXPORT_SYMBOL_GPL(cap_mmap_addr);
1076  
1077  int cap_mmap_file(struct file *file, unsigned long reqprot,
1078                   unsigned long prot, unsigned long flags)
1079  {
1080         return 0;
1081  }
1082 +EXPORT_SYMBOL_GPL(cap_mmap_file);
1083  
1084  #ifdef CONFIG_SECURITY
1085  
1086 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
1087 index cd97929..424fd23 100644
1088 --- a/security/device_cgroup.c
1089 +++ b/security/device_cgroup.c
1090 @@ -8,6 +8,7 @@
1091  #include <linux/device_cgroup.h>
1092  #include <linux/cgroup.h>
1093  #include <linux/ctype.h>
1094 +#include <linux/export.h>
1095  #include <linux/list.h>
1096  #include <linux/uaccess.h>
1097  #include <linux/seq_file.h>
1098 @@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
1099  
1100         return 0;
1101  }
1102 +EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
1103 diff --git a/security/security.c b/security/security.c
1104 index 68f46d8..3bc1348 100644
1105 --- a/security/security.c
1106 +++ b/security/security.c
1107 @@ -537,6 +537,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1108                 return 0;
1109         return call_int_hook(path_rmdir, 0, dir, dentry);
1110  }
1111 +EXPORT_SYMBOL_GPL(security_path_rmdir);
1112  
1113  int security_path_unlink(const struct path *dir, struct dentry *dentry)
1114  {
1115 @@ -553,6 +554,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
1116                 return 0;
1117         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1118  }
1119 +EXPORT_SYMBOL_GPL(security_path_symlink);
1120  
1121  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1122                        struct dentry *new_dentry)
1123 @@ -561,6 +563,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1124                 return 0;
1125         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1126  }
1127 +EXPORT_SYMBOL_GPL(security_path_link);
1128  
1129  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1130                          const struct path *new_dir, struct dentry *new_dentry,
1131 @@ -588,6 +591,7 @@ int security_path_truncate(const struct path *path)
1132                 return 0;
1133         return call_int_hook(path_truncate, 0, path);
1134  }
1135 +EXPORT_SYMBOL_GPL(security_path_truncate);
1136  
1137  int security_path_chmod(const struct path *path, umode_t mode)
1138  {
1139 @@ -595,6 +599,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1140                 return 0;
1141         return call_int_hook(path_chmod, 0, path, mode);
1142  }
1143 +EXPORT_SYMBOL_GPL(security_path_chmod);
1144  
1145  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1146  {
1147 @@ -602,6 +607,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1148                 return 0;
1149         return call_int_hook(path_chown, 0, path, uid, gid);
1150  }
1151 +EXPORT_SYMBOL_GPL(security_path_chown);
1152  
1153  int security_path_chroot(const struct path *path)
1154  {
1155 @@ -687,6 +693,7 @@ int security_inode_readlink(struct dentry *dentry)
1156                 return 0;
1157         return call_int_hook(inode_readlink, 0, dentry);
1158  }
1159 +EXPORT_SYMBOL_GPL(security_inode_readlink);
1160  
1161  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1162                                bool rcu)
1163 @@ -702,6 +709,7 @@ int security_inode_permission(struct inode *inode, int mask)
1164                 return 0;
1165         return call_int_hook(inode_permission, 0, inode, mask);
1166  }
1167 +EXPORT_SYMBOL_GPL(security_inode_permission);
1168  
1169  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1170  {
1171 @@ -873,6 +881,7 @@ int security_file_permission(struct file *file, int mask)
1172  
1173         return fsnotify_perm(file, mask);
1174  }
1175 +EXPORT_SYMBOL_GPL(security_file_permission);
1176  
1177  int security_file_alloc(struct file *file)
1178  {
1179 @@ -932,6 +941,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
1180                 return ret;
1181         return ima_file_mmap(file, prot);
1182  }
1183 +EXPORT_SYMBOL_GPL(security_mmap_file);
1184  
1185  int security_mmap_addr(unsigned long addr)
1186  {
1187 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1188 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1189 +++ linux/Documentation/ABI/testing/debugfs-aufs        2018-08-12 23:43:05.450124426 +0200
1190 @@ -0,0 +1,55 @@
1191 +What:          /debug/aufs/si_<id>/
1192 +Date:          March 2009
1193 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1194 +Description:
1195 +               Under /debug/aufs, a directory named si_<id> is created
1196 +               per aufs mount, where <id> is a unique id generated
1197 +               internally.
1198 +
1199 +What:          /debug/aufs/si_<id>/plink
1200 +Date:          Apr 2013
1201 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1202 +Description:
1203 +               It has three lines and shows the information about the
1204 +               pseudo-link. The first line is a single number
1205 +               representing a number of buckets. The second line is a
1206 +               number of pseudo-links per buckets (separated by a
1207 +               blank). The last line is a single number representing a
1208 +               total number of psedo-links.
1209 +               When the aufs mount option 'noplink' is specified, it
1210 +               will show "1\n0\n0\n".
1211 +
1212 +What:          /debug/aufs/si_<id>/xib
1213 +Date:          March 2009
1214 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1215 +Description:
1216 +               It shows the consumed blocks by xib (External Inode Number
1217 +               Bitmap), its block size and file size.
1218 +               When the aufs mount option 'noxino' is specified, it
1219 +               will be empty. About XINO files, see the aufs manual.
1220 +
1221 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1222 +Date:          March 2009
1223 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1224 +Description:
1225 +               It shows the consumed blocks by xino (External Inode Number
1226 +               Translation Table), its link count, block size and file
1227 +               size.
1228 +               Due to the file size limit, there may exist multiple
1229 +               xino files per branch.  In this case, "-N" is added to
1230 +               the filename and it corresponds to the index of the
1231 +               internal xino array.  "-0" is omitted.
1232 +               When the aufs mount option 'noxino' is specified, Those
1233 +               entries won't exist.  About XINO files, see the aufs
1234 +               manual.
1235 +
1236 +What:          /debug/aufs/si_<id>/xigen
1237 +Date:          March 2009
1238 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1239 +Description:
1240 +               It shows the consumed blocks by xigen (External Inode
1241 +               Generation Table), its block size and file size.
1242 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1243 +               be created.
1244 +               When the aufs mount option 'noxino' is specified, it
1245 +               will be empty. About XINO files, see the aufs manual.
1246 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1247 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1248 +++ linux/Documentation/ABI/testing/sysfs-aufs  2017-07-29 12:14:25.893041746 +0200
1249 @@ -0,0 +1,31 @@
1250 +What:          /sys/fs/aufs/si_<id>/
1251 +Date:          March 2009
1252 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1253 +Description:
1254 +               Under /sys/fs/aufs, a directory named si_<id> is created
1255 +               per aufs mount, where <id> is a unique id generated
1256 +               internally.
1257 +
1258 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1259 +Date:          March 2009
1260 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1261 +Description:
1262 +               It shows the abolute path of a member directory (which
1263 +               is called branch) in aufs, and its permission.
1264 +
1265 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1266 +Date:          July 2013
1267 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1268 +Description:
1269 +               It shows the id of a member directory (which is called
1270 +               branch) in aufs.
1271 +
1272 +What:          /sys/fs/aufs/si_<id>/xi_path
1273 +Date:          March 2009
1274 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1275 +Description:
1276 +               It shows the abolute path of XINO (External Inode Number
1277 +               Bitmap, Translation Table and Generation Table) file
1278 +               even if it is the default path.
1279 +               When the aufs mount option 'noxino' is specified, it
1280 +               will be empty. About XINO files, see the aufs manual.
1281 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1282 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1283 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2018-04-15 08:49:13.394483860 +0200
1284 @@ -0,0 +1,171 @@
1285 +
1286 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1287 +# 
1288 +# This program is free software; you can redistribute it and/or modify
1289 +# it under the terms of the GNU General Public License as published by
1290 +# the Free Software Foundation; either version 2 of the License, or
1291 +# (at your option) any later version.
1292 +# 
1293 +# This program is distributed in the hope that it will be useful,
1294 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1295 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1296 +# GNU General Public License for more details.
1297 +# 
1298 +# You should have received a copy of the GNU General Public License
1299 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1300 +
1301 +Introduction
1302 +----------------------------------------
1303 +
1304 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1305 +1. abbrev. for "advanced multi-layered unification filesystem".
1306 +2. abbrev. for "another unionfs".
1307 +3. abbrev. for "auf das" in German which means "on the" in English.
1308 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1309 +   But "Filesystem aufs Filesystem" is hard to understand.
1310 +4. abbrev. for "African Urban Fashion Show".
1311 +
1312 +AUFS is a filesystem with features:
1313 +- multi layered stackable unification filesystem, the member directory
1314 +  is called as a branch.
1315 +- branch permission and attribute, 'readonly', 'real-readonly',
1316 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1317 +  combination.
1318 +- internal "file copy-on-write".
1319 +- logical deletion, whiteout.
1320 +- dynamic branch manipulation, adding, deleting and changing permission.
1321 +- allow bypassing aufs, user's direct branch access.
1322 +- external inode number translation table and bitmap which maintains the
1323 +  persistent aufs inode number.
1324 +- seekable directory, including NFS readdir.
1325 +- file mapping, mmap and sharing pages.
1326 +- pseudo-link, hardlink over branches.
1327 +- loopback mounted filesystem as a branch.
1328 +- several policies to select one among multiple writable branches.
1329 +- revert a single systemcall when an error occurs in aufs.
1330 +- and more...
1331 +
1332 +
1333 +Multi Layered Stackable Unification Filesystem
1334 +----------------------------------------------------------------------
1335 +Most people already knows what it is.
1336 +It is a filesystem which unifies several directories and provides a
1337 +merged single directory. When users access a file, the access will be
1338 +passed/re-directed/converted (sorry, I am not sure which English word is
1339 +correct) to the real file on the member filesystem. The member
1340 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1341 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1342 +readonly branch is handled by creating 'whiteout' on the upper writable
1343 +branch.
1344 +
1345 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1346 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1347 +different approaches to implement the merged-view.
1348 +The former tries putting it into VFS, and the latter implements as a
1349 +separate filesystem.
1350 +(If I misunderstand about these implementations, please let me know and
1351 +I shall correct it. Because it is a long time ago when I read their
1352 +source files last time).
1353 +
1354 +UnionMount's approach will be able to small, but may be hard to share
1355 +branches between several UnionMount since the whiteout in it is
1356 +implemented in the inode on branch filesystem and always
1357 +shared. According to Bharata's post, readdir does not seems to be
1358 +finished yet.
1359 +There are several missing features known in this implementations such as
1360 +- for users, the inode number may change silently. eg. copy-up.
1361 +- link(2) may break by copy-up.
1362 +- read(2) may get an obsoleted filedata (fstat(2) too).
1363 +- fcntl(F_SETLK) may be broken by copy-up.
1364 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1365 +  open(O_RDWR).
1366 +
1367 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1368 +merged into mainline. This is another implementation of UnionMount as a
1369 +separated filesystem. All the limitations and known problems which
1370 +UnionMount are equally inherited to "overlay" filesystem.
1371 +
1372 +Unionfs has a longer history. When I started implementing a stackable
1373 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1374 +inode, dentry and file objects and they have an array pointing lower
1375 +same kind objects. After contributing many patches for Unionfs, I
1376 +re-started my project AUFS (Jun 2006).
1377 +
1378 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1379 +implemented my own ideas, approaches and enhancements and it became
1380 +totally different one.
1381 +
1382 +Comparing DM snapshot and fs based implementation
1383 +- the number of bytes to be copied between devices is much smaller.
1384 +- the type of filesystem must be one and only.
1385 +- the fs must be writable, no readonly fs, even for the lower original
1386 +  device. so the compression fs will not be usable. but if we use
1387 +  loopback mount, we may address this issue.
1388 +  for instance,
1389 +       mount /cdrom/squashfs.img /sq
1390 +       losetup /sq/ext2.img
1391 +       losetup /somewhere/cow
1392 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1393 +- it will be difficult (or needs more operations) to extract the
1394 +  difference between the original device and COW.
1395 +- DM snapshot-merge may help a lot when users try merging. in the
1396 +  fs-layer union, users will use rsync(1).
1397 +
1398 +You may want to read my old paper "Filesystems in LiveCD"
1399 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1400 +
1401 +
1402 +Several characters/aspects/persona of aufs
1403 +----------------------------------------------------------------------
1404 +
1405 +Aufs has several characters, aspects or persona.
1406 +1. a filesystem, callee of VFS helper
1407 +2. sub-VFS, caller of VFS helper for branches
1408 +3. a virtual filesystem which maintains persistent inode number
1409 +4. reader/writer of files on branches such like an application
1410 +
1411 +1. Callee of VFS Helper
1412 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1413 +unlink(2) from an application reaches sys_unlink() kernel function and
1414 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1415 +calls filesystem specific unlink operation. Actually aufs implements the
1416 +unlink operation but it behaves like a redirector.
1417 +
1418 +2. Caller of VFS Helper for Branches
1419 +aufs_unlink() passes the unlink request to the branch filesystem as if
1420 +it were called from VFS. So the called unlink operation of the branch
1421 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1422 +every necessary pre/post operation for the branch filesystem.
1423 +- acquire the lock for the parent dir on a branch
1424 +- lookup in a branch
1425 +- revalidate dentry on a branch
1426 +- mnt_want_write() for a branch
1427 +- vfs_unlink() for a branch
1428 +- mnt_drop_write() for a branch
1429 +- release the lock on a branch
1430 +
1431 +3. Persistent Inode Number
1432 +One of the most important issue for a filesystem is to maintain inode
1433 +numbers. This is particularly important to support exporting a
1434 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1435 +backend block device for its own. But some storage is necessary to
1436 +keep and maintain the inode numbers. It may be a large space and may not
1437 +suit to keep in memory. Aufs rents some space from its first writable
1438 +branch filesystem (by default) and creates file(s) on it. These files
1439 +are created by aufs internally and removed soon (currently) keeping
1440 +opened.
1441 +Note: Because these files are removed, they are totally gone after
1442 +      unmounting aufs. It means the inode numbers are not persistent
1443 +      across unmount or reboot. I have a plan to make them really
1444 +      persistent which will be important for aufs on NFS server.
1445 +
1446 +4. Read/Write Files Internally (copy-on-write)
1447 +Because a branch can be readonly, when you write a file on it, aufs will
1448 +"copy-up" it to the upper writable branch internally. And then write the
1449 +originally requested thing to the file. Generally kernel doesn't
1450 +open/read/write file actively. In aufs, even a single write may cause a
1451 +internal "file copy". This behaviour is very similar to cp(1) command.
1452 +
1453 +Some people may think it is better to pass such work to user space
1454 +helper, instead of doing in kernel space. Actually I am still thinking
1455 +about it. But currently I have implemented it in kernel space.
1456 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1457 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1458 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2018-04-15 08:49:13.394483860 +0200
1459 @@ -0,0 +1,258 @@
1460 +
1461 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1462 +# 
1463 +# This program is free software; you can redistribute it and/or modify
1464 +# it under the terms of the GNU General Public License as published by
1465 +# the Free Software Foundation; either version 2 of the License, or
1466 +# (at your option) any later version.
1467 +# 
1468 +# This program is distributed in the hope that it will be useful,
1469 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1470 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1471 +# GNU General Public License for more details.
1472 +# 
1473 +# You should have received a copy of the GNU General Public License
1474 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1475 +
1476 +Basic Aufs Internal Structure
1477 +
1478 +Superblock/Inode/Dentry/File Objects
1479 +----------------------------------------------------------------------
1480 +As like an ordinary filesystem, aufs has its own
1481 +superblock/inode/dentry/file objects. All these objects have a
1482 +dynamically allocated array and store the same kind of pointers to the
1483 +lower filesystem, branch.
1484 +For example, when you build a union with one readwrite branch and one
1485 +readonly, mounted /au, /rw and /ro respectively.
1486 +- /au = /rw + /ro
1487 +- /ro/fileA exists but /rw/fileA
1488 +
1489 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1490 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1491 +- [0] = NULL (because /rw/fileA doesn't exist)
1492 +- [1] = /ro/fileA
1493 +
1494 +This style of an array is essentially same to the aufs
1495 +superblock/inode/dentry/file objects.
1496 +
1497 +Because aufs supports manipulating branches, ie. add/delete/change
1498 +branches dynamically, these objects has its own generation. When
1499 +branches are changed, the generation in aufs superblock is
1500 +incremented. And a generation in other object are compared when it is
1501 +accessed. When a generation in other objects are obsoleted, aufs
1502 +refreshes the internal array.
1503 +
1504 +
1505 +Superblock
1506 +----------------------------------------------------------------------
1507 +Additionally aufs superblock has some data for policies to select one
1508 +among multiple writable branches, XIB files, pseudo-links and kobject.
1509 +See below in detail.
1510 +About the policies which supports copy-down a directory, see
1511 +wbr_policy.txt too.
1512 +
1513 +
1514 +Branch and XINO(External Inode Number Translation Table)
1515 +----------------------------------------------------------------------
1516 +Every branch has its own xino (external inode number translation table)
1517 +file. The xino file is created and unlinked by aufs internally. When two
1518 +members of a union exist on the same filesystem, they share the single
1519 +xino file.
1520 +The struct of a xino file is simple, just a sequence of aufs inode
1521 +numbers which is indexed by the lower inode number.
1522 +In the above sample, assume the inode number of /ro/fileA is i111 and
1523 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1524 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1525 +
1526 +When the inode numbers are not contiguous, the xino file will be sparse
1527 +which has a hole in it and doesn't consume as much disk space as it
1528 +might appear. If your branch filesystem consumes disk space for such
1529 +holes, then you should specify 'xino=' option at mounting aufs.
1530 +
1531 +Aufs has a mount option to free the disk blocks for such holes in XINO
1532 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1533 +meet a problem of disk shortage due to XINO files, then you should try
1534 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1535 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1536 +the holes in XINO files.
1537 +
1538 +Also a writable branch has three kinds of "whiteout bases". All these
1539 +are existed when the branch is joined to aufs, and their names are
1540 +whiteout-ed doubly, so that users will never see their names in aufs
1541 +hierarchy.
1542 +1. a regular file which will be hardlinked to all whiteouts.
1543 +2. a directory to store a pseudo-link.
1544 +3. a directory to store an "orphan"-ed file temporary.
1545 +
1546 +1. Whiteout Base
1547 +   When you remove a file on a readonly branch, aufs handles it as a
1548 +   logical deletion and creates a whiteout on the upper writable branch
1549 +   as a hardlink of this file in order not to consume inode on the
1550 +   writable branch.
1551 +2. Pseudo-link Dir
1552 +   See below, Pseudo-link.
1553 +3. Step-Parent Dir
1554 +   When "fileC" exists on the lower readonly branch only and it is
1555 +   opened and removed with its parent dir, and then user writes
1556 +   something into it, then aufs copies-up fileC to this
1557 +   directory. Because there is no other dir to store fileC. After
1558 +   creating a file under this dir, the file is unlinked.
1559 +
1560 +Because aufs supports manipulating branches, ie. add/delete/change
1561 +dynamically, a branch has its own id. When the branch order changes,
1562 +aufs finds the new index by searching the branch id.
1563 +
1564 +
1565 +Pseudo-link
1566 +----------------------------------------------------------------------
1567 +Assume "fileA" exists on the lower readonly branch only and it is
1568 +hardlinked to "fileB" on the branch. When you write something to fileA,
1569 +aufs copies-up it to the upper writable branch. Additionally aufs
1570 +creates a hardlink under the Pseudo-link Directory of the writable
1571 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1572 +simple list. If fileB is read after unlinking fileA, aufs returns
1573 +filedata from the pseudo-link instead of the lower readonly
1574 +branch. Because the pseudo-link is based upon the inode, to keep the
1575 +inode number by xino (see above) is essentially necessary.
1576 +
1577 +All the hardlinks under the Pseudo-link Directory of the writable branch
1578 +should be restored in a proper location later. Aufs provides a utility
1579 +to do this. The userspace helpers executed at remounting and unmounting
1580 +aufs by default.
1581 +During this utility is running, it puts aufs into the pseudo-link
1582 +maintenance mode. In this mode, only the process which began the
1583 +maintenance mode (and its child processes) is allowed to operate in
1584 +aufs. Some other processes which are not related to the pseudo-link will
1585 +be allowed to run too, but the rest have to return an error or wait
1586 +until the maintenance mode ends. If a process already acquires an inode
1587 +mutex (in VFS), it has to return an error.
1588 +
1589 +
1590 +XIB(external inode number bitmap)
1591 +----------------------------------------------------------------------
1592 +Addition to the xino file per a branch, aufs has an external inode number
1593 +bitmap in a superblock object. It is also an internal file such like a
1594 +xino file.
1595 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1596 +not.
1597 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1598 +
1599 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1600 +reduce the number of consumed disk blocks for these files.
1601 +
1602 +
1603 +Virtual or Vertical Dir, and Readdir in Userspace
1604 +----------------------------------------------------------------------
1605 +In order to support multiple layers (branches), aufs readdir operation
1606 +constructs a virtual dir block on memory. For readdir, aufs calls
1607 +vfs_readdir() internally for each dir on branches, merges their entries
1608 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1609 +object. So the file object has its entry list until it is closed. The
1610 +entry list will be updated when the file position is zero and becomes
1611 +obsoleted. This decision is made in aufs automatically.
1612 +
1613 +The dynamically allocated memory block for the name of entries has a
1614 +unit of 512 bytes (by default) and stores the names contiguously (no
1615 +padding). Another block for each entry is handled by kmem_cache too.
1616 +During building dir blocks, aufs creates hash list and judging whether
1617 +the entry is whiteouted by its upper branch or already listed.
1618 +The merged result is cached in the corresponding inode object and
1619 +maintained by a customizable life-time option.
1620 +
1621 +Some people may call it can be a security hole or invite DoS attack
1622 +since the opened and once readdir-ed dir (file object) holds its entry
1623 +list and becomes a pressure for system memory. But I'd say it is similar
1624 +to files under /proc or /sys. The virtual files in them also holds a
1625 +memory page (generally) while they are opened. When an idea to reduce
1626 +memory for them is introduced, it will be applied to aufs too.
1627 +For those who really hate this situation, I've developed readdir(3)
1628 +library which operates this merging in userspace. You just need to set
1629 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1630 +kernel space for readdir(3).
1631 +
1632 +
1633 +Workqueue
1634 +----------------------------------------------------------------------
1635 +Aufs sometimes requires privilege access to a branch. For instance,
1636 +in copy-up/down operation. When a user process is going to make changes
1637 +to a file which exists in the lower readonly branch only, and the mode
1638 +of one of ancestor directories may not be writable by a user
1639 +process. Here aufs copy-up the file with its ancestors and they may
1640 +require privilege to set its owner/group/mode/etc.
1641 +This is a typical case of a application character of aufs (see
1642 +Introduction).
1643 +
1644 +Aufs uses workqueue synchronously for this case. It creates its own
1645 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1646 +passes the request to call mkdir or write (for example), and wait for
1647 +its completion. This approach solves a problem of a signal handler
1648 +simply.
1649 +If aufs didn't adopt the workqueue and changed the privilege of the
1650 +process, then the process may receive the unexpected SIGXFSZ or other
1651 +signals.
1652 +
1653 +Also aufs uses the system global workqueue ("events" kernel thread) too
1654 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1655 +whiteout base and etc. This is unrelated to a privilege.
1656 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1657 +superblock at the beginning, at the same time waits for the completion
1658 +of all queued asynchronous tasks.
1659 +
1660 +
1661 +Whiteout
1662 +----------------------------------------------------------------------
1663 +The whiteout in aufs is very similar to Unionfs's. That is represented
1664 +by its filename. UnionMount takes an approach of a file mode, but I am
1665 +afraid several utilities (find(1) or something) will have to support it.
1666 +
1667 +Basically the whiteout represents "logical deletion" which stops aufs to
1668 +lookup further, but also it represents "dir is opaque" which also stop
1669 +further lookup.
1670 +
1671 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1672 +In order to make several functions in a single systemcall to be
1673 +revertible, aufs adopts an approach to rename a directory to a temporary
1674 +unique whiteouted name.
1675 +For example, in rename(2) dir where the target dir already existed, aufs
1676 +renames the target dir to a temporary unique whiteouted name before the
1677 +actual rename on a branch, and then handles other actions (make it opaque,
1678 +update the attributes, etc). If an error happens in these actions, aufs
1679 +simply renames the whiteouted name back and returns an error. If all are
1680 +succeeded, aufs registers a function to remove the whiteouted unique
1681 +temporary name completely and asynchronously to the system global
1682 +workqueue.
1683 +
1684 +
1685 +Copy-up
1686 +----------------------------------------------------------------------
1687 +It is a well-known feature or concept.
1688 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1689 +internally and makes change to the new file on the upper writable branch.
1690 +When the trigger systemcall does not update the timestamps of the parent
1691 +dir, aufs reverts it after copy-up.
1692 +
1693 +
1694 +Move-down (aufs3.9 and later)
1695 +----------------------------------------------------------------------
1696 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1697 +the lower readonly branch to the upper writable branch when a user
1698 +changes something about the file.
1699 +"Move-down" is an opposite action of copy-up. Basically this action is
1700 +ran manually instead of automatically and internally.
1701 +For desgin and implementation, aufs has to consider these issues.
1702 +- whiteout for the file may exist on the lower branch.
1703 +- ancestor directories may not exist on the lower branch.
1704 +- diropq for the ancestor directories may exist on the upper branch.
1705 +- free space on the lower branch will reduce.
1706 +- another access to the file may happen during moving-down, including
1707 +  UDBA (see "Revalidate Dentry and UDBA").
1708 +- the file should not be hard-linked nor pseudo-linked. they should be
1709 +  handled by auplink utility later.
1710 +
1711 +Sometimes users want to move-down a file from the upper writable branch
1712 +to the lower readonly or writable branch. For instance,
1713 +- the free space of the upper writable branch is going to run out.
1714 +- create a new intermediate branch between the upper and lower branch.
1715 +- etc.
1716 +
1717 +For this purpose, use "aumvdown" command in aufs-util.git.
1718 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1719 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1720 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2018-04-15 08:49:13.394483860 +0200
1721 @@ -0,0 +1,85 @@
1722 +
1723 +# Copyright (C) 2015-2018 Junjiro R. Okajima
1724 +# 
1725 +# This program is free software; you can redistribute it and/or modify
1726 +# it under the terms of the GNU General Public License as published by
1727 +# the Free Software Foundation; either version 2 of the License, or
1728 +# (at your option) any later version.
1729 +# 
1730 +# This program is distributed in the hope that it will be useful,
1731 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1732 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1733 +# GNU General Public License for more details.
1734 +# 
1735 +# You should have received a copy of the GNU General Public License
1736 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1737 +
1738 +Support for a branch who has its ->atomic_open()
1739 +----------------------------------------------------------------------
1740 +The filesystems who implement its ->atomic_open() are not majority. For
1741 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1742 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1743 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1744 +sure whether all filesystems who have ->atomic_open() behave like this,
1745 +but NFSv4 surely returns the error.
1746 +
1747 +In order to support ->atomic_open() for aufs, there are a few
1748 +approaches.
1749 +
1750 +A. Introduce aufs_atomic_open()
1751 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1752 +     branch fs.
1753 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1754 +   an aufs user Pip Cet's approach
1755 +   - calls aufs_create(), VFS finish_open() and notify_change().
1756 +   - pass fake-mode to finish_open(), and then correct the mode by
1757 +     notify_change().
1758 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1759 +   - no aufs_atomic_open().
1760 +   - aufs_lookup() registers the TID to an aufs internal object.
1761 +   - aufs_create() does nothing when the matching TID is registered, but
1762 +     registers the mode.
1763 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1764 +     TID is registered.
1765 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1766 +   credential
1767 +   - no aufs_atomic_open().
1768 +   - aufs_create() registers the TID to an internal object. this info
1769 +     represents "this process created this file just now."
1770 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1771 +     registered TID and re-try open() with superuser's credential.
1772 +
1773 +Pros and cons for each approach.
1774 +
1775 +A.
1776 +   - straightforward but highly depends upon VFS internal.
1777 +   - the atomic behavaiour is kept.
1778 +   - some of parameters such as nameidata are hard to reproduce for
1779 +     branch fs.
1780 +   - large overhead.
1781 +B.
1782 +   - easy to implement.
1783 +   - the atomic behavaiour is lost.
1784 +C.
1785 +   - the atomic behavaiour is kept.
1786 +   - dirty and tricky.
1787 +   - VFS checks whether the file is created correctly after calling
1788 +     ->create(), which means this approach doesn't work.
1789 +D.
1790 +   - easy to implement.
1791 +   - the atomic behavaiour is lost.
1792 +   - to open a file with superuser's credential and give it to a user
1793 +     process is a bad idea, since the file object keeps the credential
1794 +     in it. It may affect LSM or something. This approach doesn't work
1795 +     either.
1796 +
1797 +The approach A is ideal, but it hard to implement. So here is a
1798 +variation of A, which is to be implemented.
1799 +
1800 +A-1. Introduce aufs_atomic_open()
1801 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1802 +       vfs_create() and finish_open().
1803 +     - the demerit is that the several checks after branch fs
1804 +       ->atomic_open() are lost. in the ordinary case, the checks are
1805 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1806 +       be implemented in aufs, but not all I am afraid.
1807 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1808 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1809 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2018-04-15 08:49:13.394483860 +0200
1810 @@ -0,0 +1,113 @@
1811 +
1812 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1813 +# 
1814 +# This program is free software; you can redistribute it and/or modify
1815 +# it under the terms of the GNU General Public License as published by
1816 +# the Free Software Foundation; either version 2 of the License, or
1817 +# (at your option) any later version.
1818 +# 
1819 +# This program is distributed in the hope that it will be useful,
1820 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1821 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1822 +# GNU General Public License for more details.
1823 +# 
1824 +# You should have received a copy of the GNU General Public License
1825 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1826 +
1827 +Lookup in a Branch
1828 +----------------------------------------------------------------------
1829 +Since aufs has a character of sub-VFS (see Introduction), it operates
1830 +lookup for branches as VFS does. It may be a heavy work. But almost all
1831 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1832 +directly connected to its parent. Digging down the directory hierarchy
1833 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1834 +aufs calls it.
1835 +
1836 +When a branch is a remote filesystem, aufs basically relies upon its
1837 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1838 +them.
1839 +For d_revalidate, aufs implements three levels of revalidate tests. See
1840 +"Revalidate Dentry and UDBA" in detail.
1841 +
1842 +
1843 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1844 +----------------------------------------------------------------------
1845 +Let's try case study.
1846 +- aufs has two branches, upper readwrite and lower readonly.
1847 +  /au = /rw + /ro
1848 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1849 +- user invoked "chmod a+rx /au/dirA"
1850 +- the internal copy-up is activated and "/rw/dirA" is created and its
1851 +  permission bits are set to world readable.
1852 +- then "/au/dirA" becomes world readable?
1853 +
1854 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1855 +or it may be a natively readonly filesystem. If aufs respects the lower
1856 +branch, it should not respond readdir request from other users. But user
1857 +allowed it by chmod. Should really aufs rejects showing the entries
1858 +under /ro/dirA?
1859 +
1860 +To be honest, I don't have a good solution for this case. So aufs
1861 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1862 +users.
1863 +When dirperm1 is specified, aufs checks only the highest one for the
1864 +directory permission, and shows the entries. Otherwise, as usual, checks
1865 +every dir existing on all branches and rejects the request.
1866 +
1867 +As a side effect, dirperm1 option improves the performance of aufs
1868 +because the number of permission check is reduced when the number of
1869 +branch is many.
1870 +
1871 +
1872 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1873 +----------------------------------------------------------------------
1874 +Generally VFS helpers re-validate a dentry as a part of lookup.
1875 +0. digging down the directory hierarchy.
1876 +1. lock the parent dir by its i_mutex.
1877 +2. lookup the final (child) entry.
1878 +3. revalidate it.
1879 +4. call the actual operation (create, unlink, etc.)
1880 +5. unlock the parent dir
1881 +
1882 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1883 +called. Actually aufs implements it and checks the dentry on a branch is
1884 +still valid.
1885 +But it is not enough. Because aufs has to release the lock for the
1886 +parent dir on a branch at the end of ->lookup() (step 2) and
1887 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1888 +held by VFS.
1889 +If the file on a branch is changed directly, eg. bypassing aufs, after
1890 +aufs released the lock, then the subsequent operation may cause
1891 +something unpleasant result.
1892 +
1893 +This situation is a result of VFS architecture, ->lookup() and
1894 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1895 +design from VFS's point of view. It is just not suitable for sub-VFS
1896 +character in aufs.
1897 +
1898 +Aufs supports such case by three level of revalidation which is
1899 +selectable by user.
1900 +1. Simple Revalidate
1901 +   Addition to the native flow in VFS's, confirm the child-parent
1902 +   relationship on the branch just after locking the parent dir on the
1903 +   branch in the "actual operation" (step 4). When this validation
1904 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1905 +   checks the validation of the dentry on branches.
1906 +2. Monitor Changes Internally by Inotify/Fsnotify
1907 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1908 +   the dentry on the branch, and returns EBUSY if it finds different
1909 +   dentry.
1910 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1911 +   during it is in cache. When the event is notified, aufs registers a
1912 +   function to kernel 'events' thread by schedule_work(). And the
1913 +   function sets some special status to the cached aufs dentry and inode
1914 +   private data. If they are not cached, then aufs has nothing to
1915 +   do. When the same file is accessed through aufs (step 0-3) later,
1916 +   aufs will detect the status and refresh all necessary data.
1917 +   In this mode, aufs has to ignore the event which is fired by aufs
1918 +   itself.
1919 +3. No Extra Validation
1920 +   This is the simplest test and doesn't add any additional revalidation
1921 +   test, and skip the revalidation in step 4. It is useful and improves
1922 +   aufs performance when system surely hide the aufs branches from user,
1923 +   by over-mounting something (or another method).
1924 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1925 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1926 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2018-04-15 08:49:13.394483860 +0200
1927 @@ -0,0 +1,74 @@
1928 +
1929 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1930 +# 
1931 +# This program is free software; you can redistribute it and/or modify
1932 +# it under the terms of the GNU General Public License as published by
1933 +# the Free Software Foundation; either version 2 of the License, or
1934 +# (at your option) any later version.
1935 +# 
1936 +# This program is distributed in the hope that it will be useful,
1937 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1938 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1939 +# GNU General Public License for more details.
1940 +# 
1941 +# You should have received a copy of the GNU General Public License
1942 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1943 +
1944 +Branch Manipulation
1945 +
1946 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1947 +and changing its permission/attribute, there are a lot of works to do.
1948 +
1949 +
1950 +Add a Branch
1951 +----------------------------------------------------------------------
1952 +o Confirm the adding dir exists outside of aufs, including loopback
1953 +  mount, and its various attributes.
1954 +o Initialize the xino file and whiteout bases if necessary.
1955 +  See struct.txt.
1956 +
1957 +o Check the owner/group/mode of the directory
1958 +  When the owner/group/mode of the adding directory differs from the
1959 +  existing branch, aufs issues a warning because it may impose a
1960 +  security risk.
1961 +  For example, when a upper writable branch has a world writable empty
1962 +  top directory, a malicious user can create any files on the writable
1963 +  branch directly, like copy-up and modify manually. If something like
1964 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1965 +  writable branch, and the writable branch is world-writable, then a
1966 +  malicious guy may create /etc/passwd on the writable branch directly
1967 +  and the infected file will be valid in aufs.
1968 +  I am afraid it can be a security issue, but aufs can do nothing except
1969 +  producing a warning.
1970 +
1971 +
1972 +Delete a Branch
1973 +----------------------------------------------------------------------
1974 +o Confirm the deleting branch is not busy
1975 +  To be general, there is one merit to adopt "remount" interface to
1976 +  manipulate branches. It is to discard caches. At deleting a branch,
1977 +  aufs checks the still cached (and connected) dentries and inodes. If
1978 +  there are any, then they are all in-use. An inode without its
1979 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1980 +
1981 +  For the cached one, aufs checks whether the same named entry exists on
1982 +  other branches.
1983 +  If the cached one is a directory, because aufs provides a merged view
1984 +  to users, as long as one dir is left on any branch aufs can show the
1985 +  dir to users. In this case, the branch can be removed from aufs.
1986 +  Otherwise aufs rejects deleting the branch.
1987 +
1988 +  If any file on the deleting branch is opened by aufs, then aufs
1989 +  rejects deleting.
1990 +
1991 +
1992 +Modify the Permission of a Branch
1993 +----------------------------------------------------------------------
1994 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1995 +  See struct.txt.
1996 +
1997 +o rw --> ro: Confirm the modifying branch is not busy
1998 +  Aufs rejects the request if any of these conditions are true.
1999 +  - a file on the branch is mmap-ed.
2000 +  - a regular file on the branch is opened for write and there is no
2001 +    same named entry on the upper branch.
2002 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
2003 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
2004 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2018-04-15 08:49:13.394483860 +0200
2005 @@ -0,0 +1,64 @@
2006 +
2007 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2008 +# 
2009 +# This program is free software; you can redistribute it and/or modify
2010 +# it under the terms of the GNU General Public License as published by
2011 +# the Free Software Foundation; either version 2 of the License, or
2012 +# (at your option) any later version.
2013 +# 
2014 +# This program is distributed in the hope that it will be useful,
2015 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2016 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2017 +# GNU General Public License for more details.
2018 +# 
2019 +# You should have received a copy of the GNU General Public License
2020 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2021 +
2022 +Policies to Select One among Multiple Writable Branches
2023 +----------------------------------------------------------------------
2024 +When the number of writable branch is more than one, aufs has to decide
2025 +the target branch for file creation or copy-up. By default, the highest
2026 +writable branch which has the parent (or ancestor) dir of the target
2027 +file is chosen (top-down-parent policy).
2028 +By user's request, aufs implements some other policies to select the
2029 +writable branch, for file creation several policies, round-robin,
2030 +most-free-space, and other policies. For copy-up, top-down-parent,
2031 +bottom-up-parent, bottom-up and others.
2032 +
2033 +As expected, the round-robin policy selects the branch in circular. When
2034 +you have two writable branches and creates 10 new files, 5 files will be
2035 +created for each branch. mkdir(2) systemcall is an exception. When you
2036 +create 10 new directories, all will be created on the same branch.
2037 +And the most-free-space policy selects the one which has most free
2038 +space among the writable branches. The amount of free space will be
2039 +checked by aufs internally, and users can specify its time interval.
2040 +
2041 +The policies for copy-up is more simple,
2042 +top-down-parent is equivalent to the same named on in create policy,
2043 +bottom-up-parent selects the writable branch where the parent dir
2044 +exists and the nearest upper one from the copyup-source,
2045 +bottom-up selects the nearest upper writable branch from the
2046 +copyup-source, regardless the existence of the parent dir.
2047 +
2048 +There are some rules or exceptions to apply these policies.
2049 +- If there is a readonly branch above the policy-selected branch and
2050 +  the parent dir is marked as opaque (a variation of whiteout), or the
2051 +  target (creating) file is whiteout-ed on the upper readonly branch,
2052 +  then the result of the policy is ignored and the target file will be
2053 +  created on the nearest upper writable branch than the readonly branch.
2054 +- If there is a writable branch above the policy-selected branch and
2055 +  the parent dir is marked as opaque or the target file is whiteouted
2056 +  on the branch, then the result of the policy is ignored and the target
2057 +  file will be created on the highest one among the upper writable
2058 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
2059 +  it as usual.
2060 +- link(2) and rename(2) systemcalls are exceptions in every policy.
2061 +  They try selecting the branch where the source exists as possible
2062 +  since copyup a large file will take long time. If it can't be,
2063 +  ie. the branch where the source exists is readonly, then they will
2064 +  follow the copyup policy.
2065 +- There is an exception for rename(2) when the target exists.
2066 +  If the rename target exists, aufs compares the index of the branches
2067 +  where the source and the target exists and selects the higher
2068 +  one. If the selected branch is readonly, then aufs follows the
2069 +  copyup policy.
2070 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2071 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
2072 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2018-04-15 08:49:13.394483860 +0200
2073 @@ -0,0 +1,31 @@
2074 +
2075 +// to view this graph, run dot(1) command in GRAPHVIZ.
2076 +
2077 +digraph G {
2078 +node [shape=box];
2079 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2080 +
2081 +node [shape=oval];
2082 +
2083 +aufs_rename -> whinfo [label="store/remove"];
2084 +
2085 +node [shape=oval];
2086 +inode_list [label="h_inum list in branch\ncache"];
2087 +
2088 +node [shape=box];
2089 +whinode [label="h_inum list file"];
2090 +
2091 +node [shape=oval];
2092 +brmgmt [label="br_add/del/mod/umount"];
2093 +
2094 +brmgmt -> inode_list [label="create/remove"];
2095 +brmgmt -> whinode [label="load/store"];
2096 +
2097 +inode_list -> whinode [style=dashed,dir=both];
2098 +
2099 +aufs_rename -> inode_list [label="add/del"];
2100 +
2101 +aufs_lookup -> inode_list [label="search"];
2102 +
2103 +aufs_lookup -> whinfo [label="load/remove"];
2104 +}
2105 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2106 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
2107 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2018-04-15 08:49:13.394483860 +0200
2108 @@ -0,0 +1,102 @@
2109 +
2110 +# Copyright (C) 2017-2018 Junjiro R. Okajima
2111 +#
2112 +# This program is free software; you can redistribute it and/or modify
2113 +# it under the terms of the GNU General Public License as published by
2114 +# the Free Software Foundation; either version 2 of the License, or
2115 +# (at your option) any later version.
2116 +#
2117 +# This program is distributed in the hope that it will be useful,
2118 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2119 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2120 +# GNU General Public License for more details.
2121 +#
2122 +# You should have received a copy of the GNU General Public License
2123 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2124 +
2125 +Special handling for renaming a directory (DIRREN)
2126 +----------------------------------------------------------------------
2127 +First, let's assume we have a simple usecase.
2128 +
2129 +- /u = /rw + /ro
2130 +- /rw/dirA exists
2131 +- /ro/dirA and /ro/dirA/file exist too
2132 +- there is no dirB on both branches
2133 +- a user issues rename("dirA", "dirB")
2134 +
2135 +Now, what should aufs behave against this rename(2)?
2136 +There are a few possible cases.
2137 +
2138 +A. returns EROFS.
2139 +   since dirA exists on a readonly branch which cannot be renamed.
2140 +B. returns EXDEV.
2141 +   it is possible to copy-up dirA (only the dir itself), but the child
2142 +   entries ("file" in this case) should not be. it must be a bad
2143 +   approach to copy-up recursively.
2144 +C. returns a success.
2145 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
2146 +   is a violation of aufs' policy.
2147 +D. construct an extra information which indicates that /ro/dirA should
2148 +   be handled as the name of dirB.
2149 +   overlayfs has a similar feature called REDIRECT.
2150 +
2151 +Until now, aufs implements the case B only which returns EXDEV, and
2152 +expects the userspace application behaves like mv(1) which tries
2153 +issueing rename(2) recursively.
2154 +
2155 +A new aufs feature called DIRREN is introduced which implements the case
2156 +D. There are several "extra information" added.
2157 +
2158 +1. detailed info per renamed directory
2159 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2160 +2. the inode-number list of directories on a branch
2161 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
2162 +
2163 +The filename of "detailed info per directory" represents the lower
2164 +branch, and its format is
2165 +- a type of the branch id
2166 +  one of these.
2167 +  + uuid (not implemented yet)
2168 +  + fsid
2169 +  + dev
2170 +- the inode-number of the branch root dir
2171 +
2172 +And it contains these info in a single regular file.
2173 +- magic number
2174 +- branch's inode-number of the logically renamed dir
2175 +- the name of the before-renamed dir
2176 +
2177 +The "detailed info per directory" file is created in aufs rename(2), and
2178 +loaded in any lookup.
2179 +The info is considered in lookup for the matching case only. Here
2180 +"matching" means that the root of branch (in the info filename) is same
2181 +to the current looking-up branch. After looking-up the before-renamed
2182 +name, the inode-number is compared. And the matched dentry is used.
2183 +
2184 +The "inode-number list of directories" is a regular file which contains
2185 +simply the inode-numbers on the branch. The file is created or updated
2186 +in removing the branch, and loaded in adding the branch. Its lifetime is
2187 +equal to the branch.
2188 +The list is refered in lookup, and when the current target inode is
2189 +found in the list, the aufs tries loading the "detailed info per
2190 +directory" and get the changed and valid name of the dir.
2191 +
2192 +Theoretically these "extra informaiton" may be able to be put into XATTR
2193 +in the dir inode. But aufs doesn't choose this way because
2194 +1. XATTR may not be supported by the branch (or its configuration)
2195 +2. XATTR may have its size limit.
2196 +3. XATTR may be less easy to convert than a regular file, when the
2197 +   format of the info is changed in the future.
2198 +At the same time, I agree that the regular file approach is much slower
2199 +than XATTR approach. So, in the future, aufs may take the XATTR or other
2200 +better approach.
2201 +
2202 +This DIRREN feature is enabled by aufs configuration, and is activated
2203 +by a new mount option.
2204 +
2205 +For the more complicated case, there is a work with UDBA option, which
2206 +is to dected the direct access to the branches (by-passing aufs) and to
2207 +maintain the cashes in aufs. Since a single cached aufs dentry may
2208 +contains two names, before- and after-rename, the name comparision in
2209 +UDBA handler may not work correctly. In this case, the behaviour will be
2210 +equivalen to udba=reval case.
2211 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2212 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2213 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2018-04-15 08:49:13.394483860 +0200
2214 @@ -0,0 +1,120 @@
2215 +
2216 +# Copyright (C) 2011-2018 Junjiro R. Okajima
2217 +# 
2218 +# This program is free software; you can redistribute it and/or modify
2219 +# it under the terms of the GNU General Public License as published by
2220 +# the Free Software Foundation; either version 2 of the License, or
2221 +# (at your option) any later version.
2222 +# 
2223 +# This program is distributed in the hope that it will be useful,
2224 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2225 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2226 +# GNU General Public License for more details.
2227 +# 
2228 +# You should have received a copy of the GNU General Public License
2229 +# along with this program; if not, write to the Free Software
2230 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2231 +
2232 +
2233 +File-based Hierarchical Storage Management (FHSM)
2234 +----------------------------------------------------------------------
2235 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2236 +storage world. Aufs provides this feature as file-based with multiple
2237 +writable branches, based upon the principle of "Colder, the Lower".
2238 +Here the word "colder" means that the less used files, and "lower" means
2239 +that the position in the order of the stacked branches vertically.
2240 +These multiple writable branches are prioritized, ie. the topmost one
2241 +should be the fastest drive and be used heavily.
2242 +
2243 +o Characters in aufs FHSM story
2244 +- aufs itself and a new branch attribute.
2245 +- a new ioctl interface to move-down and to establish a connection with
2246 +  the daemon ("move-down" is a converse of "copy-up").
2247 +- userspace tool and daemon.
2248 +
2249 +The userspace daemon establishes a connection with aufs and waits for
2250 +the notification. The notified information is very similar to struct
2251 +statfs containing the number of consumed blocks and inodes.
2252 +When the consumed blocks/inodes of a branch exceeds the user-specified
2253 +upper watermark, the daemon activates its move-down process until the
2254 +consumed blocks/inodes reaches the user-specified lower watermark.
2255 +
2256 +The actual move-down is done by aufs based upon the request from
2257 +user-space since we need to maintain the inode number and the internal
2258 +pointer arrays in aufs.
2259 +
2260 +Currently aufs FHSM handles the regular files only. Additionally they
2261 +must not be hard-linked nor pseudo-linked.
2262 +
2263 +
2264 +o Cowork of aufs and the user-space daemon
2265 +  During the userspace daemon established the connection, aufs sends a
2266 +  small notification to it whenever aufs writes something into the
2267 +  writable branch. But it may cost high since aufs issues statfs(2)
2268 +  internally. So user can specify a new option to cache the
2269 +  info. Actually the notification is controlled by these factors.
2270 +  + the specified cache time.
2271 +  + classified as "force" by aufs internally.
2272 +  Until the specified time expires, aufs doesn't send the info
2273 +  except the forced cases. When aufs decide forcing, the info is always
2274 +  notified to userspace.
2275 +  For example, the number of free inodes is generally large enough and
2276 +  the shortage of it happens rarely. So aufs doesn't force the
2277 +  notification when creating a new file, directory and others. This is
2278 +  the typical case which aufs doesn't force.
2279 +  When aufs writes the actual filedata and the files consumes any of new
2280 +  blocks, the aufs forces notifying.
2281 +
2282 +
2283 +o Interfaces in aufs
2284 +- New branch attribute.
2285 +  + fhsm
2286 +    Specifies that the branch is managed by FHSM feature. In other word,
2287 +    participant in the FHSM.
2288 +    When nofhsm is set to the branch, it will not be the source/target
2289 +    branch of the move-down operation. This attribute is set
2290 +    independently from coo and moo attributes, and if you want full
2291 +    FHSM, you should specify them as well.
2292 +- New mount option.
2293 +  + fhsm_sec
2294 +    Specifies a second to suppress many less important info to be
2295 +    notified.
2296 +- New ioctl.
2297 +  + AUFS_CTL_FHSM_FD
2298 +    create a new file descriptor which userspace can read the notification
2299 +    (a subset of struct statfs) from aufs.
2300 +- Module parameter 'brs'
2301 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2302 +  be set.
2303 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2304 +  When there are two or more branches with fhsm attributes,
2305 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2306 +  terminates it. As a result of remounting and branch-manipulation, the
2307 +  number of branches with fhsm attribute can be one. In this case,
2308 +  /sbin/mount.aufs will terminate the user-space daemon.
2309 +
2310 +
2311 +Finally the operation is done as these steps in kernel-space.
2312 +- make sure that,
2313 +  + no one else is using the file.
2314 +  + the file is not hard-linked.
2315 +  + the file is not pseudo-linked.
2316 +  + the file is a regular file.
2317 +  + the parent dir is not opaqued.
2318 +- find the target writable branch.
2319 +- make sure the file is not whiteout-ed by the upper (than the target)
2320 +  branch.
2321 +- make the parent dir on the target branch.
2322 +- mutex lock the inode on the branch.
2323 +- unlink the whiteout on the target branch (if exists).
2324 +- lookup and create the whiteout-ed temporary name on the target branch.
2325 +- copy the file as the whiteout-ed temporary name on the target branch.
2326 +- rename the whiteout-ed temporary name to the original name.
2327 +- unlink the file on the source branch.
2328 +- maintain the internal pointer array and the external inode number
2329 +  table (XINO).
2330 +- maintain the timestamps and other attributes of the parent dir and the
2331 +  file.
2332 +
2333 +And of course, in every step, an error may happen. So the operation
2334 +should restore the original file state after an error happens.
2335 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2336 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2337 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2018-04-15 08:49:13.394483860 +0200
2338 @@ -0,0 +1,72 @@
2339 +
2340 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2341 +# 
2342 +# This program is free software; you can redistribute it and/or modify
2343 +# it under the terms of the GNU General Public License as published by
2344 +# the Free Software Foundation; either version 2 of the License, or
2345 +# (at your option) any later version.
2346 +# 
2347 +# This program is distributed in the hope that it will be useful,
2348 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2349 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2350 +# GNU General Public License for more details.
2351 +# 
2352 +# You should have received a copy of the GNU General Public License
2353 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2354 +
2355 +mmap(2) -- File Memory Mapping
2356 +----------------------------------------------------------------------
2357 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2358 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2359 +->mmap().
2360 +This approach is simple and good, but there is one problem.
2361 +Under /proc, several entries show the mmapped files by its path (with
2362 +device and inode number), and the printed path will be the path on the
2363 +branch fs's instead of virtual aufs's.
2364 +This is not a problem in most cases, but some utilities lsof(1) (and its
2365 +user) may expect the path on aufs.
2366 +
2367 +To address this issue, aufs adds a new member called vm_prfile in struct
2368 +vm_area_struct (and struct vm_region). The original vm_file points to
2369 +the file on the branch fs in order to handle everything correctly as
2370 +usual. The new vm_prfile points to a virtual file in aufs, and the
2371 +show-functions in procfs refers to vm_prfile if it is set.
2372 +Also we need to maintain several other places where touching vm_file
2373 +such like
2374 +- fork()/clone() copies vma and the reference count of vm_file is
2375 +  incremented.
2376 +- merging vma maintains the ref count too.
2377 +
2378 +This is not a good approach. It just fakes the printed path. But it
2379 +leaves all behaviour around f_mapping unchanged. This is surely an
2380 +advantage.
2381 +Actually aufs had adopted another complicated approach which calls
2382 +generic_file_mmap() and handles struct vm_operations_struct. In this
2383 +approach, aufs met a hard problem and I could not solve it without
2384 +switching the approach.
2385 +
2386 +There may be one more another approach which is
2387 +- bind-mount the branch-root onto the aufs-root internally
2388 +- grab the new vfsmount (ie. struct mount)
2389 +- lazy-umount the branch-root internally
2390 +- in open(2) the aufs-file, open the branch-file with the hidden
2391 +  vfsmount (instead of the original branch's vfsmount)
2392 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2393 +  but it may be possible from userspace by the mount helper.
2394 +
2395 +Adding the internal hidden vfsmount and using it in opening a file, the
2396 +file path under /proc will be printed correctly. This approach looks
2397 +smarter, but is not possible I am afraid.
2398 +- aufs-root may be bind-mount later. when it happens, another hidden
2399 +  vfsmount will be required.
2400 +- it is hard to get the chance to bind-mount and lazy-umount
2401 +  + in kernel-space, FS can have vfsmount in open(2) via
2402 +    file->f_path, and aufs can know its vfsmount. But several locks are
2403 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2404 +    here, then it may cause a deadlock.
2405 +  + in user-space, bind-mount doesn't invoke the mount helper.
2406 +- since /proc shows dev and ino, aufs has to give vma these info. it
2407 +  means a new member vm_prinode will be necessary. this is essentially
2408 +  equivalent to vm_prfile described above.
2409 +
2410 +I have to give up this "looks-smater" approach.
2411 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2412 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2413 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2018-04-15 08:49:13.394483860 +0200
2414 @@ -0,0 +1,96 @@
2415 +
2416 +# Copyright (C) 2014-2018 Junjiro R. Okajima
2417 +#
2418 +# This program is free software; you can redistribute it and/or modify
2419 +# it under the terms of the GNU General Public License as published by
2420 +# the Free Software Foundation; either version 2 of the License, or
2421 +# (at your option) any later version.
2422 +#
2423 +# This program is distributed in the hope that it will be useful,
2424 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2425 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2426 +# GNU General Public License for more details.
2427 +#
2428 +# You should have received a copy of the GNU General Public License
2429 +# along with this program; if not, write to the Free Software
2430 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2431 +
2432 +
2433 +Listing XATTR/EA and getting the value
2434 +----------------------------------------------------------------------
2435 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2436 +shows the values from the topmost existing file. This behaviour is good
2437 +for the non-dir entries since the bahaviour exactly matches the shown
2438 +information. But for the directories, aufs considers all the same named
2439 +entries on the lower branches. Which means, if one of the lower entry
2440 +rejects readdir call, then aufs returns an error even if the topmost
2441 +entry allows it. This behaviour is necessary to respect the branch fs's
2442 +security, but can make users confused since the user-visible standard
2443 +attributes don't match the behaviour.
2444 +To address this issue, aufs has a mount option called dirperm1 which
2445 +checks the permission for the topmost entry only, and ignores the lower
2446 +entry's permission.
2447 +
2448 +A similar issue can happen around XATTR.
2449 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2450 +always set. Otherwise these very unpleasant situation would happen.
2451 +- listxattr(2) may return the duplicated entries.
2452 +- users may not be able to remove or reset the XATTR forever,
2453 +
2454 +
2455 +XATTR/EA support in the internal (copy,move)-(up,down)
2456 +----------------------------------------------------------------------
2457 +Generally the extended attributes of inode are categorized as these.
2458 +- "security" for LSM and capability.
2459 +- "system" for posix ACL, 'acl' mount option is required for the branch
2460 +  fs generally.
2461 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2462 +- "user" for userspace, 'user_xattr' mount option is required for the
2463 +  branch fs generally.
2464 +
2465 +Moreover there are some other categories. Aufs handles these rather
2466 +unpopular categories as the ordinary ones, ie. there is no special
2467 +condition nor exception.
2468 +
2469 +In copy-up, the support for XATTR on the dst branch may differ from the
2470 +src branch. In this case, the copy-up operation will get an error and
2471 +the original user operation which triggered the copy-up will fail. It
2472 +can happen that even all copy-up will fail.
2473 +When both of src and dst branches support XATTR and if an error occurs
2474 +during copying XATTR, then the copy-up should fail obviously. That is a
2475 +good reason and aufs should return an error to userspace. But when only
2476 +the src branch support that XATTR, aufs should not return an error.
2477 +For example, the src branch supports ACL but the dst branch doesn't
2478 +because the dst branch may natively un-support it or temporary
2479 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2480 +may NOT return an error even if the XATTR is not supported. It is
2481 +totally up to the branch fs.
2482 +
2483 +Anyway when the aufs internal copy-up gets an error from the dst branch
2484 +fs, then aufs tries removing the just copied entry and returns the error
2485 +to the userspace. The worst case of this situation will be all copy-up
2486 +will fail.
2487 +
2488 +For the copy-up operation, there two basic approaches.
2489 +- copy the specified XATTR only (by category above), and return the
2490 +  error unconditionally if it happens.
2491 +- copy all XATTR, and ignore the error on the specified category only.
2492 +
2493 +In order to support XATTR and to implement the correct behaviour, aufs
2494 +chooses the latter approach and introduces some new branch attributes,
2495 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2496 +They correspond to the XATTR namespaces (see above). Additionally, to be
2497 +convenient, "icex" is also provided which means all "icex*" attributes
2498 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2499 +
2500 +The meaning of these attributes is to ignore the error from setting
2501 +XATTR on that branch.
2502 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2503 +error from the dst branch according to the specified attributes.
2504 +
2505 +Some XATTR may have its default value. The default value may come from
2506 +the parent dir or the environment. If the default value is set at the
2507 +file creating-time, it will be overwritten by copy-up.
2508 +Some contradiction may happen I am afraid.
2509 +Do we need another attribute to stop copying XATTR? I am unsure. For
2510 +now, aufs implements the branch attributes to ignore the error.
2511 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2512 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2513 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2018-04-15 08:49:13.394483860 +0200
2514 @@ -0,0 +1,58 @@
2515 +
2516 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2517 +# 
2518 +# This program is free software; you can redistribute it and/or modify
2519 +# it under the terms of the GNU General Public License as published by
2520 +# the Free Software Foundation; either version 2 of the License, or
2521 +# (at your option) any later version.
2522 +# 
2523 +# This program is distributed in the hope that it will be useful,
2524 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2525 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2526 +# GNU General Public License for more details.
2527 +# 
2528 +# You should have received a copy of the GNU General Public License
2529 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2530 +
2531 +Export Aufs via NFS
2532 +----------------------------------------------------------------------
2533 +Here is an approach.
2534 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2535 +  generation.
2536 +- iget_locked(): initialize aufs inode generation for a new inode, and
2537 +  store it in xigen file.
2538 +- destroy_inode(): increment aufs inode generation and store it in xigen
2539 +  file. it is necessary even if it is not unlinked, because any data of
2540 +  inode may be changed by UDBA.
2541 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2542 +  build file handle by
2543 +  + branch id (4 bytes)
2544 +  + superblock generation (4 bytes)
2545 +  + inode number (4 or 8 bytes)
2546 +  + parent dir inode number (4 or 8 bytes)
2547 +  + inode generation (4 bytes))
2548 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2549 +    bytes)
2550 +  + file handle for a branch (by exportfs_encode_fh())
2551 +- fh_to_dentry():
2552 +  + find the index of a branch from its id in handle, and check it is
2553 +    still exist in aufs.
2554 +  + 1st level: get the inode number from handle and search it in cache.
2555 +  + 2nd level: if not found in cache, get the parent inode number from
2556 +    the handle and search it in cache. and then open the found parent
2557 +    dir, find the matching inode number by vfs_readdir() and get its
2558 +    name, and call lookup_one_len() for the target dentry.
2559 +  + 3rd level: if the parent dir is not cached, call
2560 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2561 +    build a pathname of it, convert it a pathname in aufs, call
2562 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2563 +    the 2nd level.
2564 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2565 +    for every branch, but not itself. to get this, (currently) aufs
2566 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2567 +    idea, but I didn't get other approach.
2568 +  + test the generation of the gotten inode.
2569 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2570 +  convert it into ESTALE for NFSD.
2571 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2572 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2573 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2574 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2575 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2018-04-15 08:49:13.394483860 +0200
2576 @@ -0,0 +1,52 @@
2577 +
2578 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2579 +# 
2580 +# This program is free software; you can redistribute it and/or modify
2581 +# it under the terms of the GNU General Public License as published by
2582 +# the Free Software Foundation; either version 2 of the License, or
2583 +# (at your option) any later version.
2584 +# 
2585 +# This program is distributed in the hope that it will be useful,
2586 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2587 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2588 +# GNU General Public License for more details.
2589 +# 
2590 +# You should have received a copy of the GNU General Public License
2591 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2592 +
2593 +Show Whiteout Mode (shwh)
2594 +----------------------------------------------------------------------
2595 +Generally aufs hides the name of whiteouts. But in some cases, to show
2596 +them is very useful for users. For instance, creating a new middle layer
2597 +(branch) by merging existing layers.
2598 +
2599 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2600 +When you have three branches,
2601 +- Bottom: 'system', squashfs (underlying base system), read-only
2602 +- Middle: 'mods', squashfs, read-only
2603 +- Top: 'overlay', ram (tmpfs), read-write
2604 +
2605 +The top layer is loaded at boot time and saved at shutdown, to preserve
2606 +the changes made to the system during the session.
2607 +When larger changes have been made, or smaller changes have accumulated,
2608 +the size of the saved top layer data grows. At this point, it would be
2609 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2610 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2611 +restoring save and load speed.
2612 +
2613 +This merging is simplified by the use of another aufs mount, of just the
2614 +two overlay branches using the 'shwh' option.
2615 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2616 +       aufs /livesys/merge_union
2617 +
2618 +A merged view of these two branches is then available at
2619 +/livesys/merge_union, and the new feature is that the whiteouts are
2620 +visible!
2621 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2622 +writing to all branches. Also the default mode for all branches is 'ro'.
2623 +It is now possible to save the combined contents of the two overlay
2624 +branches to a new squashfs, e.g.:
2625 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2626 +
2627 +This new squashfs archive can be stored on the boot device and the
2628 +initramfs will use it to replace the old one at the next boot.
2629 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2630 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2631 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2018-04-15 08:49:13.394483860 +0200
2632 @@ -0,0 +1,47 @@
2633 +
2634 +# Copyright (C) 2010-2018 Junjiro R. Okajima
2635 +#
2636 +# This program is free software; you can redistribute it and/or modify
2637 +# it under the terms of the GNU General Public License as published by
2638 +# the Free Software Foundation; either version 2 of the License, or
2639 +# (at your option) any later version.
2640 +#
2641 +# This program is distributed in the hope that it will be useful,
2642 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2643 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2644 +# GNU General Public License for more details.
2645 +#
2646 +# You should have received a copy of the GNU General Public License
2647 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2648 +
2649 +Dynamically customizable FS operations
2650 +----------------------------------------------------------------------
2651 +Generally FS operations (struct inode_operations, struct
2652 +address_space_operations, struct file_operations, etc.) are defined as
2653 +"static const", but it never means that FS have only one set of
2654 +operation. Some FS have multiple sets of them. For instance, ext2 has
2655 +three sets, one for XIP, for NOBH, and for normal.
2656 +Since aufs overrides and redirects these operations, sometimes aufs has
2657 +to change its behaviour according to the branch FS type. More importantly
2658 +VFS acts differently if a function (member in the struct) is set or
2659 +not. It means aufs should have several sets of operations and select one
2660 +among them according to the branch FS definition.
2661 +
2662 +In order to solve this problem and not to affect the behaviour of VFS,
2663 +aufs defines these operations dynamically. For instance, aufs defines
2664 +dummy direct_IO function for struct address_space_operations, but it may
2665 +not be set to the address_space_operations actually. When the branch FS
2666 +doesn't have it, aufs doesn't set it to its address_space_operations
2667 +while the function definition itself is still alive. So the behaviour
2668 +itself will not change, and it will return an error when direct_IO is
2669 +not set.
2670 +
2671 +The lifetime of these dynamically generated operation object is
2672 +maintained by aufs branch object. When the branch is removed from aufs,
2673 +the reference counter of the object is decremented. When it reaches
2674 +zero, the dynamically generated operation object will be freed.
2675 +
2676 +This approach is designed to support AIO (io_submit), Direct I/O and
2677 +XIP (DAX) mainly.
2678 +Currently this approach is applied to address_space_operations for
2679 +regular files only.
2680 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2681 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2682 +++ linux/Documentation/filesystems/aufs/README 2018-06-15 11:15:15.400449109 +0200
2683 @@ -0,0 +1,393 @@
2684 +
2685 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2686 +http://aufs.sf.net
2687 +Junjiro R. Okajima
2688 +
2689 +
2690 +0. Introduction
2691 +----------------------------------------
2692 +In the early days, aufs was entirely re-designed and re-implemented
2693 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2694 +improvements and implementations, it becomes totally different from
2695 +Unionfs while keeping the basic features.
2696 +Recently, Unionfs Version 2.x series begin taking some of the same
2697 +approaches to aufs1's.
2698 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2699 +University and his team.
2700 +
2701 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2702 +If you want older kernel version support, try aufs2-2.6.git or
2703 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2704 +
2705 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2706 +      According to Christoph Hellwig, linux rejects all union-type
2707 +      filesystems but UnionMount.
2708 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2709 +
2710 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2711 +    UnionMount, and he pointed out an issue around a directory mutex
2712 +    lock and aufs addressed it. But it is still unsure whether aufs will
2713 +    be merged (or any other union solution).
2714 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2715 +
2716 +
2717 +1. Features
2718 +----------------------------------------
2719 +- unite several directories into a single virtual filesystem. The member
2720 +  directory is called as a branch.
2721 +- you can specify the permission flags to the branch, which are 'readonly',
2722 +  'readwrite' and 'whiteout-able.'
2723 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2724 +  readonly branch are modifiable logically.
2725 +- dynamic branch manipulation, add, del.
2726 +- etc...
2727 +
2728 +Also there are many enhancements in aufs, such as:
2729 +- test only the highest one for the directory permission (dirperm1)
2730 +- copyup on open (coo=)
2731 +- 'move' policy for copy-up between two writable branches, after
2732 +  checking free space.
2733 +- xattr, acl
2734 +- readdir(3) in userspace.
2735 +- keep inode number by external inode number table
2736 +- keep the timestamps of file/dir in internal copyup operation
2737 +- seekable directory, supporting NFS readdir.
2738 +- whiteout is hardlinked in order to reduce the consumption of inodes
2739 +  on branch
2740 +- do not copyup, nor create a whiteout when it is unnecessary
2741 +- revert a single systemcall when an error occurs in aufs
2742 +- remount interface instead of ioctl
2743 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2744 +- loopback mounted filesystem as a branch
2745 +- kernel thread for removing the dir who has a plenty of whiteouts
2746 +- support copyup sparse file (a file which has a 'hole' in it)
2747 +- default permission flags for branches
2748 +- selectable permission flags for ro branch, whether whiteout can
2749 +  exist or not
2750 +- export via NFS.
2751 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2752 +- support multiple writable branches, some policies to select one
2753 +  among multiple writable branches.
2754 +- a new semantics for link(2) and rename(2) to support multiple
2755 +  writable branches.
2756 +- no glibc changes are required.
2757 +- pseudo hardlink (hardlink over branches)
2758 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2759 +  including NFS or remote filesystem branch.
2760 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2761 +- and more...
2762 +
2763 +Currently these features are dropped temporary from aufs4.
2764 +See design/08plan.txt in detail.
2765 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2766 +  (robr)
2767 +- statistics of aufs thread (/sys/fs/aufs/stat)
2768 +
2769 +Features or just an idea in the future (see also design/*.txt),
2770 +- reorder the branch index without del/re-add.
2771 +- permanent xino files for NFSD
2772 +- an option for refreshing the opened files after add/del branches
2773 +- light version, without branch manipulation. (unnecessary?)
2774 +- copyup in userspace
2775 +- inotify in userspace
2776 +- readv/writev
2777 +
2778 +
2779 +2. Download
2780 +----------------------------------------
2781 +There are three GIT trees for aufs4, aufs4-linux.git,
2782 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2783 +"aufs-util.git."
2784 +While the aufs-util is always necessary, you need either of aufs4-linux
2785 +or aufs4-standalone.
2786 +
2787 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2788 +git://git.kernel.org/.../torvalds/linux.git.
2789 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2790 +build aufs4 as an external kernel module.
2791 +Several extra patches are not included in this tree. Only
2792 +aufs4-standalone tree contains them. They are described in the later
2793 +section "Configuration and Compilation."
2794 +
2795 +On the other hand, the aufs4-standalone tree has only aufs source files
2796 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2797 +But you need to apply all aufs patches manually.
2798 +
2799 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2800 +represents the linux kernel version, "linux-4.x". For instance,
2801 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2802 +"aufs4.x-rcN" branch.
2803 +
2804 +o aufs4-linux tree
2805 +$ git clone --reference /your/linux/git/tree \
2806 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2807 +- if you don't have linux GIT tree, then remove "--reference ..."
2808 +$ cd aufs4-linux.git
2809 +$ git checkout origin/aufs4.0
2810 +
2811 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2812 +leave the patch-work to GIT.
2813 +$ cd /your/linux/git/tree
2814 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2815 +$ git fetch aufs4
2816 +$ git checkout -b my4.0 v4.0
2817 +$ (add your local change...)
2818 +$ git pull aufs4 aufs4.0
2819 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2820 +- you may need to solve some conflicts between your_changes and
2821 +  aufs4.0. in this case, git-rerere is recommended so that you can
2822 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2823 +  later in the future.
2824 +
2825 +o aufs4-standalone tree
2826 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2827 +$ cd aufs4-standalone.git
2828 +$ git checkout origin/aufs4.0
2829 +
2830 +o aufs-util tree
2831 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2832 +- note that the public aufs-util.git is on SourceForge instead of
2833 +  GitHUB.
2834 +$ cd aufs-util.git
2835 +$ git checkout origin/aufs4.0
2836 +
2837 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2838 +The minor version number, 'x' in '4.x', of aufs may not always
2839 +follow the minor version number of the kernel.
2840 +Because changes in the kernel that cause the use of a new
2841 +minor version number do not always require changes to aufs-util.
2842 +
2843 +Since aufs-util has its own minor version number, you may not be
2844 +able to find a GIT branch in aufs-util for your kernel's
2845 +exact minor version number.
2846 +In this case, you should git-checkout the branch for the
2847 +nearest lower number.
2848 +
2849 +For (an unreleased) example:
2850 +If you are using "linux-4.10" and the "aufs4.10" branch
2851 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2852 +or something numerically smaller is the branch for your kernel.
2853 +
2854 +Also you can view all branches by
2855 +       $ git branch -a
2856 +
2857 +
2858 +3. Configuration and Compilation
2859 +----------------------------------------
2860 +Make sure you have git-checkout'ed the correct branch.
2861 +
2862 +For aufs4-linux tree,
2863 +- enable CONFIG_AUFS_FS.
2864 +- set other aufs configurations if necessary.
2865 +
2866 +For aufs4-standalone tree,
2867 +There are several ways to build.
2868 +
2869 +1.
2870 +- apply ./aufs4-kbuild.patch to your kernel source files.
2871 +- apply ./aufs4-base.patch too.
2872 +- apply ./aufs4-mmap.patch too.
2873 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2874 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2875 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2876 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2877 +- enable CONFIG_AUFS_FS, you can select either
2878 +  =m or =y.
2879 +- and build your kernel as usual.
2880 +- install the built kernel.
2881 +  Note: Since linux-3.9, every filesystem module requires an alias
2882 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2883 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2884 +- install the header files too by "make headers_install" to the
2885 +  directory where you specify. By default, it is $PWD/usr.
2886 +  "make help" shows a brief note for headers_install.
2887 +- and reboot your system.
2888 +
2889 +2.
2890 +- module only (CONFIG_AUFS_FS=m).
2891 +- apply ./aufs4-base.patch to your kernel source files.
2892 +- apply ./aufs4-mmap.patch too.
2893 +- apply ./aufs4-standalone.patch too.
2894 +- build your kernel, don't forget "make headers_install", and reboot.
2895 +- edit ./config.mk and set other aufs configurations if necessary.
2896 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2897 +  every aufs configurations.
2898 +- build the module by simple "make".
2899 +  Note: Since linux-3.9, every filesystem module requires an alias
2900 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2901 +  modules.aliases file.
2902 +- you can specify ${KDIR} make variable which points to your kernel
2903 +  source tree.
2904 +- install the files
2905 +  + run "make install" to install the aufs module, or copy the built
2906 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2907 +  + run "make install_headers" (instead of headers_install) to install
2908 +    the modified aufs header file (you can specify DESTDIR which is
2909 +    available in aufs standalone version's Makefile only), or copy
2910 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2911 +    you like manually. By default, the target directory is $PWD/usr.
2912 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2913 +  kernel source tree.
2914 +
2915 +Note: The header file aufs_type.h is necessary to build aufs-util
2916 +      as well as "make headers_install" in the kernel source tree.
2917 +      headers_install is subject to be forgotten, but it is essentially
2918 +      necessary, not only for building aufs-util.
2919 +      You may not meet problems without headers_install in some older
2920 +      version though.
2921 +
2922 +And then,
2923 +- read README in aufs-util, build and install it
2924 +- note that your distribution may contain an obsoleted version of
2925 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2926 +  utilities, make sure that your compiler refers the correct aufs header
2927 +  file which is built by "make headers_install."
2928 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2929 +  then run "make install_ulib" too. And refer to the aufs manual in
2930 +  detail.
2931 +
2932 +There several other patches in aufs4-standalone.git. They are all
2933 +optional. When you meet some problems, they will help you.
2934 +- aufs4-loopback.patch
2935 +  Supports a nested loopback mount in a branch-fs. This patch is
2936 +  unnecessary until aufs produces a message like "you may want to try
2937 +  another patch for loopback file".
2938 +- vfs-ino.patch
2939 +  Modifies a system global kernel internal function get_next_ino() in
2940 +  order to stop assigning 0 for an inode-number. Not directly related to
2941 +  aufs, but recommended generally.
2942 +- tmpfs-idr.patch
2943 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2944 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2945 +  duplication of inode number, which is important for backup tools and
2946 +  other utilities. When you find aufs XINO files for tmpfs branch
2947 +  growing too much, try this patch.
2948 +- lockdep-debug.patch
2949 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2950 +  also a caller of VFS functions for branch filesystems, subclassing of
2951 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2952 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2953 +  need to apply this debug patch to expand several constant values.
2954 +  If don't know what LOCKDEP, then you don't have apply this patch.
2955 +
2956 +
2957 +4. Usage
2958 +----------------------------------------
2959 +At first, make sure aufs-util are installed, and please read the aufs
2960 +manual, aufs.5 in aufs-util.git tree.
2961 +$ man -l aufs.5
2962 +
2963 +And then,
2964 +$ mkdir /tmp/rw /tmp/aufs
2965 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2966 +
2967 +Here is another example. The result is equivalent.
2968 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2969 +  Or
2970 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2971 +# mount -o remount,append:${HOME} /tmp/aufs
2972 +
2973 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2974 +you modify a file under /tmp/aufs, the one on your home directory is
2975 +not affected, instead the same named file will be newly created under
2976 +/tmp/rw. And all of your modification to a file will be applied to
2977 +the one under /tmp/rw. This is called the file based Copy on Write
2978 +(COW) method.
2979 +Aufs mount options are described in aufs.5.
2980 +If you run chroot or something and make your aufs as a root directory,
2981 +then you need to customize the shutdown script. See the aufs manual in
2982 +detail.
2983 +
2984 +Additionally, there are some sample usages of aufs which are a
2985 +diskless system with network booting, and LiveCD over NFS.
2986 +See sample dir in CVS tree on SourceForge.
2987 +
2988 +
2989 +5. Contact
2990 +----------------------------------------
2991 +When you have any problems or strange behaviour in aufs, please let me
2992 +know with:
2993 +- /proc/mounts (instead of the output of mount(8))
2994 +- /sys/module/aufs/*
2995 +- /sys/fs/aufs/* (if you have them)
2996 +- /debug/aufs/* (if you have them)
2997 +- linux kernel version
2998 +  if your kernel is not plain, for example modified by distributor,
2999 +  the url where i can download its source is necessary too.
3000 +- aufs version which was printed at loading the module or booting the
3001 +  system, instead of the date you downloaded.
3002 +- configuration (define/undefine CONFIG_AUFS_xxx)
3003 +- kernel configuration or /proc/config.gz (if you have it)
3004 +- behaviour which you think to be incorrect
3005 +- actual operation, reproducible one is better
3006 +- mailto: aufs-users at lists.sourceforge.net
3007 +
3008 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
3009 +and Feature Requests) on SourceForge. Please join and write to
3010 +aufs-users ML.
3011 +
3012 +
3013 +6. Acknowledgements
3014 +----------------------------------------
3015 +Thanks to everyone who have tried and are using aufs, whoever
3016 +have reported a bug or any feedback.
3017 +
3018 +Especially donators:
3019 +Tomas Matejicek(slax.org) made a donation (much more than once).
3020 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
3021 +       scripts) is making "doubling" donations.
3022 +       Unfortunately I cannot list all of the donators, but I really
3023 +       appreciate.
3024 +       It ends Aug 2010, but the ordinary donation URL is still available.
3025 +       <http://sourceforge.net/donate/index.php?group_id=167503>
3026 +Dai Itasaka made a donation (2007/8).
3027 +Chuck Smith made a donation (2008/4, 10 and 12).
3028 +Henk Schoneveld made a donation (2008/9).
3029 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3030 +Francois Dupoux made a donation (2008/11).
3031 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3032 +       aufs2 GIT tree (2009/2).
3033 +William Grant made a donation (2009/3).
3034 +Patrick Lane made a donation (2009/4).
3035 +The Mail Archive (mail-archive.com) made donations (2009/5).
3036 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3037 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3038 +Pavel Pronskiy made a donation (2011/2).
3039 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3040 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
3041 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3042 +11).
3043 +Sam Liddicott made a donation (2011/9).
3044 +Era Scarecrow made a donation (2013/4).
3045 +Bor Ratajc made a donation (2013/4).
3046 +Alessandro Gorreta made a donation (2013/4).
3047 +POIRETTE Marc made a donation (2013/4).
3048 +Alessandro Gorreta made a donation (2013/4).
3049 +lauri kasvandik made a donation (2013/5).
3050 +"pemasu from Finland" made a donation (2013/7).
3051 +The Parted Magic Project made a donation (2013/9 and 11).
3052 +Pavel Barta made a donation (2013/10).
3053 +Nikolay Pertsev made a donation (2014/5).
3054 +James B made a donation (2014/7 and 2015/7).
3055 +Stefano Di Biase made a donation (2014/8).
3056 +Daniel Epellei made a donation (2015/1).
3057 +OmegaPhil made a donation (2016/1, 2018/4).
3058 +Tomasz Szewczyk made a donation (2016/4).
3059 +James Burry made a donation (2016/12).
3060 +
3061 +Thank you very much.
3062 +Donations are always, including future donations, very important and
3063 +helpful for me to keep on developing aufs.
3064 +
3065 +
3066 +7.
3067 +----------------------------------------
3068 +If you are an experienced user, no explanation is needed. Aufs is
3069 +just a linux filesystem.
3070 +
3071 +
3072 +Enjoy!
3073 +
3074 +# Local variables: ;
3075 +# mode: text;
3076 +# End: ;
3077 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3078 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
3079 +++ linux/fs/aufs/aufs.h        2018-08-12 23:43:05.450124426 +0200
3080 @@ -0,0 +1,61 @@
3081 +/* SPDX-License-Identifier: GPL-2.0 */
3082 +/*
3083 + * Copyright (C) 2005-2018 Junjiro R. Okajima
3084 + *
3085 + * This program, aufs is free software; you can redistribute it and/or modify
3086 + * it under the terms of the GNU General Public License as published by
3087 + * the Free Software Foundation; either version 2 of the License, or
3088 + * (at your option) any later version.
3089 + *
3090 + * This program is distributed in the hope that it will be useful,
3091 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3092 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3093 + * GNU General Public License for more details.
3094 + *
3095 + * You should have received a copy of the GNU General Public License
3096 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3097 + */
3098 +
3099 +/*
3100 + * all header files
3101 + */
3102 +
3103 +#ifndef __AUFS_H__
3104 +#define __AUFS_H__
3105 +
3106 +#ifdef __KERNEL__
3107 +
3108 +#define AuStub(type, name, body, ...) \
3109 +       static inline type name(__VA_ARGS__) { body; }
3110 +
3111 +#define AuStubVoid(name, ...) \
3112 +       AuStub(void, name, , __VA_ARGS__)
3113 +#define AuStubInt0(name, ...) \
3114 +       AuStub(int, name, return 0, __VA_ARGS__)
3115 +
3116 +#include "debug.h"
3117 +
3118 +#include "branch.h"
3119 +#include "cpup.h"
3120 +#include "dcsub.h"
3121 +#include "dbgaufs.h"
3122 +#include "dentry.h"
3123 +#include "dir.h"
3124 +#include "dirren.h"
3125 +#include "dynop.h"
3126 +#include "file.h"
3127 +#include "fstype.h"
3128 +#include "hbl.h"
3129 +#include "inode.h"
3130 +#include "loop.h"
3131 +#include "module.h"
3132 +#include "opts.h"
3133 +#include "rwsem.h"
3134 +#include "super.h"
3135 +#include "sysaufs.h"
3136 +#include "vfsub.h"
3137 +#include "whout.h"
3138 +#include "wkq.h"
3139 +
3140 +#endif /* __KERNEL__ */
3141 +#endif /* __AUFS_H__ */
3142 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3143 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
3144 +++ linux/fs/aufs/branch.c      2018-08-12 23:43:05.450124426 +0200
3145 @@ -0,0 +1,1422 @@
3146 +// SPDX-License-Identifier: GPL-2.0
3147 +/*
3148 + * Copyright (C) 2005-2018 Junjiro R. Okajima
3149 + *
3150 + * This program, aufs is free software; you can redistribute it and/or modify
3151 + * it under the terms of the GNU General Public License as published by
3152 + * the Free Software Foundation; either version 2 of the License, or
3153 + * (at your option) any later version.
3154 + *
3155 + * This program is distributed in the hope that it will be useful,
3156 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3157 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3158 + * GNU General Public License for more details.
3159 + *
3160 + * You should have received a copy of the GNU General Public License
3161 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3162 + */
3163 +
3164 +/*
3165 + * branch management
3166 + */
3167 +
3168 +#include <linux/compat.h>
3169 +#include <linux/statfs.h>
3170 +#include "aufs.h"
3171 +
3172 +/*
3173 + * free a single branch
3174 + */
3175 +static void au_br_do_free(struct au_branch *br)
3176 +{
3177 +       int i;
3178 +       struct au_wbr *wbr;
3179 +       struct au_dykey **key;
3180 +
3181 +       au_hnotify_fin_br(br);
3182 +       /* always, regardless the mount option */
3183 +       au_dr_hino_free(&br->br_dirren);
3184 +       au_xino_put(br);
3185 +
3186 +       AuDebugOn(au_br_count(br));
3187 +       au_br_count_fin(br);
3188 +
3189 +       wbr = br->br_wbr;
3190 +       if (wbr) {
3191 +               for (i = 0; i < AuBrWh_Last; i++)
3192 +                       dput(wbr->wbr_wh[i]);
3193 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3194 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3195 +       }
3196 +
3197 +       if (br->br_fhsm) {
3198 +               au_br_fhsm_fin(br->br_fhsm);
3199 +               kfree(br->br_fhsm);
3200 +       }
3201 +
3202 +       key = br->br_dykey;
3203 +       for (i = 0; i < AuBrDynOp; i++, key++)
3204 +               if (*key)
3205 +                       au_dy_put(*key);
3206 +               else
3207 +                       break;
3208 +
3209 +       /* recursive lock, s_umount of branch's */
3210 +       lockdep_off();
3211 +       path_put(&br->br_path);
3212 +       lockdep_on();
3213 +       kfree(wbr);
3214 +       kfree(br);
3215 +}
3216 +
3217 +/*
3218 + * frees all branches
3219 + */
3220 +void au_br_free(struct au_sbinfo *sbinfo)
3221 +{
3222 +       aufs_bindex_t bmax;
3223 +       struct au_branch **br;
3224 +
3225 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3226 +
3227 +       bmax = sbinfo->si_bbot + 1;
3228 +       br = sbinfo->si_branch;
3229 +       while (bmax--)
3230 +               au_br_do_free(*br++);
3231 +}
3232 +
3233 +/*
3234 + * find the index of a branch which is specified by @br_id.
3235 + */
3236 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3237 +{
3238 +       aufs_bindex_t bindex, bbot;
3239 +
3240 +       bbot = au_sbbot(sb);
3241 +       for (bindex = 0; bindex <= bbot; bindex++)
3242 +               if (au_sbr_id(sb, bindex) == br_id)
3243 +                       return bindex;
3244 +       return -1;
3245 +}
3246 +
3247 +/* ---------------------------------------------------------------------- */
3248 +
3249 +/*
3250 + * add a branch
3251 + */
3252 +
3253 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3254 +                       struct dentry *h_root)
3255 +{
3256 +       if (unlikely(h_adding == h_root
3257 +                    || au_test_loopback_overlap(sb, h_adding)))
3258 +               return 1;
3259 +       if (h_adding->d_sb != h_root->d_sb)
3260 +               return 0;
3261 +       return au_test_subdir(h_adding, h_root)
3262 +               || au_test_subdir(h_root, h_adding);
3263 +}
3264 +
3265 +/*
3266 + * returns a newly allocated branch. @new_nbranch is a number of branches
3267 + * after adding a branch.
3268 + */
3269 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3270 +                                    int perm)
3271 +{
3272 +       struct au_branch *add_branch;
3273 +       struct dentry *root;
3274 +       struct inode *inode;
3275 +       int err;
3276 +
3277 +       err = -ENOMEM;
3278 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3279 +       if (unlikely(!add_branch))
3280 +               goto out;
3281 +       add_branch->br_xino = au_xino_alloc();
3282 +       if (unlikely(!add_branch->br_xino))
3283 +               goto out_br;
3284 +       err = au_hnotify_init_br(add_branch, perm);
3285 +       if (unlikely(err))
3286 +               goto out_xino;
3287 +
3288 +       if (au_br_writable(perm)) {
3289 +               /* may be freed separately at changing the branch permission */
3290 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3291 +                                            GFP_NOFS);
3292 +               if (unlikely(!add_branch->br_wbr))
3293 +                       goto out_hnotify;
3294 +       }
3295 +
3296 +       if (au_br_fhsm(perm)) {
3297 +               err = au_fhsm_br_alloc(add_branch);
3298 +               if (unlikely(err))
3299 +                       goto out_wbr;
3300 +       }
3301 +
3302 +       root = sb->s_root;
3303 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3304 +       if (!err)
3305 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3306 +       if (!err) {
3307 +               inode = d_inode(root);
3308 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3309 +                                       /*may_shrink*/0);
3310 +       }
3311 +       if (!err)
3312 +               return add_branch; /* success */
3313 +
3314 +out_wbr:
3315 +       kfree(add_branch->br_wbr);
3316 +out_hnotify:
3317 +       au_hnotify_fin_br(add_branch);
3318 +out_xino:
3319 +       au_xino_put(add_branch);
3320 +out_br:
3321 +       kfree(add_branch);
3322 +out:
3323 +       return ERR_PTR(err);
3324 +}
3325 +
3326 +/*
3327 + * test if the branch permission is legal or not.
3328 + */
3329 +static int test_br(struct inode *inode, int brperm, char *path)
3330 +{
3331 +       int err;
3332 +
3333 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3334 +       if (!err)
3335 +               goto out;
3336 +
3337 +       err = -EINVAL;
3338 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3339 +
3340 +out:
3341 +       return err;
3342 +}
3343 +
3344 +/*
3345 + * returns:
3346 + * 0: success, the caller will add it
3347 + * plus: success, it is already unified, the caller should ignore it
3348 + * minus: error
3349 + */
3350 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3351 +{
3352 +       int err;
3353 +       aufs_bindex_t bbot, bindex;
3354 +       struct dentry *root, *h_dentry;
3355 +       struct inode *inode, *h_inode;
3356 +
3357 +       root = sb->s_root;
3358 +       bbot = au_sbbot(sb);
3359 +       if (unlikely(bbot >= 0
3360 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3361 +               err = 1;
3362 +               if (!remount) {
3363 +                       err = -EINVAL;
3364 +                       pr_err("%s duplicated\n", add->pathname);
3365 +               }
3366 +               goto out;
3367 +       }
3368 +
3369 +       err = -ENOSPC; /* -E2BIG; */
3370 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3371 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3372 +               pr_err("number of branches exceeded %s\n", add->pathname);
3373 +               goto out;
3374 +       }
3375 +
3376 +       err = -EDOM;
3377 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3378 +               pr_err("bad index %d\n", add->bindex);
3379 +               goto out;
3380 +       }
3381 +
3382 +       inode = d_inode(add->path.dentry);
3383 +       err = -ENOENT;
3384 +       if (unlikely(!inode->i_nlink)) {
3385 +               pr_err("no existence %s\n", add->pathname);
3386 +               goto out;
3387 +       }
3388 +
3389 +       err = -EINVAL;
3390 +       if (unlikely(inode->i_sb == sb)) {
3391 +               pr_err("%s must be outside\n", add->pathname);
3392 +               goto out;
3393 +       }
3394 +
3395 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3396 +               pr_err("unsupported filesystem, %s (%s)\n",
3397 +                      add->pathname, au_sbtype(inode->i_sb));
3398 +               goto out;
3399 +       }
3400 +
3401 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3402 +               pr_err("already stacked, %s (%s)\n",
3403 +                      add->pathname, au_sbtype(inode->i_sb));
3404 +               goto out;
3405 +       }
3406 +
3407 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3408 +       if (unlikely(err))
3409 +               goto out;
3410 +
3411 +       if (bbot < 0)
3412 +               return 0; /* success */
3413 +
3414 +       err = -EINVAL;
3415 +       for (bindex = 0; bindex <= bbot; bindex++)
3416 +               if (unlikely(test_overlap(sb, add->path.dentry,
3417 +                                         au_h_dptr(root, bindex)))) {
3418 +                       pr_err("%s is overlapped\n", add->pathname);
3419 +                       goto out;
3420 +               }
3421 +
3422 +       err = 0;
3423 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3424 +               h_dentry = au_h_dptr(root, 0);
3425 +               h_inode = d_inode(h_dentry);
3426 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3427 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3428 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3429 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3430 +                               add->pathname,
3431 +                               i_uid_read(inode), i_gid_read(inode),
3432 +                               (inode->i_mode & S_IALLUGO),
3433 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3434 +                               (h_inode->i_mode & S_IALLUGO));
3435 +       }
3436 +
3437 +out:
3438 +       return err;
3439 +}
3440 +
3441 +/*
3442 + * initialize or clean the whiteouts for an adding branch
3443 + */
3444 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3445 +                        int new_perm)
3446 +{
3447 +       int err, old_perm;
3448 +       aufs_bindex_t bindex;
3449 +       struct inode *h_inode;
3450 +       struct au_wbr *wbr;
3451 +       struct au_hinode *hdir;
3452 +       struct dentry *h_dentry;
3453 +
3454 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3455 +       if (unlikely(err))
3456 +               goto out;
3457 +
3458 +       wbr = br->br_wbr;
3459 +       old_perm = br->br_perm;
3460 +       br->br_perm = new_perm;
3461 +       hdir = NULL;
3462 +       h_inode = NULL;
3463 +       bindex = au_br_index(sb, br->br_id);
3464 +       if (0 <= bindex) {
3465 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3466 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3467 +       } else {
3468 +               h_dentry = au_br_dentry(br);
3469 +               h_inode = d_inode(h_dentry);
3470 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3471 +       }
3472 +       if (!wbr)
3473 +               err = au_wh_init(br, sb);
3474 +       else {
3475 +               wbr_wh_write_lock(wbr);
3476 +               err = au_wh_init(br, sb);
3477 +               wbr_wh_write_unlock(wbr);
3478 +       }
3479 +       if (hdir)
3480 +               au_hn_inode_unlock(hdir);
3481 +       else
3482 +               inode_unlock(h_inode);
3483 +       vfsub_mnt_drop_write(au_br_mnt(br));
3484 +       br->br_perm = old_perm;
3485 +
3486 +       if (!err && wbr && !au_br_writable(new_perm)) {
3487 +               kfree(wbr);
3488 +               br->br_wbr = NULL;
3489 +       }
3490 +
3491 +out:
3492 +       return err;
3493 +}
3494 +
3495 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3496 +                      int perm)
3497 +{
3498 +       int err;
3499 +       struct kstatfs kst;
3500 +       struct au_wbr *wbr;
3501 +
3502 +       wbr = br->br_wbr;
3503 +       au_rw_init(&wbr->wbr_wh_rwsem);
3504 +       atomic_set(&wbr->wbr_wh_running, 0);
3505 +
3506 +       /*
3507 +        * a limit for rmdir/rename a dir
3508 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3509 +        */
3510 +       err = vfs_statfs(&br->br_path, &kst);
3511 +       if (unlikely(err))
3512 +               goto out;
3513 +       err = -EINVAL;
3514 +       if (kst.f_namelen >= NAME_MAX)
3515 +               err = au_br_init_wh(sb, br, perm);
3516 +       else
3517 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3518 +                      au_br_dentry(br),
3519 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3520 +
3521 +out:
3522 +       return err;
3523 +}
3524 +
3525 +/* initialize a new branch */
3526 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3527 +                     struct au_opt_add *add)
3528 +{
3529 +       int err;
3530 +       struct au_branch *brbase;
3531 +       struct file *xf;
3532 +       struct inode *h_inode;
3533 +
3534 +       err = 0;
3535 +       br->br_perm = add->perm;
3536 +       br->br_path = add->path; /* set first, path_get() later */
3537 +       spin_lock_init(&br->br_dykey_lock);
3538 +       au_br_count_init(br);
3539 +       br->br_id = au_new_br_id(sb);
3540 +       AuDebugOn(br->br_id < 0);
3541 +
3542 +       /* always, regardless the given option */
3543 +       err = au_dr_br_init(sb, br, &add->path);
3544 +       if (unlikely(err))
3545 +               goto out_err;
3546 +
3547 +       if (au_br_writable(add->perm)) {
3548 +               err = au_wbr_init(br, sb, add->perm);
3549 +               if (unlikely(err))
3550 +                       goto out_err;
3551 +       }
3552 +
3553 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3554 +               brbase = au_sbr(sb, 0);
3555 +               xf = au_xino_file(brbase);
3556 +               AuDebugOn(!xf);
3557 +               h_inode = d_inode(add->path.dentry);
3558 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3559 +               if (unlikely(err)) {
3560 +                       AuDebugOn(au_xino_file(br));
3561 +                       goto out_err;
3562 +               }
3563 +       }
3564 +
3565 +       sysaufs_br_init(br);
3566 +       path_get(&br->br_path);
3567 +       goto out; /* success */
3568 +
3569 +out_err:
3570 +       memset(&br->br_path, 0, sizeof(br->br_path));
3571 +out:
3572 +       return err;
3573 +}
3574 +
3575 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3576 +                            struct au_branch *br, aufs_bindex_t bbot,
3577 +                            aufs_bindex_t amount)
3578 +{
3579 +       struct au_branch **brp;
3580 +
3581 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3582 +
3583 +       brp = sbinfo->si_branch + bindex;
3584 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3585 +       *brp = br;
3586 +       sbinfo->si_bbot++;
3587 +       if (unlikely(bbot < 0))
3588 +               sbinfo->si_bbot = 0;
3589 +}
3590 +
3591 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3592 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3593 +{
3594 +       struct au_hdentry *hdp;
3595 +
3596 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3597 +
3598 +       hdp = au_hdentry(dinfo, bindex);
3599 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3600 +       au_h_dentry_init(hdp);
3601 +       dinfo->di_bbot++;
3602 +       if (unlikely(bbot < 0))
3603 +               dinfo->di_btop = 0;
3604 +}
3605 +
3606 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3607 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3608 +{
3609 +       struct au_hinode *hip;
3610 +
3611 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3612 +
3613 +       hip = au_hinode(iinfo, bindex);
3614 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3615 +       au_hinode_init(hip);
3616 +       iinfo->ii_bbot++;
3617 +       if (unlikely(bbot < 0))
3618 +               iinfo->ii_btop = 0;
3619 +}
3620 +
3621 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3622 +                        aufs_bindex_t bindex)
3623 +{
3624 +       struct dentry *root, *h_dentry;
3625 +       struct inode *root_inode, *h_inode;
3626 +       aufs_bindex_t bbot, amount;
3627 +
3628 +       root = sb->s_root;
3629 +       root_inode = d_inode(root);
3630 +       bbot = au_sbbot(sb);
3631 +       amount = bbot + 1 - bindex;
3632 +       h_dentry = au_br_dentry(br);
3633 +       au_sbilist_lock();
3634 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3635 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3636 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3637 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3638 +       h_inode = d_inode(h_dentry);
3639 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3640 +       au_sbilist_unlock();
3641 +}
3642 +
3643 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3644 +{
3645 +       int err;
3646 +       aufs_bindex_t bbot, add_bindex;
3647 +       struct dentry *root, *h_dentry;
3648 +       struct inode *root_inode;
3649 +       struct au_branch *add_branch;
3650 +       struct file *xf;
3651 +
3652 +       root = sb->s_root;
3653 +       root_inode = d_inode(root);
3654 +       IMustLock(root_inode);
3655 +       IiMustWriteLock(root_inode);
3656 +       err = test_add(sb, add, remount);
3657 +       if (unlikely(err < 0))
3658 +               goto out;
3659 +       if (err) {
3660 +               err = 0;
3661 +               goto out; /* success */
3662 +       }
3663 +
3664 +       bbot = au_sbbot(sb);
3665 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3666 +       err = PTR_ERR(add_branch);
3667 +       if (IS_ERR(add_branch))
3668 +               goto out;
3669 +
3670 +       err = au_br_init(add_branch, sb, add);
3671 +       if (unlikely(err)) {
3672 +               au_br_do_free(add_branch);
3673 +               goto out;
3674 +       }
3675 +
3676 +       add_bindex = add->bindex;
3677 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3678 +       au_br_do_add(sb, add_branch, add_bindex);
3679 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3680 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3681 +
3682 +       h_dentry = add->path.dentry;
3683 +       if (!add_bindex) {
3684 +               au_cpup_attr_all(root_inode, /*force*/1);
3685 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3686 +       } else
3687 +               au_add_nlink(root_inode, d_inode(h_dentry));
3688 +
3689 +       /*
3690 +        * this test/set prevents aufs from handling unnecessary notify events
3691 +        * of xino files, in case of re-adding a writable branch which was
3692 +        * once detached from aufs.
3693 +        */
3694 +       if (au_xino_brid(sb) < 0
3695 +           && au_br_writable(add_branch->br_perm)
3696 +           && !au_test_fs_bad_xino(h_dentry->d_sb)) {
3697 +               xf = au_xino_file(add_branch);
3698 +               if (xf && xf->f_path.dentry->d_parent == h_dentry)
3699 +                       au_xino_brid_set(sb, add_branch->br_id);
3700 +       }
3701 +
3702 +out:
3703 +       return err;
3704 +}
3705 +
3706 +/* ---------------------------------------------------------------------- */
3707 +
3708 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3709 +                                      unsigned long long max __maybe_unused,
3710 +                                      void *arg)
3711 +{
3712 +       unsigned long long n;
3713 +       struct file **p, *f;
3714 +       struct hlist_bl_head *files;
3715 +       struct hlist_bl_node *pos;
3716 +       struct au_finfo *finfo;
3717 +
3718 +       n = 0;
3719 +       p = a;
3720 +       files = &au_sbi(sb)->si_files;
3721 +       hlist_bl_lock(files);
3722 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3723 +               f = finfo->fi_file;
3724 +               if (file_count(f)
3725 +                   && !special_file(file_inode(f)->i_mode)) {
3726 +                       get_file(f);
3727 +                       *p++ = f;
3728 +                       n++;
3729 +                       AuDebugOn(n > max);
3730 +               }
3731 +       }
3732 +       hlist_bl_unlock(files);
3733 +
3734 +       return n;
3735 +}
3736 +
3737 +static struct file **au_farray_alloc(struct super_block *sb,
3738 +                                    unsigned long long *max)
3739 +{
3740 +       *max = au_nfiles(sb);
3741 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3742 +}
3743 +
3744 +static void au_farray_free(struct file **a, unsigned long long max)
3745 +{
3746 +       unsigned long long ull;
3747 +
3748 +       for (ull = 0; ull < max; ull++)
3749 +               if (a[ull])
3750 +                       fput(a[ull]);
3751 +       kvfree(a);
3752 +}
3753 +
3754 +/* ---------------------------------------------------------------------- */
3755 +
3756 +/*
3757 + * delete a branch
3758 + */
3759 +
3760 +/* to show the line number, do not make it inlined function */
3761 +#define AuVerbose(do_info, fmt, ...) do { \
3762 +       if (do_info) \
3763 +               pr_info(fmt, ##__VA_ARGS__); \
3764 +} while (0)
3765 +
3766 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3767 +                        aufs_bindex_t bbot)
3768 +{
3769 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3770 +}
3771 +
3772 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3773 +                        aufs_bindex_t bbot)
3774 +{
3775 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3776 +}
3777 +
3778 +/*
3779 + * test if the branch is deletable or not.
3780 + */
3781 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3782 +                           unsigned int sigen, const unsigned int verbose)
3783 +{
3784 +       int err, i, j, ndentry;
3785 +       aufs_bindex_t btop, bbot;
3786 +       struct au_dcsub_pages dpages;
3787 +       struct au_dpage *dpage;
3788 +       struct dentry *d;
3789 +
3790 +       err = au_dpages_init(&dpages, GFP_NOFS);
3791 +       if (unlikely(err))
3792 +               goto out;
3793 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3794 +       if (unlikely(err))
3795 +               goto out_dpages;
3796 +
3797 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3798 +               dpage = dpages.dpages + i;
3799 +               ndentry = dpage->ndentry;
3800 +               for (j = 0; !err && j < ndentry; j++) {
3801 +                       d = dpage->dentries[j];
3802 +                       AuDebugOn(au_dcount(d) <= 0);
3803 +                       if (!au_digen_test(d, sigen)) {
3804 +                               di_read_lock_child(d, AuLock_IR);
3805 +                               if (unlikely(au_dbrange_test(d))) {
3806 +                                       di_read_unlock(d, AuLock_IR);
3807 +                                       continue;
3808 +                               }
3809 +                       } else {
3810 +                               di_write_lock_child(d);
3811 +                               if (unlikely(au_dbrange_test(d))) {
3812 +                                       di_write_unlock(d);
3813 +                                       continue;
3814 +                               }
3815 +                               err = au_reval_dpath(d, sigen);
3816 +                               if (!err)
3817 +                                       di_downgrade_lock(d, AuLock_IR);
3818 +                               else {
3819 +                                       di_write_unlock(d);
3820 +                                       break;
3821 +                               }
3822 +                       }
3823 +
3824 +                       /* AuDbgDentry(d); */
3825 +                       btop = au_dbtop(d);
3826 +                       bbot = au_dbbot(d);
3827 +                       if (btop <= bindex
3828 +                           && bindex <= bbot
3829 +                           && au_h_dptr(d, bindex)
3830 +                           && au_test_dbusy(d, btop, bbot)) {
3831 +                               err = -EBUSY;
3832 +                               AuVerbose(verbose, "busy %pd\n", d);
3833 +                               AuDbgDentry(d);
3834 +                       }
3835 +                       di_read_unlock(d, AuLock_IR);
3836 +               }
3837 +       }
3838 +
3839 +out_dpages:
3840 +       au_dpages_free(&dpages);
3841 +out:
3842 +       return err;
3843 +}
3844 +
3845 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3846 +                          unsigned int sigen, const unsigned int verbose)
3847 +{
3848 +       int err;
3849 +       unsigned long long max, ull;
3850 +       struct inode *i, **array;
3851 +       aufs_bindex_t btop, bbot;
3852 +
3853 +       array = au_iarray_alloc(sb, &max);
3854 +       err = PTR_ERR(array);
3855 +       if (IS_ERR(array))
3856 +               goto out;
3857 +
3858 +       err = 0;
3859 +       AuDbg("b%d\n", bindex);
3860 +       for (ull = 0; !err && ull < max; ull++) {
3861 +               i = array[ull];
3862 +               if (unlikely(!i))
3863 +                       break;
3864 +               if (i->i_ino == AUFS_ROOT_INO)
3865 +                       continue;
3866 +
3867 +               /* AuDbgInode(i); */
3868 +               if (au_iigen(i, NULL) == sigen)
3869 +                       ii_read_lock_child(i);
3870 +               else {
3871 +                       ii_write_lock_child(i);
3872 +                       err = au_refresh_hinode_self(i);
3873 +                       au_iigen_dec(i);
3874 +                       if (!err)
3875 +                               ii_downgrade_lock(i);
3876 +                       else {
3877 +                               ii_write_unlock(i);
3878 +                               break;
3879 +                       }
3880 +               }
3881 +
3882 +               btop = au_ibtop(i);
3883 +               bbot = au_ibbot(i);
3884 +               if (btop <= bindex
3885 +                   && bindex <= bbot
3886 +                   && au_h_iptr(i, bindex)
3887 +                   && au_test_ibusy(i, btop, bbot)) {
3888 +                       err = -EBUSY;
3889 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3890 +                       AuDbgInode(i);
3891 +               }
3892 +               ii_read_unlock(i);
3893 +       }
3894 +       au_iarray_free(array, max);
3895 +
3896 +out:
3897 +       return err;
3898 +}
3899 +
3900 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3901 +                             const unsigned int verbose)
3902 +{
3903 +       int err;
3904 +       unsigned int sigen;
3905 +
3906 +       sigen = au_sigen(root->d_sb);
3907 +       DiMustNoWaiters(root);
3908 +       IiMustNoWaiters(d_inode(root));
3909 +       di_write_unlock(root);
3910 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3911 +       if (!err)
3912 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3913 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3914 +
3915 +       return err;
3916 +}
3917 +
3918 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3919 +                        struct file **to_free, int *idx)
3920 +{
3921 +       int err;
3922 +       unsigned char matched, root;
3923 +       aufs_bindex_t bindex, bbot;
3924 +       struct au_fidir *fidir;
3925 +       struct au_hfile *hfile;
3926 +
3927 +       err = 0;
3928 +       root = IS_ROOT(file->f_path.dentry);
3929 +       if (root) {
3930 +               get_file(file);
3931 +               to_free[*idx] = file;
3932 +               (*idx)++;
3933 +               goto out;
3934 +       }
3935 +
3936 +       matched = 0;
3937 +       fidir = au_fi(file)->fi_hdir;
3938 +       AuDebugOn(!fidir);
3939 +       bbot = au_fbbot_dir(file);
3940 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3941 +               hfile = fidir->fd_hfile + bindex;
3942 +               if (!hfile->hf_file)
3943 +                       continue;
3944 +
3945 +               if (hfile->hf_br->br_id == br_id) {
3946 +                       matched = 1;
3947 +                       break;
3948 +               }
3949 +       }
3950 +       if (matched)
3951 +               err = -EBUSY;
3952 +
3953 +out:
3954 +       return err;
3955 +}
3956 +
3957 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3958 +                         struct file **to_free, int opened)
3959 +{
3960 +       int err, idx;
3961 +       unsigned long long ull, max;
3962 +       aufs_bindex_t btop;
3963 +       struct file *file, **array;
3964 +       struct dentry *root;
3965 +       struct au_hfile *hfile;
3966 +
3967 +       array = au_farray_alloc(sb, &max);
3968 +       err = PTR_ERR(array);
3969 +       if (IS_ERR(array))
3970 +               goto out;
3971 +
3972 +       err = 0;
3973 +       idx = 0;
3974 +       root = sb->s_root;
3975 +       di_write_unlock(root);
3976 +       for (ull = 0; ull < max; ull++) {
3977 +               file = array[ull];
3978 +               if (unlikely(!file))
3979 +                       break;
3980 +
3981 +               /* AuDbg("%pD\n", file); */
3982 +               fi_read_lock(file);
3983 +               btop = au_fbtop(file);
3984 +               if (!d_is_dir(file->f_path.dentry)) {
3985 +                       hfile = &au_fi(file)->fi_htop;
3986 +                       if (hfile->hf_br->br_id == br_id)
3987 +                               err = -EBUSY;
3988 +               } else
3989 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3990 +               fi_read_unlock(file);
3991 +               if (unlikely(err))
3992 +                       break;
3993 +       }
3994 +       di_write_lock_child(root);
3995 +       au_farray_free(array, max);
3996 +       AuDebugOn(idx > opened);
3997 +
3998 +out:
3999 +       return err;
4000 +}
4001 +
4002 +static void br_del_file(struct file **to_free, unsigned long long opened,
4003 +                       aufs_bindex_t br_id)
4004 +{
4005 +       unsigned long long ull;
4006 +       aufs_bindex_t bindex, btop, bbot, bfound;
4007 +       struct file *file;
4008 +       struct au_fidir *fidir;
4009 +       struct au_hfile *hfile;
4010 +
4011 +       for (ull = 0; ull < opened; ull++) {
4012 +               file = to_free[ull];
4013 +               if (unlikely(!file))
4014 +                       break;
4015 +
4016 +               /* AuDbg("%pD\n", file); */
4017 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
4018 +               bfound = -1;
4019 +               fidir = au_fi(file)->fi_hdir;
4020 +               AuDebugOn(!fidir);
4021 +               fi_write_lock(file);
4022 +               btop = au_fbtop(file);
4023 +               bbot = au_fbbot_dir(file);
4024 +               for (bindex = btop; bindex <= bbot; bindex++) {
4025 +                       hfile = fidir->fd_hfile + bindex;
4026 +                       if (!hfile->hf_file)
4027 +                               continue;
4028 +
4029 +                       if (hfile->hf_br->br_id == br_id) {
4030 +                               bfound = bindex;
4031 +                               break;
4032 +                       }
4033 +               }
4034 +               AuDebugOn(bfound < 0);
4035 +               au_set_h_fptr(file, bfound, NULL);
4036 +               if (bfound == btop) {
4037 +                       for (btop++; btop <= bbot; btop++)
4038 +                               if (au_hf_dir(file, btop)) {
4039 +                                       au_set_fbtop(file, btop);
4040 +                                       break;
4041 +                               }
4042 +               }
4043 +               fi_write_unlock(file);
4044 +       }
4045 +}
4046 +
4047 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4048 +                            const aufs_bindex_t bindex,
4049 +                            const aufs_bindex_t bbot)
4050 +{
4051 +       struct au_branch **brp, **p;
4052 +
4053 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
4054 +
4055 +       brp = sbinfo->si_branch + bindex;
4056 +       if (bindex < bbot)
4057 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4058 +       sbinfo->si_branch[0 + bbot] = NULL;
4059 +       sbinfo->si_bbot--;
4060 +
4061 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4062 +                       /*may_shrink*/1);
4063 +       if (p)
4064 +               sbinfo->si_branch = p;
4065 +       /* harmless error */
4066 +}
4067 +
4068 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
4069 +                            const aufs_bindex_t bbot)
4070 +{
4071 +       struct au_hdentry *hdp, *p;
4072 +
4073 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4074 +
4075 +       hdp = au_hdentry(dinfo, bindex);
4076 +       if (bindex < bbot)
4077 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4078 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4079 +       dinfo->di_bbot--;
4080 +
4081 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4082 +                       /*may_shrink*/1);
4083 +       if (p)
4084 +               dinfo->di_hdentry = p;
4085 +       /* harmless error */
4086 +}
4087 +
4088 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
4089 +                            const aufs_bindex_t bbot)
4090 +{
4091 +       struct au_hinode *hip, *p;
4092 +
4093 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
4094 +
4095 +       hip = au_hinode(iinfo, bindex);
4096 +       if (bindex < bbot)
4097 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4098 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
4099 +       iinfo->ii_bbot--;
4100 +
4101 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4102 +                       /*may_shrink*/1);
4103 +       if (p)
4104 +               iinfo->ii_hinode = p;
4105 +       /* harmless error */
4106 +}
4107 +
4108 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4109 +                        struct au_branch *br)
4110 +{
4111 +       aufs_bindex_t bbot;
4112 +       struct au_sbinfo *sbinfo;
4113 +       struct dentry *root, *h_root;
4114 +       struct inode *inode, *h_inode;
4115 +       struct au_hinode *hinode;
4116 +
4117 +       SiMustWriteLock(sb);
4118 +
4119 +       root = sb->s_root;
4120 +       inode = d_inode(root);
4121 +       sbinfo = au_sbi(sb);
4122 +       bbot = sbinfo->si_bbot;
4123 +
4124 +       h_root = au_h_dptr(root, bindex);
4125 +       hinode = au_hi(inode, bindex);
4126 +       h_inode = au_igrab(hinode->hi_inode);
4127 +       au_hiput(hinode);
4128 +
4129 +       au_sbilist_lock();
4130 +       au_br_do_del_brp(sbinfo, bindex, bbot);
4131 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
4132 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
4133 +       au_sbilist_unlock();
4134 +
4135 +       /* ignore an error */
4136 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
4137 +
4138 +       dput(h_root);
4139 +       iput(h_inode);
4140 +       au_br_do_free(br);
4141 +}
4142 +
4143 +static unsigned long long empty_cb(struct super_block *sb, void *array,
4144 +                                  unsigned long long max, void *arg)
4145 +{
4146 +       return max;
4147 +}
4148 +
4149 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4150 +{
4151 +       int err, rerr, i;
4152 +       unsigned long long opened;
4153 +       unsigned int mnt_flags;
4154 +       aufs_bindex_t bindex, bbot, br_id;
4155 +       unsigned char do_wh, verbose;
4156 +       struct au_branch *br;
4157 +       struct au_wbr *wbr;
4158 +       struct dentry *root;
4159 +       struct file **to_free;
4160 +
4161 +       err = 0;
4162 +       opened = 0;
4163 +       to_free = NULL;
4164 +       root = sb->s_root;
4165 +       bindex = au_find_dbindex(root, del->h_path.dentry);
4166 +       if (bindex < 0) {
4167 +               if (remount)
4168 +                       goto out; /* success */
4169 +               err = -ENOENT;
4170 +               pr_err("%s no such branch\n", del->pathname);
4171 +               goto out;
4172 +       }
4173 +       AuDbg("bindex b%d\n", bindex);
4174 +
4175 +       err = -EBUSY;
4176 +       mnt_flags = au_mntflags(sb);
4177 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4178 +       bbot = au_sbbot(sb);
4179 +       if (unlikely(!bbot)) {
4180 +               AuVerbose(verbose, "no more branches left\n");
4181 +               goto out;
4182 +       }
4183 +       br = au_sbr(sb, bindex);
4184 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
4185 +
4186 +       br_id = br->br_id;
4187 +       opened = au_br_count(br);
4188 +       if (unlikely(opened)) {
4189 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
4190 +               err = PTR_ERR(to_free);
4191 +               if (IS_ERR(to_free))
4192 +                       goto out;
4193 +
4194 +               err = test_file_busy(sb, br_id, to_free, opened);
4195 +               if (unlikely(err)) {
4196 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4197 +                       goto out;
4198 +               }
4199 +       }
4200 +
4201 +       wbr = br->br_wbr;
4202 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4203 +       if (do_wh) {
4204 +               /* instead of WbrWhMustWriteLock(wbr) */
4205 +               SiMustWriteLock(sb);
4206 +               for (i = 0; i < AuBrWh_Last; i++) {
4207 +                       dput(wbr->wbr_wh[i]);
4208 +                       wbr->wbr_wh[i] = NULL;
4209 +               }
4210 +       }
4211 +
4212 +       err = test_children_busy(root, bindex, verbose);
4213 +       if (unlikely(err)) {
4214 +               if (do_wh)
4215 +                       goto out_wh;
4216 +               goto out;
4217 +       }
4218 +
4219 +       err = 0;
4220 +       if (to_free) {
4221 +               /*
4222 +                * now we confirmed the branch is deletable.
4223 +                * let's free the remaining opened dirs on the branch.
4224 +                */
4225 +               di_write_unlock(root);
4226 +               br_del_file(to_free, opened, br_id);
4227 +               di_write_lock_child(root);
4228 +       }
4229 +
4230 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4231 +       dbgaufs_xino_del(br);           /* remove one */
4232 +       au_br_do_del(sb, bindex, br);
4233 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4234 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4235 +
4236 +       if (!bindex) {
4237 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4238 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4239 +       } else
4240 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4241 +       if (au_opt_test(mnt_flags, PLINK))
4242 +               au_plink_half_refresh(sb, br_id);
4243 +
4244 +       if (au_xino_brid(sb) == br_id)
4245 +               au_xino_brid_set(sb, -1);
4246 +       goto out; /* success */
4247 +
4248 +out_wh:
4249 +       /* revert */
4250 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4251 +       if (rerr)
4252 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4253 +                       del->pathname, rerr);
4254 +out:
4255 +       if (to_free)
4256 +               au_farray_free(to_free, opened);
4257 +       return err;
4258 +}
4259 +
4260 +/* ---------------------------------------------------------------------- */
4261 +
4262 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4263 +{
4264 +       int err;
4265 +       aufs_bindex_t btop, bbot;
4266 +       struct aufs_ibusy ibusy;
4267 +       struct inode *inode, *h_inode;
4268 +
4269 +       err = -EPERM;
4270 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4271 +               goto out;
4272 +
4273 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4274 +       if (!err)
4275 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4276 +       if (unlikely(err)) {
4277 +               err = -EFAULT;
4278 +               AuTraceErr(err);
4279 +               goto out;
4280 +       }
4281 +
4282 +       err = -EINVAL;
4283 +       si_read_lock(sb, AuLock_FLUSH);
4284 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4285 +               goto out_unlock;
4286 +
4287 +       err = 0;
4288 +       ibusy.h_ino = 0; /* invalid */
4289 +       inode = ilookup(sb, ibusy.ino);
4290 +       if (!inode
4291 +           || inode->i_ino == AUFS_ROOT_INO
4292 +           || au_is_bad_inode(inode))
4293 +               goto out_unlock;
4294 +
4295 +       ii_read_lock_child(inode);
4296 +       btop = au_ibtop(inode);
4297 +       bbot = au_ibbot(inode);
4298 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4299 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4300 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4301 +                       ibusy.h_ino = h_inode->i_ino;
4302 +       }
4303 +       ii_read_unlock(inode);
4304 +       iput(inode);
4305 +
4306 +out_unlock:
4307 +       si_read_unlock(sb);
4308 +       if (!err) {
4309 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4310 +               if (unlikely(err)) {
4311 +                       err = -EFAULT;
4312 +                       AuTraceErr(err);
4313 +               }
4314 +       }
4315 +out:
4316 +       return err;
4317 +}
4318 +
4319 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4320 +{
4321 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4322 +}
4323 +
4324 +#ifdef CONFIG_COMPAT
4325 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4326 +{
4327 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4328 +}
4329 +#endif
4330 +
4331 +/* ---------------------------------------------------------------------- */
4332 +
4333 +/*
4334 + * change a branch permission
4335 + */
4336 +
4337 +static void au_warn_ima(void)
4338 +{
4339 +#ifdef CONFIG_IMA
4340 +       /* since it doesn't support mark_files_ro() */
4341 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4342 +#endif
4343 +}
4344 +
4345 +static int do_need_sigen_inc(int a, int b)
4346 +{
4347 +       return au_br_whable(a) && !au_br_whable(b);
4348 +}
4349 +
4350 +static int need_sigen_inc(int old, int new)
4351 +{
4352 +       return do_need_sigen_inc(old, new)
4353 +               || do_need_sigen_inc(new, old);
4354 +}
4355 +
4356 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4357 +{
4358 +       int err, do_warn;
4359 +       unsigned int mnt_flags;
4360 +       unsigned long long ull, max;
4361 +       aufs_bindex_t br_id;
4362 +       unsigned char verbose, writer;
4363 +       struct file *file, *hf, **array;
4364 +       struct au_hfile *hfile;
4365 +
4366 +       mnt_flags = au_mntflags(sb);
4367 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4368 +
4369 +       array = au_farray_alloc(sb, &max);
4370 +       err = PTR_ERR(array);
4371 +       if (IS_ERR(array))
4372 +               goto out;
4373 +
4374 +       do_warn = 0;
4375 +       br_id = au_sbr_id(sb, bindex);
4376 +       for (ull = 0; ull < max; ull++) {
4377 +               file = array[ull];
4378 +               if (unlikely(!file))
4379 +                       break;
4380 +
4381 +               /* AuDbg("%pD\n", file); */
4382 +               fi_read_lock(file);
4383 +               if (unlikely(au_test_mmapped(file))) {
4384 +                       err = -EBUSY;
4385 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4386 +                       AuDbgFile(file);
4387 +                       FiMustNoWaiters(file);
4388 +                       fi_read_unlock(file);
4389 +                       goto out_array;
4390 +               }
4391 +
4392 +               hfile = &au_fi(file)->fi_htop;
4393 +               hf = hfile->hf_file;
4394 +               if (!d_is_reg(file->f_path.dentry)
4395 +                   || !(file->f_mode & FMODE_WRITE)
4396 +                   || hfile->hf_br->br_id != br_id
4397 +                   || !(hf->f_mode & FMODE_WRITE))
4398 +                       array[ull] = NULL;
4399 +               else {
4400 +                       do_warn = 1;
4401 +                       get_file(file);
4402 +               }
4403 +
4404 +               FiMustNoWaiters(file);
4405 +               fi_read_unlock(file);
4406 +               fput(file);
4407 +       }
4408 +
4409 +       err = 0;
4410 +       if (do_warn)
4411 +               au_warn_ima();
4412 +
4413 +       for (ull = 0; ull < max; ull++) {
4414 +               file = array[ull];
4415 +               if (!file)
4416 +                       continue;
4417 +
4418 +               /* todo: already flushed? */
4419 +               /*
4420 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4421 +                * approach which resets f_mode and calls mnt_drop_write() and
4422 +                * file_release_write() for each file, because the branch
4423 +                * attribute in aufs world is totally different from the native
4424 +                * fs rw/ro mode.
4425 +               */
4426 +               /* fi_read_lock(file); */
4427 +               hfile = &au_fi(file)->fi_htop;
4428 +               hf = hfile->hf_file;
4429 +               /* fi_read_unlock(file); */
4430 +               spin_lock(&hf->f_lock);
4431 +               writer = !!(hf->f_mode & FMODE_WRITER);
4432 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4433 +               spin_unlock(&hf->f_lock);
4434 +               if (writer) {
4435 +                       put_write_access(file_inode(hf));
4436 +                       __mnt_drop_write(hf->f_path.mnt);
4437 +               }
4438 +       }
4439 +
4440 +out_array:
4441 +       au_farray_free(array, max);
4442 +out:
4443 +       AuTraceErr(err);
4444 +       return err;
4445 +}
4446 +
4447 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4448 +             int *do_refresh)
4449 +{
4450 +       int err, rerr;
4451 +       aufs_bindex_t bindex;
4452 +       struct dentry *root;
4453 +       struct au_branch *br;
4454 +       struct au_br_fhsm *bf;
4455 +
4456 +       root = sb->s_root;
4457 +       bindex = au_find_dbindex(root, mod->h_root);
4458 +       if (bindex < 0) {
4459 +               if (remount)
4460 +                       return 0; /* success */
4461 +               err = -ENOENT;
4462 +               pr_err("%s no such branch\n", mod->path);
4463 +               goto out;
4464 +       }
4465 +       AuDbg("bindex b%d\n", bindex);
4466 +
4467 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4468 +       if (unlikely(err))
4469 +               goto out;
4470 +
4471 +       br = au_sbr(sb, bindex);
4472 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4473 +       if (br->br_perm == mod->perm)
4474 +               return 0; /* success */
4475 +
4476 +       /* pre-allocate for non-fhsm --> fhsm */
4477 +       bf = NULL;
4478 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4479 +               err = au_fhsm_br_alloc(br);
4480 +               if (unlikely(err))
4481 +                       goto out;
4482 +               bf = br->br_fhsm;
4483 +               br->br_fhsm = NULL;
4484 +       }
4485 +
4486 +       if (au_br_writable(br->br_perm)) {
4487 +               /* remove whiteout base */
4488 +               err = au_br_init_wh(sb, br, mod->perm);
4489 +               if (unlikely(err))
4490 +                       goto out_bf;
4491 +
4492 +               if (!au_br_writable(mod->perm)) {
4493 +                       /* rw --> ro, file might be mmapped */
4494 +                       DiMustNoWaiters(root);
4495 +                       IiMustNoWaiters(d_inode(root));
4496 +                       di_write_unlock(root);
4497 +                       err = au_br_mod_files_ro(sb, bindex);
4498 +                       /* aufs_write_lock() calls ..._child() */
4499 +                       di_write_lock_child(root);
4500 +
4501 +                       if (unlikely(err)) {
4502 +                               rerr = -ENOMEM;
4503 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4504 +                                                    GFP_NOFS);
4505 +                               if (br->br_wbr)
4506 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4507 +                               if (unlikely(rerr)) {
4508 +                                       AuIOErr("nested error %d (%d)\n",
4509 +                                               rerr, err);
4510 +                                       br->br_perm = mod->perm;
4511 +                               }
4512 +                       }
4513 +               }
4514 +       } else if (au_br_writable(mod->perm)) {
4515 +               /* ro --> rw */
4516 +               err = -ENOMEM;
4517 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4518 +               if (br->br_wbr) {
4519 +                       err = au_wbr_init(br, sb, mod->perm);
4520 +                       if (unlikely(err)) {
4521 +                               kfree(br->br_wbr);
4522 +                               br->br_wbr = NULL;
4523 +                       }
4524 +               }
4525 +       }
4526 +       if (unlikely(err))
4527 +               goto out_bf;
4528 +
4529 +       if (au_br_fhsm(br->br_perm)) {
4530 +               if (!au_br_fhsm(mod->perm)) {
4531 +                       /* fhsm --> non-fhsm */
4532 +                       au_br_fhsm_fin(br->br_fhsm);
4533 +                       kfree(br->br_fhsm);
4534 +                       br->br_fhsm = NULL;
4535 +               }
4536 +       } else if (au_br_fhsm(mod->perm))
4537 +               /* non-fhsm --> fhsm */
4538 +               br->br_fhsm = bf;
4539 +
4540 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4541 +       br->br_perm = mod->perm;
4542 +       goto out; /* success */
4543 +
4544 +out_bf:
4545 +       kfree(bf);
4546 +out:
4547 +       AuTraceErr(err);
4548 +       return err;
4549 +}
4550 +
4551 +/* ---------------------------------------------------------------------- */
4552 +
4553 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4554 +{
4555 +       int err;
4556 +       struct kstatfs kstfs;
4557 +
4558 +       err = vfs_statfs(&br->br_path, &kstfs);
4559 +       if (!err) {
4560 +               stfs->f_blocks = kstfs.f_blocks;
4561 +               stfs->f_bavail = kstfs.f_bavail;
4562 +               stfs->f_files = kstfs.f_files;
4563 +               stfs->f_ffree = kstfs.f_ffree;
4564 +       }
4565 +
4566 +       return err;
4567 +}
4568 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4569 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4570 +++ linux/fs/aufs/branch.h      2018-08-12 23:43:05.453457863 +0200
4571 @@ -0,0 +1,374 @@
4572 +/* SPDX-License-Identifier: GPL-2.0 */
4573 +/*
4574 + * Copyright (C) 2005-2018 Junjiro R. Okajima
4575 + *
4576 + * This program, aufs is free software; you can redistribute it and/or modify
4577 + * it under the terms of the GNU General Public License as published by
4578 + * the Free Software Foundation; either version 2 of the License, or
4579 + * (at your option) any later version.
4580 + *
4581 + * This program is distributed in the hope that it will be useful,
4582 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4583 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4584 + * GNU General Public License for more details.
4585 + *
4586 + * You should have received a copy of the GNU General Public License
4587 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4588 + */
4589 +
4590 +/*
4591 + * branch filesystems and xino for them
4592 + */
4593 +
4594 +#ifndef __AUFS_BRANCH_H__
4595 +#define __AUFS_BRANCH_H__
4596 +
4597 +#ifdef __KERNEL__
4598 +
4599 +#include <linux/mount.h>
4600 +#include "dirren.h"
4601 +#include "dynop.h"
4602 +#include "rwsem.h"
4603 +#include "super.h"
4604 +
4605 +/* ---------------------------------------------------------------------- */
4606 +
4607 +/* a xino file */
4608 +struct au_xino {
4609 +       struct file             *xi_file;
4610 +       struct {
4611 +               spinlock_t              spin;
4612 +               ino_t                   *array;
4613 +               int                     total;
4614 +               /* reserved for future use */
4615 +               /* unsigned long        *bitmap; */
4616 +               wait_queue_head_t       wqh;
4617 +       } xi_nondir;
4618 +
4619 +       atomic_t                xi_truncating;
4620 +
4621 +       struct kref             xi_kref;
4622 +
4623 +       /* todo: make xino files an array to support huge inode number */
4624 +};
4625 +
4626 +/* File-based Hierarchical Storage Management */
4627 +struct au_br_fhsm {
4628 +#ifdef CONFIG_AUFS_FHSM
4629 +       struct mutex            bf_lock;
4630 +       unsigned long           bf_jiffy;
4631 +       struct aufs_stfs        bf_stfs;
4632 +       int                     bf_readable;
4633 +#endif
4634 +};
4635 +
4636 +/* members for writable branch only */
4637 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4638 +struct au_wbr {
4639 +       struct au_rwsem         wbr_wh_rwsem;
4640 +       struct dentry           *wbr_wh[AuBrWh_Last];
4641 +       atomic_t                wbr_wh_running;
4642 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4643 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4644 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4645 +
4646 +       /* mfs mode */
4647 +       unsigned long long      wbr_bytes;
4648 +};
4649 +
4650 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4651 +#define AuBrDynOp (AuDyLast * 4)
4652 +
4653 +#ifdef CONFIG_AUFS_HFSNOTIFY
4654 +/* support for asynchronous destruction */
4655 +struct au_br_hfsnotify {
4656 +       struct fsnotify_group   *hfsn_group;
4657 +};
4658 +#endif
4659 +
4660 +/* sysfs entries */
4661 +struct au_brsysfs {
4662 +       char                    name[16];
4663 +       struct attribute        attr;
4664 +};
4665 +
4666 +enum {
4667 +       AuBrSysfs_BR,
4668 +       AuBrSysfs_BRID,
4669 +       AuBrSysfs_Last
4670 +};
4671 +
4672 +/* protected by superblock rwsem */
4673 +struct au_branch {
4674 +       struct au_xino          *br_xino;
4675 +
4676 +       aufs_bindex_t           br_id;
4677 +
4678 +       int                     br_perm;
4679 +       struct path             br_path;
4680 +       spinlock_t              br_dykey_lock;
4681 +       struct au_dykey         *br_dykey[AuBrDynOp];
4682 +       struct percpu_counter   br_count;
4683 +
4684 +       struct au_wbr           *br_wbr;
4685 +       struct au_br_fhsm       *br_fhsm;
4686 +
4687 +#ifdef CONFIG_AUFS_HFSNOTIFY
4688 +       struct au_br_hfsnotify  *br_hfsn;
4689 +#endif
4690 +
4691 +#ifdef CONFIG_SYSFS
4692 +       /* entries under sysfs per mount-point */
4693 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4694 +#endif
4695 +
4696 +#ifdef CONFIG_DEBUG_FS
4697 +       struct dentry            *br_dbgaufs; /* xino */
4698 +#endif
4699 +
4700 +       struct au_dr_br         br_dirren;
4701 +};
4702 +
4703 +/* ---------------------------------------------------------------------- */
4704 +
4705 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4706 +{
4707 +       return br->br_path.mnt;
4708 +}
4709 +
4710 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4711 +{
4712 +       return br->br_path.dentry;
4713 +}
4714 +
4715 +static inline struct super_block *au_br_sb(struct au_branch *br)
4716 +{
4717 +       return au_br_mnt(br)->mnt_sb;
4718 +}
4719 +
4720 +static inline void au_br_get(struct au_branch *br)
4721 +{
4722 +       percpu_counter_inc(&br->br_count);
4723 +}
4724 +
4725 +static inline void au_br_put(struct au_branch *br)
4726 +{
4727 +       percpu_counter_dec(&br->br_count);
4728 +}
4729 +
4730 +static inline s64 au_br_count(struct au_branch *br)
4731 +{
4732 +       return percpu_counter_sum(&br->br_count);
4733 +}
4734 +
4735 +static inline void au_br_count_init(struct au_branch *br)
4736 +{
4737 +       percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4738 +}
4739 +
4740 +static inline void au_br_count_fin(struct au_branch *br)
4741 +{
4742 +       percpu_counter_destroy(&br->br_count);
4743 +}
4744 +
4745 +static inline int au_br_rdonly(struct au_branch *br)
4746 +{
4747 +       return (sb_rdonly(au_br_sb(br))
4748 +               || !au_br_writable(br->br_perm))
4749 +               ? -EROFS : 0;
4750 +}
4751 +
4752 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4753 +{
4754 +#ifdef CONFIG_AUFS_HNOTIFY
4755 +       return !(brperm & AuBrPerm_RR);
4756 +#else
4757 +       return 0;
4758 +#endif
4759 +}
4760 +
4761 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4762 +{
4763 +       int err, exec_flag;
4764 +
4765 +       err = 0;
4766 +       exec_flag = oflag & __FMODE_EXEC;
4767 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4768 +               err = -EACCES;
4769 +
4770 +       return err;
4771 +}
4772 +
4773 +static inline void au_xino_get(struct au_branch *br)
4774 +{
4775 +       struct au_xino *xi;
4776 +
4777 +       xi = br->br_xino;
4778 +       if (xi)
4779 +               kref_get(&xi->xi_kref);
4780 +}
4781 +
4782 +static inline int au_xino_count(struct au_branch *br)
4783 +{
4784 +       int v;
4785 +       struct au_xino *xi;
4786 +
4787 +       v = 0;
4788 +       xi = br->br_xino;
4789 +       if (xi)
4790 +               v = kref_read(&xi->xi_kref);
4791 +
4792 +       return v;
4793 +}
4794 +
4795 +static inline struct file *au_xino_file(struct au_branch *br)
4796 +{
4797 +       struct au_xino *xi;
4798 +
4799 +       xi = br->br_xino;
4800 +       return xi ? xi->xi_file : NULL;
4801 +}
4802 +
4803 +/* ---------------------------------------------------------------------- */
4804 +
4805 +/* branch.c */
4806 +struct au_sbinfo;
4807 +void au_br_free(struct au_sbinfo *sinfo);
4808 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4809 +struct au_opt_add;
4810 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4811 +struct au_opt_del;
4812 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4813 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4814 +#ifdef CONFIG_COMPAT
4815 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4816 +#endif
4817 +struct au_opt_mod;
4818 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4819 +             int *do_refresh);
4820 +struct aufs_stfs;
4821 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4822 +
4823 +/* xino.c */
4824 +static const loff_t au_loff_max = LLONG_MAX;
4825 +
4826 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
4827 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4828 +                            struct file *copy_src);
4829 +
4830 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4831 +                ino_t *ino);
4832 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4833 +                 ino_t ino);
4834 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4835 +                  loff_t *pos);
4836 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4837 +                   size_t size, loff_t *pos);
4838 +
4839 +int au_xib_trunc(struct super_block *sb);
4840 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4841 +
4842 +struct au_xino *au_xino_alloc(void);
4843 +int au_xino_put(struct au_branch *br);
4844 +void au_xino_file_set(struct au_branch *br, struct file *file);
4845 +
4846 +struct au_opt_xino;
4847 +void au_xino_clr(struct super_block *sb);
4848 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4849 +struct file *au_xino_def(struct super_block *sb);
4850 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4851 +                   struct path *base);
4852 +
4853 +ino_t au_xino_new_ino(struct super_block *sb);
4854 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4855 +
4856 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4857 +                      ino_t h_ino, int idx);
4858 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4859 +                     int *idx);
4860 +
4861 +int au_xino_path(struct seq_file *seq, struct file *file);
4862 +
4863 +/* ---------------------------------------------------------------------- */
4864 +
4865 +/* Superblock to branch */
4866 +static inline
4867 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4868 +{
4869 +       return au_sbr(sb, bindex)->br_id;
4870 +}
4871 +
4872 +static inline
4873 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4874 +{
4875 +       return au_br_mnt(au_sbr(sb, bindex));
4876 +}
4877 +
4878 +static inline
4879 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4880 +{
4881 +       return au_br_sb(au_sbr(sb, bindex));
4882 +}
4883 +
4884 +static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4885 +{
4886 +       au_br_get(au_sbr(sb, bindex));
4887 +}
4888 +
4889 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4890 +{
4891 +       au_br_put(au_sbr(sb, bindex));
4892 +}
4893 +
4894 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4895 +{
4896 +       return au_sbr(sb, bindex)->br_perm;
4897 +}
4898 +
4899 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4900 +{
4901 +       return au_br_whable(au_sbr_perm(sb, bindex));
4902 +}
4903 +
4904 +/* ---------------------------------------------------------------------- */
4905 +
4906 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4907 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4908 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4909 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4910 +/*
4911 +#define wbr_wh_read_trylock_nested(wbr) \
4912 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4913 +#define wbr_wh_write_trylock_nested(wbr) \
4914 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4915 +*/
4916 +
4917 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4918 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4919 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4920 +
4921 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4922 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4923 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4924 +
4925 +/* ---------------------------------------------------------------------- */
4926 +
4927 +#ifdef CONFIG_AUFS_FHSM
4928 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4929 +{
4930 +       mutex_init(&brfhsm->bf_lock);
4931 +       brfhsm->bf_jiffy = 0;
4932 +       brfhsm->bf_readable = 0;
4933 +}
4934 +
4935 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4936 +{
4937 +       mutex_destroy(&brfhsm->bf_lock);
4938 +}
4939 +#else
4940 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4941 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4942 +#endif
4943 +
4944 +#endif /* __KERNEL__ */
4945 +#endif /* __AUFS_BRANCH_H__ */
4946 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4947 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4948 +++ linux/fs/aufs/conf.mk       2018-06-04 09:08:09.181412645 +0200
4949 @@ -0,0 +1,40 @@
4950 +# SPDX-License-Identifier: GPL-2.0
4951 +
4952 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4953 +
4954 +define AuConf
4955 +ifdef ${1}
4956 +AuConfStr += ${1}=${${1}}
4957 +endif
4958 +endef
4959 +
4960 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4961 +       SBILIST \
4962 +       HNOTIFY HFSNOTIFY \
4963 +       EXPORT INO_T_64 \
4964 +       XATTR \
4965 +       FHSM \
4966 +       RDU \
4967 +       DIRREN \
4968 +       SHWH \
4969 +       BR_RAMFS \
4970 +       BR_FUSE POLL \
4971 +       BR_HFSPLUS \
4972 +       BDEV_LOOP \
4973 +       DEBUG MAGIC_SYSRQ
4974 +$(foreach i, ${AuConfAll}, \
4975 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4976 +
4977 +AuConfName = ${obj}/conf.str
4978 +${AuConfName}.tmp: FORCE
4979 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4980 +${AuConfName}: ${AuConfName}.tmp
4981 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4982 +       echo '  GEN    ' $@; \
4983 +       cp -p $< $@; \
4984 +       }
4985 +FORCE:
4986 +clean-files += ${AuConfName} ${AuConfName}.tmp
4987 +${obj}/sysfs.o: ${AuConfName}
4988 +
4989 +-include ${srctree}/${src}/conf_priv.mk
4990 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4991 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4992 +++ linux/fs/aufs/cpup.c        2018-08-12 23:43:05.453457863 +0200
4993 @@ -0,0 +1,1441 @@
4994 +// SPDX-License-Identifier: GPL-2.0
4995 +/*
4996 + * Copyright (C) 2005-2018 Junjiro R. Okajima
4997 + *
4998 + * This program, aufs is free software; you can redistribute it and/or modify
4999 + * it under the terms of the GNU General Public License as published by
5000 + * the Free Software Foundation; either version 2 of the License, or
5001 + * (at your option) any later version.
5002 + *
5003 + * This program is distributed in the hope that it will be useful,
5004 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5005 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5006 + * GNU General Public License for more details.
5007 + *
5008 + * You should have received a copy of the GNU General Public License
5009 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5010 + */
5011 +
5012 +/*
5013 + * copy-up functions, see wbr_policy.c for copy-down
5014 + */
5015 +
5016 +#include <linux/fs_stack.h>
5017 +#include <linux/mm.h>
5018 +#include <linux/task_work.h>
5019 +#include "aufs.h"
5020 +
5021 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
5022 +{
5023 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
5024 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
5025 +
5026 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
5027 +
5028 +       dst->i_flags |= iflags & ~mask;
5029 +       if (au_test_fs_notime(dst->i_sb))
5030 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
5031 +}
5032 +
5033 +void au_cpup_attr_timesizes(struct inode *inode)
5034 +{
5035 +       struct inode *h_inode;
5036 +
5037 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5038 +       fsstack_copy_attr_times(inode, h_inode);
5039 +       fsstack_copy_inode_size(inode, h_inode);
5040 +}
5041 +
5042 +void au_cpup_attr_nlink(struct inode *inode, int force)
5043 +{
5044 +       struct inode *h_inode;
5045 +       struct super_block *sb;
5046 +       aufs_bindex_t bindex, bbot;
5047 +
5048 +       sb = inode->i_sb;
5049 +       bindex = au_ibtop(inode);
5050 +       h_inode = au_h_iptr(inode, bindex);
5051 +       if (!force
5052 +           && !S_ISDIR(h_inode->i_mode)
5053 +           && au_opt_test(au_mntflags(sb), PLINK)
5054 +           && au_plink_test(inode))
5055 +               return;
5056 +
5057 +       /*
5058 +        * 0 can happen in revalidating.
5059 +        * h_inode->i_mutex may not be held here, but it is harmless since once
5060 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
5061 +        * case.
5062 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5063 +        *       the incorrect link count.
5064 +        */
5065 +       set_nlink(inode, h_inode->i_nlink);
5066 +
5067 +       /*
5068 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5069 +        * it may includes whplink directory.
5070 +        */
5071 +       if (S_ISDIR(h_inode->i_mode)) {
5072 +               bbot = au_ibbot(inode);
5073 +               for (bindex++; bindex <= bbot; bindex++) {
5074 +                       h_inode = au_h_iptr(inode, bindex);
5075 +                       if (h_inode)
5076 +                               au_add_nlink(inode, h_inode);
5077 +               }
5078 +       }
5079 +}
5080 +
5081 +void au_cpup_attr_changeable(struct inode *inode)
5082 +{
5083 +       struct inode *h_inode;
5084 +
5085 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5086 +       inode->i_mode = h_inode->i_mode;
5087 +       inode->i_uid = h_inode->i_uid;
5088 +       inode->i_gid = h_inode->i_gid;
5089 +       au_cpup_attr_timesizes(inode);
5090 +       au_cpup_attr_flags(inode, h_inode->i_flags);
5091 +}
5092 +
5093 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5094 +{
5095 +       struct au_iinfo *iinfo = au_ii(inode);
5096 +
5097 +       IiMustWriteLock(inode);
5098 +
5099 +       iinfo->ii_higen = h_inode->i_generation;
5100 +       iinfo->ii_hsb1 = h_inode->i_sb;
5101 +}
5102 +
5103 +void au_cpup_attr_all(struct inode *inode, int force)
5104 +{
5105 +       struct inode *h_inode;
5106 +
5107 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5108 +       au_cpup_attr_changeable(inode);
5109 +       if (inode->i_nlink > 0)
5110 +               au_cpup_attr_nlink(inode, force);
5111 +       inode->i_rdev = h_inode->i_rdev;
5112 +       inode->i_blkbits = h_inode->i_blkbits;
5113 +       au_cpup_igen(inode, h_inode);
5114 +}
5115 +
5116 +/* ---------------------------------------------------------------------- */
5117 +
5118 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5119 +
5120 +/* keep the timestamps of the parent dir when cpup */
5121 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5122 +                   struct path *h_path)
5123 +{
5124 +       struct inode *h_inode;
5125 +
5126 +       dt->dt_dentry = dentry;
5127 +       dt->dt_h_path = *h_path;
5128 +       h_inode = d_inode(h_path->dentry);
5129 +       dt->dt_atime = h_inode->i_atime;
5130 +       dt->dt_mtime = h_inode->i_mtime;
5131 +       /* smp_mb(); */
5132 +}
5133 +
5134 +void au_dtime_revert(struct au_dtime *dt)
5135 +{
5136 +       struct iattr attr;
5137 +       int err;
5138 +
5139 +       attr.ia_atime = dt->dt_atime;
5140 +       attr.ia_mtime = dt->dt_mtime;
5141 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5142 +               | ATTR_ATIME | ATTR_ATIME_SET;
5143 +
5144 +       /* no delegation since this is a directory */
5145 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
5146 +       if (unlikely(err))
5147 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
5148 +}
5149 +
5150 +/* ---------------------------------------------------------------------- */
5151 +
5152 +/* internal use only */
5153 +struct au_cpup_reg_attr {
5154 +       int             valid;
5155 +       struct kstat    st;
5156 +       unsigned int    iflags; /* inode->i_flags */
5157 +};
5158 +
5159 +static noinline_for_stack
5160 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5161 +              struct au_cpup_reg_attr *h_src_attr)
5162 +{
5163 +       int err, sbits, icex;
5164 +       unsigned int mnt_flags;
5165 +       unsigned char verbose;
5166 +       struct iattr ia;
5167 +       struct path h_path;
5168 +       struct inode *h_isrc, *h_idst;
5169 +       struct kstat *h_st;
5170 +       struct au_branch *br;
5171 +
5172 +       h_path.dentry = au_h_dptr(dst, bindex);
5173 +       h_idst = d_inode(h_path.dentry);
5174 +       br = au_sbr(dst->d_sb, bindex);
5175 +       h_path.mnt = au_br_mnt(br);
5176 +       h_isrc = d_inode(h_src);
5177 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
5178 +               | ATTR_ATIME | ATTR_MTIME
5179 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
5180 +       if (h_src_attr && h_src_attr->valid) {
5181 +               h_st = &h_src_attr->st;
5182 +               ia.ia_uid = h_st->uid;
5183 +               ia.ia_gid = h_st->gid;
5184 +               ia.ia_atime = h_st->atime;
5185 +               ia.ia_mtime = h_st->mtime;
5186 +               if (h_idst->i_mode != h_st->mode
5187 +                   && !S_ISLNK(h_idst->i_mode)) {
5188 +                       ia.ia_valid |= ATTR_MODE;
5189 +                       ia.ia_mode = h_st->mode;
5190 +               }
5191 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5192 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5193 +       } else {
5194 +               ia.ia_uid = h_isrc->i_uid;
5195 +               ia.ia_gid = h_isrc->i_gid;
5196 +               ia.ia_atime = h_isrc->i_atime;
5197 +               ia.ia_mtime = h_isrc->i_mtime;
5198 +               if (h_idst->i_mode != h_isrc->i_mode
5199 +                   && !S_ISLNK(h_idst->i_mode)) {
5200 +                       ia.ia_valid |= ATTR_MODE;
5201 +                       ia.ia_mode = h_isrc->i_mode;
5202 +               }
5203 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5204 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5205 +       }
5206 +       /* no delegation since it is just created */
5207 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5208 +
5209 +       /* is this nfs only? */
5210 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5211 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5212 +               ia.ia_mode = h_isrc->i_mode;
5213 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5214 +       }
5215 +
5216 +       icex = br->br_perm & AuBrAttr_ICEX;
5217 +       if (!err) {
5218 +               mnt_flags = au_mntflags(dst->d_sb);
5219 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5220 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5221 +       }
5222 +
5223 +       return err;
5224 +}
5225 +
5226 +/* ---------------------------------------------------------------------- */
5227 +
5228 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5229 +                          char *buf, unsigned long blksize)
5230 +{
5231 +       int err;
5232 +       size_t sz, rbytes, wbytes;
5233 +       unsigned char all_zero;
5234 +       char *p, *zp;
5235 +       struct inode *h_inode;
5236 +       /* reduce stack usage */
5237 +       struct iattr *ia;
5238 +
5239 +       zp = page_address(ZERO_PAGE(0));
5240 +       if (unlikely(!zp))
5241 +               return -ENOMEM; /* possible? */
5242 +
5243 +       err = 0;
5244 +       all_zero = 0;
5245 +       while (len) {
5246 +               AuDbg("len %lld\n", len);
5247 +               sz = blksize;
5248 +               if (len < blksize)
5249 +                       sz = len;
5250 +
5251 +               rbytes = 0;
5252 +               /* todo: signal_pending? */
5253 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5254 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5255 +                       err = rbytes;
5256 +               }
5257 +               if (unlikely(err < 0))
5258 +                       break;
5259 +
5260 +               all_zero = 0;
5261 +               if (len >= rbytes && rbytes == blksize)
5262 +                       all_zero = !memcmp(buf, zp, rbytes);
5263 +               if (!all_zero) {
5264 +                       wbytes = rbytes;
5265 +                       p = buf;
5266 +                       while (wbytes) {
5267 +                               size_t b;
5268 +
5269 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5270 +                               err = b;
5271 +                               /* todo: signal_pending? */
5272 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5273 +                                       continue;
5274 +                               if (unlikely(err < 0))
5275 +                                       break;
5276 +                               wbytes -= b;
5277 +                               p += b;
5278 +                       }
5279 +                       if (unlikely(err < 0))
5280 +                               break;
5281 +               } else {
5282 +                       loff_t res;
5283 +
5284 +                       AuLabel(hole);
5285 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5286 +                       err = res;
5287 +                       if (unlikely(res < 0))
5288 +                               break;
5289 +               }
5290 +               len -= rbytes;
5291 +               err = 0;
5292 +       }
5293 +
5294 +       /* the last block may be a hole */
5295 +       if (!err && all_zero) {
5296 +               AuLabel(last hole);
5297 +
5298 +               err = 1;
5299 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5300 +                       /* nfs requires this step to make last hole */
5301 +                       /* is this only nfs? */
5302 +                       do {
5303 +                               /* todo: signal_pending? */
5304 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5305 +                       } while (err == -EAGAIN || err == -EINTR);
5306 +                       if (err == 1)
5307 +                               dst->f_pos--;
5308 +               }
5309 +
5310 +               if (err == 1) {
5311 +                       ia = (void *)buf;
5312 +                       ia->ia_size = dst->f_pos;
5313 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5314 +                       ia->ia_file = dst;
5315 +                       h_inode = file_inode(dst);
5316 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5317 +                       /* no delegation since it is just created */
5318 +                       err = vfsub_notify_change(&dst->f_path, ia,
5319 +                                                 /*delegated*/NULL);
5320 +                       inode_unlock(h_inode);
5321 +               }
5322 +       }
5323 +
5324 +       return err;
5325 +}
5326 +
5327 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5328 +{
5329 +       int err;
5330 +       unsigned long blksize;
5331 +       unsigned char do_kfree;
5332 +       char *buf;
5333 +
5334 +       err = -ENOMEM;
5335 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
5336 +       if (!blksize || PAGE_SIZE < blksize)
5337 +               blksize = PAGE_SIZE;
5338 +       AuDbg("blksize %lu\n", blksize);
5339 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5340 +       if (do_kfree)
5341 +               buf = kmalloc(blksize, GFP_NOFS);
5342 +       else
5343 +               buf = (void *)__get_free_page(GFP_NOFS);
5344 +       if (unlikely(!buf))
5345 +               goto out;
5346 +
5347 +       if (len > (1 << 22))
5348 +               AuDbg("copying a large file %lld\n", (long long)len);
5349 +
5350 +       src->f_pos = 0;
5351 +       dst->f_pos = 0;
5352 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5353 +       if (do_kfree)
5354 +               kfree(buf);
5355 +       else
5356 +               free_page((unsigned long)buf);
5357 +
5358 +out:
5359 +       return err;
5360 +}
5361 +
5362 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5363 +{
5364 +       int err;
5365 +       struct super_block *h_src_sb;
5366 +       struct inode *h_src_inode;
5367 +
5368 +       h_src_inode = file_inode(src);
5369 +       h_src_sb = h_src_inode->i_sb;
5370 +
5371 +       /* XFS acquires inode_lock */
5372 +       if (!au_test_xfs(h_src_sb))
5373 +               err = au_copy_file(dst, src, len);
5374 +       else {
5375 +               inode_unlock_shared(h_src_inode);
5376 +               err = au_copy_file(dst, src, len);
5377 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5378 +       }
5379 +
5380 +       return err;
5381 +}
5382 +
5383 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5384 +{
5385 +       int err;
5386 +       struct super_block *h_src_sb;
5387 +       struct inode *h_src_inode;
5388 +
5389 +       h_src_inode = file_inode(src);
5390 +       h_src_sb = h_src_inode->i_sb;
5391 +       if (h_src_sb != file_inode(dst)->i_sb
5392 +           || !dst->f_op->clone_file_range) {
5393 +               err = au_do_copy(dst, src, len);
5394 +               goto out;
5395 +       }
5396 +
5397 +       if (!au_test_nfs(h_src_sb)) {
5398 +               inode_unlock_shared(h_src_inode);
5399 +               err = vfsub_clone_file_range(src, dst, len);
5400 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5401 +       } else
5402 +               err = vfsub_clone_file_range(src, dst, len);
5403 +       /* older XFS has a condition in cloning */
5404 +       if (unlikely(err != -EOPNOTSUPP))
5405 +               goto out;
5406 +
5407 +       /* the backend fs on NFS may not support cloning */
5408 +       err = au_do_copy(dst, src, len);
5409 +
5410 +out:
5411 +       AuTraceErr(err);
5412 +       return err;
5413 +}
5414 +
5415 +/*
5416 + * to support a sparse file which is opened with O_APPEND,
5417 + * we need to close the file.
5418 + */
5419 +static int au_cp_regular(struct au_cp_generic *cpg)
5420 +{
5421 +       int err, i;
5422 +       enum { SRC, DST };
5423 +       struct {
5424 +               aufs_bindex_t bindex;
5425 +               unsigned int flags;
5426 +               struct dentry *dentry;
5427 +               int force_wr;
5428 +               struct file *file;
5429 +               void *label;
5430 +       } *f, file[] = {
5431 +               {
5432 +                       .bindex = cpg->bsrc,
5433 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5434 +                       .label = &&out
5435 +               },
5436 +               {
5437 +                       .bindex = cpg->bdst,
5438 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5439 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5440 +                       .label = &&out_src
5441 +               }
5442 +       };
5443 +       struct super_block *sb, *h_src_sb;
5444 +       struct inode *h_src_inode;
5445 +       struct task_struct *tsk = current;
5446 +
5447 +       /* bsrc branch can be ro/rw. */
5448 +       sb = cpg->dentry->d_sb;
5449 +       f = file;
5450 +       for (i = 0; i < 2; i++, f++) {
5451 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5452 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5453 +                                   /*file*/NULL, f->force_wr);
5454 +               err = PTR_ERR(f->file);
5455 +               if (IS_ERR(f->file))
5456 +                       goto *f->label;
5457 +       }
5458 +
5459 +       /* try stopping to update while we copyup */
5460 +       h_src_inode = d_inode(file[SRC].dentry);
5461 +       h_src_sb = h_src_inode->i_sb;
5462 +       if (!au_test_nfs(h_src_sb))
5463 +               IMustLock(h_src_inode);
5464 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5465 +
5466 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5467 +       if (tsk->flags & PF_KTHREAD)
5468 +               __fput_sync(file[DST].file);
5469 +       else {
5470 +               /* it happened actually */
5471 +               fput(file[DST].file);
5472 +               /*
5473 +                * too bad.
5474 +                * we have to call both since we don't know which place the file
5475 +                * was added to.
5476 +                */
5477 +               task_work_run();
5478 +               flush_delayed_fput();
5479 +       }
5480 +       au_sbr_put(sb, file[DST].bindex);
5481 +
5482 +out_src:
5483 +       fput(file[SRC].file);
5484 +       au_sbr_put(sb, file[SRC].bindex);
5485 +out:
5486 +       return err;
5487 +}
5488 +
5489 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5490 +                             struct au_cpup_reg_attr *h_src_attr)
5491 +{
5492 +       int err, rerr;
5493 +       loff_t l;
5494 +       struct path h_path;
5495 +       struct inode *h_src_inode, *h_dst_inode;
5496 +
5497 +       err = 0;
5498 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5499 +       l = i_size_read(h_src_inode);
5500 +       if (cpg->len == -1 || l < cpg->len)
5501 +               cpg->len = l;
5502 +       if (cpg->len) {
5503 +               /* try stopping to update while we are referencing */
5504 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5505 +               au_pin_hdir_unlock(cpg->pin);
5506 +
5507 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5508 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5509 +               h_src_attr->iflags = h_src_inode->i_flags;
5510 +               if (!au_test_nfs(h_src_inode->i_sb))
5511 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5512 +               else {
5513 +                       inode_unlock_shared(h_src_inode);
5514 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5515 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5516 +               }
5517 +               if (unlikely(err)) {
5518 +                       inode_unlock_shared(h_src_inode);
5519 +                       goto out;
5520 +               }
5521 +               h_src_attr->valid = 1;
5522 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5523 +                       err = au_cp_regular(cpg);
5524 +                       inode_unlock_shared(h_src_inode);
5525 +               } else {
5526 +                       inode_unlock_shared(h_src_inode);
5527 +                       err = au_cp_regular(cpg);
5528 +               }
5529 +               rerr = au_pin_hdir_relock(cpg->pin);
5530 +               if (!err && rerr)
5531 +                       err = rerr;
5532 +       }
5533 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5534 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5535 +               h_dst_inode = d_inode(h_path.dentry);
5536 +               spin_lock(&h_dst_inode->i_lock);
5537 +               h_dst_inode->i_state |= I_LINKABLE;
5538 +               spin_unlock(&h_dst_inode->i_lock);
5539 +       }
5540 +
5541 +out:
5542 +       return err;
5543 +}
5544 +
5545 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5546 +                             struct inode *h_dir)
5547 +{
5548 +       int err, symlen;
5549 +       mm_segment_t old_fs;
5550 +       union {
5551 +               char *k;
5552 +               char __user *u;
5553 +       } sym;
5554 +
5555 +       err = -ENOMEM;
5556 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5557 +       if (unlikely(!sym.k))
5558 +               goto out;
5559 +
5560 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5561 +       old_fs = get_fs();
5562 +       set_fs(KERNEL_DS);
5563 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5564 +       err = symlen;
5565 +       set_fs(old_fs);
5566 +
5567 +       if (symlen > 0) {
5568 +               sym.k[symlen] = 0;
5569 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5570 +       }
5571 +       free_page((unsigned long)sym.k);
5572 +
5573 +out:
5574 +       return err;
5575 +}
5576 +
5577 +/*
5578 + * regardless 'acl' option, reset all ACL.
5579 + * All ACL will be copied up later from the original entry on the lower branch.
5580 + */
5581 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5582 +{
5583 +       int err;
5584 +       struct dentry *h_dentry;
5585 +       struct inode *h_inode;
5586 +
5587 +       h_dentry = h_path->dentry;
5588 +       h_inode = d_inode(h_dentry);
5589 +       /* forget_all_cached_acls(h_inode)); */
5590 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5591 +       AuTraceErr(err);
5592 +       if (err == -EOPNOTSUPP)
5593 +               err = 0;
5594 +       if (!err)
5595 +               err = vfsub_acl_chmod(h_inode, mode);
5596 +
5597 +       AuTraceErr(err);
5598 +       return err;
5599 +}
5600 +
5601 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5602 +                         struct inode *h_dir, struct path *h_path)
5603 +{
5604 +       int err;
5605 +       struct inode *dir, *inode;
5606 +
5607 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5608 +       AuTraceErr(err);
5609 +       if (err == -EOPNOTSUPP)
5610 +               err = 0;
5611 +       if (unlikely(err))
5612 +               goto out;
5613 +
5614 +       /*
5615 +        * strange behaviour from the users view,
5616 +        * particularry setattr case
5617 +        */
5618 +       dir = d_inode(dst_parent);
5619 +       if (au_ibtop(dir) == cpg->bdst)
5620 +               au_cpup_attr_nlink(dir, /*force*/1);
5621 +       inode = d_inode(cpg->dentry);
5622 +       au_cpup_attr_nlink(inode, /*force*/1);
5623 +
5624 +out:
5625 +       return err;
5626 +}
5627 +
5628 +static noinline_for_stack
5629 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5630 +              struct au_cpup_reg_attr *h_src_attr)
5631 +{
5632 +       int err;
5633 +       umode_t mode;
5634 +       unsigned int mnt_flags;
5635 +       unsigned char isdir, isreg, force;
5636 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5637 +       struct au_dtime dt;
5638 +       struct path h_path;
5639 +       struct dentry *h_src, *h_dst, *h_parent;
5640 +       struct inode *h_inode, *h_dir;
5641 +       struct super_block *sb;
5642 +
5643 +       /* bsrc branch can be ro/rw. */
5644 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5645 +       h_inode = d_inode(h_src);
5646 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5647 +
5648 +       /* try stopping to be referenced while we are creating */
5649 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5650 +       if (au_ftest_cpup(cpg->flags, RENAME))
5651 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5652 +                                 AUFS_WH_PFX_LEN));
5653 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5654 +       h_dir = d_inode(h_parent);
5655 +       IMustLock(h_dir);
5656 +       AuDebugOn(h_parent != h_dst->d_parent);
5657 +
5658 +       sb = cpg->dentry->d_sb;
5659 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5660 +       if (do_dt) {
5661 +               h_path.dentry = h_parent;
5662 +               au_dtime_store(&dt, dst_parent, &h_path);
5663 +       }
5664 +       h_path.dentry = h_dst;
5665 +
5666 +       isreg = 0;
5667 +       isdir = 0;
5668 +       mode = h_inode->i_mode;
5669 +       switch (mode & S_IFMT) {
5670 +       case S_IFREG:
5671 +               isreg = 1;
5672 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5673 +               if (!err)
5674 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5675 +               break;
5676 +       case S_IFDIR:
5677 +               isdir = 1;
5678 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5679 +               if (!err)
5680 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5681 +               break;
5682 +       case S_IFLNK:
5683 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5684 +               break;
5685 +       case S_IFCHR:
5686 +       case S_IFBLK:
5687 +               AuDebugOn(!capable(CAP_MKNOD));
5688 +               /*FALLTHROUGH*/
5689 +       case S_IFIFO:
5690 +       case S_IFSOCK:
5691 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5692 +               break;
5693 +       default:
5694 +               AuIOErr("Unknown inode type 0%o\n", mode);
5695 +               err = -EIO;
5696 +       }
5697 +       if (!err)
5698 +               err = au_reset_acl(h_dir, &h_path, mode);
5699 +
5700 +       mnt_flags = au_mntflags(sb);
5701 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5702 +           && !isdir
5703 +           && au_opt_test(mnt_flags, XINO)
5704 +           && (h_inode->i_nlink == 1
5705 +               || (h_inode->i_state & I_LINKABLE))
5706 +           /* todo: unnecessary? */
5707 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5708 +           && cpg->bdst < cpg->bsrc
5709 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5710 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5711 +               /* ignore this error */
5712 +
5713 +       if (!err) {
5714 +               force = 0;
5715 +               if (isreg) {
5716 +                       force = !!cpg->len;
5717 +                       if (cpg->len == -1)
5718 +                               force = !!i_size_read(h_inode);
5719 +               }
5720 +               au_fhsm_wrote(sb, cpg->bdst, force);
5721 +       }
5722 +
5723 +       if (do_dt)
5724 +               au_dtime_revert(&dt);
5725 +       return err;
5726 +}
5727 +
5728 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5729 +{
5730 +       int err;
5731 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5732 +       struct inode *h_dir;
5733 +       aufs_bindex_t bdst;
5734 +
5735 +       dentry = cpg->dentry;
5736 +       bdst = cpg->bdst;
5737 +       h_dentry = au_h_dptr(dentry, bdst);
5738 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5739 +               dget(h_dentry);
5740 +               au_set_h_dptr(dentry, bdst, NULL);
5741 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5742 +               if (!err)
5743 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5744 +               au_set_h_dptr(dentry, bdst, h_dentry);
5745 +       } else {
5746 +               err = 0;
5747 +               parent = dget_parent(dentry);
5748 +               h_parent = au_h_dptr(parent, bdst);
5749 +               dput(parent);
5750 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5751 +               if (IS_ERR(h_path->dentry))
5752 +                       err = PTR_ERR(h_path->dentry);
5753 +       }
5754 +       if (unlikely(err))
5755 +               goto out;
5756 +
5757 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5758 +       h_dir = d_inode(h_parent);
5759 +       IMustLock(h_dir);
5760 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5761 +       /* no delegation since it is just created */
5762 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5763 +                          /*flags*/0);
5764 +       dput(h_path->dentry);
5765 +
5766 +out:
5767 +       return err;
5768 +}
5769 +
5770 +/*
5771 + * copyup the @dentry from @bsrc to @bdst.
5772 + * the caller must set the both of lower dentries.
5773 + * @len is for truncating when it is -1 copyup the entire file.
5774 + * in link/rename cases, @dst_parent may be different from the real one.
5775 + * basic->bsrc can be larger than basic->bdst.
5776 + * aufs doesn't touch the credential so
5777 + * security_inode_copy_up{,_xattr}() are unnecrssary.
5778 + */
5779 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5780 +{
5781 +       int err, rerr;
5782 +       aufs_bindex_t old_ibtop;
5783 +       unsigned char isdir, plink;
5784 +       struct dentry *h_src, *h_dst, *h_parent;
5785 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5786 +       struct super_block *sb;
5787 +       struct au_branch *br;
5788 +       /* to reuduce stack size */
5789 +       struct {
5790 +               struct au_dtime dt;
5791 +               struct path h_path;
5792 +               struct au_cpup_reg_attr h_src_attr;
5793 +       } *a;
5794 +
5795 +       err = -ENOMEM;
5796 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5797 +       if (unlikely(!a))
5798 +               goto out;
5799 +       a->h_src_attr.valid = 0;
5800 +
5801 +       sb = cpg->dentry->d_sb;
5802 +       br = au_sbr(sb, cpg->bdst);
5803 +       a->h_path.mnt = au_br_mnt(br);
5804 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5805 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5806 +       h_dir = d_inode(h_parent);
5807 +       IMustLock(h_dir);
5808 +
5809 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5810 +       inode = d_inode(cpg->dentry);
5811 +
5812 +       if (!dst_parent)
5813 +               dst_parent = dget_parent(cpg->dentry);
5814 +       else
5815 +               dget(dst_parent);
5816 +
5817 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5818 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5819 +       if (dst_inode) {
5820 +               if (unlikely(!plink)) {
5821 +                       err = -EIO;
5822 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5823 +                               "but plink is disabled\n",
5824 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5825 +                       goto out_parent;
5826 +               }
5827 +
5828 +               if (dst_inode->i_nlink) {
5829 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5830 +
5831 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5832 +                       err = PTR_ERR(h_src);
5833 +                       if (IS_ERR(h_src))
5834 +                               goto out_parent;
5835 +                       if (unlikely(d_is_negative(h_src))) {
5836 +                               err = -EIO;
5837 +                               AuIOErr("i%lu exists on b%d "
5838 +                                       "but not pseudo-linked\n",
5839 +                                       inode->i_ino, cpg->bdst);
5840 +                               dput(h_src);
5841 +                               goto out_parent;
5842 +                       }
5843 +
5844 +                       if (do_dt) {
5845 +                               a->h_path.dentry = h_parent;
5846 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5847 +                       }
5848 +
5849 +                       a->h_path.dentry = h_dst;
5850 +                       delegated = NULL;
5851 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5852 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5853 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5854 +                       if (do_dt)
5855 +                               au_dtime_revert(&a->dt);
5856 +                       if (unlikely(err == -EWOULDBLOCK)) {
5857 +                               pr_warn("cannot retry for NFSv4 delegation"
5858 +                                       " for an internal link\n");
5859 +                               iput(delegated);
5860 +                       }
5861 +                       dput(h_src);
5862 +                       goto out_parent;
5863 +               } else
5864 +                       /* todo: cpup_wh_file? */
5865 +                       /* udba work */
5866 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5867 +       }
5868 +
5869 +       isdir = S_ISDIR(inode->i_mode);
5870 +       old_ibtop = au_ibtop(inode);
5871 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5872 +       if (unlikely(err))
5873 +               goto out_rev;
5874 +       dst_inode = d_inode(h_dst);
5875 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5876 +       /* todo: necessary? */
5877 +       /* au_pin_hdir_unlock(cpg->pin); */
5878 +
5879 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5880 +       if (unlikely(err)) {
5881 +               /* todo: necessary? */
5882 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5883 +               inode_unlock(dst_inode);
5884 +               goto out_rev;
5885 +       }
5886 +
5887 +       if (cpg->bdst < old_ibtop) {
5888 +               if (S_ISREG(inode->i_mode)) {
5889 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5890 +                       if (unlikely(err)) {
5891 +                               /* ignore an error */
5892 +                               /* au_pin_hdir_relock(cpg->pin); */
5893 +                               inode_unlock(dst_inode);
5894 +                               goto out_rev;
5895 +                       }
5896 +               }
5897 +               au_set_ibtop(inode, cpg->bdst);
5898 +       } else
5899 +               au_set_ibbot(inode, cpg->bdst);
5900 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5901 +                     au_hi_flags(inode, isdir));
5902 +
5903 +       /* todo: necessary? */
5904 +       /* err = au_pin_hdir_relock(cpg->pin); */
5905 +       inode_unlock(dst_inode);
5906 +       if (unlikely(err))
5907 +               goto out_rev;
5908 +
5909 +       src_inode = d_inode(h_src);
5910 +       if (!isdir
5911 +           && (src_inode->i_nlink > 1
5912 +               || src_inode->i_state & I_LINKABLE)
5913 +           && plink)
5914 +               au_plink_append(inode, cpg->bdst, h_dst);
5915 +
5916 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5917 +               a->h_path.dentry = h_dst;
5918 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5919 +       }
5920 +       if (!err)
5921 +               goto out_parent; /* success */
5922 +
5923 +       /* revert */
5924 +out_rev:
5925 +       a->h_path.dentry = h_parent;
5926 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5927 +       a->h_path.dentry = h_dst;
5928 +       rerr = 0;
5929 +       if (d_is_positive(h_dst)) {
5930 +               if (!isdir) {
5931 +                       /* no delegation since it is just created */
5932 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5933 +                                           /*delegated*/NULL, /*force*/0);
5934 +               } else
5935 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5936 +       }
5937 +       au_dtime_revert(&a->dt);
5938 +       if (rerr) {
5939 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5940 +               err = -EIO;
5941 +       }
5942 +out_parent:
5943 +       dput(dst_parent);
5944 +       kfree(a);
5945 +out:
5946 +       return err;
5947 +}
5948 +
5949 +#if 0 /* reserved */
5950 +struct au_cpup_single_args {
5951 +       int *errp;
5952 +       struct au_cp_generic *cpg;
5953 +       struct dentry *dst_parent;
5954 +};
5955 +
5956 +static void au_call_cpup_single(void *args)
5957 +{
5958 +       struct au_cpup_single_args *a = args;
5959 +
5960 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5961 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5962 +       au_pin_hdir_release(a->cpg->pin);
5963 +}
5964 +#endif
5965 +
5966 +/*
5967 + * prevent SIGXFSZ in copy-up.
5968 + * testing CAP_MKNOD is for generic fs,
5969 + * but CAP_FSETID is for xfs only, currently.
5970 + */
5971 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5972 +{
5973 +       int do_sio;
5974 +       struct super_block *sb;
5975 +       struct inode *h_dir;
5976 +
5977 +       do_sio = 0;
5978 +       sb = au_pinned_parent(pin)->d_sb;
5979 +       if (!au_wkq_test()
5980 +           && (!au_sbi(sb)->si_plink_maint_pid
5981 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5982 +               switch (mode & S_IFMT) {
5983 +               case S_IFREG:
5984 +                       /* no condition about RLIMIT_FSIZE and the file size */
5985 +                       do_sio = 1;
5986 +                       break;
5987 +               case S_IFCHR:
5988 +               case S_IFBLK:
5989 +                       do_sio = !capable(CAP_MKNOD);
5990 +                       break;
5991 +               }
5992 +               if (!do_sio)
5993 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5994 +                                 && !capable(CAP_FSETID));
5995 +               /* this workaround may be removed in the future */
5996 +               if (!do_sio) {
5997 +                       h_dir = au_pinned_h_dir(pin);
5998 +                       do_sio = h_dir->i_mode & S_ISVTX;
5999 +               }
6000 +       }
6001 +
6002 +       return do_sio;
6003 +}
6004 +
6005 +#if 0 /* reserved */
6006 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
6007 +{
6008 +       int err, wkq_err;
6009 +       struct dentry *h_dentry;
6010 +
6011 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
6012 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
6013 +               err = au_cpup_single(cpg, dst_parent);
6014 +       else {
6015 +               struct au_cpup_single_args args = {
6016 +                       .errp           = &err,
6017 +                       .cpg            = cpg,
6018 +                       .dst_parent     = dst_parent
6019 +               };
6020 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
6021 +               if (unlikely(wkq_err))
6022 +                       err = wkq_err;
6023 +       }
6024 +
6025 +       return err;
6026 +}
6027 +#endif
6028 +
6029 +/*
6030 + * copyup the @dentry from the first active lower branch to @bdst,
6031 + * using au_cpup_single().
6032 + */
6033 +static int au_cpup_simple(struct au_cp_generic *cpg)
6034 +{
6035 +       int err;
6036 +       unsigned int flags_orig;
6037 +       struct dentry *dentry;
6038 +
6039 +       AuDebugOn(cpg->bsrc < 0);
6040 +
6041 +       dentry = cpg->dentry;
6042 +       DiMustWriteLock(dentry);
6043 +
6044 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
6045 +       if (!err) {
6046 +               flags_orig = cpg->flags;
6047 +               au_fset_cpup(cpg->flags, RENAME);
6048 +               err = au_cpup_single(cpg, NULL);
6049 +               cpg->flags = flags_orig;
6050 +               if (!err)
6051 +                       return 0; /* success */
6052 +
6053 +               /* revert */
6054 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
6055 +               au_set_dbtop(dentry, cpg->bsrc);
6056 +       }
6057 +
6058 +       return err;
6059 +}
6060 +
6061 +struct au_cpup_simple_args {
6062 +       int *errp;
6063 +       struct au_cp_generic *cpg;
6064 +};
6065 +
6066 +static void au_call_cpup_simple(void *args)
6067 +{
6068 +       struct au_cpup_simple_args *a = args;
6069 +
6070 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6071 +       *a->errp = au_cpup_simple(a->cpg);
6072 +       au_pin_hdir_release(a->cpg->pin);
6073 +}
6074 +
6075 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
6076 +{
6077 +       int err, wkq_err;
6078 +       struct dentry *dentry, *parent;
6079 +       struct file *h_file;
6080 +       struct inode *h_dir;
6081 +
6082 +       dentry = cpg->dentry;
6083 +       h_file = NULL;
6084 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
6085 +               AuDebugOn(cpg->bsrc < 0);
6086 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
6087 +               err = PTR_ERR(h_file);
6088 +               if (IS_ERR(h_file))
6089 +                       goto out;
6090 +       }
6091 +
6092 +       parent = dget_parent(dentry);
6093 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
6094 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
6095 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6096 +               err = au_cpup_simple(cpg);
6097 +       else {
6098 +               struct au_cpup_simple_args args = {
6099 +                       .errp           = &err,
6100 +                       .cpg            = cpg
6101 +               };
6102 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6103 +               if (unlikely(wkq_err))
6104 +                       err = wkq_err;
6105 +       }
6106 +
6107 +       dput(parent);
6108 +       if (h_file)
6109 +               au_h_open_post(dentry, cpg->bsrc, h_file);
6110 +
6111 +out:
6112 +       return err;
6113 +}
6114 +
6115 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
6116 +{
6117 +       aufs_bindex_t bsrc, bbot;
6118 +       struct dentry *dentry, *h_dentry;
6119 +
6120 +       if (cpg->bsrc < 0) {
6121 +               dentry = cpg->dentry;
6122 +               bbot = au_dbbot(dentry);
6123 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
6124 +                       h_dentry = au_h_dptr(dentry, bsrc);
6125 +                       if (h_dentry) {
6126 +                               AuDebugOn(d_is_negative(h_dentry));
6127 +                               break;
6128 +                       }
6129 +               }
6130 +               AuDebugOn(bsrc > bbot);
6131 +               cpg->bsrc = bsrc;
6132 +       }
6133 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
6134 +       return au_do_sio_cpup_simple(cpg);
6135 +}
6136 +
6137 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6138 +{
6139 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
6140 +       return au_do_sio_cpup_simple(cpg);
6141 +}
6142 +
6143 +/* ---------------------------------------------------------------------- */
6144 +
6145 +/*
6146 + * copyup the deleted file for writing.
6147 + */
6148 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6149 +                        struct file *file)
6150 +{
6151 +       int err;
6152 +       unsigned int flags_orig;
6153 +       aufs_bindex_t bsrc_orig;
6154 +       struct au_dinfo *dinfo;
6155 +       struct {
6156 +               struct au_hdentry *hd;
6157 +               struct dentry *h_dentry;
6158 +       } hdst, hsrc;
6159 +
6160 +       dinfo = au_di(cpg->dentry);
6161 +       AuRwMustWriteLock(&dinfo->di_rwsem);
6162 +
6163 +       bsrc_orig = cpg->bsrc;
6164 +       cpg->bsrc = dinfo->di_btop;
6165 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
6166 +       hdst.h_dentry = hdst.hd->hd_dentry;
6167 +       hdst.hd->hd_dentry = wh_dentry;
6168 +       dinfo->di_btop = cpg->bdst;
6169 +
6170 +       hsrc.h_dentry = NULL;
6171 +       if (file) {
6172 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6173 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
6174 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
6175 +       }
6176 +       flags_orig = cpg->flags;
6177 +       cpg->flags = !AuCpup_DTIME;
6178 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
6179 +       cpg->flags = flags_orig;
6180 +       if (file) {
6181 +               if (!err)
6182 +                       err = au_reopen_nondir(file);
6183 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
6184 +       }
6185 +       hdst.hd->hd_dentry = hdst.h_dentry;
6186 +       dinfo->di_btop = cpg->bsrc;
6187 +       cpg->bsrc = bsrc_orig;
6188 +
6189 +       return err;
6190 +}
6191 +
6192 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6193 +{
6194 +       int err;
6195 +       aufs_bindex_t bdst;
6196 +       struct au_dtime dt;
6197 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6198 +       struct au_branch *br;
6199 +       struct path h_path;
6200 +
6201 +       dentry = cpg->dentry;
6202 +       bdst = cpg->bdst;
6203 +       br = au_sbr(dentry->d_sb, bdst);
6204 +       parent = dget_parent(dentry);
6205 +       h_parent = au_h_dptr(parent, bdst);
6206 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6207 +       err = PTR_ERR(wh_dentry);
6208 +       if (IS_ERR(wh_dentry))
6209 +               goto out;
6210 +
6211 +       h_path.dentry = h_parent;
6212 +       h_path.mnt = au_br_mnt(br);
6213 +       au_dtime_store(&dt, parent, &h_path);
6214 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6215 +       if (unlikely(err))
6216 +               goto out_wh;
6217 +
6218 +       dget(wh_dentry);
6219 +       h_path.dentry = wh_dentry;
6220 +       if (!d_is_dir(wh_dentry)) {
6221 +               /* no delegation since it is just created */
6222 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6223 +                                  /*delegated*/NULL, /*force*/0);
6224 +       } else
6225 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6226 +       if (unlikely(err)) {
6227 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6228 +                       wh_dentry, err);
6229 +               err = -EIO;
6230 +       }
6231 +       au_dtime_revert(&dt);
6232 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6233 +
6234 +out_wh:
6235 +       dput(wh_dentry);
6236 +out:
6237 +       dput(parent);
6238 +       return err;
6239 +}
6240 +
6241 +struct au_cpup_wh_args {
6242 +       int *errp;
6243 +       struct au_cp_generic *cpg;
6244 +       struct file *file;
6245 +};
6246 +
6247 +static void au_call_cpup_wh(void *args)
6248 +{
6249 +       struct au_cpup_wh_args *a = args;
6250 +
6251 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6252 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6253 +       au_pin_hdir_release(a->cpg->pin);
6254 +}
6255 +
6256 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6257 +{
6258 +       int err, wkq_err;
6259 +       aufs_bindex_t bdst;
6260 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6261 +       struct inode *dir, *h_dir, *h_tmpdir;
6262 +       struct au_wbr *wbr;
6263 +       struct au_pin wh_pin, *pin_orig;
6264 +
6265 +       dentry = cpg->dentry;
6266 +       bdst = cpg->bdst;
6267 +       parent = dget_parent(dentry);
6268 +       dir = d_inode(parent);
6269 +       h_orph = NULL;
6270 +       h_parent = NULL;
6271 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6272 +       h_tmpdir = h_dir;
6273 +       pin_orig = NULL;
6274 +       if (!h_dir->i_nlink) {
6275 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6276 +               h_orph = wbr->wbr_orph;
6277 +
6278 +               h_parent = dget(au_h_dptr(parent, bdst));
6279 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6280 +               h_tmpdir = d_inode(h_orph);
6281 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6282 +
6283 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6284 +               /* todo: au_h_open_pre()? */
6285 +
6286 +               pin_orig = cpg->pin;
6287 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6288 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6289 +               cpg->pin = &wh_pin;
6290 +       }
6291 +
6292 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6293 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6294 +               err = au_cpup_wh(cpg, file);
6295 +       else {
6296 +               struct au_cpup_wh_args args = {
6297 +                       .errp   = &err,
6298 +                       .cpg    = cpg,
6299 +                       .file   = file
6300 +               };
6301 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6302 +               if (unlikely(wkq_err))
6303 +                       err = wkq_err;
6304 +       }
6305 +
6306 +       if (h_orph) {
6307 +               inode_unlock(h_tmpdir);
6308 +               /* todo: au_h_open_post()? */
6309 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6310 +               au_set_h_dptr(parent, bdst, h_parent);
6311 +               AuDebugOn(!pin_orig);
6312 +               cpg->pin = pin_orig;
6313 +       }
6314 +       iput(h_dir);
6315 +       dput(parent);
6316 +
6317 +       return err;
6318 +}
6319 +
6320 +/* ---------------------------------------------------------------------- */
6321 +
6322 +/*
6323 + * generic routine for both of copy-up and copy-down.
6324 + */
6325 +/* cf. revalidate function in file.c */
6326 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6327 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6328 +                        struct au_pin *pin,
6329 +                        struct dentry *h_parent, void *arg),
6330 +              void *arg)
6331 +{
6332 +       int err;
6333 +       struct au_pin pin;
6334 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6335 +
6336 +       err = 0;
6337 +       parent = dget_parent(dentry);
6338 +       if (IS_ROOT(parent))
6339 +               goto out;
6340 +
6341 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6342 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6343 +
6344 +       /* do not use au_dpage */
6345 +       real_parent = parent;
6346 +       while (1) {
6347 +               dput(parent);
6348 +               parent = dget_parent(dentry);
6349 +               h_parent = au_h_dptr(parent, bdst);
6350 +               if (h_parent)
6351 +                       goto out; /* success */
6352 +
6353 +               /* find top dir which is necessary to cpup */
6354 +               do {
6355 +                       d = parent;
6356 +                       dput(parent);
6357 +                       parent = dget_parent(d);
6358 +                       di_read_lock_parent3(parent, !AuLock_IR);
6359 +                       h_parent = au_h_dptr(parent, bdst);
6360 +                       di_read_unlock(parent, !AuLock_IR);
6361 +               } while (!h_parent);
6362 +
6363 +               if (d != real_parent)
6364 +                       di_write_lock_child3(d);
6365 +
6366 +               /* somebody else might create while we were sleeping */
6367 +               h_dentry = au_h_dptr(d, bdst);
6368 +               if (!h_dentry || d_is_negative(h_dentry)) {
6369 +                       if (h_dentry)
6370 +                               au_update_dbtop(d);
6371 +
6372 +                       au_pin_set_dentry(&pin, d);
6373 +                       err = au_do_pin(&pin);
6374 +                       if (!err) {
6375 +                               err = cp(d, bdst, &pin, h_parent, arg);
6376 +                               au_unpin(&pin);
6377 +                       }
6378 +               }
6379 +
6380 +               if (d != real_parent)
6381 +                       di_write_unlock(d);
6382 +               if (unlikely(err))
6383 +                       break;
6384 +       }
6385 +
6386 +out:
6387 +       dput(parent);
6388 +       return err;
6389 +}
6390 +
6391 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6392 +                      struct au_pin *pin,
6393 +                      struct dentry *h_parent __maybe_unused,
6394 +                      void *arg __maybe_unused)
6395 +{
6396 +       struct au_cp_generic cpg = {
6397 +               .dentry = dentry,
6398 +               .bdst   = bdst,
6399 +               .bsrc   = -1,
6400 +               .len    = 0,
6401 +               .pin    = pin,
6402 +               .flags  = AuCpup_DTIME
6403 +       };
6404 +       return au_sio_cpup_simple(&cpg);
6405 +}
6406 +
6407 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6408 +{
6409 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6410 +}
6411 +
6412 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6413 +{
6414 +       int err;
6415 +       struct dentry *parent;
6416 +       struct inode *dir;
6417 +
6418 +       parent = dget_parent(dentry);
6419 +       dir = d_inode(parent);
6420 +       err = 0;
6421 +       if (au_h_iptr(dir, bdst))
6422 +               goto out;
6423 +
6424 +       di_read_unlock(parent, AuLock_IR);
6425 +       di_write_lock_parent(parent);
6426 +       /* someone else might change our inode while we were sleeping */
6427 +       if (!au_h_iptr(dir, bdst))
6428 +               err = au_cpup_dirs(dentry, bdst);
6429 +       di_downgrade_lock(parent, AuLock_IR);
6430 +
6431 +out:
6432 +       dput(parent);
6433 +       return err;
6434 +}
6435 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6436 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6437 +++ linux/fs/aufs/cpup.h        2018-08-12 23:43:05.453457863 +0200
6438 @@ -0,0 +1,100 @@
6439 +/* SPDX-License-Identifier: GPL-2.0 */
6440 +/*
6441 + * Copyright (C) 2005-2018 Junjiro R. Okajima
6442 + *
6443 + * This program, aufs is free software; you can redistribute it and/or modify
6444 + * it under the terms of the GNU General Public License as published by
6445 + * the Free Software Foundation; either version 2 of the License, or
6446 + * (at your option) any later version.
6447 + *
6448 + * This program is distributed in the hope that it will be useful,
6449 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6450 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6451 + * GNU General Public License for more details.
6452 + *
6453 + * You should have received a copy of the GNU General Public License
6454 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6455 + */
6456 +
6457 +/*
6458 + * copy-up/down functions
6459 + */
6460 +
6461 +#ifndef __AUFS_CPUP_H__
6462 +#define __AUFS_CPUP_H__
6463 +
6464 +#ifdef __KERNEL__
6465 +
6466 +#include <linux/path.h>
6467 +
6468 +struct inode;
6469 +struct file;
6470 +struct au_pin;
6471 +
6472 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6473 +void au_cpup_attr_timesizes(struct inode *inode);
6474 +void au_cpup_attr_nlink(struct inode *inode, int force);
6475 +void au_cpup_attr_changeable(struct inode *inode);
6476 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6477 +void au_cpup_attr_all(struct inode *inode, int force);
6478 +
6479 +/* ---------------------------------------------------------------------- */
6480 +
6481 +struct au_cp_generic {
6482 +       struct dentry   *dentry;
6483 +       aufs_bindex_t   bdst, bsrc;
6484 +       loff_t          len;
6485 +       struct au_pin   *pin;
6486 +       unsigned int    flags;
6487 +};
6488 +
6489 +/* cpup flags */
6490 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6491 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6492 +                                                  for link(2) */
6493 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6494 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6495 +                                                  cpup */
6496 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6497 +                                                  existing entry */
6498 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6499 +                                                  the branch is marked as RO */
6500 +
6501 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6502 +#undef AuCpup_HOPEN
6503 +#define AuCpup_HOPEN           0
6504 +#endif
6505 +
6506 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6507 +#define au_fset_cpup(flags, name) \
6508 +       do { (flags) |= AuCpup_##name; } while (0)
6509 +#define au_fclr_cpup(flags, name) \
6510 +       do { (flags) &= ~AuCpup_##name; } while (0)
6511 +
6512 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6513 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6514 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6515 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6516 +
6517 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6518 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6519 +                        struct au_pin *pin,
6520 +                        struct dentry *h_parent, void *arg),
6521 +              void *arg);
6522 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6523 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6524 +
6525 +/* ---------------------------------------------------------------------- */
6526 +
6527 +/* keep timestamps when copyup */
6528 +struct au_dtime {
6529 +       struct dentry *dt_dentry;
6530 +       struct path dt_h_path;
6531 +       struct timespec64 dt_atime, dt_mtime;
6532 +};
6533 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6534 +                   struct path *h_path);
6535 +void au_dtime_revert(struct au_dtime *dt);
6536 +
6537 +#endif /* __KERNEL__ */
6538 +#endif /* __AUFS_CPUP_H__ */
6539 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6540 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6541 +++ linux/fs/aufs/dbgaufs.c     2018-08-12 23:43:05.453457863 +0200
6542 @@ -0,0 +1,478 @@
6543 +// SPDX-License-Identifier: GPL-2.0
6544 +/*
6545 + * Copyright (C) 2005-2018 Junjiro R. Okajima
6546 + *
6547 + * This program, aufs is free software; you can redistribute it and/or modify
6548 + * it under the terms of the GNU General Public License as published by
6549 + * the Free Software Foundation; either version 2 of the License, or
6550 + * (at your option) any later version.
6551 + *
6552 + * This program is distributed in the hope that it will be useful,
6553 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6554 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6555 + * GNU General Public License for more details.
6556 + *
6557 + * You should have received a copy of the GNU General Public License
6558 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6559 + */
6560 +
6561 +/*
6562 + * debugfs interface
6563 + */
6564 +
6565 +#include <linux/debugfs.h>
6566 +#include "aufs.h"
6567 +
6568 +#ifndef CONFIG_SYSFS
6569 +#error DEBUG_FS depends upon SYSFS
6570 +#endif
6571 +
6572 +static struct dentry *dbgaufs;
6573 +static const mode_t dbgaufs_mode = 0444;
6574 +
6575 +/* 20 is max digits length of ulong 64 */
6576 +struct dbgaufs_arg {
6577 +       int n;
6578 +       char a[20 * 4];
6579 +};
6580 +
6581 +/*
6582 + * common function for all XINO files
6583 + */
6584 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6585 +                             struct file *file)
6586 +{
6587 +       kfree(file->private_data);
6588 +       return 0;
6589 +}
6590 +
6591 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6592 +                          int cnt)
6593 +{
6594 +       int err;
6595 +       struct kstat st;
6596 +       struct dbgaufs_arg *p;
6597 +
6598 +       err = -ENOMEM;
6599 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6600 +       if (unlikely(!p))
6601 +               goto out;
6602 +
6603 +       err = 0;
6604 +       p->n = 0;
6605 +       file->private_data = p;
6606 +       if (!xf)
6607 +               goto out;
6608 +
6609 +       err = vfsub_getattr(&xf->f_path, &st);
6610 +       if (!err) {
6611 +               if (do_fcnt)
6612 +                       p->n = snprintf
6613 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6614 +                                cnt, st.blocks, st.blksize,
6615 +                                (long long)st.size);
6616 +               else
6617 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6618 +                                       st.blocks, st.blksize,
6619 +                                       (long long)st.size);
6620 +               AuDebugOn(p->n >= sizeof(p->a));
6621 +       } else {
6622 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6623 +               err = 0;
6624 +       }
6625 +
6626 +out:
6627 +       return err;
6628 +
6629 +}
6630 +
6631 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6632 +                              size_t count, loff_t *ppos)
6633 +{
6634 +       struct dbgaufs_arg *p;
6635 +
6636 +       p = file->private_data;
6637 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6638 +}
6639 +
6640 +/* ---------------------------------------------------------------------- */
6641 +
6642 +struct dbgaufs_plink_arg {
6643 +       int n;
6644 +       char a[];
6645 +};
6646 +
6647 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6648 +                                struct file *file)
6649 +{
6650 +       free_page((unsigned long)file->private_data);
6651 +       return 0;
6652 +}
6653 +
6654 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6655 +{
6656 +       int err, i, limit;
6657 +       unsigned long n, sum;
6658 +       struct dbgaufs_plink_arg *p;
6659 +       struct au_sbinfo *sbinfo;
6660 +       struct super_block *sb;
6661 +       struct hlist_bl_head *hbl;
6662 +
6663 +       err = -ENOMEM;
6664 +       p = (void *)get_zeroed_page(GFP_NOFS);
6665 +       if (unlikely(!p))
6666 +               goto out;
6667 +
6668 +       err = -EFBIG;
6669 +       sbinfo = inode->i_private;
6670 +       sb = sbinfo->si_sb;
6671 +       si_noflush_read_lock(sb);
6672 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6673 +               limit = PAGE_SIZE - sizeof(p->n);
6674 +
6675 +               /* the number of buckets */
6676 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6677 +               p->n += n;
6678 +               limit -= n;
6679 +
6680 +               sum = 0;
6681 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6682 +                    i++, hbl++) {
6683 +                       n = au_hbl_count(hbl);
6684 +                       sum += n;
6685 +
6686 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6687 +                       p->n += n;
6688 +                       limit -= n;
6689 +                       if (unlikely(limit <= 0))
6690 +                               goto out_free;
6691 +               }
6692 +               p->a[p->n - 1] = '\n';
6693 +
6694 +               /* the sum of plinks */
6695 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6696 +               p->n += n;
6697 +               limit -= n;
6698 +               if (unlikely(limit <= 0))
6699 +                       goto out_free;
6700 +       } else {
6701 +#define str "1\n0\n0\n"
6702 +               p->n = sizeof(str) - 1;
6703 +               strcpy(p->a, str);
6704 +#undef str
6705 +       }
6706 +       si_read_unlock(sb);
6707 +
6708 +       err = 0;
6709 +       file->private_data = p;
6710 +       goto out; /* success */
6711 +
6712 +out_free:
6713 +       free_page((unsigned long)p);
6714 +out:
6715 +       return err;
6716 +}
6717 +
6718 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6719 +                                 size_t count, loff_t *ppos)
6720 +{
6721 +       struct dbgaufs_plink_arg *p;
6722 +
6723 +       p = file->private_data;
6724 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6725 +}
6726 +
6727 +static const struct file_operations dbgaufs_plink_fop = {
6728 +       .owner          = THIS_MODULE,
6729 +       .open           = dbgaufs_plink_open,
6730 +       .release        = dbgaufs_plink_release,
6731 +       .read           = dbgaufs_plink_read
6732 +};
6733 +
6734 +/* ---------------------------------------------------------------------- */
6735 +
6736 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6737 +{
6738 +       int err;
6739 +       struct au_sbinfo *sbinfo;
6740 +       struct super_block *sb;
6741 +
6742 +       sbinfo = inode->i_private;
6743 +       sb = sbinfo->si_sb;
6744 +       si_noflush_read_lock(sb);
6745 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6746 +       si_read_unlock(sb);
6747 +       return err;
6748 +}
6749 +
6750 +static const struct file_operations dbgaufs_xib_fop = {
6751 +       .owner          = THIS_MODULE,
6752 +       .open           = dbgaufs_xib_open,
6753 +       .release        = dbgaufs_xi_release,
6754 +       .read           = dbgaufs_xi_read
6755 +};
6756 +
6757 +/* ---------------------------------------------------------------------- */
6758 +
6759 +#define DbgaufsXi_PREFIX "xi"
6760 +
6761 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6762 +{
6763 +       int err;
6764 +       long l;
6765 +       struct au_sbinfo *sbinfo;
6766 +       struct super_block *sb;
6767 +       struct file *xf;
6768 +       struct qstr *name;
6769 +       struct au_branch *br;
6770 +
6771 +       err = -ENOENT;
6772 +       xf = NULL;
6773 +       name = &file->f_path.dentry->d_name;
6774 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6775 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6776 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6777 +               goto out;
6778 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6779 +       if (unlikely(err))
6780 +               goto out;
6781 +
6782 +       sbinfo = inode->i_private;
6783 +       sb = sbinfo->si_sb;
6784 +       si_noflush_read_lock(sb);
6785 +       if (l <= au_sbbot(sb)) {
6786 +               br = au_sbr(sb, (aufs_bindex_t)l);
6787 +               xf = au_xino_file(br);
6788 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6789 +                                     au_xino_count(br));
6790 +       } else
6791 +               err = -ENOENT;
6792 +       si_read_unlock(sb);
6793 +
6794 +out:
6795 +       return err;
6796 +}
6797 +
6798 +static const struct file_operations dbgaufs_xino_fop = {
6799 +       .owner          = THIS_MODULE,
6800 +       .open           = dbgaufs_xino_open,
6801 +       .release        = dbgaufs_xi_release,
6802 +       .read           = dbgaufs_xi_read
6803 +};
6804 +
6805 +void dbgaufs_xino_del(struct au_branch *br)
6806 +{
6807 +       struct dentry *dbgaufs;
6808 +
6809 +       dbgaufs = br->br_dbgaufs;
6810 +       if (!dbgaufs)
6811 +               return;
6812 +
6813 +       br->br_dbgaufs = NULL;
6814 +       /* debugfs acquires the parent i_mutex */
6815 +       lockdep_off();
6816 +       debugfs_remove(dbgaufs);
6817 +       lockdep_on();
6818 +}
6819 +
6820 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6821 +{
6822 +       aufs_bindex_t bbot;
6823 +       struct au_branch *br;
6824 +
6825 +       if (!au_sbi(sb)->si_dbgaufs)
6826 +               return;
6827 +
6828 +       bbot = au_sbbot(sb);
6829 +       for (; bindex <= bbot; bindex++) {
6830 +               br = au_sbr(sb, bindex);
6831 +               dbgaufs_xino_del(br);
6832 +       }
6833 +}
6834 +
6835 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6836 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6837 +{
6838 +       struct au_branch *br;
6839 +       struct dentry *d;
6840 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6841 +
6842 +       snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6843 +       br = au_sbr(sb, bindex);
6844 +       if (br->br_dbgaufs) {
6845 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6846 +
6847 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6848 +                       /* debugfs acquires the parent i_mutex */
6849 +                       lockdep_off();
6850 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6851 +                                          name);
6852 +                       lockdep_on();
6853 +                       if (unlikely(!d))
6854 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6855 +                                       parent, name);
6856 +               }
6857 +       } else {
6858 +               lockdep_off();
6859 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6860 +                                                    sbinfo, &dbgaufs_xino_fop);
6861 +               lockdep_on();
6862 +               if (unlikely(!br->br_dbgaufs))
6863 +                       pr_warn("failed creaiting %pd/%s, ignored.\n",
6864 +                               parent, name);
6865 +       }
6866 +}
6867 +
6868 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6869 +{
6870 +       struct au_sbinfo *sbinfo;
6871 +       struct dentry *parent;
6872 +       aufs_bindex_t bbot;
6873 +
6874 +       if (!au_opt_test(au_mntflags(sb), XINO))
6875 +               return;
6876 +
6877 +       sbinfo = au_sbi(sb);
6878 +       parent = sbinfo->si_dbgaufs;
6879 +       if (!parent)
6880 +               return;
6881 +
6882 +       bbot = au_sbbot(sb);
6883 +       if (topdown)
6884 +               for (; bindex <= bbot; bindex++)
6885 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6886 +       else
6887 +               for (; bbot >= bindex; bbot--)
6888 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6889 +}
6890 +
6891 +/* ---------------------------------------------------------------------- */
6892 +
6893 +#ifdef CONFIG_AUFS_EXPORT
6894 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6895 +{
6896 +       int err;
6897 +       struct au_sbinfo *sbinfo;
6898 +       struct super_block *sb;
6899 +
6900 +       sbinfo = inode->i_private;
6901 +       sb = sbinfo->si_sb;
6902 +       si_noflush_read_lock(sb);
6903 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6904 +       si_read_unlock(sb);
6905 +       return err;
6906 +}
6907 +
6908 +static const struct file_operations dbgaufs_xigen_fop = {
6909 +       .owner          = THIS_MODULE,
6910 +       .open           = dbgaufs_xigen_open,
6911 +       .release        = dbgaufs_xi_release,
6912 +       .read           = dbgaufs_xi_read
6913 +};
6914 +
6915 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6916 +{
6917 +       int err;
6918 +
6919 +       /*
6920 +        * This function is a dynamic '__init' function actually,
6921 +        * so the tiny check for si_rwsem is unnecessary.
6922 +        */
6923 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6924 +
6925 +       err = -EIO;
6926 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6927 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6928 +                &dbgaufs_xigen_fop);
6929 +       if (sbinfo->si_dbgaufs_xigen)
6930 +               err = 0;
6931 +
6932 +       return err;
6933 +}
6934 +#else
6935 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6936 +{
6937 +       return 0;
6938 +}
6939 +#endif /* CONFIG_AUFS_EXPORT */
6940 +
6941 +/* ---------------------------------------------------------------------- */
6942 +
6943 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6944 +{
6945 +       /*
6946 +        * This function is a dynamic '__fin' function actually,
6947 +        * so the tiny check for si_rwsem is unnecessary.
6948 +        */
6949 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6950 +
6951 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6952 +       sbinfo->si_dbgaufs = NULL;
6953 +}
6954 +
6955 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6956 +{
6957 +       int err;
6958 +       char name[SysaufsSiNameLen];
6959 +
6960 +       /*
6961 +        * This function is a dynamic '__init' function actually,
6962 +        * so the tiny check for si_rwsem is unnecessary.
6963 +        */
6964 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6965 +
6966 +       err = -ENOENT;
6967 +       if (!dbgaufs) {
6968 +               AuErr1("/debug/aufs is uninitialized\n");
6969 +               goto out;
6970 +       }
6971 +
6972 +       err = -EIO;
6973 +       sysaufs_name(sbinfo, name);
6974 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6975 +       if (unlikely(!sbinfo->si_dbgaufs))
6976 +               goto out;
6977 +
6978 +       /* regardless plink/noplink option */
6979 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6980 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6981 +                &dbgaufs_plink_fop);
6982 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6983 +               goto out_dir;
6984 +
6985 +       /* regardless xino/noxino option */
6986 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6987 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6988 +                &dbgaufs_xib_fop);
6989 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6990 +               goto out_dir;
6991 +
6992 +       err = dbgaufs_xigen_init(sbinfo);
6993 +       if (!err)
6994 +               goto out; /* success */
6995 +
6996 +out_dir:
6997 +       dbgaufs_si_fin(sbinfo);
6998 +out:
6999 +       if (unlikely(err))
7000 +               pr_err("debugfs/aufs failed\n");
7001 +       return err;
7002 +}
7003 +
7004 +/* ---------------------------------------------------------------------- */
7005 +
7006 +void dbgaufs_fin(void)
7007 +{
7008 +       debugfs_remove(dbgaufs);
7009 +}
7010 +
7011 +int __init dbgaufs_init(void)
7012 +{
7013 +       int err;
7014 +
7015 +       err = -EIO;
7016 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7017 +       if (dbgaufs)
7018 +               err = 0;
7019 +       return err;
7020 +}
7021 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7022 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
7023 +++ linux/fs/aufs/dbgaufs.h     2018-08-12 23:43:05.453457863 +0200
7024 @@ -0,0 +1,53 @@
7025 +/* SPDX-License-Identifier: GPL-2.0 */
7026 +/*
7027 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7028 + *
7029 + * This program, aufs is free software; you can redistribute it and/or modify
7030 + * it under the terms of the GNU General Public License as published by
7031 + * the Free Software Foundation; either version 2 of the License, or
7032 + * (at your option) any later version.
7033 + *
7034 + * This program is distributed in the hope that it will be useful,
7035 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7036 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7037 + * GNU General Public License for more details.
7038 + *
7039 + * You should have received a copy of the GNU General Public License
7040 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7041 + */
7042 +
7043 +/*
7044 + * debugfs interface
7045 + */
7046 +
7047 +#ifndef __DBGAUFS_H__
7048 +#define __DBGAUFS_H__
7049 +
7050 +#ifdef __KERNEL__
7051 +
7052 +struct super_block;
7053 +struct au_sbinfo;
7054 +struct au_branch;
7055 +
7056 +#ifdef CONFIG_DEBUG_FS
7057 +/* dbgaufs.c */
7058 +void dbgaufs_xino_del(struct au_branch *br);
7059 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
7060 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
7061 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7062 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7063 +void dbgaufs_fin(void);
7064 +int __init dbgaufs_init(void);
7065 +#else
7066 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
7067 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
7068 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7069 +          int topdown)
7070 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7071 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7072 +AuStubVoid(dbgaufs_fin, void)
7073 +AuStubInt0(__init dbgaufs_init, void)
7074 +#endif /* CONFIG_DEBUG_FS */
7075 +
7076 +#endif /* __KERNEL__ */
7077 +#endif /* __DBGAUFS_H__ */
7078 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7079 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
7080 +++ linux/fs/aufs/dcsub.c       2018-08-12 23:43:05.453457863 +0200
7081 @@ -0,0 +1,225 @@
7082 +// SPDX-License-Identifier: GPL-2.0
7083 +/*
7084 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7085 + *
7086 + * This program, aufs is free software; you can redistribute it and/or modify
7087 + * it under the terms of the GNU General Public License as published by
7088 + * the Free Software Foundation; either version 2 of the License, or
7089 + * (at your option) any later version.
7090 + *
7091 + * This program is distributed in the hope that it will be useful,
7092 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7093 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7094 + * GNU General Public License for more details.
7095 + *
7096 + * You should have received a copy of the GNU General Public License
7097 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7098 + */
7099 +
7100 +/*
7101 + * sub-routines for dentry cache
7102 + */
7103 +
7104 +#include "aufs.h"
7105 +
7106 +static void au_dpage_free(struct au_dpage *dpage)
7107 +{
7108 +       int i;
7109 +       struct dentry **p;
7110 +
7111 +       p = dpage->dentries;
7112 +       for (i = 0; i < dpage->ndentry; i++)
7113 +               dput(*p++);
7114 +       free_page((unsigned long)dpage->dentries);
7115 +}
7116 +
7117 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7118 +{
7119 +       int err;
7120 +       void *p;
7121 +
7122 +       err = -ENOMEM;
7123 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7124 +       if (unlikely(!dpages->dpages))
7125 +               goto out;
7126 +
7127 +       p = (void *)__get_free_page(gfp);
7128 +       if (unlikely(!p))
7129 +               goto out_dpages;
7130 +
7131 +       dpages->dpages[0].ndentry = 0;
7132 +       dpages->dpages[0].dentries = p;
7133 +       dpages->ndpage = 1;
7134 +       return 0; /* success */
7135 +
7136 +out_dpages:
7137 +       kfree(dpages->dpages);
7138 +out:
7139 +       return err;
7140 +}
7141 +
7142 +void au_dpages_free(struct au_dcsub_pages *dpages)
7143 +{
7144 +       int i;
7145 +       struct au_dpage *p;
7146 +
7147 +       p = dpages->dpages;
7148 +       for (i = 0; i < dpages->ndpage; i++)
7149 +               au_dpage_free(p++);
7150 +       kfree(dpages->dpages);
7151 +}
7152 +
7153 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7154 +                           struct dentry *dentry, gfp_t gfp)
7155 +{
7156 +       int err, sz;
7157 +       struct au_dpage *dpage;
7158 +       void *p;
7159 +
7160 +       dpage = dpages->dpages + dpages->ndpage - 1;
7161 +       sz = PAGE_SIZE / sizeof(dentry);
7162 +       if (unlikely(dpage->ndentry >= sz)) {
7163 +               AuLabel(new dpage);
7164 +               err = -ENOMEM;
7165 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7166 +               p = au_kzrealloc(dpages->dpages, sz,
7167 +                                sz + sizeof(*dpages->dpages), gfp,
7168 +                                /*may_shrink*/0);
7169 +               if (unlikely(!p))
7170 +                       goto out;
7171 +
7172 +               dpages->dpages = p;
7173 +               dpage = dpages->dpages + dpages->ndpage;
7174 +               p = (void *)__get_free_page(gfp);
7175 +               if (unlikely(!p))
7176 +                       goto out;
7177 +
7178 +               dpage->ndentry = 0;
7179 +               dpage->dentries = p;
7180 +               dpages->ndpage++;
7181 +       }
7182 +
7183 +       AuDebugOn(au_dcount(dentry) <= 0);
7184 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7185 +       return 0; /* success */
7186 +
7187 +out:
7188 +       return err;
7189 +}
7190 +
7191 +/* todo: BAD approach */
7192 +/* copied from linux/fs/dcache.c */
7193 +enum d_walk_ret {
7194 +       D_WALK_CONTINUE,
7195 +       D_WALK_QUIT,
7196 +       D_WALK_NORETRY,
7197 +       D_WALK_SKIP,
7198 +};
7199 +
7200 +extern void d_walk(struct dentry *parent, void *data,
7201 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7202 +
7203 +struct ac_dpages_arg {
7204 +       int err;
7205 +       struct au_dcsub_pages *dpages;
7206 +       struct super_block *sb;
7207 +       au_dpages_test test;
7208 +       void *arg;
7209 +};
7210 +
7211 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7212 +{
7213 +       enum d_walk_ret ret;
7214 +       struct ac_dpages_arg *arg = _arg;
7215 +
7216 +       ret = D_WALK_CONTINUE;
7217 +       if (dentry->d_sb == arg->sb
7218 +           && !IS_ROOT(dentry)
7219 +           && au_dcount(dentry) > 0
7220 +           && au_di(dentry)
7221 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7222 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7223 +               if (unlikely(arg->err))
7224 +                       ret = D_WALK_QUIT;
7225 +       }
7226 +
7227 +       return ret;
7228 +}
7229 +
7230 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7231 +                  au_dpages_test test, void *arg)
7232 +{
7233 +       struct ac_dpages_arg args = {
7234 +               .err    = 0,
7235 +               .dpages = dpages,
7236 +               .sb     = root->d_sb,
7237 +               .test   = test,
7238 +               .arg    = arg
7239 +       };
7240 +
7241 +       d_walk(root, &args, au_call_dpages_append);
7242 +
7243 +       return args.err;
7244 +}
7245 +
7246 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7247 +                      int do_include, au_dpages_test test, void *arg)
7248 +{
7249 +       int err;
7250 +
7251 +       err = 0;
7252 +       write_seqlock(&rename_lock);
7253 +       spin_lock(&dentry->d_lock);
7254 +       if (do_include
7255 +           && au_dcount(dentry) > 0
7256 +           && (!test || test(dentry, arg)))
7257 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7258 +       spin_unlock(&dentry->d_lock);
7259 +       if (unlikely(err))
7260 +               goto out;
7261 +
7262 +       /*
7263 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7264 +        * mount
7265 +        */
7266 +       while (!IS_ROOT(dentry)) {
7267 +               dentry = dentry->d_parent; /* rename_lock is locked */
7268 +               spin_lock(&dentry->d_lock);
7269 +               if (au_dcount(dentry) > 0
7270 +                   && (!test || test(dentry, arg)))
7271 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7272 +               spin_unlock(&dentry->d_lock);
7273 +               if (unlikely(err))
7274 +                       break;
7275 +       }
7276 +
7277 +out:
7278 +       write_sequnlock(&rename_lock);
7279 +       return err;
7280 +}
7281 +
7282 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7283 +{
7284 +       return au_di(dentry) && dentry->d_sb == arg;
7285 +}
7286 +
7287 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7288 +                           struct dentry *dentry, int do_include)
7289 +{
7290 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7291 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7292 +}
7293 +
7294 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7295 +{
7296 +       struct path path[2] = {
7297 +               {
7298 +                       .dentry = d1
7299 +               },
7300 +               {
7301 +                       .dentry = d2
7302 +               }
7303 +       };
7304 +
7305 +       return path_is_under(path + 0, path + 1);
7306 +}
7307 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7308 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7309 +++ linux/fs/aufs/dcsub.h       2018-08-12 23:43:05.453457863 +0200
7310 @@ -0,0 +1,137 @@
7311 +/* SPDX-License-Identifier: GPL-2.0 */
7312 +/*
7313 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7314 + *
7315 + * This program, aufs is free software; you can redistribute it and/or modify
7316 + * it under the terms of the GNU General Public License as published by
7317 + * the Free Software Foundation; either version 2 of the License, or
7318 + * (at your option) any later version.
7319 + *
7320 + * This program is distributed in the hope that it will be useful,
7321 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7322 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7323 + * GNU General Public License for more details.
7324 + *
7325 + * You should have received a copy of the GNU General Public License
7326 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7327 + */
7328 +
7329 +/*
7330 + * sub-routines for dentry cache
7331 + */
7332 +
7333 +#ifndef __AUFS_DCSUB_H__
7334 +#define __AUFS_DCSUB_H__
7335 +
7336 +#ifdef __KERNEL__
7337 +
7338 +#include <linux/dcache.h>
7339 +#include <linux/fs.h>
7340 +
7341 +struct au_dpage {
7342 +       int ndentry;
7343 +       struct dentry **dentries;
7344 +};
7345 +
7346 +struct au_dcsub_pages {
7347 +       int ndpage;
7348 +       struct au_dpage *dpages;
7349 +};
7350 +
7351 +/* ---------------------------------------------------------------------- */
7352 +
7353 +/* dcsub.c */
7354 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7355 +void au_dpages_free(struct au_dcsub_pages *dpages);
7356 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7357 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7358 +                  au_dpages_test test, void *arg);
7359 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7360 +                      int do_include, au_dpages_test test, void *arg);
7361 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7362 +                           struct dentry *dentry, int do_include);
7363 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7364 +
7365 +/* ---------------------------------------------------------------------- */
7366 +
7367 +/*
7368 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7369 + * include/linux/dcache.h. Try them (in the future).
7370 + */
7371 +
7372 +static inline int au_d_hashed_positive(struct dentry *d)
7373 +{
7374 +       int err;
7375 +       struct inode *inode = d_inode(d);
7376 +
7377 +       err = 0;
7378 +       if (unlikely(d_unhashed(d)
7379 +                    || d_is_negative(d)
7380 +                    || !inode->i_nlink))
7381 +               err = -ENOENT;
7382 +       return err;
7383 +}
7384 +
7385 +static inline int au_d_linkable(struct dentry *d)
7386 +{
7387 +       int err;
7388 +       struct inode *inode = d_inode(d);
7389 +
7390 +       err = au_d_hashed_positive(d);
7391 +       if (err
7392 +           && d_is_positive(d)
7393 +           && (inode->i_state & I_LINKABLE))
7394 +               err = 0;
7395 +       return err;
7396 +}
7397 +
7398 +static inline int au_d_alive(struct dentry *d)
7399 +{
7400 +       int err;
7401 +       struct inode *inode;
7402 +
7403 +       err = 0;
7404 +       if (!IS_ROOT(d))
7405 +               err = au_d_hashed_positive(d);
7406 +       else {
7407 +               inode = d_inode(d);
7408 +               if (unlikely(d_unlinked(d)
7409 +                            || d_is_negative(d)
7410 +                            || !inode->i_nlink))
7411 +                       err = -ENOENT;
7412 +       }
7413 +       return err;
7414 +}
7415 +
7416 +static inline int au_alive_dir(struct dentry *d)
7417 +{
7418 +       int err;
7419 +
7420 +       err = au_d_alive(d);
7421 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7422 +               err = -ENOENT;
7423 +       return err;
7424 +}
7425 +
7426 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7427 +{
7428 +       return a->len == b->len
7429 +               && !memcmp(a->name, b->name, a->len);
7430 +}
7431 +
7432 +/*
7433 + * by the commit
7434 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7435 + *                     taking d_lock
7436 + * the type of d_lockref.count became int, but the inlined function d_count()
7437 + * still returns unsigned int.
7438 + * I don't know why. Maybe it is for every d_count() users?
7439 + * Anyway au_dcount() lives on.
7440 + */
7441 +static inline int au_dcount(struct dentry *d)
7442 +{
7443 +       return (int)d_count(d);
7444 +}
7445 +
7446 +#endif /* __KERNEL__ */
7447 +#endif /* __AUFS_DCSUB_H__ */
7448 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7449 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7450 +++ linux/fs/aufs/debug.c       2018-08-12 23:43:05.453457863 +0200
7451 @@ -0,0 +1,441 @@
7452 +// SPDX-License-Identifier: GPL-2.0
7453 +/*
7454 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7455 + *
7456 + * This program, aufs is free software; you can redistribute it and/or modify
7457 + * it under the terms of the GNU General Public License as published by
7458 + * the Free Software Foundation; either version 2 of the License, or
7459 + * (at your option) any later version.
7460 + *
7461 + * This program is distributed in the hope that it will be useful,
7462 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7463 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7464 + * GNU General Public License for more details.
7465 + *
7466 + * You should have received a copy of the GNU General Public License
7467 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7468 + */
7469 +
7470 +/*
7471 + * debug print functions
7472 + */
7473 +
7474 +#include "aufs.h"
7475 +
7476 +/* Returns 0, or -errno.  arg is in kp->arg. */
7477 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7478 +{
7479 +       int err, n;
7480 +
7481 +       err = kstrtoint(val, 0, &n);
7482 +       if (!err) {
7483 +               if (n > 0)
7484 +                       au_debug_on();
7485 +               else
7486 +                       au_debug_off();
7487 +       }
7488 +       return err;
7489 +}
7490 +
7491 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7492 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7493 +{
7494 +       atomic_t *a;
7495 +
7496 +       a = kp->arg;
7497 +       return sprintf(buffer, "%d", atomic_read(a));
7498 +}
7499 +
7500 +static struct kernel_param_ops param_ops_atomic_t = {
7501 +       .set = param_atomic_t_set,
7502 +       .get = param_atomic_t_get
7503 +       /* void (*free)(void *arg) */
7504 +};
7505 +
7506 +atomic_t aufs_debug = ATOMIC_INIT(0);
7507 +MODULE_PARM_DESC(debug, "debug print");
7508 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7509 +
7510 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7511 +char *au_plevel = KERN_DEBUG;
7512 +#define dpri(fmt, ...) do {                                    \
7513 +       if ((au_plevel                                          \
7514 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7515 +           || au_debug_test())                                 \
7516 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7517 +} while (0)
7518 +
7519 +/* ---------------------------------------------------------------------- */
7520 +
7521 +void au_dpri_whlist(struct au_nhash *whlist)
7522 +{
7523 +       unsigned long ul, n;
7524 +       struct hlist_head *head;
7525 +       struct au_vdir_wh *pos;
7526 +
7527 +       n = whlist->nh_num;
7528 +       head = whlist->nh_head;
7529 +       for (ul = 0; ul < n; ul++) {
7530 +               hlist_for_each_entry(pos, head, wh_hash)
7531 +                       dpri("b%d, %.*s, %d\n",
7532 +                            pos->wh_bindex,
7533 +                            pos->wh_str.len, pos->wh_str.name,
7534 +                            pos->wh_str.len);
7535 +               head++;
7536 +       }
7537 +}
7538 +
7539 +void au_dpri_vdir(struct au_vdir *vdir)
7540 +{
7541 +       unsigned long ul;
7542 +       union au_vdir_deblk_p p;
7543 +       unsigned char *o;
7544 +
7545 +       if (!vdir || IS_ERR(vdir)) {
7546 +               dpri("err %ld\n", PTR_ERR(vdir));
7547 +               return;
7548 +       }
7549 +
7550 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7551 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7552 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7553 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7554 +               p.deblk = vdir->vd_deblk[ul];
7555 +               o = p.deblk;
7556 +               dpri("[%lu]: %p\n", ul, o);
7557 +       }
7558 +}
7559 +
7560 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7561 +                       struct dentry *wh)
7562 +{
7563 +       char *n = NULL;
7564 +       int l = 0;
7565 +
7566 +       if (!inode || IS_ERR(inode)) {
7567 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7568 +               return -1;
7569 +       }
7570 +
7571 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7572 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7573 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7574 +       if (wh) {
7575 +               n = (void *)wh->d_name.name;
7576 +               l = wh->d_name.len;
7577 +       }
7578 +
7579 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7580 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7581 +            bindex, inode,
7582 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7583 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7584 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7585 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7586 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7587 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7588 +            inode->i_generation,
7589 +            l ? ", wh " : "", l, n);
7590 +       return 0;
7591 +}
7592 +
7593 +void au_dpri_inode(struct inode *inode)
7594 +{
7595 +       struct au_iinfo *iinfo;
7596 +       struct au_hinode *hi;
7597 +       aufs_bindex_t bindex;
7598 +       int err, hn;
7599 +
7600 +       err = do_pri_inode(-1, inode, -1, NULL);
7601 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7602 +               return;
7603 +
7604 +       iinfo = au_ii(inode);
7605 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7606 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7607 +       if (iinfo->ii_btop < 0)
7608 +               return;
7609 +       hn = 0;
7610 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7611 +               hi = au_hinode(iinfo, bindex);
7612 +               hn = !!au_hn(hi);
7613 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7614 +       }
7615 +}
7616 +
7617 +void au_dpri_dalias(struct inode *inode)
7618 +{
7619 +       struct dentry *d;
7620 +
7621 +       spin_lock(&inode->i_lock);
7622 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7623 +               au_dpri_dentry(d);
7624 +       spin_unlock(&inode->i_lock);
7625 +}
7626 +
7627 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7628 +{
7629 +       struct dentry *wh = NULL;
7630 +       int hn;
7631 +       struct inode *inode;
7632 +       struct au_iinfo *iinfo;
7633 +       struct au_hinode *hi;
7634 +
7635 +       if (!dentry || IS_ERR(dentry)) {
7636 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7637 +               return -1;
7638 +       }
7639 +       /* do not call dget_parent() here */
7640 +       /* note: access d_xxx without d_lock */
7641 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7642 +            bindex, dentry, dentry,
7643 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7644 +            au_dcount(dentry), dentry->d_flags,
7645 +            d_unhashed(dentry) ? "un" : "");
7646 +       hn = -1;
7647 +       inode = NULL;
7648 +       if (d_is_positive(dentry))
7649 +               inode = d_inode(dentry);
7650 +       if (inode
7651 +           && au_test_aufs(dentry->d_sb)
7652 +           && bindex >= 0
7653 +           && !au_is_bad_inode(inode)) {
7654 +               iinfo = au_ii(inode);
7655 +               hi = au_hinode(iinfo, bindex);
7656 +               hn = !!au_hn(hi);
7657 +               wh = hi->hi_whdentry;
7658 +       }
7659 +       do_pri_inode(bindex, inode, hn, wh);
7660 +       return 0;
7661 +}
7662 +
7663 +void au_dpri_dentry(struct dentry *dentry)
7664 +{
7665 +       struct au_dinfo *dinfo;
7666 +       aufs_bindex_t bindex;
7667 +       int err;
7668 +
7669 +       err = do_pri_dentry(-1, dentry);
7670 +       if (err || !au_test_aufs(dentry->d_sb))
7671 +               return;
7672 +
7673 +       dinfo = au_di(dentry);
7674 +       if (!dinfo)
7675 +               return;
7676 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7677 +            dinfo->di_btop, dinfo->di_bbot,
7678 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7679 +            dinfo->di_tmpfile);
7680 +       if (dinfo->di_btop < 0)
7681 +               return;
7682 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7683 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7684 +}
7685 +
7686 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7687 +{
7688 +       char a[32];
7689 +
7690 +       if (!file || IS_ERR(file)) {
7691 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7692 +               return -1;
7693 +       }
7694 +       a[0] = 0;
7695 +       if (bindex < 0
7696 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7697 +           && au_test_aufs(file->f_path.dentry->d_sb)
7698 +           && au_fi(file))
7699 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7700 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7701 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7702 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7703 +            file->f_version, file->f_pos, a);
7704 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7705 +               do_pri_dentry(bindex, file->f_path.dentry);
7706 +       return 0;
7707 +}
7708 +
7709 +void au_dpri_file(struct file *file)
7710 +{
7711 +       struct au_finfo *finfo;
7712 +       struct au_fidir *fidir;
7713 +       struct au_hfile *hfile;
7714 +       aufs_bindex_t bindex;
7715 +       int err;
7716 +
7717 +       err = do_pri_file(-1, file);
7718 +       if (err
7719 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7720 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7721 +               return;
7722 +
7723 +       finfo = au_fi(file);
7724 +       if (!finfo)
7725 +               return;
7726 +       if (finfo->fi_btop < 0)
7727 +               return;
7728 +       fidir = finfo->fi_hdir;
7729 +       if (!fidir)
7730 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7731 +       else
7732 +               for (bindex = finfo->fi_btop;
7733 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7734 +                    bindex++) {
7735 +                       hfile = fidir->fd_hfile + bindex;
7736 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7737 +               }
7738 +}
7739 +
7740 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7741 +{
7742 +       struct vfsmount *mnt;
7743 +       struct super_block *sb;
7744 +
7745 +       if (!br || IS_ERR(br))
7746 +               goto out;
7747 +       mnt = au_br_mnt(br);
7748 +       if (!mnt || IS_ERR(mnt))
7749 +               goto out;
7750 +       sb = mnt->mnt_sb;
7751 +       if (!sb || IS_ERR(sb))
7752 +               goto out;
7753 +
7754 +       dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
7755 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7756 +            "xino %d\n",
7757 +            bindex, br->br_perm, br->br_id, au_br_count(br),
7758 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7759 +            sb->s_flags, sb->s_count,
7760 +            atomic_read(&sb->s_active), !!au_xino_file(br));
7761 +       return 0;
7762 +
7763 +out:
7764 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7765 +       return -1;
7766 +}
7767 +
7768 +void au_dpri_sb(struct super_block *sb)
7769 +{
7770 +       struct au_sbinfo *sbinfo;
7771 +       aufs_bindex_t bindex;
7772 +       int err;
7773 +       /* to reuduce stack size */
7774 +       struct {
7775 +               struct vfsmount mnt;
7776 +               struct au_branch fake;
7777 +       } *a;
7778 +
7779 +       /* this function can be called from magic sysrq */
7780 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7781 +       if (unlikely(!a)) {
7782 +               dpri("no memory\n");
7783 +               return;
7784 +       }
7785 +
7786 +       a->mnt.mnt_sb = sb;
7787 +       a->fake.br_path.mnt = &a->mnt;
7788 +       au_br_count_init(&a->fake);
7789 +       err = do_pri_br(-1, &a->fake);
7790 +       au_br_count_fin(&a->fake);
7791 +       kfree(a);
7792 +       dpri("dev 0x%x\n", sb->s_dev);
7793 +       if (err || !au_test_aufs(sb))
7794 +               return;
7795 +
7796 +       sbinfo = au_sbi(sb);
7797 +       if (!sbinfo)
7798 +               return;
7799 +       dpri("nw %d, gen %u, kobj %d\n",
7800 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7801 +            kref_read(&sbinfo->si_kobj.kref));
7802 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7803 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7804 +}
7805 +
7806 +/* ---------------------------------------------------------------------- */
7807 +
7808 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7809 +{
7810 +       struct inode *h_inode, *inode = d_inode(dentry);
7811 +       struct dentry *h_dentry;
7812 +       aufs_bindex_t bindex, bbot, bi;
7813 +
7814 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7815 +               return;
7816 +
7817 +       bbot = au_dbbot(dentry);
7818 +       bi = au_ibbot(inode);
7819 +       if (bi < bbot)
7820 +               bbot = bi;
7821 +       bindex = au_dbtop(dentry);
7822 +       bi = au_ibtop(inode);
7823 +       if (bi > bindex)
7824 +               bindex = bi;
7825 +
7826 +       for (; bindex <= bbot; bindex++) {
7827 +               h_dentry = au_h_dptr(dentry, bindex);
7828 +               if (!h_dentry)
7829 +                       continue;
7830 +               h_inode = au_h_iptr(inode, bindex);
7831 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7832 +                       au_debug_on();
7833 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7834 +                       AuDbgDentry(dentry);
7835 +                       AuDbgInode(inode);
7836 +                       au_debug_off();
7837 +                       BUG();
7838 +               }
7839 +       }
7840 +}
7841 +
7842 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7843 +{
7844 +       int err, i, j;
7845 +       struct au_dcsub_pages dpages;
7846 +       struct au_dpage *dpage;
7847 +       struct dentry **dentries;
7848 +
7849 +       err = au_dpages_init(&dpages, GFP_NOFS);
7850 +       AuDebugOn(err);
7851 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7852 +       AuDebugOn(err);
7853 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7854 +               dpage = dpages.dpages + i;
7855 +               dentries = dpage->dentries;
7856 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7857 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7858 +       }
7859 +       au_dpages_free(&dpages);
7860 +}
7861 +
7862 +void au_dbg_verify_kthread(void)
7863 +{
7864 +       if (au_wkq_test()) {
7865 +               au_dbg_blocked();
7866 +               /*
7867 +                * It may be recursive, but udba=notify between two aufs mounts,
7868 +                * where a single ro branch is shared, is not a problem.
7869 +                */
7870 +               /* WARN_ON(1); */
7871 +       }
7872 +}
7873 +
7874 +/* ---------------------------------------------------------------------- */
7875 +
7876 +int __init au_debug_init(void)
7877 +{
7878 +       aufs_bindex_t bindex;
7879 +       struct au_vdir_destr destr;
7880 +
7881 +       bindex = -1;
7882 +       AuDebugOn(bindex >= 0);
7883 +
7884 +       destr.len = -1;
7885 +       AuDebugOn(destr.len < NAME_MAX);
7886 +
7887 +#ifdef CONFIG_4KSTACKS
7888 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7889 +#endif
7890 +
7891 +       return 0;
7892 +}
7893 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7894 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7895 +++ linux/fs/aufs/debug.h       2018-08-12 23:43:05.453457863 +0200
7896 @@ -0,0 +1,226 @@
7897 +/* SPDX-License-Identifier: GPL-2.0 */
7898 +/*
7899 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7900 + *
7901 + * This program, aufs is free software; you can redistribute it and/or modify
7902 + * it under the terms of the GNU General Public License as published by
7903 + * the Free Software Foundation; either version 2 of the License, or
7904 + * (at your option) any later version.
7905 + *
7906 + * This program is distributed in the hope that it will be useful,
7907 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7908 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7909 + * GNU General Public License for more details.
7910 + *
7911 + * You should have received a copy of the GNU General Public License
7912 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7913 + */
7914 +
7915 +/*
7916 + * debug print functions
7917 + */
7918 +
7919 +#ifndef __AUFS_DEBUG_H__
7920 +#define __AUFS_DEBUG_H__
7921 +
7922 +#ifdef __KERNEL__
7923 +
7924 +#include <linux/atomic.h>
7925 +#include <linux/module.h>
7926 +#include <linux/kallsyms.h>
7927 +#include <linux/sysrq.h>
7928 +
7929 +#ifdef CONFIG_AUFS_DEBUG
7930 +#define AuDebugOn(a)           BUG_ON(a)
7931 +
7932 +/* module parameter */
7933 +extern atomic_t aufs_debug;
7934 +static inline void au_debug_on(void)
7935 +{
7936 +       atomic_inc(&aufs_debug);
7937 +}
7938 +static inline void au_debug_off(void)
7939 +{
7940 +       atomic_dec_if_positive(&aufs_debug);
7941 +}
7942 +
7943 +static inline int au_debug_test(void)
7944 +{
7945 +       return atomic_read(&aufs_debug) > 0;
7946 +}
7947 +#else
7948 +#define AuDebugOn(a)           do {} while (0)
7949 +AuStubVoid(au_debug_on, void)
7950 +AuStubVoid(au_debug_off, void)
7951 +AuStubInt0(au_debug_test, void)
7952 +#endif /* CONFIG_AUFS_DEBUG */
7953 +
7954 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7955 +
7956 +/* ---------------------------------------------------------------------- */
7957 +
7958 +/* debug print */
7959 +
7960 +#define AuDbg(fmt, ...) do { \
7961 +       if (au_debug_test()) \
7962 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7963 +} while (0)
7964 +#define AuLabel(l)             AuDbg(#l "\n")
7965 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7966 +#define AuWarn1(fmt, ...) do { \
7967 +       static unsigned char _c; \
7968 +       if (!_c++) \
7969 +               pr_warn(fmt, ##__VA_ARGS__); \
7970 +} while (0)
7971 +
7972 +#define AuErr1(fmt, ...) do { \
7973 +       static unsigned char _c; \
7974 +       if (!_c++) \
7975 +               pr_err(fmt, ##__VA_ARGS__); \
7976 +} while (0)
7977 +
7978 +#define AuIOErr1(fmt, ...) do { \
7979 +       static unsigned char _c; \
7980 +       if (!_c++) \
7981 +               AuIOErr(fmt, ##__VA_ARGS__); \
7982 +} while (0)
7983 +
7984 +#define AuUnsupportMsg "This operation is not supported." \
7985 +                       " Please report this application to aufs-users ML."
7986 +#define AuUnsupport(fmt, ...) do { \
7987 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7988 +       dump_stack(); \
7989 +} while (0)
7990 +
7991 +#define AuTraceErr(e) do { \
7992 +       if (unlikely((e) < 0)) \
7993 +               AuDbg("err %d\n", (int)(e)); \
7994 +} while (0)
7995 +
7996 +#define AuTraceErrPtr(p) do { \
7997 +       if (IS_ERR(p)) \
7998 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7999 +} while (0)
8000 +
8001 +/* dirty macros for debug print, use with "%.*s" and caution */
8002 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
8003 +
8004 +/* ---------------------------------------------------------------------- */
8005 +
8006 +struct dentry;
8007 +#ifdef CONFIG_AUFS_DEBUG
8008 +extern struct mutex au_dbg_mtx;
8009 +extern char *au_plevel;
8010 +struct au_nhash;
8011 +void au_dpri_whlist(struct au_nhash *whlist);
8012 +struct au_vdir;
8013 +void au_dpri_vdir(struct au_vdir *vdir);
8014 +struct inode;
8015 +void au_dpri_inode(struct inode *inode);
8016 +void au_dpri_dalias(struct inode *inode);
8017 +void au_dpri_dentry(struct dentry *dentry);
8018 +struct file;
8019 +void au_dpri_file(struct file *filp);
8020 +struct super_block;
8021 +void au_dpri_sb(struct super_block *sb);
8022 +
8023 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8024 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
8025 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
8026 +void au_dbg_verify_kthread(void);
8027 +
8028 +int __init au_debug_init(void);
8029 +
8030 +#define AuDbgWhlist(w) do { \
8031 +       mutex_lock(&au_dbg_mtx); \
8032 +       AuDbg(#w "\n"); \
8033 +       au_dpri_whlist(w); \
8034 +       mutex_unlock(&au_dbg_mtx); \
8035 +} while (0)
8036 +
8037 +#define AuDbgVdir(v) do { \
8038 +       mutex_lock(&au_dbg_mtx); \
8039 +       AuDbg(#v "\n"); \
8040 +       au_dpri_vdir(v); \
8041 +       mutex_unlock(&au_dbg_mtx); \
8042 +} while (0)
8043 +
8044 +#define AuDbgInode(i) do { \
8045 +       mutex_lock(&au_dbg_mtx); \
8046 +       AuDbg(#i "\n"); \
8047 +       au_dpri_inode(i); \
8048 +       mutex_unlock(&au_dbg_mtx); \
8049 +} while (0)
8050 +
8051 +#define AuDbgDAlias(i) do { \
8052 +       mutex_lock(&au_dbg_mtx); \
8053 +       AuDbg(#i "\n"); \
8054 +       au_dpri_dalias(i); \
8055 +       mutex_unlock(&au_dbg_mtx); \
8056 +} while (0)
8057 +
8058 +#define AuDbgDentry(d) do { \
8059 +       mutex_lock(&au_dbg_mtx); \
8060 +       AuDbg(#d "\n"); \
8061 +       au_dpri_dentry(d); \
8062 +       mutex_unlock(&au_dbg_mtx); \
8063 +} while (0)
8064 +
8065 +#define AuDbgFile(f) do { \
8066 +       mutex_lock(&au_dbg_mtx); \
8067 +       AuDbg(#f "\n"); \
8068 +       au_dpri_file(f); \
8069 +       mutex_unlock(&au_dbg_mtx); \
8070 +} while (0)
8071 +
8072 +#define AuDbgSb(sb) do { \
8073 +       mutex_lock(&au_dbg_mtx); \
8074 +       AuDbg(#sb "\n"); \
8075 +       au_dpri_sb(sb); \
8076 +       mutex_unlock(&au_dbg_mtx); \
8077 +} while (0)
8078 +
8079 +#define AuDbgSym(addr) do {                            \
8080 +       char sym[KSYM_SYMBOL_LEN];                      \
8081 +       sprint_symbol(sym, (unsigned long)addr);        \
8082 +       AuDbg("%s\n", sym);                             \
8083 +} while (0)
8084 +#else
8085 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
8086 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8087 +AuStubVoid(au_dbg_verify_kthread, void)
8088 +AuStubInt0(__init au_debug_init, void)
8089 +
8090 +#define AuDbgWhlist(w)         do {} while (0)
8091 +#define AuDbgVdir(v)           do {} while (0)
8092 +#define AuDbgInode(i)          do {} while (0)
8093 +#define AuDbgDAlias(i)         do {} while (0)
8094 +#define AuDbgDentry(d)         do {} while (0)
8095 +#define AuDbgFile(f)           do {} while (0)
8096 +#define AuDbgSb(sb)            do {} while (0)
8097 +#define AuDbgSym(addr)         do {} while (0)
8098 +#endif /* CONFIG_AUFS_DEBUG */
8099 +
8100 +/* ---------------------------------------------------------------------- */
8101 +
8102 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8103 +int __init au_sysrq_init(void);
8104 +void au_sysrq_fin(void);
8105 +
8106 +#ifdef CONFIG_HW_CONSOLE
8107 +#define au_dbg_blocked() do { \
8108 +       WARN_ON(1); \
8109 +       handle_sysrq('w'); \
8110 +} while (0)
8111 +#else
8112 +AuStubVoid(au_dbg_blocked, void)
8113 +#endif
8114 +
8115 +#else
8116 +AuStubInt0(__init au_sysrq_init, void)
8117 +AuStubVoid(au_sysrq_fin, void)
8118 +AuStubVoid(au_dbg_blocked, void)
8119 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8120 +
8121 +#endif /* __KERNEL__ */
8122 +#endif /* __AUFS_DEBUG_H__ */
8123 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8124 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
8125 +++ linux/fs/aufs/dentry.c      2018-08-12 23:43:05.453457863 +0200
8126 @@ -0,0 +1,1153 @@
8127 +// SPDX-License-Identifier: GPL-2.0
8128 +/*
8129 + * Copyright (C) 2005-2018 Junjiro R. Okajima
8130 + *
8131 + * This program, aufs is free software; you can redistribute it and/or modify
8132 + * it under the terms of the GNU General Public License as published by
8133 + * the Free Software Foundation; either version 2 of the License, or
8134 + * (at your option) any later version.
8135 + *
8136 + * This program is distributed in the hope that it will be useful,
8137 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8138 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8139 + * GNU General Public License for more details.
8140 + *
8141 + * You should have received a copy of the GNU General Public License
8142 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8143 + */
8144 +
8145 +/*
8146 + * lookup and dentry operations
8147 + */
8148 +
8149 +#include <linux/namei.h>
8150 +#include "aufs.h"
8151 +
8152 +/*
8153 + * returns positive/negative dentry, NULL or an error.
8154 + * NULL means whiteout-ed or not-found.
8155 + */
8156 +static struct dentry*
8157 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8158 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8159 +{
8160 +       struct dentry *h_dentry;
8161 +       struct inode *h_inode;
8162 +       struct au_branch *br;
8163 +       int wh_found, opq;
8164 +       unsigned char wh_able;
8165 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8166 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8167 +                                                         IGNORE_PERM);
8168 +
8169 +       wh_found = 0;
8170 +       br = au_sbr(dentry->d_sb, bindex);
8171 +       wh_able = !!au_br_whable(br->br_perm);
8172 +       if (wh_able)
8173 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8174 +       h_dentry = ERR_PTR(wh_found);
8175 +       if (!wh_found)
8176 +               goto real_lookup;
8177 +       if (unlikely(wh_found < 0))
8178 +               goto out;
8179 +
8180 +       /* We found a whiteout */
8181 +       /* au_set_dbbot(dentry, bindex); */
8182 +       au_set_dbwh(dentry, bindex);
8183 +       if (!allow_neg)
8184 +               return NULL; /* success */
8185 +
8186 +real_lookup:
8187 +       if (!ignore_perm)
8188 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8189 +       else
8190 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8191 +       if (IS_ERR(h_dentry)) {
8192 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8193 +                   && !allow_neg)
8194 +                       h_dentry = NULL;
8195 +               goto out;
8196 +       }
8197 +
8198 +       h_inode = d_inode(h_dentry);
8199 +       if (d_is_negative(h_dentry)) {
8200 +               if (!allow_neg)
8201 +                       goto out_neg;
8202 +       } else if (wh_found
8203 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8204 +               goto out_neg;
8205 +       else if (au_ftest_lkup(args->flags, DIRREN)
8206 +                /* && h_inode */
8207 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8208 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8209 +                     (unsigned long long)h_inode->i_ino);
8210 +               goto out_neg;
8211 +       }
8212 +
8213 +       if (au_dbbot(dentry) <= bindex)
8214 +               au_set_dbbot(dentry, bindex);
8215 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8216 +               au_set_dbtop(dentry, bindex);
8217 +       au_set_h_dptr(dentry, bindex, h_dentry);
8218 +
8219 +       if (!d_is_dir(h_dentry)
8220 +           || !wh_able
8221 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8222 +               goto out; /* success */
8223 +
8224 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8225 +       opq = au_diropq_test(h_dentry);
8226 +       inode_unlock_shared(h_inode);
8227 +       if (opq > 0)
8228 +               au_set_dbdiropq(dentry, bindex);
8229 +       else if (unlikely(opq < 0)) {
8230 +               au_set_h_dptr(dentry, bindex, NULL);
8231 +               h_dentry = ERR_PTR(opq);
8232 +       }
8233 +       goto out;
8234 +
8235 +out_neg:
8236 +       dput(h_dentry);
8237 +       h_dentry = NULL;
8238 +out:
8239 +       return h_dentry;
8240 +}
8241 +
8242 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8243 +{
8244 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8245 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8246 +               return -EPERM;
8247 +       return 0;
8248 +}
8249 +
8250 +/*
8251 + * returns the number of lower positive dentries,
8252 + * otherwise an error.
8253 + * can be called at unlinking with @type is zero.
8254 + */
8255 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8256 +                  unsigned int flags)
8257 +{
8258 +       int npositive, err;
8259 +       aufs_bindex_t bindex, btail, bdiropq;
8260 +       unsigned char isdir, dirperm1, dirren;
8261 +       struct au_do_lookup_args args = {
8262 +               .flags          = flags,
8263 +               .name           = &dentry->d_name
8264 +       };
8265 +       struct dentry *parent;
8266 +       struct super_block *sb;
8267 +
8268 +       sb = dentry->d_sb;
8269 +       err = au_test_shwh(sb, args.name);
8270 +       if (unlikely(err))
8271 +               goto out;
8272 +
8273 +       err = au_wh_name_alloc(&args.whname, args.name);
8274 +       if (unlikely(err))
8275 +               goto out;
8276 +
8277 +       isdir = !!d_is_dir(dentry);
8278 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8279 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8280 +       if (dirren)
8281 +               au_fset_lkup(args.flags, DIRREN);
8282 +
8283 +       npositive = 0;
8284 +       parent = dget_parent(dentry);
8285 +       btail = au_dbtaildir(parent);
8286 +       for (bindex = btop; bindex <= btail; bindex++) {
8287 +               struct dentry *h_parent, *h_dentry;
8288 +               struct inode *h_inode, *h_dir;
8289 +               struct au_branch *br;
8290 +
8291 +               h_dentry = au_h_dptr(dentry, bindex);
8292 +               if (h_dentry) {
8293 +                       if (d_is_positive(h_dentry))
8294 +                               npositive++;
8295 +                       break;
8296 +               }
8297 +               h_parent = au_h_dptr(parent, bindex);
8298 +               if (!h_parent || !d_is_dir(h_parent))
8299 +                       continue;
8300 +
8301 +               if (dirren) {
8302 +                       /* if the inum matches, then use the prepared name */
8303 +                       err = au_dr_lkup_name(&args, bindex);
8304 +                       if (unlikely(err))
8305 +                               goto out_parent;
8306 +               }
8307 +
8308 +               h_dir = d_inode(h_parent);
8309 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8310 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8311 +               inode_unlock_shared(h_dir);
8312 +               err = PTR_ERR(h_dentry);
8313 +               if (IS_ERR(h_dentry))
8314 +                       goto out_parent;
8315 +               if (h_dentry)
8316 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8317 +               if (dirperm1)
8318 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8319 +
8320 +               if (au_dbwh(dentry) == bindex)
8321 +                       break;
8322 +               if (!h_dentry)
8323 +                       continue;
8324 +               if (d_is_negative(h_dentry))
8325 +                       continue;
8326 +               h_inode = d_inode(h_dentry);
8327 +               npositive++;
8328 +               if (!args.type)
8329 +                       args.type = h_inode->i_mode & S_IFMT;
8330 +               if (args.type != S_IFDIR)
8331 +                       break;
8332 +               else if (isdir) {
8333 +                       /* the type of lower may be different */
8334 +                       bdiropq = au_dbdiropq(dentry);
8335 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8336 +                               break;
8337 +               }
8338 +               br = au_sbr(sb, bindex);
8339 +               if (dirren
8340 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8341 +                                          /*add_ent*/NULL)) {
8342 +                       /* prepare next name to lookup */
8343 +                       err = au_dr_lkup(&args, dentry, bindex);
8344 +                       if (unlikely(err))
8345 +                               goto out_parent;
8346 +               }
8347 +       }
8348 +
8349 +       if (npositive) {
8350 +               AuLabel(positive);
8351 +               au_update_dbtop(dentry);
8352 +       }
8353 +       err = npositive;
8354 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8355 +                    && au_dbtop(dentry) < 0)) {
8356 +               err = -EIO;
8357 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8358 +                       dentry, err);
8359 +       }
8360 +
8361 +out_parent:
8362 +       dput(parent);
8363 +       kfree(args.whname.name);
8364 +       if (dirren)
8365 +               au_dr_lkup_fin(&args);
8366 +out:
8367 +       return err;
8368 +}
8369 +
8370 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8371 +{
8372 +       struct dentry *dentry;
8373 +       int wkq_err;
8374 +
8375 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8376 +               dentry = vfsub_lkup_one(name, parent);
8377 +       else {
8378 +               struct vfsub_lkup_one_args args = {
8379 +                       .errp   = &dentry,
8380 +                       .name   = name,
8381 +                       .parent = parent
8382 +               };
8383 +
8384 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8385 +               if (unlikely(wkq_err))
8386 +                       dentry = ERR_PTR(wkq_err);
8387 +       }
8388 +
8389 +       return dentry;
8390 +}
8391 +
8392 +/*
8393 + * lookup @dentry on @bindex which should be negative.
8394 + */
8395 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8396 +{
8397 +       int err;
8398 +       struct dentry *parent, *h_parent, *h_dentry;
8399 +       struct au_branch *br;
8400 +
8401 +       parent = dget_parent(dentry);
8402 +       h_parent = au_h_dptr(parent, bindex);
8403 +       br = au_sbr(dentry->d_sb, bindex);
8404 +       if (wh)
8405 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8406 +       else
8407 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8408 +       err = PTR_ERR(h_dentry);
8409 +       if (IS_ERR(h_dentry))
8410 +               goto out;
8411 +       if (unlikely(d_is_positive(h_dentry))) {
8412 +               err = -EIO;
8413 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8414 +               dput(h_dentry);
8415 +               goto out;
8416 +       }
8417 +
8418 +       err = 0;
8419 +       if (bindex < au_dbtop(dentry))
8420 +               au_set_dbtop(dentry, bindex);
8421 +       if (au_dbbot(dentry) < bindex)
8422 +               au_set_dbbot(dentry, bindex);
8423 +       au_set_h_dptr(dentry, bindex, h_dentry);
8424 +
8425 +out:
8426 +       dput(parent);
8427 +       return err;
8428 +}
8429 +
8430 +/* ---------------------------------------------------------------------- */
8431 +
8432 +/* subset of struct inode */
8433 +struct au_iattr {
8434 +       unsigned long           i_ino;
8435 +       /* unsigned int         i_nlink; */
8436 +       kuid_t                  i_uid;
8437 +       kgid_t                  i_gid;
8438 +       u64                     i_version;
8439 +/*
8440 +       loff_t                  i_size;
8441 +       blkcnt_t                i_blocks;
8442 +*/
8443 +       umode_t                 i_mode;
8444 +};
8445 +
8446 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8447 +{
8448 +       ia->i_ino = h_inode->i_ino;
8449 +       /* ia->i_nlink = h_inode->i_nlink; */
8450 +       ia->i_uid = h_inode->i_uid;
8451 +       ia->i_gid = h_inode->i_gid;
8452 +       ia->i_version = inode_query_iversion(h_inode);
8453 +/*
8454 +       ia->i_size = h_inode->i_size;
8455 +       ia->i_blocks = h_inode->i_blocks;
8456 +*/
8457 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8458 +}
8459 +
8460 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8461 +{
8462 +       return ia->i_ino != h_inode->i_ino
8463 +               /* || ia->i_nlink != h_inode->i_nlink */
8464 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8465 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8466 +               || !inode_eq_iversion(h_inode, ia->i_version)
8467 +/*
8468 +               || ia->i_size != h_inode->i_size
8469 +               || ia->i_blocks != h_inode->i_blocks
8470 +*/
8471 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8472 +}
8473 +
8474 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8475 +                             struct au_branch *br)
8476 +{
8477 +       int err;
8478 +       struct au_iattr ia;
8479 +       struct inode *h_inode;
8480 +       struct dentry *h_d;
8481 +       struct super_block *h_sb;
8482 +
8483 +       err = 0;
8484 +       memset(&ia, -1, sizeof(ia));
8485 +       h_sb = h_dentry->d_sb;
8486 +       h_inode = NULL;
8487 +       if (d_is_positive(h_dentry)) {
8488 +               h_inode = d_inode(h_dentry);
8489 +               au_iattr_save(&ia, h_inode);
8490 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8491 +               /* nfs d_revalidate may return 0 for negative dentry */
8492 +               /* fuse d_revalidate always return 0 for negative dentry */
8493 +               goto out;
8494 +
8495 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8496 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8497 +       err = PTR_ERR(h_d);
8498 +       if (IS_ERR(h_d))
8499 +               goto out;
8500 +
8501 +       err = 0;
8502 +       if (unlikely(h_d != h_dentry
8503 +                    || d_inode(h_d) != h_inode
8504 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8505 +               err = au_busy_or_stale();
8506 +       dput(h_d);
8507 +
8508 +out:
8509 +       AuTraceErr(err);
8510 +       return err;
8511 +}
8512 +
8513 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8514 +               struct dentry *h_parent, struct au_branch *br)
8515 +{
8516 +       int err;
8517 +
8518 +       err = 0;
8519 +       if (udba == AuOpt_UDBA_REVAL
8520 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8521 +               IMustLock(h_dir);
8522 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8523 +       } else if (udba != AuOpt_UDBA_NONE)
8524 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8525 +
8526 +       return err;
8527 +}
8528 +
8529 +/* ---------------------------------------------------------------------- */
8530 +
8531 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8532 +{
8533 +       int err;
8534 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8535 +       struct au_hdentry tmp, *p, *q;
8536 +       struct au_dinfo *dinfo;
8537 +       struct super_block *sb;
8538 +
8539 +       DiMustWriteLock(dentry);
8540 +
8541 +       sb = dentry->d_sb;
8542 +       dinfo = au_di(dentry);
8543 +       bbot = dinfo->di_bbot;
8544 +       bwh = dinfo->di_bwh;
8545 +       bdiropq = dinfo->di_bdiropq;
8546 +       bindex = dinfo->di_btop;
8547 +       p = au_hdentry(dinfo, bindex);
8548 +       for (; bindex <= bbot; bindex++, p++) {
8549 +               if (!p->hd_dentry)
8550 +                       continue;
8551 +
8552 +               new_bindex = au_br_index(sb, p->hd_id);
8553 +               if (new_bindex == bindex)
8554 +                       continue;
8555 +
8556 +               if (dinfo->di_bwh == bindex)
8557 +                       bwh = new_bindex;
8558 +               if (dinfo->di_bdiropq == bindex)
8559 +                       bdiropq = new_bindex;
8560 +               if (new_bindex < 0) {
8561 +                       au_hdput(p);
8562 +                       p->hd_dentry = NULL;
8563 +                       continue;
8564 +               }
8565 +
8566 +               /* swap two lower dentries, and loop again */
8567 +               q = au_hdentry(dinfo, new_bindex);
8568 +               tmp = *q;
8569 +               *q = *p;
8570 +               *p = tmp;
8571 +               if (tmp.hd_dentry) {
8572 +                       bindex--;
8573 +                       p--;
8574 +               }
8575 +       }
8576 +
8577 +       dinfo->di_bwh = -1;
8578 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8579 +               dinfo->di_bwh = bwh;
8580 +
8581 +       dinfo->di_bdiropq = -1;
8582 +       if (bdiropq >= 0
8583 +           && bdiropq <= au_sbbot(sb)
8584 +           && au_sbr_whable(sb, bdiropq))
8585 +               dinfo->di_bdiropq = bdiropq;
8586 +
8587 +       err = -EIO;
8588 +       dinfo->di_btop = -1;
8589 +       dinfo->di_bbot = -1;
8590 +       bbot = au_dbbot(parent);
8591 +       bindex = 0;
8592 +       p = au_hdentry(dinfo, bindex);
8593 +       for (; bindex <= bbot; bindex++, p++)
8594 +               if (p->hd_dentry) {
8595 +                       dinfo->di_btop = bindex;
8596 +                       break;
8597 +               }
8598 +
8599 +       if (dinfo->di_btop >= 0) {
8600 +               bindex = bbot;
8601 +               p = au_hdentry(dinfo, bindex);
8602 +               for (; bindex >= 0; bindex--, p--)
8603 +                       if (p->hd_dentry) {
8604 +                               dinfo->di_bbot = bindex;
8605 +                               err = 0;
8606 +                               break;
8607 +                       }
8608 +       }
8609 +
8610 +       return err;
8611 +}
8612 +
8613 +static void au_do_hide(struct dentry *dentry)
8614 +{
8615 +       struct inode *inode;
8616 +
8617 +       if (d_really_is_positive(dentry)) {
8618 +               inode = d_inode(dentry);
8619 +               if (!d_is_dir(dentry)) {
8620 +                       if (inode->i_nlink && !d_unhashed(dentry))
8621 +                               drop_nlink(inode);
8622 +               } else {
8623 +                       clear_nlink(inode);
8624 +                       /* stop next lookup */
8625 +                       inode->i_flags |= S_DEAD;
8626 +               }
8627 +               smp_mb(); /* necessary? */
8628 +       }
8629 +       d_drop(dentry);
8630 +}
8631 +
8632 +static int au_hide_children(struct dentry *parent)
8633 +{
8634 +       int err, i, j, ndentry;
8635 +       struct au_dcsub_pages dpages;
8636 +       struct au_dpage *dpage;
8637 +       struct dentry *dentry;
8638 +
8639 +       err = au_dpages_init(&dpages, GFP_NOFS);
8640 +       if (unlikely(err))
8641 +               goto out;
8642 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8643 +       if (unlikely(err))
8644 +               goto out_dpages;
8645 +
8646 +       /* in reverse order */
8647 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8648 +               dpage = dpages.dpages + i;
8649 +               ndentry = dpage->ndentry;
8650 +               for (j = ndentry - 1; j >= 0; j--) {
8651 +                       dentry = dpage->dentries[j];
8652 +                       if (dentry != parent)
8653 +                               au_do_hide(dentry);
8654 +               }
8655 +       }
8656 +
8657 +out_dpages:
8658 +       au_dpages_free(&dpages);
8659 +out:
8660 +       return err;
8661 +}
8662 +
8663 +static void au_hide(struct dentry *dentry)
8664 +{
8665 +       int err;
8666 +
8667 +       AuDbgDentry(dentry);
8668 +       if (d_is_dir(dentry)) {
8669 +               /* shrink_dcache_parent(dentry); */
8670 +               err = au_hide_children(dentry);
8671 +               if (unlikely(err))
8672 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8673 +                               dentry, err);
8674 +       }
8675 +       au_do_hide(dentry);
8676 +}
8677 +
8678 +/*
8679 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8680 + *
8681 + * a dirty branch is added
8682 + * - on the top of layers
8683 + * - in the middle of layers
8684 + * - to the bottom of layers
8685 + *
8686 + * on the added branch there exists
8687 + * - a whiteout
8688 + * - a diropq
8689 + * - a same named entry
8690 + *   + exist
8691 + *     * negative --> positive
8692 + *     * positive --> positive
8693 + *      - type is unchanged
8694 + *      - type is changed
8695 + *   + doesn't exist
8696 + *     * negative --> negative
8697 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8698 + * - none
8699 + */
8700 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8701 +                              struct au_dinfo *tmp)
8702 +{
8703 +       int err;
8704 +       aufs_bindex_t bindex, bbot;
8705 +       struct {
8706 +               struct dentry *dentry;
8707 +               struct inode *inode;
8708 +               mode_t mode;
8709 +       } orig_h, tmp_h = {
8710 +               .dentry = NULL
8711 +       };
8712 +       struct au_hdentry *hd;
8713 +       struct inode *inode, *h_inode;
8714 +       struct dentry *h_dentry;
8715 +
8716 +       err = 0;
8717 +       AuDebugOn(dinfo->di_btop < 0);
8718 +       orig_h.mode = 0;
8719 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8720 +       orig_h.inode = NULL;
8721 +       if (d_is_positive(orig_h.dentry)) {
8722 +               orig_h.inode = d_inode(orig_h.dentry);
8723 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8724 +       }
8725 +       if (tmp->di_btop >= 0) {
8726 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8727 +               if (d_is_positive(tmp_h.dentry)) {
8728 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8729 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8730 +               }
8731 +       }
8732 +
8733 +       inode = NULL;
8734 +       if (d_really_is_positive(dentry))
8735 +               inode = d_inode(dentry);
8736 +       if (!orig_h.inode) {
8737 +               AuDbg("nagative originally\n");
8738 +               if (inode) {
8739 +                       au_hide(dentry);
8740 +                       goto out;
8741 +               }
8742 +               AuDebugOn(inode);
8743 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8744 +               AuDebugOn(dinfo->di_bdiropq != -1);
8745 +
8746 +               if (!tmp_h.inode) {
8747 +                       AuDbg("negative --> negative\n");
8748 +                       /* should have only one negative lower */
8749 +                       if (tmp->di_btop >= 0
8750 +                           && tmp->di_btop < dinfo->di_btop) {
8751 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8752 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8753 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8754 +                               au_di_cp(dinfo, tmp);
8755 +                               hd = au_hdentry(tmp, tmp->di_btop);
8756 +                               au_set_h_dptr(dentry, tmp->di_btop,
8757 +                                             dget(hd->hd_dentry));
8758 +                       }
8759 +                       au_dbg_verify_dinode(dentry);
8760 +               } else {
8761 +                       AuDbg("negative --> positive\n");
8762 +                       /*
8763 +                        * similar to the behaviour of creating with bypassing
8764 +                        * aufs.
8765 +                        * unhash it in order to force an error in the
8766 +                        * succeeding create operation.
8767 +                        * we should not set S_DEAD here.
8768 +                        */
8769 +                       d_drop(dentry);
8770 +                       /* au_di_swap(tmp, dinfo); */
8771 +                       au_dbg_verify_dinode(dentry);
8772 +               }
8773 +       } else {
8774 +               AuDbg("positive originally\n");
8775 +               /* inode may be NULL */
8776 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8777 +               if (!tmp_h.inode) {
8778 +                       AuDbg("positive --> negative\n");
8779 +                       /* or bypassing aufs */
8780 +                       au_hide(dentry);
8781 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8782 +                               dinfo->di_bwh = tmp->di_bwh;
8783 +                       if (inode)
8784 +                               err = au_refresh_hinode_self(inode);
8785 +                       au_dbg_verify_dinode(dentry);
8786 +               } else if (orig_h.mode == tmp_h.mode) {
8787 +                       AuDbg("positive --> positive, same type\n");
8788 +                       if (!S_ISDIR(orig_h.mode)
8789 +                           && dinfo->di_btop > tmp->di_btop) {
8790 +                               /*
8791 +                                * similar to the behaviour of removing and
8792 +                                * creating.
8793 +                                */
8794 +                               au_hide(dentry);
8795 +                               if (inode)
8796 +                                       err = au_refresh_hinode_self(inode);
8797 +                               au_dbg_verify_dinode(dentry);
8798 +                       } else {
8799 +                               /* fill empty slots */
8800 +                               if (dinfo->di_btop > tmp->di_btop)
8801 +                                       dinfo->di_btop = tmp->di_btop;
8802 +                               if (dinfo->di_bbot < tmp->di_bbot)
8803 +                                       dinfo->di_bbot = tmp->di_bbot;
8804 +                               dinfo->di_bwh = tmp->di_bwh;
8805 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8806 +                               bbot = dinfo->di_bbot;
8807 +                               bindex = tmp->di_btop;
8808 +                               hd = au_hdentry(tmp, bindex);
8809 +                               for (; bindex <= bbot; bindex++, hd++) {
8810 +                                       if (au_h_dptr(dentry, bindex))
8811 +                                               continue;
8812 +                                       h_dentry = hd->hd_dentry;
8813 +                                       if (!h_dentry)
8814 +                                               continue;
8815 +                                       AuDebugOn(d_is_negative(h_dentry));
8816 +                                       h_inode = d_inode(h_dentry);
8817 +                                       AuDebugOn(orig_h.mode
8818 +                                                 != (h_inode->i_mode
8819 +                                                     & S_IFMT));
8820 +                                       au_set_h_dptr(dentry, bindex,
8821 +                                                     dget(h_dentry));
8822 +                               }
8823 +                               if (inode)
8824 +                                       err = au_refresh_hinode(inode, dentry);
8825 +                               au_dbg_verify_dinode(dentry);
8826 +                       }
8827 +               } else {
8828 +                       AuDbg("positive --> positive, different type\n");
8829 +                       /* similar to the behaviour of removing and creating */
8830 +                       au_hide(dentry);
8831 +                       if (inode)
8832 +                               err = au_refresh_hinode_self(inode);
8833 +                       au_dbg_verify_dinode(dentry);
8834 +               }
8835 +       }
8836 +
8837 +out:
8838 +       return err;
8839 +}
8840 +
8841 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8842 +{
8843 +       const struct dentry_operations *dop
8844 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8845 +       static const unsigned int mask
8846 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8847 +
8848 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8849 +
8850 +       if (dentry->d_op == dop)
8851 +               return;
8852 +
8853 +       AuDbg("%pd\n", dentry);
8854 +       spin_lock(&dentry->d_lock);
8855 +       if (dop == &aufs_dop)
8856 +               dentry->d_flags |= mask;
8857 +       else
8858 +               dentry->d_flags &= ~mask;
8859 +       dentry->d_op = dop;
8860 +       spin_unlock(&dentry->d_lock);
8861 +}
8862 +
8863 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8864 +{
8865 +       int err, ebrange, nbr;
8866 +       unsigned int sigen;
8867 +       struct au_dinfo *dinfo, *tmp;
8868 +       struct super_block *sb;
8869 +       struct inode *inode;
8870 +
8871 +       DiMustWriteLock(dentry);
8872 +       AuDebugOn(IS_ROOT(dentry));
8873 +       AuDebugOn(d_really_is_negative(parent));
8874 +
8875 +       sb = dentry->d_sb;
8876 +       sigen = au_sigen(sb);
8877 +       err = au_digen_test(parent, sigen);
8878 +       if (unlikely(err))
8879 +               goto out;
8880 +
8881 +       nbr = au_sbbot(sb) + 1;
8882 +       dinfo = au_di(dentry);
8883 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8884 +       if (unlikely(err))
8885 +               goto out;
8886 +       ebrange = au_dbrange_test(dentry);
8887 +       if (!ebrange)
8888 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8889 +
8890 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8891 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8892 +               if (d_really_is_positive(dentry)) {
8893 +                       inode = d_inode(dentry);
8894 +                       err = au_refresh_hinode_self(inode);
8895 +               }
8896 +               au_dbg_verify_dinode(dentry);
8897 +               if (!err)
8898 +                       goto out_dgen; /* success */
8899 +               goto out;
8900 +       }
8901 +
8902 +       /* temporary dinfo */
8903 +       AuDbgDentry(dentry);
8904 +       err = -ENOMEM;
8905 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8906 +       if (unlikely(!tmp))
8907 +               goto out;
8908 +       au_di_swap(tmp, dinfo);
8909 +       /* returns the number of positive dentries */
8910 +       /*
8911 +        * if current working dir is removed, it returns an error.
8912 +        * but the dentry is legal.
8913 +        */
8914 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8915 +       AuDbgDentry(dentry);
8916 +       au_di_swap(tmp, dinfo);
8917 +       if (err == -ENOENT)
8918 +               err = 0;
8919 +       if (err >= 0) {
8920 +               /* compare/refresh by dinfo */
8921 +               AuDbgDentry(dentry);
8922 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8923 +               au_dbg_verify_dinode(dentry);
8924 +               AuTraceErr(err);
8925 +       }
8926 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8927 +       au_rw_write_unlock(&tmp->di_rwsem);
8928 +       au_di_free(tmp);
8929 +       if (unlikely(err))
8930 +               goto out;
8931 +
8932 +out_dgen:
8933 +       au_update_digen(dentry);
8934 +out:
8935 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8936 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8937 +               AuDbgDentry(dentry);
8938 +       }
8939 +       AuTraceErr(err);
8940 +       return err;
8941 +}
8942 +
8943 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8944 +                          struct dentry *dentry, aufs_bindex_t bindex)
8945 +{
8946 +       int err, valid;
8947 +
8948 +       err = 0;
8949 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8950 +               goto out;
8951 +
8952 +       AuDbg("b%d\n", bindex);
8953 +       /*
8954 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8955 +        * due to whiteout and branch permission.
8956 +        */
8957 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8958 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8959 +       /* it may return tri-state */
8960 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8961 +
8962 +       if (unlikely(valid < 0))
8963 +               err = valid;
8964 +       else if (!valid)
8965 +               err = -EINVAL;
8966 +
8967 +out:
8968 +       AuTraceErr(err);
8969 +       return err;
8970 +}
8971 +
8972 +/* todo: remove this */
8973 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8974 +                         unsigned int flags, int do_udba, int dirren)
8975 +{
8976 +       int err;
8977 +       umode_t mode, h_mode;
8978 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8979 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8980 +       struct inode *h_inode, *h_cached_inode;
8981 +       struct dentry *h_dentry;
8982 +       struct qstr *name, *h_name;
8983 +
8984 +       err = 0;
8985 +       plus = 0;
8986 +       mode = 0;
8987 +       ibs = -1;
8988 +       ibe = -1;
8989 +       unhashed = !!d_unhashed(dentry);
8990 +       is_root = !!IS_ROOT(dentry);
8991 +       name = &dentry->d_name;
8992 +       tmpfile = au_di(dentry)->di_tmpfile;
8993 +
8994 +       /*
8995 +        * Theoretically, REVAL test should be unnecessary in case of
8996 +        * {FS,I}NOTIFY.
8997 +        * But {fs,i}notify doesn't fire some necessary events,
8998 +        *      IN_ATTRIB for atime/nlink/pageio
8999 +        * Let's do REVAL test too.
9000 +        */
9001 +       if (do_udba && inode) {
9002 +               mode = (inode->i_mode & S_IFMT);
9003 +               plus = (inode->i_nlink > 0);
9004 +               ibs = au_ibtop(inode);
9005 +               ibe = au_ibbot(inode);
9006 +       }
9007 +
9008 +       btop = au_dbtop(dentry);
9009 +       btail = btop;
9010 +       if (inode && S_ISDIR(inode->i_mode))
9011 +               btail = au_dbtaildir(dentry);
9012 +       for (bindex = btop; bindex <= btail; bindex++) {
9013 +               h_dentry = au_h_dptr(dentry, bindex);
9014 +               if (!h_dentry)
9015 +                       continue;
9016 +
9017 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
9018 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
9019 +               spin_lock(&h_dentry->d_lock);
9020 +               h_name = &h_dentry->d_name;
9021 +               if (unlikely(do_udba
9022 +                            && !is_root
9023 +                            && ((!h_nfs
9024 +                                 && (unhashed != !!d_unhashed(h_dentry)
9025 +                                     || (!tmpfile && !dirren
9026 +                                         && !au_qstreq(name, h_name))
9027 +                                         ))
9028 +                                || (h_nfs
9029 +                                    && !(flags & LOOKUP_OPEN)
9030 +                                    && (h_dentry->d_flags
9031 +                                        & DCACHE_NFSFS_RENAMED)))
9032 +                           )) {
9033 +                       int h_unhashed;
9034 +
9035 +                       h_unhashed = d_unhashed(h_dentry);
9036 +                       spin_unlock(&h_dentry->d_lock);
9037 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9038 +                             unhashed, h_unhashed, dentry, h_dentry);
9039 +                       goto err;
9040 +               }
9041 +               spin_unlock(&h_dentry->d_lock);
9042 +
9043 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
9044 +               if (unlikely(err))
9045 +                       /* do not goto err, to keep the errno */
9046 +                       break;
9047 +
9048 +               /* todo: plink too? */
9049 +               if (!do_udba)
9050 +                       continue;
9051 +
9052 +               /* UDBA tests */
9053 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
9054 +                       goto err;
9055 +
9056 +               h_inode = NULL;
9057 +               if (d_is_positive(h_dentry))
9058 +                       h_inode = d_inode(h_dentry);
9059 +               h_plus = plus;
9060 +               h_mode = mode;
9061 +               h_cached_inode = h_inode;
9062 +               if (h_inode) {
9063 +                       h_mode = (h_inode->i_mode & S_IFMT);
9064 +                       h_plus = (h_inode->i_nlink > 0);
9065 +               }
9066 +               if (inode && ibs <= bindex && bindex <= ibe)
9067 +                       h_cached_inode = au_h_iptr(inode, bindex);
9068 +
9069 +               if (!h_nfs) {
9070 +                       if (unlikely(plus != h_plus && !tmpfile))
9071 +                               goto err;
9072 +               } else {
9073 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9074 +                                    && !is_root
9075 +                                    && !IS_ROOT(h_dentry)
9076 +                                    && unhashed != d_unhashed(h_dentry)))
9077 +                               goto err;
9078 +               }
9079 +               if (unlikely(mode != h_mode
9080 +                            || h_cached_inode != h_inode))
9081 +                       goto err;
9082 +               continue;
9083 +
9084 +err:
9085 +               err = -EINVAL;
9086 +               break;
9087 +       }
9088 +
9089 +       AuTraceErr(err);
9090 +       return err;
9091 +}
9092 +
9093 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
9094 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9095 +{
9096 +       int err;
9097 +       struct dentry *parent;
9098 +
9099 +       if (!au_digen_test(dentry, sigen))
9100 +               return 0;
9101 +
9102 +       parent = dget_parent(dentry);
9103 +       di_read_lock_parent(parent, AuLock_IR);
9104 +       AuDebugOn(au_digen_test(parent, sigen));
9105 +       au_dbg_verify_gen(parent, sigen);
9106 +       err = au_refresh_dentry(dentry, parent);
9107 +       di_read_unlock(parent, AuLock_IR);
9108 +       dput(parent);
9109 +       AuTraceErr(err);
9110 +       return err;
9111 +}
9112 +
9113 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9114 +{
9115 +       int err;
9116 +       struct dentry *d, *parent;
9117 +
9118 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9119 +               return simple_reval_dpath(dentry, sigen);
9120 +
9121 +       /* slow loop, keep it simple and stupid */
9122 +       /* cf: au_cpup_dirs() */
9123 +       err = 0;
9124 +       parent = NULL;
9125 +       while (au_digen_test(dentry, sigen)) {
9126 +               d = dentry;
9127 +               while (1) {
9128 +                       dput(parent);
9129 +                       parent = dget_parent(d);
9130 +                       if (!au_digen_test(parent, sigen))
9131 +                               break;
9132 +                       d = parent;
9133 +               }
9134 +
9135 +               if (d != dentry)
9136 +                       di_write_lock_child2(d);
9137 +
9138 +               /* someone might update our dentry while we were sleeping */
9139 +               if (au_digen_test(d, sigen)) {
9140 +                       /*
9141 +                        * todo: consolidate with simple_reval_dpath(),
9142 +                        * do_refresh() and au_reval_for_attr().
9143 +                        */
9144 +                       di_read_lock_parent(parent, AuLock_IR);
9145 +                       err = au_refresh_dentry(d, parent);
9146 +                       di_read_unlock(parent, AuLock_IR);
9147 +               }
9148 +
9149 +               if (d != dentry)
9150 +                       di_write_unlock(d);
9151 +               dput(parent);
9152 +               if (unlikely(err))
9153 +                       break;
9154 +       }
9155 +
9156 +       return err;
9157 +}
9158 +
9159 +/*
9160 + * if valid returns 1, otherwise 0.
9161 + */
9162 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9163 +{
9164 +       int valid, err;
9165 +       unsigned int sigen;
9166 +       unsigned char do_udba, dirren;
9167 +       struct super_block *sb;
9168 +       struct inode *inode;
9169 +
9170 +       /* todo: support rcu-walk? */
9171 +       if (flags & LOOKUP_RCU)
9172 +               return -ECHILD;
9173 +
9174 +       valid = 0;
9175 +       if (unlikely(!au_di(dentry)))
9176 +               goto out;
9177 +
9178 +       valid = 1;
9179 +       sb = dentry->d_sb;
9180 +       /*
9181 +        * todo: very ugly
9182 +        * i_mutex of parent dir may be held,
9183 +        * but we should not return 'invalid' due to busy.
9184 +        */
9185 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9186 +       if (unlikely(err)) {
9187 +               valid = err;
9188 +               AuTraceErr(err);
9189 +               goto out;
9190 +       }
9191 +       inode = NULL;
9192 +       if (d_really_is_positive(dentry))
9193 +               inode = d_inode(dentry);
9194 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9195 +               err = -EINVAL;
9196 +               AuTraceErr(err);
9197 +               goto out_dgrade;
9198 +       }
9199 +       if (unlikely(au_dbrange_test(dentry))) {
9200 +               err = -EINVAL;
9201 +               AuTraceErr(err);
9202 +               goto out_dgrade;
9203 +       }
9204 +
9205 +       sigen = au_sigen(sb);
9206 +       if (au_digen_test(dentry, sigen)) {
9207 +               AuDebugOn(IS_ROOT(dentry));
9208 +               err = au_reval_dpath(dentry, sigen);
9209 +               if (unlikely(err)) {
9210 +                       AuTraceErr(err);
9211 +                       goto out_dgrade;
9212 +               }
9213 +       }
9214 +       di_downgrade_lock(dentry, AuLock_IR);
9215 +
9216 +       err = -EINVAL;
9217 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9218 +           && inode
9219 +           && !(inode->i_state && I_LINKABLE)
9220 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9221 +               AuTraceErr(err);
9222 +               goto out_inval;
9223 +       }
9224 +
9225 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9226 +       if (do_udba && inode) {
9227 +               aufs_bindex_t btop = au_ibtop(inode);
9228 +               struct inode *h_inode;
9229 +
9230 +               if (btop >= 0) {
9231 +                       h_inode = au_h_iptr(inode, btop);
9232 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9233 +                               AuTraceErr(err);
9234 +                               goto out_inval;
9235 +                       }
9236 +               }
9237 +       }
9238 +
9239 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9240 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9241 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9242 +               err = -EIO;
9243 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9244 +                     dentry, err);
9245 +       }
9246 +       goto out_inval;
9247 +
9248 +out_dgrade:
9249 +       di_downgrade_lock(dentry, AuLock_IR);
9250 +out_inval:
9251 +       aufs_read_unlock(dentry, AuLock_IR);
9252 +       AuTraceErr(err);
9253 +       valid = !err;
9254 +out:
9255 +       if (!valid) {
9256 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9257 +               d_drop(dentry);
9258 +       }
9259 +       return valid;
9260 +}
9261 +
9262 +static void aufs_d_release(struct dentry *dentry)
9263 +{
9264 +       if (au_di(dentry)) {
9265 +               au_di_fin(dentry);
9266 +               au_hn_di_reinit(dentry);
9267 +       }
9268 +}
9269 +
9270 +const struct dentry_operations aufs_dop = {
9271 +       .d_revalidate           = aufs_d_revalidate,
9272 +       .d_weak_revalidate      = aufs_d_revalidate,
9273 +       .d_release              = aufs_d_release
9274 +};
9275 +
9276 +/* aufs_dop without d_revalidate */
9277 +const struct dentry_operations aufs_dop_noreval = {
9278 +       .d_release              = aufs_d_release
9279 +};
9280 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9281 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9282 +++ linux/fs/aufs/dentry.h      2018-08-12 23:43:05.453457863 +0200
9283 @@ -0,0 +1,267 @@
9284 +/* SPDX-License-Identifier: GPL-2.0 */
9285 +/*
9286 + * Copyright (C) 2005-2018 Junjiro R. Okajima
9287 + *
9288 + * This program, aufs is free software; you can redistribute it and/or modify
9289 + * it under the terms of the GNU General Public License as published by
9290 + * the Free Software Foundation; either version 2 of the License, or
9291 + * (at your option) any later version.
9292 + *
9293 + * This program is distributed in the hope that it will be useful,
9294 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9295 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9296 + * GNU General Public License for more details.
9297 + *
9298 + * You should have received a copy of the GNU General Public License
9299 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9300 + */
9301 +
9302 +/*
9303 + * lookup and dentry operations
9304 + */
9305 +
9306 +#ifndef __AUFS_DENTRY_H__
9307 +#define __AUFS_DENTRY_H__
9308 +
9309 +#ifdef __KERNEL__
9310 +
9311 +#include <linux/dcache.h>
9312 +#include "dirren.h"
9313 +#include "rwsem.h"
9314 +
9315 +struct au_hdentry {
9316 +       struct dentry           *hd_dentry;
9317 +       aufs_bindex_t           hd_id;
9318 +};
9319 +
9320 +struct au_dinfo {
9321 +       atomic_t                di_generation;
9322 +
9323 +       struct au_rwsem         di_rwsem;
9324 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9325 +       unsigned char           di_tmpfile; /* to allow the different name */
9326 +       struct au_hdentry       *di_hdentry;
9327 +} ____cacheline_aligned_in_smp;
9328 +
9329 +/* ---------------------------------------------------------------------- */
9330 +
9331 +/* flags for au_lkup_dentry() */
9332 +#define AuLkup_ALLOW_NEG       1
9333 +#define AuLkup_IGNORE_PERM     (1 << 1)
9334 +#define AuLkup_DIRREN          (1 << 2)
9335 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9336 +#define au_fset_lkup(flags, name) \
9337 +       do { (flags) |= AuLkup_##name; } while (0)
9338 +#define au_fclr_lkup(flags, name) \
9339 +       do { (flags) &= ~AuLkup_##name; } while (0)
9340 +
9341 +#ifndef CONFIG_AUFS_DIRREN
9342 +#undef AuLkup_DIRREN
9343 +#define AuLkup_DIRREN 0
9344 +#endif
9345 +
9346 +struct au_do_lookup_args {
9347 +       unsigned int            flags;
9348 +       mode_t                  type;
9349 +       struct qstr             whname, *name;
9350 +       struct au_dr_lookup     dirren;
9351 +};
9352 +
9353 +/* ---------------------------------------------------------------------- */
9354 +
9355 +/* dentry.c */
9356 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9357 +struct au_branch;
9358 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9359 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9360 +               struct dentry *h_parent, struct au_branch *br);
9361 +
9362 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9363 +                  unsigned int flags);
9364 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9365 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9366 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9367 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9368 +
9369 +/* dinfo.c */
9370 +void au_di_init_once(void *_di);
9371 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9372 +void au_di_free(struct au_dinfo *dinfo);
9373 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9374 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9375 +int au_di_init(struct dentry *dentry);
9376 +void au_di_fin(struct dentry *dentry);
9377 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9378 +
9379 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9380 +void di_read_unlock(struct dentry *d, int flags);
9381 +void di_downgrade_lock(struct dentry *d, int flags);
9382 +void di_write_lock(struct dentry *d, unsigned int lsc);
9383 +void di_write_unlock(struct dentry *d);
9384 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9385 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9386 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9387 +
9388 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9389 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9390 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9391 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9392 +
9393 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9394 +                  struct dentry *h_dentry);
9395 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9396 +int au_dbrange_test(struct dentry *dentry);
9397 +void au_update_digen(struct dentry *dentry);
9398 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9399 +void au_update_dbtop(struct dentry *dentry);
9400 +void au_update_dbbot(struct dentry *dentry);
9401 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9402 +
9403 +/* ---------------------------------------------------------------------- */
9404 +
9405 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9406 +{
9407 +       return dentry->d_fsdata;
9408 +}
9409 +
9410 +/* ---------------------------------------------------------------------- */
9411 +
9412 +/* lock subclass for dinfo */
9413 +enum {
9414 +       AuLsc_DI_CHILD,         /* child first */
9415 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9416 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9417 +       AuLsc_DI_PARENT,
9418 +       AuLsc_DI_PARENT2,
9419 +       AuLsc_DI_PARENT3,
9420 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9421 +};
9422 +
9423 +/*
9424 + * di_read_lock_child, di_write_lock_child,
9425 + * di_read_lock_child2, di_write_lock_child2,
9426 + * di_read_lock_child3, di_write_lock_child3,
9427 + * di_read_lock_parent, di_write_lock_parent,
9428 + * di_read_lock_parent2, di_write_lock_parent2,
9429 + * di_read_lock_parent3, di_write_lock_parent3,
9430 + */
9431 +#define AuReadLockFunc(name, lsc) \
9432 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9433 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9434 +
9435 +#define AuWriteLockFunc(name, lsc) \
9436 +static inline void di_write_lock_##name(struct dentry *d) \
9437 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9438 +
9439 +#define AuRWLockFuncs(name, lsc) \
9440 +       AuReadLockFunc(name, lsc) \
9441 +       AuWriteLockFunc(name, lsc)
9442 +
9443 +AuRWLockFuncs(child, CHILD);
9444 +AuRWLockFuncs(child2, CHILD2);
9445 +AuRWLockFuncs(child3, CHILD3);
9446 +AuRWLockFuncs(parent, PARENT);
9447 +AuRWLockFuncs(parent2, PARENT2);
9448 +AuRWLockFuncs(parent3, PARENT3);
9449 +
9450 +#undef AuReadLockFunc
9451 +#undef AuWriteLockFunc
9452 +#undef AuRWLockFuncs
9453 +
9454 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9455 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9456 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9457 +
9458 +/* ---------------------------------------------------------------------- */
9459 +
9460 +/* todo: memory barrier? */
9461 +static inline unsigned int au_digen(struct dentry *d)
9462 +{
9463 +       return atomic_read(&au_di(d)->di_generation);
9464 +}
9465 +
9466 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9467 +{
9468 +       hdentry->hd_dentry = NULL;
9469 +}
9470 +
9471 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9472 +                                           aufs_bindex_t bindex)
9473 +{
9474 +       return di->di_hdentry + bindex;
9475 +}
9476 +
9477 +static inline void au_hdput(struct au_hdentry *hd)
9478 +{
9479 +       if (hd)
9480 +               dput(hd->hd_dentry);
9481 +}
9482 +
9483 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9484 +{
9485 +       DiMustAnyLock(dentry);
9486 +       return au_di(dentry)->di_btop;
9487 +}
9488 +
9489 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9490 +{
9491 +       DiMustAnyLock(dentry);
9492 +       return au_di(dentry)->di_bbot;
9493 +}
9494 +
9495 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9496 +{
9497 +       DiMustAnyLock(dentry);
9498 +       return au_di(dentry)->di_bwh;
9499 +}
9500 +
9501 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9502 +{
9503 +       DiMustAnyLock(dentry);
9504 +       return au_di(dentry)->di_bdiropq;
9505 +}
9506 +
9507 +/* todo: hard/soft set? */
9508 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9509 +{
9510 +       DiMustWriteLock(dentry);
9511 +       au_di(dentry)->di_btop = bindex;
9512 +}
9513 +
9514 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9515 +{
9516 +       DiMustWriteLock(dentry);
9517 +       au_di(dentry)->di_bbot = bindex;
9518 +}
9519 +
9520 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9521 +{
9522 +       DiMustWriteLock(dentry);
9523 +       /* dbwh can be outside of btop - bbot range */
9524 +       au_di(dentry)->di_bwh = bindex;
9525 +}
9526 +
9527 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9528 +{
9529 +       DiMustWriteLock(dentry);
9530 +       au_di(dentry)->di_bdiropq = bindex;
9531 +}
9532 +
9533 +/* ---------------------------------------------------------------------- */
9534 +
9535 +#ifdef CONFIG_AUFS_HNOTIFY
9536 +static inline void au_digen_dec(struct dentry *d)
9537 +{
9538 +       atomic_dec(&au_di(d)->di_generation);
9539 +}
9540 +
9541 +static inline void au_hn_di_reinit(struct dentry *dentry)
9542 +{
9543 +       dentry->d_fsdata = NULL;
9544 +}
9545 +#else
9546 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9547 +#endif /* CONFIG_AUFS_HNOTIFY */
9548 +
9549 +#endif /* __KERNEL__ */
9550 +#endif /* __AUFS_DENTRY_H__ */
9551 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9552 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9553 +++ linux/fs/aufs/dinfo.c       2018-08-12 23:43:05.453457863 +0200
9554 @@ -0,0 +1,554 @@
9555 +// SPDX-License-Identifier: GPL-2.0
9556 +/*
9557 + * Copyright (C) 2005-2018 Junjiro R. Okajima
9558 + *
9559 + * This program, aufs is free software; you can redistribute it and/or modify
9560 + * it under the terms of the GNU General Public License as published by
9561 + * the Free Software Foundation; either version 2 of the License, or
9562 + * (at your option) any later version.
9563 + *
9564 + * This program is distributed in the hope that it will be useful,
9565 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9566 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9567 + * GNU General Public License for more details.
9568 + *
9569 + * You should have received a copy of the GNU General Public License
9570 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9571 + */
9572 +
9573 +/*
9574 + * dentry private data
9575 + */
9576 +
9577 +#include "aufs.h"
9578 +
9579 +void au_di_init_once(void *_dinfo)
9580 +{
9581 +       struct au_dinfo *dinfo = _dinfo;
9582 +
9583 +       au_rw_init(&dinfo->di_rwsem);
9584 +}
9585 +
9586 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9587 +{
9588 +       struct au_dinfo *dinfo;
9589 +       int nbr, i;
9590 +
9591 +       dinfo = au_cache_alloc_dinfo();
9592 +       if (unlikely(!dinfo))
9593 +               goto out;
9594 +
9595 +       nbr = au_sbbot(sb) + 1;
9596 +       if (nbr <= 0)
9597 +               nbr = 1;
9598 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9599 +       if (dinfo->di_hdentry) {
9600 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9601 +               dinfo->di_btop = -1;
9602 +               dinfo->di_bbot = -1;
9603 +               dinfo->di_bwh = -1;
9604 +               dinfo->di_bdiropq = -1;
9605 +               dinfo->di_tmpfile = 0;
9606 +               for (i = 0; i < nbr; i++)
9607 +                       dinfo->di_hdentry[i].hd_id = -1;
9608 +               goto out;
9609 +       }
9610 +
9611 +       au_cache_free_dinfo(dinfo);
9612 +       dinfo = NULL;
9613 +
9614 +out:
9615 +       return dinfo;
9616 +}
9617 +
9618 +void au_di_free(struct au_dinfo *dinfo)
9619 +{
9620 +       struct au_hdentry *p;
9621 +       aufs_bindex_t bbot, bindex;
9622 +
9623 +       /* dentry may not be revalidated */
9624 +       bindex = dinfo->di_btop;
9625 +       if (bindex >= 0) {
9626 +               bbot = dinfo->di_bbot;
9627 +               p = au_hdentry(dinfo, bindex);
9628 +               while (bindex++ <= bbot)
9629 +                       au_hdput(p++);
9630 +       }
9631 +       kfree(dinfo->di_hdentry);
9632 +       au_cache_free_dinfo(dinfo);
9633 +}
9634 +
9635 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9636 +{
9637 +       struct au_hdentry *p;
9638 +       aufs_bindex_t bi;
9639 +
9640 +       AuRwMustWriteLock(&a->di_rwsem);
9641 +       AuRwMustWriteLock(&b->di_rwsem);
9642 +
9643 +#define DiSwap(v, name)                                \
9644 +       do {                                    \
9645 +               v = a->di_##name;               \
9646 +               a->di_##name = b->di_##name;    \
9647 +               b->di_##name = v;               \
9648 +       } while (0)
9649 +
9650 +       DiSwap(p, hdentry);
9651 +       DiSwap(bi, btop);
9652 +       DiSwap(bi, bbot);
9653 +       DiSwap(bi, bwh);
9654 +       DiSwap(bi, bdiropq);
9655 +       /* smp_mb(); */
9656 +
9657 +#undef DiSwap
9658 +}
9659 +
9660 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9661 +{
9662 +       AuRwMustWriteLock(&dst->di_rwsem);
9663 +       AuRwMustWriteLock(&src->di_rwsem);
9664 +
9665 +       dst->di_btop = src->di_btop;
9666 +       dst->di_bbot = src->di_bbot;
9667 +       dst->di_bwh = src->di_bwh;
9668 +       dst->di_bdiropq = src->di_bdiropq;
9669 +       /* smp_mb(); */
9670 +}
9671 +
9672 +int au_di_init(struct dentry *dentry)
9673 +{
9674 +       int err;
9675 +       struct super_block *sb;
9676 +       struct au_dinfo *dinfo;
9677 +
9678 +       err = 0;
9679 +       sb = dentry->d_sb;
9680 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9681 +       if (dinfo) {
9682 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9683 +               /* smp_mb(); */ /* atomic_set */
9684 +               dentry->d_fsdata = dinfo;
9685 +       } else
9686 +               err = -ENOMEM;
9687 +
9688 +       return err;
9689 +}
9690 +
9691 +void au_di_fin(struct dentry *dentry)
9692 +{
9693 +       struct au_dinfo *dinfo;
9694 +
9695 +       dinfo = au_di(dentry);
9696 +       AuRwDestroy(&dinfo->di_rwsem);
9697 +       au_di_free(dinfo);
9698 +}
9699 +
9700 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9701 +{
9702 +       int err, sz;
9703 +       struct au_hdentry *hdp;
9704 +
9705 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9706 +
9707 +       err = -ENOMEM;
9708 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9709 +       if (!sz)
9710 +               sz = sizeof(*hdp);
9711 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9712 +                          may_shrink);
9713 +       if (hdp) {
9714 +               dinfo->di_hdentry = hdp;
9715 +               err = 0;
9716 +       }
9717 +
9718 +       return err;
9719 +}
9720 +
9721 +/* ---------------------------------------------------------------------- */
9722 +
9723 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9724 +{
9725 +       switch (lsc) {
9726 +       case AuLsc_DI_CHILD:
9727 +               ii_write_lock_child(inode);
9728 +               break;
9729 +       case AuLsc_DI_CHILD2:
9730 +               ii_write_lock_child2(inode);
9731 +               break;
9732 +       case AuLsc_DI_CHILD3:
9733 +               ii_write_lock_child3(inode);
9734 +               break;
9735 +       case AuLsc_DI_PARENT:
9736 +               ii_write_lock_parent(inode);
9737 +               break;
9738 +       case AuLsc_DI_PARENT2:
9739 +               ii_write_lock_parent2(inode);
9740 +               break;
9741 +       case AuLsc_DI_PARENT3:
9742 +               ii_write_lock_parent3(inode);
9743 +               break;
9744 +       default:
9745 +               BUG();
9746 +       }
9747 +}
9748 +
9749 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9750 +{
9751 +       switch (lsc) {
9752 +       case AuLsc_DI_CHILD:
9753 +               ii_read_lock_child(inode);
9754 +               break;
9755 +       case AuLsc_DI_CHILD2:
9756 +               ii_read_lock_child2(inode);
9757 +               break;
9758 +       case AuLsc_DI_CHILD3:
9759 +               ii_read_lock_child3(inode);
9760 +               break;
9761 +       case AuLsc_DI_PARENT:
9762 +               ii_read_lock_parent(inode);
9763 +               break;
9764 +       case AuLsc_DI_PARENT2:
9765 +               ii_read_lock_parent2(inode);
9766 +               break;
9767 +       case AuLsc_DI_PARENT3:
9768 +               ii_read_lock_parent3(inode);
9769 +               break;
9770 +       default:
9771 +               BUG();
9772 +       }
9773 +}
9774 +
9775 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9776 +{
9777 +       struct inode *inode;
9778 +
9779 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9780 +       if (d_really_is_positive(d)) {
9781 +               inode = d_inode(d);
9782 +               if (au_ftest_lock(flags, IW))
9783 +                       do_ii_write_lock(inode, lsc);
9784 +               else if (au_ftest_lock(flags, IR))
9785 +                       do_ii_read_lock(inode, lsc);
9786 +       }
9787 +}
9788 +
9789 +void di_read_unlock(struct dentry *d, int flags)
9790 +{
9791 +       struct inode *inode;
9792 +
9793 +       if (d_really_is_positive(d)) {
9794 +               inode = d_inode(d);
9795 +               if (au_ftest_lock(flags, IW)) {
9796 +                       au_dbg_verify_dinode(d);
9797 +                       ii_write_unlock(inode);
9798 +               } else if (au_ftest_lock(flags, IR)) {
9799 +                       au_dbg_verify_dinode(d);
9800 +                       ii_read_unlock(inode);
9801 +               }
9802 +       }
9803 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9804 +}
9805 +
9806 +void di_downgrade_lock(struct dentry *d, int flags)
9807 +{
9808 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9809 +               ii_downgrade_lock(d_inode(d));
9810 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9811 +}
9812 +
9813 +void di_write_lock(struct dentry *d, unsigned int lsc)
9814 +{
9815 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9816 +       if (d_really_is_positive(d))
9817 +               do_ii_write_lock(d_inode(d), lsc);
9818 +}
9819 +
9820 +void di_write_unlock(struct dentry *d)
9821 +{
9822 +       au_dbg_verify_dinode(d);
9823 +       if (d_really_is_positive(d))
9824 +               ii_write_unlock(d_inode(d));
9825 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9826 +}
9827 +
9828 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9829 +{
9830 +       AuDebugOn(d1 == d2
9831 +                 || d_inode(d1) == d_inode(d2)
9832 +                 || d1->d_sb != d2->d_sb);
9833 +
9834 +       if ((isdir && au_test_subdir(d1, d2))
9835 +           || d1 < d2) {
9836 +               di_write_lock_child(d1);
9837 +               di_write_lock_child2(d2);
9838 +       } else {
9839 +               di_write_lock_child(d2);
9840 +               di_write_lock_child2(d1);
9841 +       }
9842 +}
9843 +
9844 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9845 +{
9846 +       AuDebugOn(d1 == d2
9847 +                 || d_inode(d1) == d_inode(d2)
9848 +                 || d1->d_sb != d2->d_sb);
9849 +
9850 +       if ((isdir && au_test_subdir(d1, d2))
9851 +           || d1 < d2) {
9852 +               di_write_lock_parent(d1);
9853 +               di_write_lock_parent2(d2);
9854 +       } else {
9855 +               di_write_lock_parent(d2);
9856 +               di_write_lock_parent2(d1);
9857 +       }
9858 +}
9859 +
9860 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9861 +{
9862 +       di_write_unlock(d1);
9863 +       if (d_inode(d1) == d_inode(d2))
9864 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9865 +       else
9866 +               di_write_unlock(d2);
9867 +}
9868 +
9869 +/* ---------------------------------------------------------------------- */
9870 +
9871 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9872 +{
9873 +       struct dentry *d;
9874 +
9875 +       DiMustAnyLock(dentry);
9876 +
9877 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9878 +               return NULL;
9879 +       AuDebugOn(bindex < 0);
9880 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9881 +       AuDebugOn(d && au_dcount(d) <= 0);
9882 +       return d;
9883 +}
9884 +
9885 +/*
9886 + * extended version of au_h_dptr().
9887 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9888 + * error.
9889 + */
9890 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9891 +{
9892 +       struct dentry *h_dentry;
9893 +       struct inode *inode, *h_inode;
9894 +
9895 +       AuDebugOn(d_really_is_negative(dentry));
9896 +
9897 +       h_dentry = NULL;
9898 +       if (au_dbtop(dentry) <= bindex
9899 +           && bindex <= au_dbbot(dentry))
9900 +               h_dentry = au_h_dptr(dentry, bindex);
9901 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9902 +               dget(h_dentry);
9903 +               goto out; /* success */
9904 +       }
9905 +
9906 +       inode = d_inode(dentry);
9907 +       AuDebugOn(bindex < au_ibtop(inode));
9908 +       AuDebugOn(au_ibbot(inode) < bindex);
9909 +       h_inode = au_h_iptr(inode, bindex);
9910 +       h_dentry = d_find_alias(h_inode);
9911 +       if (h_dentry) {
9912 +               if (!IS_ERR(h_dentry)) {
9913 +                       if (!au_d_linkable(h_dentry))
9914 +                               goto out; /* success */
9915 +                       dput(h_dentry);
9916 +               } else
9917 +                       goto out;
9918 +       }
9919 +
9920 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9921 +               h_dentry = au_plink_lkup(inode, bindex);
9922 +               AuDebugOn(!h_dentry);
9923 +               if (!IS_ERR(h_dentry)) {
9924 +                       if (!au_d_hashed_positive(h_dentry))
9925 +                               goto out; /* success */
9926 +                       dput(h_dentry);
9927 +                       h_dentry = NULL;
9928 +               }
9929 +       }
9930 +
9931 +out:
9932 +       AuDbgDentry(h_dentry);
9933 +       return h_dentry;
9934 +}
9935 +
9936 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9937 +{
9938 +       aufs_bindex_t bbot, bwh;
9939 +
9940 +       bbot = au_dbbot(dentry);
9941 +       if (0 <= bbot) {
9942 +               bwh = au_dbwh(dentry);
9943 +               if (!bwh)
9944 +                       return bwh;
9945 +               if (0 < bwh && bwh < bbot)
9946 +                       return bwh - 1;
9947 +       }
9948 +       return bbot;
9949 +}
9950 +
9951 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9952 +{
9953 +       aufs_bindex_t bbot, bopq;
9954 +
9955 +       bbot = au_dbtail(dentry);
9956 +       if (0 <= bbot) {
9957 +               bopq = au_dbdiropq(dentry);
9958 +               if (0 <= bopq && bopq < bbot)
9959 +                       bbot = bopq;
9960 +       }
9961 +       return bbot;
9962 +}
9963 +
9964 +/* ---------------------------------------------------------------------- */
9965 +
9966 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9967 +                  struct dentry *h_dentry)
9968 +{
9969 +       struct au_dinfo *dinfo;
9970 +       struct au_hdentry *hd;
9971 +       struct au_branch *br;
9972 +
9973 +       DiMustWriteLock(dentry);
9974 +
9975 +       dinfo = au_di(dentry);
9976 +       hd = au_hdentry(dinfo, bindex);
9977 +       au_hdput(hd);
9978 +       hd->hd_dentry = h_dentry;
9979 +       if (h_dentry) {
9980 +               br = au_sbr(dentry->d_sb, bindex);
9981 +               hd->hd_id = br->br_id;
9982 +       }
9983 +}
9984 +
9985 +int au_dbrange_test(struct dentry *dentry)
9986 +{
9987 +       int err;
9988 +       aufs_bindex_t btop, bbot;
9989 +
9990 +       err = 0;
9991 +       btop = au_dbtop(dentry);
9992 +       bbot = au_dbbot(dentry);
9993 +       if (btop >= 0)
9994 +               AuDebugOn(bbot < 0 && btop > bbot);
9995 +       else {
9996 +               err = -EIO;
9997 +               AuDebugOn(bbot >= 0);
9998 +       }
9999 +
10000 +       return err;
10001 +}
10002 +
10003 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
10004 +{
10005 +       int err;
10006 +
10007 +       err = 0;
10008 +       if (unlikely(au_digen(dentry) != sigen
10009 +                    || au_iigen_test(d_inode(dentry), sigen)))
10010 +               err = -EIO;
10011 +
10012 +       return err;
10013 +}
10014 +
10015 +void au_update_digen(struct dentry *dentry)
10016 +{
10017 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10018 +       /* smp_mb(); */ /* atomic_set */
10019 +}
10020 +
10021 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10022 +{
10023 +       struct au_dinfo *dinfo;
10024 +       struct dentry *h_d;
10025 +       struct au_hdentry *hdp;
10026 +       aufs_bindex_t bindex, bbot;
10027 +
10028 +       DiMustWriteLock(dentry);
10029 +
10030 +       dinfo = au_di(dentry);
10031 +       if (!dinfo || dinfo->di_btop < 0)
10032 +               return;
10033 +
10034 +       if (do_put_zero) {
10035 +               bbot = dinfo->di_bbot;
10036 +               bindex = dinfo->di_btop;
10037 +               hdp = au_hdentry(dinfo, bindex);
10038 +               for (; bindex <= bbot; bindex++, hdp++) {
10039 +                       h_d = hdp->hd_dentry;
10040 +                       if (h_d && d_is_negative(h_d))
10041 +                               au_set_h_dptr(dentry, bindex, NULL);
10042 +               }
10043 +       }
10044 +
10045 +       dinfo->di_btop = 0;
10046 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
10047 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10048 +               if (hdp->hd_dentry)
10049 +                       break;
10050 +       if (dinfo->di_btop > dinfo->di_bbot) {
10051 +               dinfo->di_btop = -1;
10052 +               dinfo->di_bbot = -1;
10053 +               return;
10054 +       }
10055 +
10056 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
10057 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10058 +               if (hdp->hd_dentry)
10059 +                       break;
10060 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
10061 +}
10062 +
10063 +void au_update_dbtop(struct dentry *dentry)
10064 +{
10065 +       aufs_bindex_t bindex, bbot;
10066 +       struct dentry *h_dentry;
10067 +
10068 +       bbot = au_dbbot(dentry);
10069 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
10070 +               h_dentry = au_h_dptr(dentry, bindex);
10071 +               if (!h_dentry)
10072 +                       continue;
10073 +               if (d_is_positive(h_dentry)) {
10074 +                       au_set_dbtop(dentry, bindex);
10075 +                       return;
10076 +               }
10077 +               au_set_h_dptr(dentry, bindex, NULL);
10078 +       }
10079 +}
10080 +
10081 +void au_update_dbbot(struct dentry *dentry)
10082 +{
10083 +       aufs_bindex_t bindex, btop;
10084 +       struct dentry *h_dentry;
10085 +
10086 +       btop = au_dbtop(dentry);
10087 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
10088 +               h_dentry = au_h_dptr(dentry, bindex);
10089 +               if (!h_dentry)
10090 +                       continue;
10091 +               if (d_is_positive(h_dentry)) {
10092 +                       au_set_dbbot(dentry, bindex);
10093 +                       return;
10094 +               }
10095 +               au_set_h_dptr(dentry, bindex, NULL);
10096 +       }
10097 +}
10098 +
10099 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10100 +{
10101 +       aufs_bindex_t bindex, bbot;
10102 +
10103 +       bbot = au_dbbot(dentry);
10104 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
10105 +               if (au_h_dptr(dentry, bindex) == h_dentry)
10106 +                       return bindex;
10107 +       return -1;
10108 +}
10109 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10110 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
10111 +++ linux/fs/aufs/dir.c 2018-08-12 23:43:05.453457863 +0200
10112 @@ -0,0 +1,760 @@
10113 +// SPDX-License-Identifier: GPL-2.0
10114 +/*
10115 + * Copyright (C) 2005-2018 Junjiro R. Okajima
10116 + *
10117 + * This program, aufs is free software; you can redistribute it and/or modify
10118 + * it under the terms of the GNU General Public License as published by
10119 + * the Free Software Foundation; either version 2 of the License, or
10120 + * (at your option) any later version.
10121 + *
10122 + * This program is distributed in the hope that it will be useful,
10123 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10124 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10125 + * GNU General Public License for more details.
10126 + *
10127 + * You should have received a copy of the GNU General Public License
10128 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10129 + */
10130 +
10131 +/*
10132 + * directory operations
10133 + */
10134 +
10135 +#include <linux/fs_stack.h>
10136 +#include "aufs.h"
10137 +
10138 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10139 +{
10140 +       unsigned int nlink;
10141 +
10142 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10143 +
10144 +       nlink = dir->i_nlink;
10145 +       nlink += h_dir->i_nlink - 2;
10146 +       if (h_dir->i_nlink < 2)
10147 +               nlink += 2;
10148 +       smp_mb(); /* for i_nlink */
10149 +       /* 0 can happen in revaliding */
10150 +       set_nlink(dir, nlink);
10151 +}
10152 +
10153 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10154 +{
10155 +       unsigned int nlink;
10156 +
10157 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10158 +
10159 +       nlink = dir->i_nlink;
10160 +       nlink -= h_dir->i_nlink - 2;
10161 +       if (h_dir->i_nlink < 2)
10162 +               nlink -= 2;
10163 +       smp_mb(); /* for i_nlink */
10164 +       /* nlink == 0 means the branch-fs is broken */
10165 +       set_nlink(dir, nlink);
10166 +}
10167 +
10168 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10169 +{
10170 +       loff_t sz;
10171 +       aufs_bindex_t bindex, bbot;
10172 +       struct file *h_file;
10173 +       struct dentry *h_dentry;
10174 +
10175 +       sz = 0;
10176 +       if (file) {
10177 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10178 +
10179 +               bbot = au_fbbot_dir(file);
10180 +               for (bindex = au_fbtop(file);
10181 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10182 +                    bindex++) {
10183 +                       h_file = au_hf_dir(file, bindex);
10184 +                       if (h_file && file_inode(h_file))
10185 +                               sz += vfsub_f_size_read(h_file);
10186 +               }
10187 +       } else {
10188 +               AuDebugOn(!dentry);
10189 +               AuDebugOn(!d_is_dir(dentry));
10190 +
10191 +               bbot = au_dbtaildir(dentry);
10192 +               for (bindex = au_dbtop(dentry);
10193 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10194 +                    bindex++) {
10195 +                       h_dentry = au_h_dptr(dentry, bindex);
10196 +                       if (h_dentry && d_is_positive(h_dentry))
10197 +                               sz += i_size_read(d_inode(h_dentry));
10198 +               }
10199 +       }
10200 +       if (sz < KMALLOC_MAX_SIZE)
10201 +               sz = roundup_pow_of_two(sz);
10202 +       if (sz > KMALLOC_MAX_SIZE)
10203 +               sz = KMALLOC_MAX_SIZE;
10204 +       else if (sz < NAME_MAX) {
10205 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10206 +               sz = AUFS_RDBLK_DEF;
10207 +       }
10208 +       return sz;
10209 +}
10210 +
10211 +struct au_dir_ts_arg {
10212 +       struct dentry *dentry;
10213 +       aufs_bindex_t brid;
10214 +};
10215 +
10216 +static void au_do_dir_ts(void *arg)
10217 +{
10218 +       struct au_dir_ts_arg *a = arg;
10219 +       struct au_dtime dt;
10220 +       struct path h_path;
10221 +       struct inode *dir, *h_dir;
10222 +       struct super_block *sb;
10223 +       struct au_branch *br;
10224 +       struct au_hinode *hdir;
10225 +       int err;
10226 +       aufs_bindex_t btop, bindex;
10227 +
10228 +       sb = a->dentry->d_sb;
10229 +       if (d_really_is_negative(a->dentry))
10230 +               goto out;
10231 +       /* no dir->i_mutex lock */
10232 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10233 +
10234 +       dir = d_inode(a->dentry);
10235 +       btop = au_ibtop(dir);
10236 +       bindex = au_br_index(sb, a->brid);
10237 +       if (bindex < btop)
10238 +               goto out_unlock;
10239 +
10240 +       br = au_sbr(sb, bindex);
10241 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10242 +       if (!h_path.dentry)
10243 +               goto out_unlock;
10244 +       h_path.mnt = au_br_mnt(br);
10245 +       au_dtime_store(&dt, a->dentry, &h_path);
10246 +
10247 +       br = au_sbr(sb, btop);
10248 +       if (!au_br_writable(br->br_perm))
10249 +               goto out_unlock;
10250 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10251 +       h_path.mnt = au_br_mnt(br);
10252 +       err = vfsub_mnt_want_write(h_path.mnt);
10253 +       if (err)
10254 +               goto out_unlock;
10255 +       hdir = au_hi(dir, btop);
10256 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10257 +       h_dir = au_h_iptr(dir, btop);
10258 +       if (h_dir->i_nlink
10259 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10260 +               dt.dt_h_path = h_path;
10261 +               au_dtime_revert(&dt);
10262 +       }
10263 +       au_hn_inode_unlock(hdir);
10264 +       vfsub_mnt_drop_write(h_path.mnt);
10265 +       au_cpup_attr_timesizes(dir);
10266 +
10267 +out_unlock:
10268 +       aufs_read_unlock(a->dentry, AuLock_DW);
10269 +out:
10270 +       dput(a->dentry);
10271 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10272 +       kfree(arg);
10273 +}
10274 +
10275 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10276 +{
10277 +       int perm, wkq_err;
10278 +       aufs_bindex_t btop;
10279 +       struct au_dir_ts_arg *arg;
10280 +       struct dentry *dentry;
10281 +       struct super_block *sb;
10282 +
10283 +       IMustLock(dir);
10284 +
10285 +       dentry = d_find_any_alias(dir);
10286 +       AuDebugOn(!dentry);
10287 +       sb = dentry->d_sb;
10288 +       btop = au_ibtop(dir);
10289 +       if (btop == bindex) {
10290 +               au_cpup_attr_timesizes(dir);
10291 +               goto out;
10292 +       }
10293 +
10294 +       perm = au_sbr_perm(sb, btop);
10295 +       if (!au_br_writable(perm))
10296 +               goto out;
10297 +
10298 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10299 +       if (!arg)
10300 +               goto out;
10301 +
10302 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10303 +       arg->brid = au_sbr_id(sb, bindex);
10304 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10305 +       if (unlikely(wkq_err)) {
10306 +               pr_err("wkq %d\n", wkq_err);
10307 +               dput(dentry);
10308 +               kfree(arg);
10309 +       }
10310 +
10311 +out:
10312 +       dput(dentry);
10313 +}
10314 +
10315 +/* ---------------------------------------------------------------------- */
10316 +
10317 +static int reopen_dir(struct file *file)
10318 +{
10319 +       int err;
10320 +       unsigned int flags;
10321 +       aufs_bindex_t bindex, btail, btop;
10322 +       struct dentry *dentry, *h_dentry;
10323 +       struct file *h_file;
10324 +
10325 +       /* open all lower dirs */
10326 +       dentry = file->f_path.dentry;
10327 +       btop = au_dbtop(dentry);
10328 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10329 +               au_set_h_fptr(file, bindex, NULL);
10330 +       au_set_fbtop(file, btop);
10331 +
10332 +       btail = au_dbtaildir(dentry);
10333 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10334 +               au_set_h_fptr(file, bindex, NULL);
10335 +       au_set_fbbot_dir(file, btail);
10336 +
10337 +       flags = vfsub_file_flags(file);
10338 +       for (bindex = btop; bindex <= btail; bindex++) {
10339 +               h_dentry = au_h_dptr(dentry, bindex);
10340 +               if (!h_dentry)
10341 +                       continue;
10342 +               h_file = au_hf_dir(file, bindex);
10343 +               if (h_file)
10344 +                       continue;
10345 +
10346 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10347 +               err = PTR_ERR(h_file);
10348 +               if (IS_ERR(h_file))
10349 +                       goto out; /* close all? */
10350 +               au_set_h_fptr(file, bindex, h_file);
10351 +       }
10352 +       au_update_figen(file);
10353 +       /* todo: necessary? */
10354 +       /* file->f_ra = h_file->f_ra; */
10355 +       err = 0;
10356 +
10357 +out:
10358 +       return err;
10359 +}
10360 +
10361 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10362 +{
10363 +       int err;
10364 +       aufs_bindex_t bindex, btail;
10365 +       struct dentry *dentry, *h_dentry;
10366 +       struct vfsmount *mnt;
10367 +
10368 +       FiMustWriteLock(file);
10369 +       AuDebugOn(h_file);
10370 +
10371 +       err = 0;
10372 +       mnt = file->f_path.mnt;
10373 +       dentry = file->f_path.dentry;
10374 +       file->f_version = inode_query_iversion(d_inode(dentry));
10375 +       bindex = au_dbtop(dentry);
10376 +       au_set_fbtop(file, bindex);
10377 +       btail = au_dbtaildir(dentry);
10378 +       au_set_fbbot_dir(file, btail);
10379 +       for (; !err && bindex <= btail; bindex++) {
10380 +               h_dentry = au_h_dptr(dentry, bindex);
10381 +               if (!h_dentry)
10382 +                       continue;
10383 +
10384 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10385 +               if (unlikely(err))
10386 +                       break;
10387 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10388 +               if (IS_ERR(h_file)) {
10389 +                       err = PTR_ERR(h_file);
10390 +                       break;
10391 +               }
10392 +               au_set_h_fptr(file, bindex, h_file);
10393 +       }
10394 +       au_update_figen(file);
10395 +       /* todo: necessary? */
10396 +       /* file->f_ra = h_file->f_ra; */
10397 +       if (!err)
10398 +               return 0; /* success */
10399 +
10400 +       /* close all */
10401 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10402 +               au_set_h_fptr(file, bindex, NULL);
10403 +       au_set_fbtop(file, -1);
10404 +       au_set_fbbot_dir(file, -1);
10405 +
10406 +       return err;
10407 +}
10408 +
10409 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10410 +                        struct file *file)
10411 +{
10412 +       int err;
10413 +       struct super_block *sb;
10414 +       struct au_fidir *fidir;
10415 +
10416 +       err = -ENOMEM;
10417 +       sb = file->f_path.dentry->d_sb;
10418 +       si_read_lock(sb, AuLock_FLUSH);
10419 +       fidir = au_fidir_alloc(sb);
10420 +       if (fidir) {
10421 +               struct au_do_open_args args = {
10422 +                       .open   = do_open_dir,
10423 +                       .fidir  = fidir
10424 +               };
10425 +               err = au_do_open(file, &args);
10426 +               if (unlikely(err))
10427 +                       kfree(fidir);
10428 +       }
10429 +       si_read_unlock(sb);
10430 +       return err;
10431 +}
10432 +
10433 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10434 +                           struct file *file)
10435 +{
10436 +       struct au_vdir *vdir_cache;
10437 +       struct au_finfo *finfo;
10438 +       struct au_fidir *fidir;
10439 +       struct au_hfile *hf;
10440 +       aufs_bindex_t bindex, bbot;
10441 +
10442 +       finfo = au_fi(file);
10443 +       fidir = finfo->fi_hdir;
10444 +       if (fidir) {
10445 +               au_hbl_del(&finfo->fi_hlist,
10446 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10447 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10448 +               if (vdir_cache)
10449 +                       au_vdir_free(vdir_cache);
10450 +
10451 +               bindex = finfo->fi_btop;
10452 +               if (bindex >= 0) {
10453 +                       hf = fidir->fd_hfile + bindex;
10454 +                       /*
10455 +                        * calls fput() instead of filp_close(),
10456 +                        * since no dnotify or lock for the lower file.
10457 +                        */
10458 +                       bbot = fidir->fd_bbot;
10459 +                       for (; bindex <= bbot; bindex++, hf++)
10460 +                               if (hf->hf_file)
10461 +                                       au_hfput(hf, /*execed*/0);
10462 +               }
10463 +               kfree(fidir);
10464 +               finfo->fi_hdir = NULL;
10465 +       }
10466 +       au_finfo_fin(file);
10467 +       return 0;
10468 +}
10469 +
10470 +/* ---------------------------------------------------------------------- */
10471 +
10472 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10473 +{
10474 +       int err;
10475 +       aufs_bindex_t bindex, bbot;
10476 +       struct file *h_file;
10477 +
10478 +       err = 0;
10479 +       bbot = au_fbbot_dir(file);
10480 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10481 +               h_file = au_hf_dir(file, bindex);
10482 +               if (h_file)
10483 +                       err = vfsub_flush(h_file, id);
10484 +       }
10485 +       return err;
10486 +}
10487 +
10488 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10489 +{
10490 +       return au_do_flush(file, id, au_do_flush_dir);
10491 +}
10492 +
10493 +/* ---------------------------------------------------------------------- */
10494 +
10495 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10496 +{
10497 +       int err;
10498 +       aufs_bindex_t bbot, bindex;
10499 +       struct inode *inode;
10500 +       struct super_block *sb;
10501 +
10502 +       err = 0;
10503 +       sb = dentry->d_sb;
10504 +       inode = d_inode(dentry);
10505 +       IMustLock(inode);
10506 +       bbot = au_dbbot(dentry);
10507 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10508 +               struct path h_path;
10509 +
10510 +               if (au_test_ro(sb, bindex, inode))
10511 +                       continue;
10512 +               h_path.dentry = au_h_dptr(dentry, bindex);
10513 +               if (!h_path.dentry)
10514 +                       continue;
10515 +
10516 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10517 +               err = vfsub_fsync(NULL, &h_path, datasync);
10518 +       }
10519 +
10520 +       return err;
10521 +}
10522 +
10523 +static int au_do_fsync_dir(struct file *file, int datasync)
10524 +{
10525 +       int err;
10526 +       aufs_bindex_t bbot, bindex;
10527 +       struct file *h_file;
10528 +       struct super_block *sb;
10529 +       struct inode *inode;
10530 +
10531 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10532 +       if (unlikely(err))
10533 +               goto out;
10534 +
10535 +       inode = file_inode(file);
10536 +       sb = inode->i_sb;
10537 +       bbot = au_fbbot_dir(file);
10538 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10539 +               h_file = au_hf_dir(file, bindex);
10540 +               if (!h_file || au_test_ro(sb, bindex, inode))
10541 +                       continue;
10542 +
10543 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10544 +       }
10545 +
10546 +out:
10547 +       return err;
10548 +}
10549 +
10550 +/*
10551 + * @file may be NULL
10552 + */
10553 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10554 +                         int datasync)
10555 +{
10556 +       int err;
10557 +       struct dentry *dentry;
10558 +       struct inode *inode;
10559 +       struct super_block *sb;
10560 +
10561 +       err = 0;
10562 +       dentry = file->f_path.dentry;
10563 +       inode = d_inode(dentry);
10564 +       inode_lock(inode);
10565 +       sb = dentry->d_sb;
10566 +       si_noflush_read_lock(sb);
10567 +       if (file)
10568 +               err = au_do_fsync_dir(file, datasync);
10569 +       else {
10570 +               di_write_lock_child(dentry);
10571 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10572 +       }
10573 +       au_cpup_attr_timesizes(inode);
10574 +       di_write_unlock(dentry);
10575 +       if (file)
10576 +               fi_write_unlock(file);
10577 +
10578 +       si_read_unlock(sb);
10579 +       inode_unlock(inode);
10580 +       return err;
10581 +}
10582 +
10583 +/* ---------------------------------------------------------------------- */
10584 +
10585 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10586 +{
10587 +       int err;
10588 +       struct dentry *dentry;
10589 +       struct inode *inode, *h_inode;
10590 +       struct super_block *sb;
10591 +
10592 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10593 +
10594 +       dentry = file->f_path.dentry;
10595 +       inode = d_inode(dentry);
10596 +       IMustLock(inode);
10597 +
10598 +       sb = dentry->d_sb;
10599 +       si_read_lock(sb, AuLock_FLUSH);
10600 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10601 +       if (unlikely(err))
10602 +               goto out;
10603 +       err = au_alive_dir(dentry);
10604 +       if (!err)
10605 +               err = au_vdir_init(file);
10606 +       di_downgrade_lock(dentry, AuLock_IR);
10607 +       if (unlikely(err))
10608 +               goto out_unlock;
10609 +
10610 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10611 +       if (!au_test_nfsd()) {
10612 +               err = au_vdir_fill_de(file, ctx);
10613 +               fsstack_copy_attr_atime(inode, h_inode);
10614 +       } else {
10615 +               /*
10616 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10617 +                * encode_fh() and others.
10618 +                */
10619 +               atomic_inc(&h_inode->i_count);
10620 +               di_read_unlock(dentry, AuLock_IR);
10621 +               si_read_unlock(sb);
10622 +               err = au_vdir_fill_de(file, ctx);
10623 +               fsstack_copy_attr_atime(inode, h_inode);
10624 +               fi_write_unlock(file);
10625 +               iput(h_inode);
10626 +
10627 +               AuTraceErr(err);
10628 +               return err;
10629 +       }
10630 +
10631 +out_unlock:
10632 +       di_read_unlock(dentry, AuLock_IR);
10633 +       fi_write_unlock(file);
10634 +out:
10635 +       si_read_unlock(sb);
10636 +       return err;
10637 +}
10638 +
10639 +/* ---------------------------------------------------------------------- */
10640 +
10641 +#define AuTestEmpty_WHONLY     1
10642 +#define AuTestEmpty_CALLED     (1 << 1)
10643 +#define AuTestEmpty_SHWH       (1 << 2)
10644 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10645 +#define au_fset_testempty(flags, name) \
10646 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10647 +#define au_fclr_testempty(flags, name) \
10648 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10649 +
10650 +#ifndef CONFIG_AUFS_SHWH
10651 +#undef AuTestEmpty_SHWH
10652 +#define AuTestEmpty_SHWH       0
10653 +#endif
10654 +
10655 +struct test_empty_arg {
10656 +       struct dir_context ctx;
10657 +       struct au_nhash *whlist;
10658 +       unsigned int flags;
10659 +       int err;
10660 +       aufs_bindex_t bindex;
10661 +};
10662 +
10663 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10664 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10665 +                        unsigned int d_type)
10666 +{
10667 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10668 +                                                 ctx);
10669 +       char *name = (void *)__name;
10670 +
10671 +       arg->err = 0;
10672 +       au_fset_testempty(arg->flags, CALLED);
10673 +       /* smp_mb(); */
10674 +       if (name[0] == '.'
10675 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10676 +               goto out; /* success */
10677 +
10678 +       if (namelen <= AUFS_WH_PFX_LEN
10679 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10680 +               if (au_ftest_testempty(arg->flags, WHONLY)
10681 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10682 +                       arg->err = -ENOTEMPTY;
10683 +               goto out;
10684 +       }
10685 +
10686 +       name += AUFS_WH_PFX_LEN;
10687 +       namelen -= AUFS_WH_PFX_LEN;
10688 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10689 +               arg->err = au_nhash_append_wh
10690 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10691 +                        au_ftest_testempty(arg->flags, SHWH));
10692 +
10693 +out:
10694 +       /* smp_mb(); */
10695 +       AuTraceErr(arg->err);
10696 +       return arg->err;
10697 +}
10698 +
10699 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10700 +{
10701 +       int err;
10702 +       struct file *h_file;
10703 +
10704 +       h_file = au_h_open(dentry, arg->bindex,
10705 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10706 +                          /*file*/NULL, /*force_wr*/0);
10707 +       err = PTR_ERR(h_file);
10708 +       if (IS_ERR(h_file))
10709 +               goto out;
10710 +
10711 +       err = 0;
10712 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10713 +           && !file_inode(h_file)->i_nlink)
10714 +               goto out_put;
10715 +
10716 +       do {
10717 +               arg->err = 0;
10718 +               au_fclr_testempty(arg->flags, CALLED);
10719 +               /* smp_mb(); */
10720 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10721 +               if (err >= 0)
10722 +                       err = arg->err;
10723 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10724 +
10725 +out_put:
10726 +       fput(h_file);
10727 +       au_sbr_put(dentry->d_sb, arg->bindex);
10728 +out:
10729 +       return err;
10730 +}
10731 +
10732 +struct do_test_empty_args {
10733 +       int *errp;
10734 +       struct dentry *dentry;
10735 +       struct test_empty_arg *arg;
10736 +};
10737 +
10738 +static void call_do_test_empty(void *args)
10739 +{
10740 +       struct do_test_empty_args *a = args;
10741 +       *a->errp = do_test_empty(a->dentry, a->arg);
10742 +}
10743 +
10744 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10745 +{
10746 +       int err, wkq_err;
10747 +       struct dentry *h_dentry;
10748 +       struct inode *h_inode;
10749 +
10750 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10751 +       h_inode = d_inode(h_dentry);
10752 +       /* todo: i_mode changes anytime? */
10753 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10754 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10755 +       inode_unlock_shared(h_inode);
10756 +       if (!err)
10757 +               err = do_test_empty(dentry, arg);
10758 +       else {
10759 +               struct do_test_empty_args args = {
10760 +                       .errp   = &err,
10761 +                       .dentry = dentry,
10762 +                       .arg    = arg
10763 +               };
10764 +               unsigned int flags = arg->flags;
10765 +
10766 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10767 +               if (unlikely(wkq_err))
10768 +                       err = wkq_err;
10769 +               arg->flags = flags;
10770 +       }
10771 +
10772 +       return err;
10773 +}
10774 +
10775 +int au_test_empty_lower(struct dentry *dentry)
10776 +{
10777 +       int err;
10778 +       unsigned int rdhash;
10779 +       aufs_bindex_t bindex, btop, btail;
10780 +       struct au_nhash whlist;
10781 +       struct test_empty_arg arg = {
10782 +               .ctx = {
10783 +                       .actor = test_empty_cb
10784 +               }
10785 +       };
10786 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10787 +
10788 +       SiMustAnyLock(dentry->d_sb);
10789 +
10790 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10791 +       if (!rdhash)
10792 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10793 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10794 +       if (unlikely(err))
10795 +               goto out;
10796 +
10797 +       arg.flags = 0;
10798 +       arg.whlist = &whlist;
10799 +       btop = au_dbtop(dentry);
10800 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10801 +               au_fset_testempty(arg.flags, SHWH);
10802 +       test_empty = do_test_empty;
10803 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10804 +               test_empty = sio_test_empty;
10805 +       arg.bindex = btop;
10806 +       err = test_empty(dentry, &arg);
10807 +       if (unlikely(err))
10808 +               goto out_whlist;
10809 +
10810 +       au_fset_testempty(arg.flags, WHONLY);
10811 +       btail = au_dbtaildir(dentry);
10812 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10813 +               struct dentry *h_dentry;
10814 +
10815 +               h_dentry = au_h_dptr(dentry, bindex);
10816 +               if (h_dentry && d_is_positive(h_dentry)) {
10817 +                       arg.bindex = bindex;
10818 +                       err = test_empty(dentry, &arg);
10819 +               }
10820 +       }
10821 +
10822 +out_whlist:
10823 +       au_nhash_wh_free(&whlist);
10824 +out:
10825 +       return err;
10826 +}
10827 +
10828 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10829 +{
10830 +       int err;
10831 +       struct test_empty_arg arg = {
10832 +               .ctx = {
10833 +                       .actor = test_empty_cb
10834 +               }
10835 +       };
10836 +       aufs_bindex_t bindex, btail;
10837 +
10838 +       err = 0;
10839 +       arg.whlist = whlist;
10840 +       arg.flags = AuTestEmpty_WHONLY;
10841 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10842 +               au_fset_testempty(arg.flags, SHWH);
10843 +       btail = au_dbtaildir(dentry);
10844 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10845 +               struct dentry *h_dentry;
10846 +
10847 +               h_dentry = au_h_dptr(dentry, bindex);
10848 +               if (h_dentry && d_is_positive(h_dentry)) {
10849 +                       arg.bindex = bindex;
10850 +                       err = sio_test_empty(dentry, &arg);
10851 +               }
10852 +       }
10853 +
10854 +       return err;
10855 +}
10856 +
10857 +/* ---------------------------------------------------------------------- */
10858 +
10859 +const struct file_operations aufs_dir_fop = {
10860 +       .owner          = THIS_MODULE,
10861 +       .llseek         = default_llseek,
10862 +       .read           = generic_read_dir,
10863 +       .iterate_shared = aufs_iterate_shared,
10864 +       .unlocked_ioctl = aufs_ioctl_dir,
10865 +#ifdef CONFIG_COMPAT
10866 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10867 +#endif
10868 +       .open           = aufs_open_dir,
10869 +       .release        = aufs_release_dir,
10870 +       .flush          = aufs_flush_dir,
10871 +       .fsync          = aufs_fsync_dir
10872 +};
10873 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10874 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10875 +++ linux/fs/aufs/dir.h 2018-08-12 23:43:05.453457863 +0200
10876 @@ -0,0 +1,132 @@
10877 +/* SPDX-License-Identifier: GPL-2.0 */
10878 +/*
10879 + * Copyright (C) 2005-2018 Junjiro R. Okajima
10880 + *
10881 + * This program, aufs is free software; you can redistribute it and/or modify
10882 + * it under the terms of the GNU General Public License as published by
10883 + * the Free Software Foundation; either version 2 of the License, or
10884 + * (at your option) any later version.
10885 + *
10886 + * This program is distributed in the hope that it will be useful,
10887 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10888 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10889 + * GNU General Public License for more details.
10890 + *
10891 + * You should have received a copy of the GNU General Public License
10892 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10893 + */
10894 +
10895 +/*
10896 + * directory operations
10897 + */
10898 +
10899 +#ifndef __AUFS_DIR_H__
10900 +#define __AUFS_DIR_H__
10901 +
10902 +#ifdef __KERNEL__
10903 +
10904 +#include <linux/fs.h>
10905 +
10906 +/* ---------------------------------------------------------------------- */
10907 +
10908 +/* need to be faster and smaller */
10909 +
10910 +struct au_nhash {
10911 +       unsigned int            nh_num;
10912 +       struct hlist_head       *nh_head;
10913 +};
10914 +
10915 +struct au_vdir_destr {
10916 +       unsigned char   len;
10917 +       unsigned char   name[0];
10918 +} __packed;
10919 +
10920 +struct au_vdir_dehstr {
10921 +       struct hlist_node       hash;
10922 +       struct au_vdir_destr    *str;
10923 +} ____cacheline_aligned_in_smp;
10924 +
10925 +struct au_vdir_de {
10926 +       ino_t                   de_ino;
10927 +       unsigned char           de_type;
10928 +       /* caution: packed */
10929 +       struct au_vdir_destr    de_str;
10930 +} __packed;
10931 +
10932 +struct au_vdir_wh {
10933 +       struct hlist_node       wh_hash;
10934 +#ifdef CONFIG_AUFS_SHWH
10935 +       ino_t                   wh_ino;
10936 +       aufs_bindex_t           wh_bindex;
10937 +       unsigned char           wh_type;
10938 +#else
10939 +       aufs_bindex_t           wh_bindex;
10940 +#endif
10941 +       /* caution: packed */
10942 +       struct au_vdir_destr    wh_str;
10943 +} __packed;
10944 +
10945 +union au_vdir_deblk_p {
10946 +       unsigned char           *deblk;
10947 +       struct au_vdir_de       *de;
10948 +};
10949 +
10950 +struct au_vdir {
10951 +       unsigned char   **vd_deblk;
10952 +       unsigned long   vd_nblk;
10953 +       struct {
10954 +               unsigned long           ul;
10955 +               union au_vdir_deblk_p   p;
10956 +       } vd_last;
10957 +
10958 +       u64             vd_version;
10959 +       unsigned int    vd_deblk_sz;
10960 +       unsigned long           vd_jiffy;
10961 +} ____cacheline_aligned_in_smp;
10962 +
10963 +/* ---------------------------------------------------------------------- */
10964 +
10965 +/* dir.c */
10966 +extern const struct file_operations aufs_dir_fop;
10967 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10968 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10969 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10970 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10971 +int au_test_empty_lower(struct dentry *dentry);
10972 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10973 +
10974 +/* vdir.c */
10975 +unsigned int au_rdhash_est(loff_t sz);
10976 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10977 +void au_nhash_wh_free(struct au_nhash *whlist);
10978 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10979 +                           int limit);
10980 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10981 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10982 +                      unsigned int d_type, aufs_bindex_t bindex,
10983 +                      unsigned char shwh);
10984 +void au_vdir_free(struct au_vdir *vdir);
10985 +int au_vdir_init(struct file *file);
10986 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10987 +
10988 +/* ioctl.c */
10989 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10990 +
10991 +#ifdef CONFIG_AUFS_RDU
10992 +/* rdu.c */
10993 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10994 +#ifdef CONFIG_COMPAT
10995 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10996 +                        unsigned long arg);
10997 +#endif
10998 +#else
10999 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11000 +       unsigned int cmd, unsigned long arg)
11001 +#ifdef CONFIG_COMPAT
11002 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11003 +       unsigned int cmd, unsigned long arg)
11004 +#endif
11005 +#endif
11006 +
11007 +#endif /* __KERNEL__ */
11008 +#endif /* __AUFS_DIR_H__ */
11009 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11010 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
11011 +++ linux/fs/aufs/dirren.c      2018-08-12 23:43:05.453457863 +0200
11012 @@ -0,0 +1,1316 @@
11013 +// SPDX-License-Identifier: GPL-2.0
11014 +/*
11015 + * Copyright (C) 2017-2018 Junjiro R. Okajima
11016 + *
11017 + * This program, aufs is free software; you can redistribute it and/or modify
11018 + * it under the terms of the GNU General Public License as published by
11019 + * the Free Software Foundation; either version 2 of the License, or
11020 + * (at your option) any later version.
11021 + *
11022 + * This program is distributed in the hope that it will be useful,
11023 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11024 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11025 + * GNU General Public License for more details.
11026 + *
11027 + * You should have received a copy of the GNU General Public License
11028 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11029 + */
11030 +
11031 +/*
11032 + * special handling in renaming a directoy
11033 + * in order to support looking-up the before-renamed name on the lower readonly
11034 + * branches
11035 + */
11036 +
11037 +#include <linux/byteorder/generic.h>
11038 +#include "aufs.h"
11039 +
11040 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11041 +{
11042 +       int idx;
11043 +
11044 +       idx = au_dr_ihash(ent->dr_h_ino);
11045 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11046 +}
11047 +
11048 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
11049 +{
11050 +       int ret, i;
11051 +       struct hlist_bl_head *hbl;
11052 +
11053 +       ret = 1;
11054 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
11055 +               hbl = dr->dr_h_ino + i;
11056 +               hlist_bl_lock(hbl);
11057 +               ret &= hlist_bl_empty(hbl);
11058 +               hlist_bl_unlock(hbl);
11059 +       }
11060 +
11061 +       return ret;
11062 +}
11063 +
11064 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11065 +{
11066 +       struct au_dr_hino *found, *ent;
11067 +       struct hlist_bl_head *hbl;
11068 +       struct hlist_bl_node *pos;
11069 +       int idx;
11070 +
11071 +       found = NULL;
11072 +       idx = au_dr_ihash(ino);
11073 +       hbl = dr->dr_h_ino + idx;
11074 +       hlist_bl_lock(hbl);
11075 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11076 +               if (ent->dr_h_ino == ino) {
11077 +                       found = ent;
11078 +                       break;
11079 +               }
11080 +       hlist_bl_unlock(hbl);
11081 +
11082 +       return found;
11083 +}
11084 +
11085 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11086 +                       struct au_dr_hino *add_ent)
11087 +{
11088 +       int found, idx;
11089 +       struct hlist_bl_head *hbl;
11090 +       struct hlist_bl_node *pos;
11091 +       struct au_dr_hino *ent;
11092 +
11093 +       found = 0;
11094 +       idx = au_dr_ihash(ino);
11095 +       hbl = dr->dr_h_ino + idx;
11096 +#if 0
11097 +       {
11098 +               struct hlist_bl_node *tmp;
11099 +
11100 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11101 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11102 +       }
11103 +#endif
11104 +       hlist_bl_lock(hbl);
11105 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11106 +               if (ent->dr_h_ino == ino) {
11107 +                       found = 1;
11108 +                       break;
11109 +               }
11110 +       if (!found && add_ent)
11111 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11112 +       hlist_bl_unlock(hbl);
11113 +
11114 +       if (!found && add_ent)
11115 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11116 +
11117 +       return found;
11118 +}
11119 +
11120 +void au_dr_hino_free(struct au_dr_br *dr)
11121 +{
11122 +       int i;
11123 +       struct hlist_bl_head *hbl;
11124 +       struct hlist_bl_node *pos, *tmp;
11125 +       struct au_dr_hino *ent;
11126 +
11127 +       /* SiMustWriteLock(sb); */
11128 +
11129 +       for (i = 0; i < AuDirren_NHASH; i++) {
11130 +               hbl = dr->dr_h_ino + i;
11131 +               /* no spinlock since sbinfo must be write-locked */
11132 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11133 +                       kfree(ent);
11134 +               INIT_HLIST_BL_HEAD(hbl);
11135 +       }
11136 +}
11137 +
11138 +/* returns the number of inodes or an error */
11139 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11140 +                           struct file *hinofile)
11141 +{
11142 +       int err, i;
11143 +       ssize_t ssz;
11144 +       loff_t pos, oldsize;
11145 +       __be64 u64;
11146 +       struct inode *hinoinode;
11147 +       struct hlist_bl_head *hbl;
11148 +       struct hlist_bl_node *n1, *n2;
11149 +       struct au_dr_hino *ent;
11150 +
11151 +       SiMustWriteLock(sb);
11152 +       AuDebugOn(!au_br_writable(br->br_perm));
11153 +
11154 +       hinoinode = file_inode(hinofile);
11155 +       oldsize = i_size_read(hinoinode);
11156 +
11157 +       err = 0;
11158 +       pos = 0;
11159 +       hbl = br->br_dirren.dr_h_ino;
11160 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11161 +               /* no bit-lock since sbinfo must be write-locked */
11162 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11163 +                       AuDbg("hi%llu, %pD2\n",
11164 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11165 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11166 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11167 +                       if (ssz == sizeof(u64))
11168 +                               continue;
11169 +
11170 +                       /* write error */
11171 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11172 +                       err = -ENOSPC;
11173 +                       if (ssz < 0)
11174 +                               err = ssz;
11175 +                       break;
11176 +               }
11177 +       }
11178 +       /* regardless the error */
11179 +       if (pos < oldsize) {
11180 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11181 +               AuTraceErr(err);
11182 +       }
11183 +
11184 +       AuTraceErr(err);
11185 +       return err;
11186 +}
11187 +
11188 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11189 +{
11190 +       int err, hidx;
11191 +       ssize_t ssz;
11192 +       size_t sz, n;
11193 +       loff_t pos;
11194 +       uint64_t u64;
11195 +       struct au_dr_hino *ent;
11196 +       struct inode *hinoinode;
11197 +       struct hlist_bl_head *hbl;
11198 +
11199 +       err = 0;
11200 +       pos = 0;
11201 +       hbl = dr->dr_h_ino;
11202 +       hinoinode = file_inode(hinofile);
11203 +       sz = i_size_read(hinoinode);
11204 +       AuDebugOn(sz % sizeof(u64));
11205 +       n = sz / sizeof(u64);
11206 +       while (n--) {
11207 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11208 +               if (unlikely(ssz != sizeof(u64))) {
11209 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11210 +                       err = -EINVAL;
11211 +                       if (ssz < 0)
11212 +                               err = ssz;
11213 +                       goto out_free;
11214 +               }
11215 +
11216 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11217 +               if (!ent) {
11218 +                       err = -ENOMEM;
11219 +                       AuTraceErr(err);
11220 +                       goto out_free;
11221 +               }
11222 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11223 +               AuDbg("hi%llu, %pD2\n",
11224 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11225 +               hidx = au_dr_ihash(ent->dr_h_ino);
11226 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11227 +       }
11228 +       goto out; /* success */
11229 +
11230 +out_free:
11231 +       au_dr_hino_free(dr);
11232 +out:
11233 +       AuTraceErr(err);
11234 +       return err;
11235 +}
11236 +
11237 +/*
11238 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11239 + * @path is a switch to distinguish load and store.
11240 + */
11241 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11242 +                     struct au_branch *br, const struct path *path)
11243 +{
11244 +       int err, flags;
11245 +       unsigned char load, suspend;
11246 +       struct file *hinofile;
11247 +       struct au_hinode *hdir;
11248 +       struct inode *dir, *delegated;
11249 +       struct path hinopath;
11250 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11251 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11252 +
11253 +       AuDebugOn(bindex < 0 && !br);
11254 +       AuDebugOn(bindex >= 0 && br);
11255 +
11256 +       err = -EINVAL;
11257 +       suspend = !br;
11258 +       if (suspend)
11259 +               br = au_sbr(sb, bindex);
11260 +       load = !!path;
11261 +       if (!load) {
11262 +               path = &br->br_path;
11263 +               AuDebugOn(!au_br_writable(br->br_perm));
11264 +               if (unlikely(!au_br_writable(br->br_perm)))
11265 +                       goto out;
11266 +       }
11267 +
11268 +       hdir = NULL;
11269 +       if (suspend) {
11270 +               dir = d_inode(sb->s_root);
11271 +               hdir = au_hinode(au_ii(dir), bindex);
11272 +               dir = hdir->hi_inode;
11273 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11274 +       } else {
11275 +               dir = d_inode(path->dentry);
11276 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11277 +       }
11278 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11279 +       err = PTR_ERR(hinopath.dentry);
11280 +       if (IS_ERR(hinopath.dentry))
11281 +               goto out_unlock;
11282 +
11283 +       err = 0;
11284 +       flags = O_RDONLY;
11285 +       if (load) {
11286 +               if (d_is_negative(hinopath.dentry))
11287 +                       goto out_dput; /* success */
11288 +       } else {
11289 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11290 +                       if (d_is_positive(hinopath.dentry)) {
11291 +                               delegated = NULL;
11292 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11293 +                                                  /*force*/0);
11294 +                               AuTraceErr(err);
11295 +                               if (unlikely(err))
11296 +                                       pr_err("ignored err %d, %pd2\n",
11297 +                                              err, hinopath.dentry);
11298 +                               if (unlikely(err == -EWOULDBLOCK))
11299 +                                       iput(delegated);
11300 +                               err = 0;
11301 +                       }
11302 +                       goto out_dput;
11303 +               } else if (!d_is_positive(hinopath.dentry)) {
11304 +                       err = vfsub_create(dir, &hinopath, 0600,
11305 +                                          /*want_excl*/false);
11306 +                       AuTraceErr(err);
11307 +                       if (unlikely(err))
11308 +                               goto out_dput;
11309 +               }
11310 +               flags = O_WRONLY;
11311 +       }
11312 +       hinopath.mnt = path->mnt;
11313 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11314 +       if (suspend)
11315 +               au_hn_inode_unlock(hdir);
11316 +       else
11317 +               inode_unlock(dir);
11318 +       dput(hinopath.dentry);
11319 +       AuTraceErrPtr(hinofile);
11320 +       if (IS_ERR(hinofile)) {
11321 +               err = PTR_ERR(hinofile);
11322 +               goto out;
11323 +       }
11324 +
11325 +       if (load)
11326 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11327 +       else
11328 +               err = au_dr_hino_store(sb, br, hinofile);
11329 +       fput(hinofile);
11330 +       goto out;
11331 +
11332 +out_dput:
11333 +       dput(hinopath.dentry);
11334 +out_unlock:
11335 +       if (suspend)
11336 +               au_hn_inode_unlock(hdir);
11337 +       else
11338 +               inode_unlock(dir);
11339 +out:
11340 +       AuTraceErr(err);
11341 +       return err;
11342 +}
11343 +
11344 +/* ---------------------------------------------------------------------- */
11345 +
11346 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11347 +{
11348 +       int err;
11349 +       struct kstatfs kstfs;
11350 +       dev_t dev;
11351 +       struct dentry *dentry;
11352 +       struct super_block *sb;
11353 +
11354 +       err = vfs_statfs((void *)path, &kstfs);
11355 +       AuTraceErr(err);
11356 +       if (unlikely(err))
11357 +               goto out;
11358 +
11359 +       /* todo: support for UUID */
11360 +
11361 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11362 +               brid->type = AuBrid_FSID;
11363 +               brid->fsid = kstfs.f_fsid;
11364 +       } else {
11365 +               dentry = path->dentry;
11366 +               sb = dentry->d_sb;
11367 +               dev = sb->s_dev;
11368 +               if (dev) {
11369 +                       brid->type = AuBrid_DEV;
11370 +                       brid->dev = dev;
11371 +               }
11372 +       }
11373 +
11374 +out:
11375 +       return err;
11376 +}
11377 +
11378 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11379 +                 const struct path *path)
11380 +{
11381 +       int err, i;
11382 +       struct au_dr_br *dr;
11383 +       struct hlist_bl_head *hbl;
11384 +
11385 +       dr = &br->br_dirren;
11386 +       hbl = dr->dr_h_ino;
11387 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11388 +               INIT_HLIST_BL_HEAD(hbl);
11389 +
11390 +       err = au_dr_brid_init(&dr->dr_brid, path);
11391 +       if (unlikely(err))
11392 +               goto out;
11393 +
11394 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11395 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11396 +
11397 +out:
11398 +       AuTraceErr(err);
11399 +       return err;
11400 +}
11401 +
11402 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11403 +{
11404 +       int err;
11405 +
11406 +       err = 0;
11407 +       if (au_br_writable(br->br_perm))
11408 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11409 +       if (!err)
11410 +               au_dr_hino_free(&br->br_dirren);
11411 +
11412 +       return err;
11413 +}
11414 +
11415 +/* ---------------------------------------------------------------------- */
11416 +
11417 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11418 +                      char *buf, size_t sz)
11419 +{
11420 +       int err;
11421 +       unsigned int major, minor;
11422 +       char *p;
11423 +
11424 +       p = buf;
11425 +       err = snprintf(p, sz, "%d_", brid->type);
11426 +       AuDebugOn(err > sz);
11427 +       p += err;
11428 +       sz -= err;
11429 +       switch (brid->type) {
11430 +       case AuBrid_Unset:
11431 +               return -EINVAL;
11432 +       case AuBrid_UUID:
11433 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11434 +               break;
11435 +       case AuBrid_FSID:
11436 +               err = snprintf(p, sz, "%08x-%08x",
11437 +                              brid->fsid.val[0], brid->fsid.val[1]);
11438 +               break;
11439 +       case AuBrid_DEV:
11440 +               major = MAJOR(brid->dev);
11441 +               minor = MINOR(brid->dev);
11442 +               if (major <= 0xff && minor <= 0xff)
11443 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11444 +               else
11445 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11446 +               break;
11447 +       }
11448 +       AuDebugOn(err > sz);
11449 +       p += err;
11450 +       sz -= err;
11451 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11452 +       AuDebugOn(err > sz);
11453 +       p += err;
11454 +       sz -= err;
11455 +
11456 +       return p - buf;
11457 +}
11458 +
11459 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11460 +{
11461 +       int rlen;
11462 +       struct dentry *br_dentry;
11463 +       struct inode *br_inode;
11464 +
11465 +       br_dentry = au_br_dentry(br);
11466 +       br_inode = d_inode(br_dentry);
11467 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11468 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11469 +       AuDebugOn(rlen > len);
11470 +
11471 +       return rlen;
11472 +}
11473 +
11474 +/* ---------------------------------------------------------------------- */
11475 +
11476 +/*
11477 + * from the given @h_dentry, construct drinfo at @*fdata.
11478 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11479 + * @allocated.
11480 + */
11481 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11482 +                              struct dentry *h_dentry,
11483 +                              unsigned char *allocated)
11484 +{
11485 +       int err, v;
11486 +       struct au_drinfo_fdata *f, *p;
11487 +       struct au_drinfo *drinfo;
11488 +       struct inode *h_inode;
11489 +       struct qstr *qname;
11490 +
11491 +       err = 0;
11492 +       f = *fdata;
11493 +       h_inode = d_inode(h_dentry);
11494 +       qname = &h_dentry->d_name;
11495 +       drinfo = &f->drinfo;
11496 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11497 +       drinfo->oldnamelen = qname->len;
11498 +       if (*allocated < sizeof(*f) + qname->len) {
11499 +               v = roundup_pow_of_two(*allocated + qname->len);
11500 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11501 +               if (unlikely(!p)) {
11502 +                       err = -ENOMEM;
11503 +                       AuTraceErr(err);
11504 +                       goto out;
11505 +               }
11506 +               f = p;
11507 +               *fdata = f;
11508 +               *allocated = v;
11509 +               drinfo = &f->drinfo;
11510 +       }
11511 +       memcpy(drinfo->oldname, qname->name, qname->len);
11512 +       AuDbg("i%llu, %.*s\n",
11513 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11514 +             drinfo->oldname);
11515 +
11516 +out:
11517 +       AuTraceErr(err);
11518 +       return err;
11519 +}
11520 +
11521 +/* callers have to free the return value */
11522 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11523 +{
11524 +       struct au_drinfo *ret, *drinfo;
11525 +       struct au_drinfo_fdata fdata;
11526 +       int len;
11527 +       loff_t pos;
11528 +       ssize_t ssz;
11529 +
11530 +       ret = ERR_PTR(-EIO);
11531 +       pos = 0;
11532 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11533 +       if (unlikely(ssz != sizeof(fdata))) {
11534 +               AuIOErr("ssz %zd, %u, %pD2\n",
11535 +                       ssz, (unsigned int)sizeof(fdata), file);
11536 +               goto out;
11537 +       }
11538 +
11539 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11540 +       switch (fdata.magic) {
11541 +       case AUFS_DRINFO_MAGIC_V1:
11542 +               break;
11543 +       default:
11544 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11545 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11546 +               goto out;
11547 +       }
11548 +
11549 +       drinfo = &fdata.drinfo;
11550 +       len = drinfo->oldnamelen;
11551 +       if (!len) {
11552 +               AuIOErr("broken drinfo %pD2\n", file);
11553 +               goto out;
11554 +       }
11555 +
11556 +       ret = NULL;
11557 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11558 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11559 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11560 +                     (unsigned long long)drinfo->ino,
11561 +                     (unsigned long long)h_ino, file);
11562 +               goto out; /* success */
11563 +       }
11564 +
11565 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11566 +       if (unlikely(!ret)) {
11567 +               ret = ERR_PTR(-ENOMEM);
11568 +               AuTraceErrPtr(ret);
11569 +               goto out;
11570 +       }
11571 +
11572 +       *ret = *drinfo;
11573 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11574 +       if (unlikely(ssz != len)) {
11575 +               kfree(ret);
11576 +               ret = ERR_PTR(-EIO);
11577 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11578 +               goto out;
11579 +       }
11580 +
11581 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11582 +
11583 +out:
11584 +       return ret;
11585 +}
11586 +
11587 +/* ---------------------------------------------------------------------- */
11588 +
11589 +/* in order to be revertible */
11590 +struct au_drinfo_rev_elm {
11591 +       int                     created;
11592 +       struct dentry           *info_dentry;
11593 +       struct au_drinfo        *info_last;
11594 +};
11595 +
11596 +struct au_drinfo_rev {
11597 +       unsigned char                   already;
11598 +       aufs_bindex_t                   nelm;
11599 +       struct au_drinfo_rev_elm        elm[0];
11600 +};
11601 +
11602 +/* todo: isn't it too large? */
11603 +struct au_drinfo_store {
11604 +       struct path h_ppath;
11605 +       struct dentry *h_dentry;
11606 +       struct au_drinfo_fdata *fdata;
11607 +       char *infoname;                 /* inside of whname, just after PFX */
11608 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11609 +       aufs_bindex_t btgt, btail;
11610 +       unsigned char no_sio,
11611 +               allocated,              /* current size of *fdata */
11612 +               infonamelen,            /* room size for p */
11613 +               whnamelen,              /* length of the genarated name */
11614 +               renameback;             /* renamed back */
11615 +};
11616 +
11617 +/* on rename(2) error, the caller should revert it using @elm */
11618 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11619 +                             struct au_drinfo_rev_elm *elm)
11620 +{
11621 +       int err, len;
11622 +       ssize_t ssz;
11623 +       loff_t pos;
11624 +       struct path infopath = {
11625 +               .mnt = w->h_ppath.mnt
11626 +       };
11627 +       struct inode *h_dir, *h_inode, *delegated;
11628 +       struct file *infofile;
11629 +       struct qstr *qname;
11630 +
11631 +       AuDebugOn(elm
11632 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11633 +
11634 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11635 +                                              w->whnamelen);
11636 +       AuTraceErrPtr(infopath.dentry);
11637 +       if (IS_ERR(infopath.dentry)) {
11638 +               err = PTR_ERR(infopath.dentry);
11639 +               goto out;
11640 +       }
11641 +
11642 +       err = 0;
11643 +       h_dir = d_inode(w->h_ppath.dentry);
11644 +       if (elm && d_is_negative(infopath.dentry)) {
11645 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11646 +               AuTraceErr(err);
11647 +               if (unlikely(err))
11648 +                       goto out_dput;
11649 +               elm->created = 1;
11650 +               elm->info_dentry = dget(infopath.dentry);
11651 +       }
11652 +
11653 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11654 +       AuTraceErrPtr(infofile);
11655 +       if (IS_ERR(infofile)) {
11656 +               err = PTR_ERR(infofile);
11657 +               goto out_dput;
11658 +       }
11659 +
11660 +       h_inode = d_inode(infopath.dentry);
11661 +       if (elm && i_size_read(h_inode)) {
11662 +               h_inode = d_inode(w->h_dentry);
11663 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11664 +               AuTraceErrPtr(elm->info_last);
11665 +               if (IS_ERR(elm->info_last)) {
11666 +                       err = PTR_ERR(elm->info_last);
11667 +                       elm->info_last = NULL;
11668 +                       AuDebugOn(elm->info_dentry);
11669 +                       goto out_fput;
11670 +               }
11671 +       }
11672 +
11673 +       if (elm && w->renameback) {
11674 +               delegated = NULL;
11675 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11676 +               AuTraceErr(err);
11677 +               if (unlikely(err == -EWOULDBLOCK))
11678 +                       iput(delegated);
11679 +               goto out_fput;
11680 +       }
11681 +
11682 +       pos = 0;
11683 +       qname = &w->h_dentry->d_name;
11684 +       len = sizeof(*w->fdata) + qname->len;
11685 +       if (!elm)
11686 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11687 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11688 +       if (ssz == len) {
11689 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11690 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11691 +               goto out_fput; /* success */
11692 +       } else {
11693 +               err = -EIO;
11694 +               if (ssz < 0)
11695 +                       err = ssz;
11696 +               /* the caller should revert it using @elm */
11697 +       }
11698 +
11699 +out_fput:
11700 +       fput(infofile);
11701 +out_dput:
11702 +       dput(infopath.dentry);
11703 +out:
11704 +       AuTraceErr(err);
11705 +       return err;
11706 +}
11707 +
11708 +struct au_call_drinfo_do_store_args {
11709 +       int *errp;
11710 +       struct au_drinfo_store *w;
11711 +       struct au_drinfo_rev_elm *elm;
11712 +};
11713 +
11714 +static void au_call_drinfo_do_store(void *args)
11715 +{
11716 +       struct au_call_drinfo_do_store_args *a = args;
11717 +
11718 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11719 +}
11720 +
11721 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11722 +                              struct au_drinfo_rev_elm *elm)
11723 +{
11724 +       int err, wkq_err;
11725 +
11726 +       if (w->no_sio)
11727 +               err = au_drinfo_do_store(w, elm);
11728 +       else {
11729 +               struct au_call_drinfo_do_store_args a = {
11730 +                       .errp   = &err,
11731 +                       .w      = w,
11732 +                       .elm    = elm
11733 +               };
11734 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11735 +               if (unlikely(wkq_err))
11736 +                       err = wkq_err;
11737 +       }
11738 +       AuTraceErr(err);
11739 +
11740 +       return err;
11741 +}
11742 +
11743 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11744 +                                    aufs_bindex_t btgt)
11745 +{
11746 +       int err;
11747 +
11748 +       memset(w, 0, sizeof(*w));
11749 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11750 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11751 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11752 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11753 +       w->btgt = btgt;
11754 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11755 +
11756 +       err = -ENOMEM;
11757 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11758 +       if (unlikely(!w->fdata)) {
11759 +               AuTraceErr(err);
11760 +               goto out;
11761 +       }
11762 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11763 +       err = 0;
11764 +
11765 +out:
11766 +       return err;
11767 +}
11768 +
11769 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11770 +{
11771 +       kfree(w->fdata);
11772 +}
11773 +
11774 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11775 +                               struct au_drinfo_store *w)
11776 +{
11777 +       struct au_drinfo_rev_elm *elm;
11778 +       struct inode *h_dir, *delegated;
11779 +       int err, nelm;
11780 +       struct path infopath = {
11781 +               .mnt = w->h_ppath.mnt
11782 +       };
11783 +
11784 +       h_dir = d_inode(w->h_ppath.dentry);
11785 +       IMustLock(h_dir);
11786 +
11787 +       err = 0;
11788 +       elm = rev->elm;
11789 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11790 +               AuDebugOn(elm->created && elm->info_last);
11791 +               if (elm->created) {
11792 +                       AuDbg("here\n");
11793 +                       delegated = NULL;
11794 +                       infopath.dentry = elm->info_dentry;
11795 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11796 +                                          !w->no_sio);
11797 +                       AuTraceErr(err);
11798 +                       if (unlikely(err == -EWOULDBLOCK))
11799 +                               iput(delegated);
11800 +                       dput(elm->info_dentry);
11801 +               } else if (elm->info_last) {
11802 +                       AuDbg("here\n");
11803 +                       w->fdata->drinfo = *elm->info_last;
11804 +                       memcpy(w->fdata->drinfo.oldname,
11805 +                              elm->info_last->oldname,
11806 +                              elm->info_last->oldnamelen);
11807 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11808 +                       kfree(elm->info_last);
11809 +               }
11810 +               if (unlikely(err))
11811 +                       AuIOErr("%d, %s\n", err, w->whname);
11812 +               /* go on even if err */
11813 +       }
11814 +}
11815 +
11816 +/* caller has to call au_dr_rename_fin() later */
11817 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11818 +                          struct qstr *dst_name, void *_rev)
11819 +{
11820 +       int err, sz, nelm;
11821 +       aufs_bindex_t bindex, btail;
11822 +       struct au_drinfo_store work;
11823 +       struct au_drinfo_rev *rev, **p;
11824 +       struct au_drinfo_rev_elm *elm;
11825 +       struct super_block *sb;
11826 +       struct au_branch *br;
11827 +       struct au_hinode *hdir;
11828 +
11829 +       err = au_drinfo_store_work_init(&work, btgt);
11830 +       AuTraceErr(err);
11831 +       if (unlikely(err))
11832 +               goto out;
11833 +
11834 +       err = -ENOMEM;
11835 +       btail = au_dbtaildir(dentry);
11836 +       nelm = btail - btgt;
11837 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11838 +       rev = kcalloc(1, sz, GFP_NOFS);
11839 +       if (unlikely(!rev)) {
11840 +               AuTraceErr(err);
11841 +               goto out_args;
11842 +       }
11843 +       rev->nelm = nelm;
11844 +       elm = rev->elm;
11845 +       p = _rev;
11846 +       *p = rev;
11847 +
11848 +       err = 0;
11849 +       sb = dentry->d_sb;
11850 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11851 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11852 +       hdir = au_hi(d_inode(dentry), btgt);
11853 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11854 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11855 +               work.h_dentry = au_h_dptr(dentry, bindex);
11856 +               if (!work.h_dentry)
11857 +                       continue;
11858 +
11859 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11860 +                                         &work.allocated);
11861 +               AuTraceErr(err);
11862 +               if (unlikely(err))
11863 +                       break;
11864 +
11865 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11866 +               br = au_sbr(sb, bindex);
11867 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11868 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11869 +                                                work.infonamelen);
11870 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11871 +                     work.whnamelen, work.whname,
11872 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11873 +                     work.fdata->drinfo.oldnamelen,
11874 +                     work.fdata->drinfo.oldname);
11875 +
11876 +               err = au_drinfo_store_sio(&work, elm);
11877 +               AuTraceErr(err);
11878 +               if (unlikely(err))
11879 +                       break;
11880 +       }
11881 +       if (unlikely(err)) {
11882 +               /* revert all drinfo */
11883 +               au_drinfo_store_rev(rev, &work);
11884 +               kfree(rev);
11885 +               *p = NULL;
11886 +       }
11887 +       au_hn_inode_unlock(hdir);
11888 +
11889 +out_args:
11890 +       au_drinfo_store_work_fin(&work);
11891 +out:
11892 +       return err;
11893 +}
11894 +
11895 +/* ---------------------------------------------------------------------- */
11896 +
11897 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11898 +                struct qstr *dst_name, void *_rev)
11899 +{
11900 +       int err, already;
11901 +       ino_t ino;
11902 +       struct super_block *sb;
11903 +       struct au_branch *br;
11904 +       struct au_dr_br *dr;
11905 +       struct dentry *h_dentry;
11906 +       struct inode *h_inode;
11907 +       struct au_dr_hino *ent;
11908 +       struct au_drinfo_rev *rev, **p;
11909 +
11910 +       AuDbg("bindex %d\n", bindex);
11911 +
11912 +       err = -ENOMEM;
11913 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11914 +       if (unlikely(!ent))
11915 +               goto out;
11916 +
11917 +       sb = src->d_sb;
11918 +       br = au_sbr(sb, bindex);
11919 +       dr = &br->br_dirren;
11920 +       h_dentry = au_h_dptr(src, bindex);
11921 +       h_inode = d_inode(h_dentry);
11922 +       ino = h_inode->i_ino;
11923 +       ent->dr_h_ino = ino;
11924 +       already = au_dr_hino_test_add(dr, ino, ent);
11925 +       AuDbg("b%d, hi%llu, already %d\n",
11926 +             bindex, (unsigned long long)ino, already);
11927 +
11928 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11929 +       AuTraceErr(err);
11930 +       if (!err) {
11931 +               p = _rev;
11932 +               rev = *p;
11933 +               rev->already = already;
11934 +               goto out; /* success */
11935 +       }
11936 +
11937 +       /* revert */
11938 +       if (!already)
11939 +               au_dr_hino_del(dr, ent);
11940 +       kfree(ent);
11941 +
11942 +out:
11943 +       AuTraceErr(err);
11944 +       return err;
11945 +}
11946 +
11947 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11948 +{
11949 +       struct au_drinfo_rev *rev;
11950 +       struct au_drinfo_rev_elm *elm;
11951 +       int nelm;
11952 +
11953 +       rev = _rev;
11954 +       elm = rev->elm;
11955 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11956 +               dput(elm->info_dentry);
11957 +               kfree(elm->info_last);
11958 +       }
11959 +       kfree(rev);
11960 +}
11961 +
11962 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11963 +{
11964 +       int err;
11965 +       struct au_drinfo_store work;
11966 +       struct au_drinfo_rev *rev = _rev;
11967 +       struct super_block *sb;
11968 +       struct au_branch *br;
11969 +       struct inode *h_inode;
11970 +       struct au_dr_br *dr;
11971 +       struct au_dr_hino *ent;
11972 +
11973 +       err = au_drinfo_store_work_init(&work, btgt);
11974 +       if (unlikely(err))
11975 +               goto out;
11976 +
11977 +       sb = src->d_sb;
11978 +       br = au_sbr(sb, btgt);
11979 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11980 +       work.h_ppath.mnt = au_br_mnt(br);
11981 +       au_drinfo_store_rev(rev, &work);
11982 +       au_drinfo_store_work_fin(&work);
11983 +       if (rev->already)
11984 +               goto out;
11985 +
11986 +       dr = &br->br_dirren;
11987 +       h_inode = d_inode(work.h_ppath.dentry);
11988 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11989 +       BUG_ON(!ent);
11990 +       au_dr_hino_del(dr, ent);
11991 +       kfree(ent);
11992 +
11993 +out:
11994 +       kfree(rev);
11995 +       if (unlikely(err))
11996 +               pr_err("failed to remove dirren info\n");
11997 +}
11998 +
11999 +/* ---------------------------------------------------------------------- */
12000 +
12001 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12002 +                                          char *whname, int whnamelen,
12003 +                                          struct dentry **info_dentry)
12004 +{
12005 +       struct au_drinfo *drinfo;
12006 +       struct file *f;
12007 +       struct inode *h_dir;
12008 +       struct path infopath;
12009 +       int unlocked;
12010 +
12011 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12012 +
12013 +       *info_dentry = NULL;
12014 +       drinfo = NULL;
12015 +       unlocked = 0;
12016 +       h_dir = d_inode(h_ppath->dentry);
12017 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
12018 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12019 +                                              whnamelen);
12020 +       if (IS_ERR(infopath.dentry)) {
12021 +               drinfo = (void *)infopath.dentry;
12022 +               goto out;
12023 +       }
12024 +
12025 +       if (d_is_negative(infopath.dentry))
12026 +               goto out_dput; /* success */
12027 +
12028 +       infopath.mnt = h_ppath->mnt;
12029 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
12030 +       inode_unlock_shared(h_dir);
12031 +       unlocked = 1;
12032 +       if (IS_ERR(f)) {
12033 +               drinfo = (void *)f;
12034 +               goto out_dput;
12035 +       }
12036 +
12037 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12038 +       if (IS_ERR_OR_NULL(drinfo))
12039 +               goto out_fput;
12040 +
12041 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12042 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
12043 +
12044 +out_fput:
12045 +       fput(f);
12046 +out_dput:
12047 +       dput(infopath.dentry);
12048 +out:
12049 +       if (!unlocked)
12050 +               inode_unlock_shared(h_dir);
12051 +       AuTraceErrPtr(drinfo);
12052 +       return drinfo;
12053 +}
12054 +
12055 +struct au_drinfo_do_load_args {
12056 +       struct au_drinfo **drinfop;
12057 +       struct path *h_ppath;
12058 +       char *whname;
12059 +       int whnamelen;
12060 +       struct dentry **info_dentry;
12061 +};
12062 +
12063 +static void au_call_drinfo_do_load(void *args)
12064 +{
12065 +       struct au_drinfo_do_load_args *a = args;
12066 +
12067 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12068 +                                       a->info_dentry);
12069 +}
12070 +
12071 +struct au_drinfo_load {
12072 +       struct path h_ppath;
12073 +       struct qstr *qname;
12074 +       unsigned char no_sio;
12075 +
12076 +       aufs_bindex_t ninfo;
12077 +       struct au_drinfo **drinfo;
12078 +};
12079 +
12080 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12081 +                         struct au_branch *br)
12082 +{
12083 +       int err, wkq_err, whnamelen, e;
12084 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12085 +               = AUFS_WH_DR_INFO_PFX;
12086 +       struct au_drinfo *drinfo;
12087 +       struct qstr oldname;
12088 +       struct inode *h_dir, *delegated;
12089 +       struct dentry *info_dentry;
12090 +       struct path infopath;
12091 +
12092 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12093 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
12094 +                                   sizeof(whname) - whnamelen);
12095 +       if (w->no_sio)
12096 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12097 +                                          &info_dentry);
12098 +       else {
12099 +               struct au_drinfo_do_load_args args = {
12100 +                       .drinfop        = &drinfo,
12101 +                       .h_ppath        = &w->h_ppath,
12102 +                       .whname         = whname,
12103 +                       .whnamelen      = whnamelen,
12104 +                       .info_dentry    = &info_dentry
12105 +               };
12106 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12107 +               if (unlikely(wkq_err))
12108 +                       drinfo = ERR_PTR(wkq_err);
12109 +       }
12110 +       err = PTR_ERR(drinfo);
12111 +       if (IS_ERR_OR_NULL(drinfo))
12112 +               goto out;
12113 +
12114 +       err = 0;
12115 +       oldname.len = drinfo->oldnamelen;
12116 +       oldname.name = drinfo->oldname;
12117 +       if (au_qstreq(w->qname, &oldname)) {
12118 +               /* the name is renamed back */
12119 +               kfree(drinfo);
12120 +               drinfo = NULL;
12121 +
12122 +               infopath.dentry = info_dentry;
12123 +               infopath.mnt = w->h_ppath.mnt;
12124 +               h_dir = d_inode(w->h_ppath.dentry);
12125 +               delegated = NULL;
12126 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12127 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12128 +               inode_unlock(h_dir);
12129 +               if (unlikely(e))
12130 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12131 +               if (unlikely(e == -EWOULDBLOCK))
12132 +                       iput(delegated);
12133 +       }
12134 +       kfree(w->drinfo[bindex]);
12135 +       w->drinfo[bindex] = drinfo;
12136 +       dput(info_dentry);
12137 +
12138 +out:
12139 +       AuTraceErr(err);
12140 +       return err;
12141 +}
12142 +
12143 +/* ---------------------------------------------------------------------- */
12144 +
12145 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12146 +{
12147 +       struct au_drinfo **p = drinfo;
12148 +
12149 +       while (n-- > 0)
12150 +               kfree(*drinfo++);
12151 +       kfree(p);
12152 +}
12153 +
12154 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12155 +              aufs_bindex_t btgt)
12156 +{
12157 +       int err, ninfo;
12158 +       struct au_drinfo_load w;
12159 +       aufs_bindex_t bindex, bbot;
12160 +       struct au_branch *br;
12161 +       struct inode *h_dir;
12162 +       struct au_dr_hino *ent;
12163 +       struct super_block *sb;
12164 +
12165 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12166 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12167 +             AuLNPair(&lkup->whname), btgt);
12168 +
12169 +       sb = dentry->d_sb;
12170 +       bbot = au_sbbot(sb);
12171 +       w.ninfo = bbot + 1;
12172 +       if (!lkup->dirren.drinfo) {
12173 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12174 +                                             sizeof(*lkup->dirren.drinfo),
12175 +                                             GFP_NOFS);
12176 +               if (unlikely(!lkup->dirren.drinfo)) {
12177 +                       err = -ENOMEM;
12178 +                       goto out;
12179 +               }
12180 +               lkup->dirren.ninfo = w.ninfo;
12181 +       }
12182 +       w.drinfo = lkup->dirren.drinfo;
12183 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12184 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12185 +       AuDebugOn(!w.h_ppath.dentry);
12186 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12187 +       w.qname = &dentry->d_name;
12188 +
12189 +       ninfo = 0;
12190 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12191 +               br = au_sbr(sb, bindex);
12192 +               err = au_drinfo_load(&w, bindex, br);
12193 +               if (unlikely(err))
12194 +                       goto out_free;
12195 +               if (w.drinfo[bindex])
12196 +                       ninfo++;
12197 +       }
12198 +       if (!ninfo) {
12199 +               br = au_sbr(sb, btgt);
12200 +               h_dir = d_inode(w.h_ppath.dentry);
12201 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12202 +               AuDebugOn(!ent);
12203 +               au_dr_hino_del(&br->br_dirren, ent);
12204 +               kfree(ent);
12205 +       }
12206 +       goto out; /* success */
12207 +
12208 +out_free:
12209 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12210 +       lkup->dirren.ninfo = 0;
12211 +       lkup->dirren.drinfo = NULL;
12212 +out:
12213 +       AuTraceErr(err);
12214 +       return err;
12215 +}
12216 +
12217 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12218 +{
12219 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12220 +}
12221 +
12222 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12223 +{
12224 +       int err;
12225 +       struct au_drinfo *drinfo;
12226 +
12227 +       err = 0;
12228 +       if (!lkup->dirren.drinfo)
12229 +               goto out;
12230 +       AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12231 +       drinfo = lkup->dirren.drinfo[btgt + 1];
12232 +       if (!drinfo)
12233 +               goto out;
12234 +
12235 +       kfree(lkup->whname.name);
12236 +       lkup->whname.name = NULL;
12237 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12238 +       lkup->dirren.dr_name.name = drinfo->oldname;
12239 +       lkup->name = &lkup->dirren.dr_name;
12240 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12241 +       if (!err)
12242 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12243 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12244 +                     btgt);
12245 +
12246 +out:
12247 +       AuTraceErr(err);
12248 +       return err;
12249 +}
12250 +
12251 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12252 +                    ino_t h_ino)
12253 +{
12254 +       int match;
12255 +       struct au_drinfo *drinfo;
12256 +
12257 +       match = 1;
12258 +       if (!lkup->dirren.drinfo)
12259 +               goto out;
12260 +       AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12261 +       drinfo = lkup->dirren.drinfo[bindex + 1];
12262 +       if (!drinfo)
12263 +               goto out;
12264 +
12265 +       match = (drinfo->ino == h_ino);
12266 +       AuDbg("match %d\n", match);
12267 +
12268 +out:
12269 +       return match;
12270 +}
12271 +
12272 +/* ---------------------------------------------------------------------- */
12273 +
12274 +int au_dr_opt_set(struct super_block *sb)
12275 +{
12276 +       int err;
12277 +       aufs_bindex_t bindex, bbot;
12278 +       struct au_branch *br;
12279 +
12280 +       err = 0;
12281 +       bbot = au_sbbot(sb);
12282 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12283 +               br = au_sbr(sb, bindex);
12284 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12285 +       }
12286 +
12287 +       return err;
12288 +}
12289 +
12290 +int au_dr_opt_flush(struct super_block *sb)
12291 +{
12292 +       int err;
12293 +       aufs_bindex_t bindex, bbot;
12294 +       struct au_branch *br;
12295 +
12296 +       err = 0;
12297 +       bbot = au_sbbot(sb);
12298 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12299 +               br = au_sbr(sb, bindex);
12300 +               if (au_br_writable(br->br_perm))
12301 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12302 +       }
12303 +
12304 +       return err;
12305 +}
12306 +
12307 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12308 +{
12309 +       int err;
12310 +       aufs_bindex_t bindex, bbot;
12311 +       struct au_branch *br;
12312 +
12313 +       err = 0;
12314 +       if (!no_flush) {
12315 +               err = au_dr_opt_flush(sb);
12316 +               if (unlikely(err))
12317 +                       goto out;
12318 +       }
12319 +
12320 +       bbot = au_sbbot(sb);
12321 +       for (bindex = 0; bindex <= bbot; bindex++) {
12322 +               br = au_sbr(sb, bindex);
12323 +               au_dr_hino_free(&br->br_dirren);
12324 +       }
12325 +
12326 +out:
12327 +       return err;
12328 +}
12329 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12330 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12331 +++ linux/fs/aufs/dirren.h      2018-08-12 23:43:05.453457863 +0200
12332 @@ -0,0 +1,140 @@
12333 +/* SPDX-License-Identifier: GPL-2.0 */
12334 +/*
12335 + * Copyright (C) 2017-2018 Junjiro R. Okajima
12336 + *
12337 + * This program, aufs is free software; you can redistribute it and/or modify
12338 + * it under the terms of the GNU General Public License as published by
12339 + * the Free Software Foundation; either version 2 of the License, or
12340 + * (at your option) any later version.
12341 + *
12342 + * This program is distributed in the hope that it will be useful,
12343 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12344 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12345 + * GNU General Public License for more details.
12346 + *
12347 + * You should have received a copy of the GNU General Public License
12348 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12349 + */
12350 +
12351 +/*
12352 + * renamed dir info
12353 + */
12354 +
12355 +#ifndef __AUFS_DIRREN_H__
12356 +#define __AUFS_DIRREN_H__
12357 +
12358 +#ifdef __KERNEL__
12359 +
12360 +#include <linux/dcache.h>
12361 +#include <linux/statfs.h>
12362 +#include <linux/uuid.h>
12363 +#include "hbl.h"
12364 +
12365 +#define AuDirren_NHASH 100
12366 +
12367 +#ifdef CONFIG_AUFS_DIRREN
12368 +enum au_brid_type {
12369 +       AuBrid_Unset,
12370 +       AuBrid_UUID,
12371 +       AuBrid_FSID,
12372 +       AuBrid_DEV
12373 +};
12374 +
12375 +struct au_dr_brid {
12376 +       enum au_brid_type       type;
12377 +       union {
12378 +               uuid_t  uuid;   /* unimplemented yet */
12379 +               fsid_t  fsid;
12380 +               dev_t   dev;
12381 +       };
12382 +};
12383 +
12384 +/* 20 is the max digits length of ulong 64 */
12385 +/* brid-type "_" uuid "_" inum */
12386 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12387 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12388 +
12389 +struct au_dr_hino {
12390 +       struct hlist_bl_node    dr_hnode;
12391 +       ino_t                   dr_h_ino;
12392 +};
12393 +
12394 +struct au_dr_br {
12395 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12396 +       struct au_dr_brid       dr_brid;
12397 +};
12398 +
12399 +struct au_dr_lookup {
12400 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12401 +       struct qstr             dr_name; /* subset of dr_info */
12402 +       aufs_bindex_t           ninfo;
12403 +       struct au_drinfo        **drinfo;
12404 +};
12405 +#else
12406 +struct au_dr_hino;
12407 +/* empty */
12408 +struct au_dr_br { };
12409 +struct au_dr_lookup { };
12410 +#endif
12411 +
12412 +/* ---------------------------------------------------------------------- */
12413 +
12414 +struct au_branch;
12415 +struct au_do_lookup_args;
12416 +struct au_hinode;
12417 +#ifdef CONFIG_AUFS_DIRREN
12418 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12419 +                       struct au_dr_hino *add_ent);
12420 +void au_dr_hino_free(struct au_dr_br *dr);
12421 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12422 +                 const struct path *path);
12423 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12424 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12425 +                struct qstr *dst_name, void *_rev);
12426 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12427 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12428 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12429 +              aufs_bindex_t bindex);
12430 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12431 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12432 +                    ino_t h_ino);
12433 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12434 +int au_dr_opt_set(struct super_block *sb);
12435 +int au_dr_opt_flush(struct super_block *sb);
12436 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12437 +#else
12438 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12439 +          struct au_dr_hino *add_ent);
12440 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12441 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12442 +          const struct path *path);
12443 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12444 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12445 +          struct qstr *dst_name, void *_rev);
12446 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12447 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12448 +          void *rev);
12449 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12450 +          aufs_bindex_t bindex);
12451 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12452 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12453 +          aufs_bindex_t bindex, ino_t h_ino);
12454 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12455 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12456 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12457 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12458 +#endif
12459 +
12460 +/* ---------------------------------------------------------------------- */
12461 +
12462 +#ifdef CONFIG_AUFS_DIRREN
12463 +static inline int au_dr_ihash(ino_t h_ino)
12464 +{
12465 +       return h_ino % AuDirren_NHASH;
12466 +}
12467 +#else
12468 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12469 +#endif
12470 +
12471 +#endif /* __KERNEL__ */
12472 +#endif /* __AUFS_DIRREN_H__ */
12473 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12474 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12475 +++ linux/fs/aufs/dynop.c       2018-08-12 23:43:05.456791299 +0200
12476 @@ -0,0 +1,370 @@
12477 +// SPDX-License-Identifier: GPL-2.0
12478 +/*
12479 + * Copyright (C) 2010-2018 Junjiro R. Okajima
12480 + *
12481 + * This program, aufs is free software; you can redistribute it and/or modify
12482 + * it under the terms of the GNU General Public License as published by
12483 + * the Free Software Foundation; either version 2 of the License, or
12484 + * (at your option) any later version.
12485 + *
12486 + * This program is distributed in the hope that it will be useful,
12487 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12488 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12489 + * GNU General Public License for more details.
12490 + *
12491 + * You should have received a copy of the GNU General Public License
12492 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12493 + */
12494 +
12495 +/*
12496 + * dynamically customizable operations for regular files
12497 + */
12498 +
12499 +#include "aufs.h"
12500 +
12501 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12502 +
12503 +/*
12504 + * How large will these lists be?
12505 + * Usually just a few elements, 20-30 at most for each, I guess.
12506 + */
12507 +static struct hlist_bl_head dynop[AuDyLast];
12508 +
12509 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12510 +                                    const void *h_op)
12511 +{
12512 +       struct au_dykey *key, *tmp;
12513 +       struct hlist_bl_node *pos;
12514 +
12515 +       key = NULL;
12516 +       hlist_bl_lock(hbl);
12517 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12518 +               if (tmp->dk_op.dy_hop == h_op) {
12519 +                       key = tmp;
12520 +                       kref_get(&key->dk_kref);
12521 +                       break;
12522 +               }
12523 +       hlist_bl_unlock(hbl);
12524 +
12525 +       return key;
12526 +}
12527 +
12528 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12529 +{
12530 +       struct au_dykey **k, *found;
12531 +       const void *h_op = key->dk_op.dy_hop;
12532 +       int i;
12533 +
12534 +       found = NULL;
12535 +       k = br->br_dykey;
12536 +       for (i = 0; i < AuBrDynOp; i++)
12537 +               if (k[i]) {
12538 +                       if (k[i]->dk_op.dy_hop == h_op) {
12539 +                               found = k[i];
12540 +                               break;
12541 +                       }
12542 +               } else
12543 +                       break;
12544 +       if (!found) {
12545 +               spin_lock(&br->br_dykey_lock);
12546 +               for (; i < AuBrDynOp; i++)
12547 +                       if (k[i]) {
12548 +                               if (k[i]->dk_op.dy_hop == h_op) {
12549 +                                       found = k[i];
12550 +                                       break;
12551 +                               }
12552 +                       } else {
12553 +                               k[i] = key;
12554 +                               break;
12555 +                       }
12556 +               spin_unlock(&br->br_dykey_lock);
12557 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12558 +       }
12559 +
12560 +       return found;
12561 +}
12562 +
12563 +/* kref_get() if @key is already added */
12564 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12565 +{
12566 +       struct au_dykey *tmp, *found;
12567 +       struct hlist_bl_node *pos;
12568 +       const void *h_op = key->dk_op.dy_hop;
12569 +
12570 +       found = NULL;
12571 +       hlist_bl_lock(hbl);
12572 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12573 +               if (tmp->dk_op.dy_hop == h_op) {
12574 +                       kref_get(&tmp->dk_kref);
12575 +                       found = tmp;
12576 +                       break;
12577 +               }
12578 +       if (!found)
12579 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12580 +       hlist_bl_unlock(hbl);
12581 +
12582 +       if (!found)
12583 +               DyPrSym(key);
12584 +       return found;
12585 +}
12586 +
12587 +static void dy_free_rcu(struct rcu_head *rcu)
12588 +{
12589 +       struct au_dykey *key;
12590 +
12591 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12592 +       DyPrSym(key);
12593 +       kfree(key);
12594 +}
12595 +
12596 +static void dy_free(struct kref *kref)
12597 +{
12598 +       struct au_dykey *key;
12599 +       struct hlist_bl_head *hbl;
12600 +
12601 +       key = container_of(kref, struct au_dykey, dk_kref);
12602 +       hbl = dynop + key->dk_op.dy_type;
12603 +       au_hbl_del(&key->dk_hnode, hbl);
12604 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12605 +}
12606 +
12607 +void au_dy_put(struct au_dykey *key)
12608 +{
12609 +       kref_put(&key->dk_kref, dy_free);
12610 +}
12611 +
12612 +/* ---------------------------------------------------------------------- */
12613 +
12614 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12615 +
12616 +#ifdef CONFIG_AUFS_DEBUG
12617 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12618 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12619 +#else
12620 +#define DyDbgDeclare(cnt)      do {} while (0)
12621 +#define DyDbgInc(cnt)          do {} while (0)
12622 +#endif
12623 +
12624 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12625 +       DyDbgInc(cnt);                                                  \
12626 +       if (h_op->func) {                                               \
12627 +               if (src.func)                                           \
12628 +                       dst.func = src.func;                            \
12629 +               else                                                    \
12630 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12631 +       }                                                               \
12632 +} while (0)
12633 +
12634 +#define DySetForce(func, dst, src) do {                \
12635 +       AuDebugOn(!src.func);                   \
12636 +       DyDbgInc(cnt);                          \
12637 +       dst.func = src.func;                    \
12638 +} while (0)
12639 +
12640 +#define DySetAop(func) \
12641 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12642 +#define DySetAopForce(func) \
12643 +       DySetForce(func, dyaop->da_op, aufs_aop)
12644 +
12645 +static void dy_aop(struct au_dykey *key, const void *h_op,
12646 +                  struct super_block *h_sb __maybe_unused)
12647 +{
12648 +       struct au_dyaop *dyaop = (void *)key;
12649 +       const struct address_space_operations *h_aop = h_op;
12650 +       DyDbgDeclare(cnt);
12651 +
12652 +       AuDbg("%s\n", au_sbtype(h_sb));
12653 +
12654 +       DySetAop(writepage);
12655 +       DySetAopForce(readpage);        /* force */
12656 +       DySetAop(writepages);
12657 +       DySetAop(set_page_dirty);
12658 +       DySetAop(readpages);
12659 +       DySetAop(write_begin);
12660 +       DySetAop(write_end);
12661 +       DySetAop(bmap);
12662 +       DySetAop(invalidatepage);
12663 +       DySetAop(releasepage);
12664 +       DySetAop(freepage);
12665 +       /* this one will be changed according to an aufs mount option */
12666 +       DySetAop(direct_IO);
12667 +       DySetAop(migratepage);
12668 +       DySetAop(isolate_page);
12669 +       DySetAop(putback_page);
12670 +       DySetAop(launder_page);
12671 +       DySetAop(is_partially_uptodate);
12672 +       DySetAop(is_dirty_writeback);
12673 +       DySetAop(error_remove_page);
12674 +       DySetAop(swap_activate);
12675 +       DySetAop(swap_deactivate);
12676 +
12677 +       DyDbgSize(cnt, *h_aop);
12678 +}
12679 +
12680 +/* ---------------------------------------------------------------------- */
12681 +
12682 +static void dy_bug(struct kref *kref)
12683 +{
12684 +       BUG();
12685 +}
12686 +
12687 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12688 +{
12689 +       struct au_dykey *key, *old;
12690 +       struct hlist_bl_head *hbl;
12691 +       struct op {
12692 +               unsigned int sz;
12693 +               void (*set)(struct au_dykey *key, const void *h_op,
12694 +                           struct super_block *h_sb __maybe_unused);
12695 +       };
12696 +       static const struct op a[] = {
12697 +               [AuDy_AOP] = {
12698 +                       .sz     = sizeof(struct au_dyaop),
12699 +                       .set    = dy_aop
12700 +               }
12701 +       };
12702 +       const struct op *p;
12703 +
12704 +       hbl = dynop + op->dy_type;
12705 +       key = dy_gfind_get(hbl, op->dy_hop);
12706 +       if (key)
12707 +               goto out_add; /* success */
12708 +
12709 +       p = a + op->dy_type;
12710 +       key = kzalloc(p->sz, GFP_NOFS);
12711 +       if (unlikely(!key)) {
12712 +               key = ERR_PTR(-ENOMEM);
12713 +               goto out;
12714 +       }
12715 +
12716 +       key->dk_op.dy_hop = op->dy_hop;
12717 +       kref_init(&key->dk_kref);
12718 +       p->set(key, op->dy_hop, au_br_sb(br));
12719 +       old = dy_gadd(hbl, key);
12720 +       if (old) {
12721 +               kfree(key);
12722 +               key = old;
12723 +       }
12724 +
12725 +out_add:
12726 +       old = dy_bradd(br, key);
12727 +       if (old)
12728 +               /* its ref-count should never be zero here */
12729 +               kref_put(&key->dk_kref, dy_bug);
12730 +out:
12731 +       return key;
12732 +}
12733 +
12734 +/* ---------------------------------------------------------------------- */
12735 +/*
12736 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
12737 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12738 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12739 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12740 + * See the aufs manual in detail.
12741 + */
12742 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12743 +{
12744 +       if (!do_dx)
12745 +               dyaop->da_op.direct_IO = NULL;
12746 +       else
12747 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12748 +}
12749 +
12750 +static struct au_dyaop *dy_aget(struct au_branch *br,
12751 +                               const struct address_space_operations *h_aop,
12752 +                               int do_dx)
12753 +{
12754 +       struct au_dyaop *dyaop;
12755 +       struct au_dynop op;
12756 +
12757 +       op.dy_type = AuDy_AOP;
12758 +       op.dy_haop = h_aop;
12759 +       dyaop = (void *)dy_get(&op, br);
12760 +       if (IS_ERR(dyaop))
12761 +               goto out;
12762 +       dy_adx(dyaop, do_dx);
12763 +
12764 +out:
12765 +       return dyaop;
12766 +}
12767 +
12768 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12769 +               struct inode *h_inode)
12770 +{
12771 +       int err, do_dx;
12772 +       struct super_block *sb;
12773 +       struct au_branch *br;
12774 +       struct au_dyaop *dyaop;
12775 +
12776 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12777 +       IiMustWriteLock(inode);
12778 +
12779 +       sb = inode->i_sb;
12780 +       br = au_sbr(sb, bindex);
12781 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12782 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12783 +       err = PTR_ERR(dyaop);
12784 +       if (IS_ERR(dyaop))
12785 +               /* unnecessary to call dy_fput() */
12786 +               goto out;
12787 +
12788 +       err = 0;
12789 +       inode->i_mapping->a_ops = &dyaop->da_op;
12790 +
12791 +out:
12792 +       return err;
12793 +}
12794 +
12795 +/*
12796 + * Is it safe to replace a_ops during the inode/file is in operation?
12797 + * Yes, I hope so.
12798 + */
12799 +int au_dy_irefresh(struct inode *inode)
12800 +{
12801 +       int err;
12802 +       aufs_bindex_t btop;
12803 +       struct inode *h_inode;
12804 +
12805 +       err = 0;
12806 +       if (S_ISREG(inode->i_mode)) {
12807 +               btop = au_ibtop(inode);
12808 +               h_inode = au_h_iptr(inode, btop);
12809 +               err = au_dy_iaop(inode, btop, h_inode);
12810 +       }
12811 +       return err;
12812 +}
12813 +
12814 +void au_dy_arefresh(int do_dx)
12815 +{
12816 +       struct hlist_bl_head *hbl;
12817 +       struct hlist_bl_node *pos;
12818 +       struct au_dykey *key;
12819 +
12820 +       hbl = dynop + AuDy_AOP;
12821 +       hlist_bl_lock(hbl);
12822 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12823 +               dy_adx((void *)key, do_dx);
12824 +       hlist_bl_unlock(hbl);
12825 +}
12826 +
12827 +/* ---------------------------------------------------------------------- */
12828 +
12829 +void __init au_dy_init(void)
12830 +{
12831 +       int i;
12832 +
12833 +       /* make sure that 'struct au_dykey *' can be any type */
12834 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
12835 +
12836 +       for (i = 0; i < AuDyLast; i++)
12837 +               INIT_HLIST_BL_HEAD(dynop + i);
12838 +}
12839 +
12840 +void au_dy_fin(void)
12841 +{
12842 +       int i;
12843 +
12844 +       for (i = 0; i < AuDyLast; i++)
12845 +               WARN_ON(!hlist_bl_empty(dynop + i));
12846 +}
12847 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12848 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12849 +++ linux/fs/aufs/dynop.h       2018-08-12 23:43:05.456791299 +0200
12850 @@ -0,0 +1,75 @@
12851 +/* SPDX-License-Identifier: GPL-2.0 */
12852 +/*
12853 + * Copyright (C) 2010-2018 Junjiro R. Okajima
12854 + *
12855 + * This program, aufs is free software; you can redistribute it and/or modify
12856 + * it under the terms of the GNU General Public License as published by
12857 + * the Free Software Foundation; either version 2 of the License, or
12858 + * (at your option) any later version.
12859 + *
12860 + * This program is distributed in the hope that it will be useful,
12861 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12862 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12863 + * GNU General Public License for more details.
12864 + *
12865 + * You should have received a copy of the GNU General Public License
12866 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12867 + */
12868 +
12869 +/*
12870 + * dynamically customizable operations (for regular files only)
12871 + */
12872 +
12873 +#ifndef __AUFS_DYNOP_H__
12874 +#define __AUFS_DYNOP_H__
12875 +
12876 +#ifdef __KERNEL__
12877 +
12878 +#include <linux/fs.h>
12879 +#include <linux/kref.h>
12880 +
12881 +enum {AuDy_AOP, AuDyLast};
12882 +
12883 +struct au_dynop {
12884 +       int                                             dy_type;
12885 +       union {
12886 +               const void                              *dy_hop;
12887 +               const struct address_space_operations   *dy_haop;
12888 +       };
12889 +};
12890 +
12891 +struct au_dykey {
12892 +       union {
12893 +               struct hlist_bl_node    dk_hnode;
12894 +               struct rcu_head         dk_rcu;
12895 +       };
12896 +       struct au_dynop         dk_op;
12897 +
12898 +       /*
12899 +        * during I am in the branch local array, kref is gotten. when the
12900 +        * branch is removed, kref is put.
12901 +        */
12902 +       struct kref             dk_kref;
12903 +};
12904 +
12905 +/* stop unioning since their sizes are very different from each other */
12906 +struct au_dyaop {
12907 +       struct au_dykey                 da_key;
12908 +       struct address_space_operations da_op; /* not const */
12909 +};
12910 +
12911 +/* ---------------------------------------------------------------------- */
12912 +
12913 +/* dynop.c */
12914 +struct au_branch;
12915 +void au_dy_put(struct au_dykey *key);
12916 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12917 +               struct inode *h_inode);
12918 +int au_dy_irefresh(struct inode *inode);
12919 +void au_dy_arefresh(int do_dio);
12920 +
12921 +void __init au_dy_init(void);
12922 +void au_dy_fin(void);
12923 +
12924 +#endif /* __KERNEL__ */
12925 +#endif /* __AUFS_DYNOP_H__ */
12926 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12927 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12928 +++ linux/fs/aufs/export.c      2018-08-12 23:43:05.456791299 +0200
12929 @@ -0,0 +1,838 @@
12930 +// SPDX-License-Identifier: GPL-2.0
12931 +/*
12932 + * Copyright (C) 2005-2018 Junjiro R. Okajima
12933 + *
12934 + * This program, aufs is free software; you can redistribute it and/or modify
12935 + * it under the terms of the GNU General Public License as published by
12936 + * the Free Software Foundation; either version 2 of the License, or
12937 + * (at your option) any later version.
12938 + *
12939 + * This program is distributed in the hope that it will be useful,
12940 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12941 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12942 + * GNU General Public License for more details.
12943 + *
12944 + * You should have received a copy of the GNU General Public License
12945 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12946 + */
12947 +
12948 +/*
12949 + * export via nfs
12950 + */
12951 +
12952 +#include <linux/exportfs.h>
12953 +#include <linux/fs_struct.h>
12954 +#include <linux/namei.h>
12955 +#include <linux/nsproxy.h>
12956 +#include <linux/random.h>
12957 +#include <linux/writeback.h>
12958 +#include "aufs.h"
12959 +
12960 +union conv {
12961 +#ifdef CONFIG_AUFS_INO_T_64
12962 +       __u32 a[2];
12963 +#else
12964 +       __u32 a[1];
12965 +#endif
12966 +       ino_t ino;
12967 +};
12968 +
12969 +static ino_t decode_ino(__u32 *a)
12970 +{
12971 +       union conv u;
12972 +
12973 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12974 +       u.a[0] = a[0];
12975 +#ifdef CONFIG_AUFS_INO_T_64
12976 +       u.a[1] = a[1];
12977 +#endif
12978 +       return u.ino;
12979 +}
12980 +
12981 +static void encode_ino(__u32 *a, ino_t ino)
12982 +{
12983 +       union conv u;
12984 +
12985 +       u.ino = ino;
12986 +       a[0] = u.a[0];
12987 +#ifdef CONFIG_AUFS_INO_T_64
12988 +       a[1] = u.a[1];
12989 +#endif
12990 +}
12991 +
12992 +/* NFS file handle */
12993 +enum {
12994 +       Fh_br_id,
12995 +       Fh_sigen,
12996 +#ifdef CONFIG_AUFS_INO_T_64
12997 +       /* support 64bit inode number */
12998 +       Fh_ino1,
12999 +       Fh_ino2,
13000 +       Fh_dir_ino1,
13001 +       Fh_dir_ino2,
13002 +#else
13003 +       Fh_ino1,
13004 +       Fh_dir_ino1,
13005 +#endif
13006 +       Fh_igen,
13007 +       Fh_h_type,
13008 +       Fh_tail,
13009 +
13010 +       Fh_ino = Fh_ino1,
13011 +       Fh_dir_ino = Fh_dir_ino1
13012 +};
13013 +
13014 +static int au_test_anon(struct dentry *dentry)
13015 +{
13016 +       /* note: read d_flags without d_lock */
13017 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13018 +}
13019 +
13020 +int au_test_nfsd(void)
13021 +{
13022 +       int ret;
13023 +       struct task_struct *tsk = current;
13024 +       char comm[sizeof(tsk->comm)];
13025 +
13026 +       ret = 0;
13027 +       if (tsk->flags & PF_KTHREAD) {
13028 +               get_task_comm(comm, tsk);
13029 +               ret = !strcmp(comm, "nfsd");
13030 +       }
13031 +
13032 +       return ret;
13033 +}
13034 +
13035 +/* ---------------------------------------------------------------------- */
13036 +/* inode generation external table */
13037 +
13038 +void au_xigen_inc(struct inode *inode)
13039 +{
13040 +       loff_t pos;
13041 +       ssize_t sz;
13042 +       __u32 igen;
13043 +       struct super_block *sb;
13044 +       struct au_sbinfo *sbinfo;
13045 +
13046 +       sb = inode->i_sb;
13047 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
13048 +
13049 +       sbinfo = au_sbi(sb);
13050 +       pos = inode->i_ino;
13051 +       pos *= sizeof(igen);
13052 +       igen = inode->i_generation + 1;
13053 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13054 +                        sizeof(igen), &pos);
13055 +       if (sz == sizeof(igen))
13056 +               return; /* success */
13057 +
13058 +       if (unlikely(sz >= 0))
13059 +               AuIOErr("xigen error (%zd)\n", sz);
13060 +}
13061 +
13062 +int au_xigen_new(struct inode *inode)
13063 +{
13064 +       int err;
13065 +       loff_t pos;
13066 +       ssize_t sz;
13067 +       struct super_block *sb;
13068 +       struct au_sbinfo *sbinfo;
13069 +       struct file *file;
13070 +
13071 +       err = 0;
13072 +       /* todo: dirty, at mount time */
13073 +       if (inode->i_ino == AUFS_ROOT_INO)
13074 +               goto out;
13075 +       sb = inode->i_sb;
13076 +       SiMustAnyLock(sb);
13077 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13078 +               goto out;
13079 +
13080 +       err = -EFBIG;
13081 +       pos = inode->i_ino;
13082 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13083 +               AuIOErr1("too large i%lld\n", pos);
13084 +               goto out;
13085 +       }
13086 +       pos *= sizeof(inode->i_generation);
13087 +
13088 +       err = 0;
13089 +       sbinfo = au_sbi(sb);
13090 +       file = sbinfo->si_xigen;
13091 +       BUG_ON(!file);
13092 +
13093 +       if (vfsub_f_size_read(file)
13094 +           < pos + sizeof(inode->i_generation)) {
13095 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13096 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13097 +                                sizeof(inode->i_generation), &pos);
13098 +       } else
13099 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13100 +                               sizeof(inode->i_generation), &pos);
13101 +       if (sz == sizeof(inode->i_generation))
13102 +               goto out; /* success */
13103 +
13104 +       err = sz;
13105 +       if (unlikely(sz >= 0)) {
13106 +               err = -EIO;
13107 +               AuIOErr("xigen error (%zd)\n", sz);
13108 +       }
13109 +
13110 +out:
13111 +       return err;
13112 +}
13113 +
13114 +int au_xigen_set(struct super_block *sb, struct path *path)
13115 +{
13116 +       int err;
13117 +       struct au_sbinfo *sbinfo;
13118 +       struct file *file;
13119 +
13120 +       SiMustWriteLock(sb);
13121 +
13122 +       sbinfo = au_sbi(sb);
13123 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13124 +       err = PTR_ERR(file);
13125 +       if (IS_ERR(file))
13126 +               goto out;
13127 +       err = 0;
13128 +       if (sbinfo->si_xigen)
13129 +               fput(sbinfo->si_xigen);
13130 +       sbinfo->si_xigen = file;
13131 +
13132 +out:
13133 +       AuTraceErr(err);
13134 +       return err;
13135 +}
13136 +
13137 +void au_xigen_clr(struct super_block *sb)
13138 +{
13139 +       struct au_sbinfo *sbinfo;
13140 +
13141 +       SiMustWriteLock(sb);
13142 +
13143 +       sbinfo = au_sbi(sb);
13144 +       if (sbinfo->si_xigen) {
13145 +               fput(sbinfo->si_xigen);
13146 +               sbinfo->si_xigen = NULL;
13147 +       }
13148 +}
13149 +
13150 +/* ---------------------------------------------------------------------- */
13151 +
13152 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13153 +                                   ino_t dir_ino)
13154 +{
13155 +       struct dentry *dentry, *d;
13156 +       struct inode *inode;
13157 +       unsigned int sigen;
13158 +
13159 +       dentry = NULL;
13160 +       inode = ilookup(sb, ino);
13161 +       if (!inode)
13162 +               goto out;
13163 +
13164 +       dentry = ERR_PTR(-ESTALE);
13165 +       sigen = au_sigen(sb);
13166 +       if (unlikely(au_is_bad_inode(inode)
13167 +                    || IS_DEADDIR(inode)
13168 +                    || sigen != au_iigen(inode, NULL)))
13169 +               goto out_iput;
13170 +
13171 +       dentry = NULL;
13172 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13173 +               dentry = d_find_alias(inode);
13174 +       else {
13175 +               spin_lock(&inode->i_lock);
13176 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13177 +                       spin_lock(&d->d_lock);
13178 +                       if (!au_test_anon(d)
13179 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13180 +                               dentry = dget_dlock(d);
13181 +                               spin_unlock(&d->d_lock);
13182 +                               break;
13183 +                       }
13184 +                       spin_unlock(&d->d_lock);
13185 +               }
13186 +               spin_unlock(&inode->i_lock);
13187 +       }
13188 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13189 +               /* need to refresh */
13190 +               dput(dentry);
13191 +               dentry = NULL;
13192 +       }
13193 +
13194 +out_iput:
13195 +       iput(inode);
13196 +out:
13197 +       AuTraceErrPtr(dentry);
13198 +       return dentry;
13199 +}
13200 +
13201 +/* ---------------------------------------------------------------------- */
13202 +
13203 +/* todo: dirty? */
13204 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13205 +
13206 +struct au_compare_mnt_args {
13207 +       /* input */
13208 +       struct super_block *sb;
13209 +
13210 +       /* output */
13211 +       struct vfsmount *mnt;
13212 +};
13213 +
13214 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13215 +{
13216 +       struct au_compare_mnt_args *a = arg;
13217 +
13218 +       if (mnt->mnt_sb != a->sb)
13219 +               return 0;
13220 +       a->mnt = mntget(mnt);
13221 +       return 1;
13222 +}
13223 +
13224 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13225 +{
13226 +       int err;
13227 +       struct path root;
13228 +       struct au_compare_mnt_args args = {
13229 +               .sb = sb
13230 +       };
13231 +
13232 +       get_fs_root(current->fs, &root);
13233 +       rcu_read_lock();
13234 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13235 +       rcu_read_unlock();
13236 +       path_put(&root);
13237 +       AuDebugOn(!err);
13238 +       AuDebugOn(!args.mnt);
13239 +       return args.mnt;
13240 +}
13241 +
13242 +struct au_nfsd_si_lock {
13243 +       unsigned int sigen;
13244 +       aufs_bindex_t bindex, br_id;
13245 +       unsigned char force_lock;
13246 +};
13247 +
13248 +static int si_nfsd_read_lock(struct super_block *sb,
13249 +                            struct au_nfsd_si_lock *nsi_lock)
13250 +{
13251 +       int err;
13252 +       aufs_bindex_t bindex;
13253 +
13254 +       si_read_lock(sb, AuLock_FLUSH);
13255 +
13256 +       /* branch id may be wrapped around */
13257 +       err = 0;
13258 +       bindex = au_br_index(sb, nsi_lock->br_id);
13259 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13260 +               goto out; /* success */
13261 +
13262 +       err = -ESTALE;
13263 +       bindex = -1;
13264 +       if (!nsi_lock->force_lock)
13265 +               si_read_unlock(sb);
13266 +
13267 +out:
13268 +       nsi_lock->bindex = bindex;
13269 +       return err;
13270 +}
13271 +
13272 +struct find_name_by_ino {
13273 +       struct dir_context ctx;
13274 +       int called, found;
13275 +       ino_t ino;
13276 +       char *name;
13277 +       int namelen;
13278 +};
13279 +
13280 +static int
13281 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13282 +                loff_t offset, u64 ino, unsigned int d_type)
13283 +{
13284 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13285 +                                                 ctx);
13286 +
13287 +       a->called++;
13288 +       if (a->ino != ino)
13289 +               return 0;
13290 +
13291 +       memcpy(a->name, name, namelen);
13292 +       a->namelen = namelen;
13293 +       a->found = 1;
13294 +       return 1;
13295 +}
13296 +
13297 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13298 +                                    struct au_nfsd_si_lock *nsi_lock)
13299 +{
13300 +       struct dentry *dentry, *parent;
13301 +       struct file *file;
13302 +       struct inode *dir;
13303 +       struct find_name_by_ino arg = {
13304 +               .ctx = {
13305 +                       .actor = find_name_by_ino
13306 +               }
13307 +       };
13308 +       int err;
13309 +
13310 +       parent = path->dentry;
13311 +       if (nsi_lock)
13312 +               si_read_unlock(parent->d_sb);
13313 +       file = vfsub_dentry_open(path, au_dir_roflags);
13314 +       dentry = (void *)file;
13315 +       if (IS_ERR(file))
13316 +               goto out;
13317 +
13318 +       dentry = ERR_PTR(-ENOMEM);
13319 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13320 +       if (unlikely(!arg.name))
13321 +               goto out_file;
13322 +       arg.ino = ino;
13323 +       arg.found = 0;
13324 +       do {
13325 +               arg.called = 0;
13326 +               /* smp_mb(); */
13327 +               err = vfsub_iterate_dir(file, &arg.ctx);
13328 +       } while (!err && !arg.found && arg.called);
13329 +       dentry = ERR_PTR(err);
13330 +       if (unlikely(err))
13331 +               goto out_name;
13332 +       /* instead of ENOENT */
13333 +       dentry = ERR_PTR(-ESTALE);
13334 +       if (!arg.found)
13335 +               goto out_name;
13336 +
13337 +       /* do not call vfsub_lkup_one() */
13338 +       dir = d_inode(parent);
13339 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13340 +       AuTraceErrPtr(dentry);
13341 +       if (IS_ERR(dentry))
13342 +               goto out_name;
13343 +       AuDebugOn(au_test_anon(dentry));
13344 +       if (unlikely(d_really_is_negative(dentry))) {
13345 +               dput(dentry);
13346 +               dentry = ERR_PTR(-ENOENT);
13347 +       }
13348 +
13349 +out_name:
13350 +       free_page((unsigned long)arg.name);
13351 +out_file:
13352 +       fput(file);
13353 +out:
13354 +       if (unlikely(nsi_lock
13355 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13356 +               if (!IS_ERR(dentry)) {
13357 +                       dput(dentry);
13358 +                       dentry = ERR_PTR(-ESTALE);
13359 +               }
13360 +       AuTraceErrPtr(dentry);
13361 +       return dentry;
13362 +}
13363 +
13364 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13365 +                                       ino_t dir_ino,
13366 +                                       struct au_nfsd_si_lock *nsi_lock)
13367 +{
13368 +       struct dentry *dentry;
13369 +       struct path path;
13370 +
13371 +       if (dir_ino != AUFS_ROOT_INO) {
13372 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13373 +               dentry = path.dentry;
13374 +               if (!path.dentry || IS_ERR(path.dentry))
13375 +                       goto out;
13376 +               AuDebugOn(au_test_anon(path.dentry));
13377 +       } else
13378 +               path.dentry = dget(sb->s_root);
13379 +
13380 +       path.mnt = au_mnt_get(sb);
13381 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13382 +       path_put(&path);
13383 +
13384 +out:
13385 +       AuTraceErrPtr(dentry);
13386 +       return dentry;
13387 +}
13388 +
13389 +/* ---------------------------------------------------------------------- */
13390 +
13391 +static int h_acceptable(void *expv, struct dentry *dentry)
13392 +{
13393 +       return 1;
13394 +}
13395 +
13396 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13397 +                          char *buf, int len, struct super_block *sb)
13398 +{
13399 +       char *p;
13400 +       int n;
13401 +       struct path path;
13402 +
13403 +       p = d_path(h_rootpath, buf, len);
13404 +       if (IS_ERR(p))
13405 +               goto out;
13406 +       n = strlen(p);
13407 +
13408 +       path.mnt = h_rootpath->mnt;
13409 +       path.dentry = h_parent;
13410 +       p = d_path(&path, buf, len);
13411 +       if (IS_ERR(p))
13412 +               goto out;
13413 +       if (n != 1)
13414 +               p += n;
13415 +
13416 +       path.mnt = au_mnt_get(sb);
13417 +       path.dentry = sb->s_root;
13418 +       p = d_path(&path, buf, len - strlen(p));
13419 +       mntput(path.mnt);
13420 +       if (IS_ERR(p))
13421 +               goto out;
13422 +       if (n != 1)
13423 +               p[strlen(p)] = '/';
13424 +
13425 +out:
13426 +       AuTraceErrPtr(p);
13427 +       return p;
13428 +}
13429 +
13430 +static
13431 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13432 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13433 +{
13434 +       struct dentry *dentry, *h_parent, *root;
13435 +       struct super_block *h_sb;
13436 +       char *pathname, *p;
13437 +       struct vfsmount *h_mnt;
13438 +       struct au_branch *br;
13439 +       int err;
13440 +       struct path path;
13441 +
13442 +       br = au_sbr(sb, nsi_lock->bindex);
13443 +       h_mnt = au_br_mnt(br);
13444 +       h_sb = h_mnt->mnt_sb;
13445 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13446 +       lockdep_off();
13447 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13448 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13449 +                                     h_acceptable, /*context*/NULL);
13450 +       lockdep_on();
13451 +       dentry = h_parent;
13452 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13453 +               AuWarn1("%s decode_fh failed, %ld\n",
13454 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13455 +               goto out;
13456 +       }
13457 +       dentry = NULL;
13458 +       if (unlikely(au_test_anon(h_parent))) {
13459 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13460 +                       au_sbtype(h_sb));
13461 +               goto out_h_parent;
13462 +       }
13463 +
13464 +       dentry = ERR_PTR(-ENOMEM);
13465 +       pathname = (void *)__get_free_page(GFP_NOFS);
13466 +       if (unlikely(!pathname))
13467 +               goto out_h_parent;
13468 +
13469 +       root = sb->s_root;
13470 +       path.mnt = h_mnt;
13471 +       di_read_lock_parent(root, !AuLock_IR);
13472 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13473 +       di_read_unlock(root, !AuLock_IR);
13474 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13475 +       dentry = (void *)p;
13476 +       if (IS_ERR(p))
13477 +               goto out_pathname;
13478 +
13479 +       si_read_unlock(sb);
13480 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13481 +       dentry = ERR_PTR(err);
13482 +       if (unlikely(err))
13483 +               goto out_relock;
13484 +
13485 +       dentry = ERR_PTR(-ENOENT);
13486 +       AuDebugOn(au_test_anon(path.dentry));
13487 +       if (unlikely(d_really_is_negative(path.dentry)))
13488 +               goto out_path;
13489 +
13490 +       if (ino != d_inode(path.dentry)->i_ino)
13491 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13492 +       else
13493 +               dentry = dget(path.dentry);
13494 +
13495 +out_path:
13496 +       path_put(&path);
13497 +out_relock:
13498 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13499 +               if (!IS_ERR(dentry)) {
13500 +                       dput(dentry);
13501 +                       dentry = ERR_PTR(-ESTALE);
13502 +               }
13503 +out_pathname:
13504 +       free_page((unsigned long)pathname);
13505 +out_h_parent:
13506 +       dput(h_parent);
13507 +out:
13508 +       AuTraceErrPtr(dentry);
13509 +       return dentry;
13510 +}
13511 +
13512 +/* ---------------------------------------------------------------------- */
13513 +
13514 +static struct dentry *
13515 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13516 +                 int fh_type)
13517 +{
13518 +       struct dentry *dentry;
13519 +       __u32 *fh = fid->raw;
13520 +       struct au_branch *br;
13521 +       ino_t ino, dir_ino;
13522 +       struct au_nfsd_si_lock nsi_lock = {
13523 +               .force_lock     = 0
13524 +       };
13525 +
13526 +       dentry = ERR_PTR(-ESTALE);
13527 +       /* it should never happen, but the file handle is unreliable */
13528 +       if (unlikely(fh_len < Fh_tail))
13529 +               goto out;
13530 +       nsi_lock.sigen = fh[Fh_sigen];
13531 +       nsi_lock.br_id = fh[Fh_br_id];
13532 +
13533 +       /* branch id may be wrapped around */
13534 +       br = NULL;
13535 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13536 +               goto out;
13537 +       nsi_lock.force_lock = 1;
13538 +
13539 +       /* is this inode still cached? */
13540 +       ino = decode_ino(fh + Fh_ino);
13541 +       /* it should never happen */
13542 +       if (unlikely(ino == AUFS_ROOT_INO))
13543 +               goto out_unlock;
13544 +
13545 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13546 +       dentry = decode_by_ino(sb, ino, dir_ino);
13547 +       if (IS_ERR(dentry))
13548 +               goto out_unlock;
13549 +       if (dentry)
13550 +               goto accept;
13551 +
13552 +       /* is the parent dir cached? */
13553 +       br = au_sbr(sb, nsi_lock.bindex);
13554 +       au_br_get(br);
13555 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13556 +       if (IS_ERR(dentry))
13557 +               goto out_unlock;
13558 +       if (dentry)
13559 +               goto accept;
13560 +
13561 +       /* lookup path */
13562 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13563 +       if (IS_ERR(dentry))
13564 +               goto out_unlock;
13565 +       if (unlikely(!dentry))
13566 +               /* todo?: make it ESTALE */
13567 +               goto out_unlock;
13568 +
13569 +accept:
13570 +       if (!au_digen_test(dentry, au_sigen(sb))
13571 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13572 +               goto out_unlock; /* success */
13573 +
13574 +       dput(dentry);
13575 +       dentry = ERR_PTR(-ESTALE);
13576 +out_unlock:
13577 +       if (br)
13578 +               au_br_put(br);
13579 +       si_read_unlock(sb);
13580 +out:
13581 +       AuTraceErrPtr(dentry);
13582 +       return dentry;
13583 +}
13584 +
13585 +#if 0 /* reserved for future use */
13586 +/* support subtreecheck option */
13587 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13588 +                                       int fh_len, int fh_type)
13589 +{
13590 +       struct dentry *parent;
13591 +       __u32 *fh = fid->raw;
13592 +       ino_t dir_ino;
13593 +
13594 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13595 +       parent = decode_by_ino(sb, dir_ino, 0);
13596 +       if (IS_ERR(parent))
13597 +               goto out;
13598 +       if (!parent)
13599 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13600 +                                       dir_ino, fh, fh_len);
13601 +
13602 +out:
13603 +       AuTraceErrPtr(parent);
13604 +       return parent;
13605 +}
13606 +#endif
13607 +
13608 +/* ---------------------------------------------------------------------- */
13609 +
13610 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13611 +                         struct inode *dir)
13612 +{
13613 +       int err;
13614 +       aufs_bindex_t bindex;
13615 +       struct super_block *sb, *h_sb;
13616 +       struct dentry *dentry, *parent, *h_parent;
13617 +       struct inode *h_dir;
13618 +       struct au_branch *br;
13619 +
13620 +       err = -ENOSPC;
13621 +       if (unlikely(*max_len <= Fh_tail)) {
13622 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13623 +               goto out;
13624 +       }
13625 +
13626 +       err = FILEID_ROOT;
13627 +       if (inode->i_ino == AUFS_ROOT_INO) {
13628 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13629 +               goto out;
13630 +       }
13631 +
13632 +       h_parent = NULL;
13633 +       sb = inode->i_sb;
13634 +       err = si_read_lock(sb, AuLock_FLUSH);
13635 +       if (unlikely(err))
13636 +               goto out;
13637 +
13638 +#ifdef CONFIG_AUFS_DEBUG
13639 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13640 +               AuWarn1("NFS-exporting requires xino\n");
13641 +#endif
13642 +       err = -EIO;
13643 +       parent = NULL;
13644 +       ii_read_lock_child(inode);
13645 +       bindex = au_ibtop(inode);
13646 +       if (!dir) {
13647 +               dentry = d_find_any_alias(inode);
13648 +               if (unlikely(!dentry))
13649 +                       goto out_unlock;
13650 +               AuDebugOn(au_test_anon(dentry));
13651 +               parent = dget_parent(dentry);
13652 +               dput(dentry);
13653 +               if (unlikely(!parent))
13654 +                       goto out_unlock;
13655 +               if (d_really_is_positive(parent))
13656 +                       dir = d_inode(parent);
13657 +       }
13658 +
13659 +       ii_read_lock_parent(dir);
13660 +       h_dir = au_h_iptr(dir, bindex);
13661 +       ii_read_unlock(dir);
13662 +       if (unlikely(!h_dir))
13663 +               goto out_parent;
13664 +       h_parent = d_find_any_alias(h_dir);
13665 +       if (unlikely(!h_parent))
13666 +               goto out_hparent;
13667 +
13668 +       err = -EPERM;
13669 +       br = au_sbr(sb, bindex);
13670 +       h_sb = au_br_sb(br);
13671 +       if (unlikely(!h_sb->s_export_op)) {
13672 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13673 +               goto out_hparent;
13674 +       }
13675 +
13676 +       fh[Fh_br_id] = br->br_id;
13677 +       fh[Fh_sigen] = au_sigen(sb);
13678 +       encode_ino(fh + Fh_ino, inode->i_ino);
13679 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13680 +       fh[Fh_igen] = inode->i_generation;
13681 +
13682 +       *max_len -= Fh_tail;
13683 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13684 +                                          max_len,
13685 +                                          /*connectable or subtreecheck*/0);
13686 +       err = fh[Fh_h_type];
13687 +       *max_len += Fh_tail;
13688 +       /* todo: macros? */
13689 +       if (err != FILEID_INVALID)
13690 +               err = 99;
13691 +       else
13692 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13693 +
13694 +out_hparent:
13695 +       dput(h_parent);
13696 +out_parent:
13697 +       dput(parent);
13698 +out_unlock:
13699 +       ii_read_unlock(inode);
13700 +       si_read_unlock(sb);
13701 +out:
13702 +       if (unlikely(err < 0))
13703 +               err = FILEID_INVALID;
13704 +       return err;
13705 +}
13706 +
13707 +/* ---------------------------------------------------------------------- */
13708 +
13709 +static int aufs_commit_metadata(struct inode *inode)
13710 +{
13711 +       int err;
13712 +       aufs_bindex_t bindex;
13713 +       struct super_block *sb;
13714 +       struct inode *h_inode;
13715 +       int (*f)(struct inode *inode);
13716 +
13717 +       sb = inode->i_sb;
13718 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13719 +       ii_write_lock_child(inode);
13720 +       bindex = au_ibtop(inode);
13721 +       AuDebugOn(bindex < 0);
13722 +       h_inode = au_h_iptr(inode, bindex);
13723 +
13724 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13725 +       if (f)
13726 +               err = f(h_inode);
13727 +       else {
13728 +               struct writeback_control wbc = {
13729 +                       .sync_mode      = WB_SYNC_ALL,
13730 +                       .nr_to_write    = 0 /* metadata only */
13731 +               };
13732 +
13733 +               err = sync_inode(h_inode, &wbc);
13734 +       }
13735 +
13736 +       au_cpup_attr_timesizes(inode);
13737 +       ii_write_unlock(inode);
13738 +       si_read_unlock(sb);
13739 +       return err;
13740 +}
13741 +
13742 +/* ---------------------------------------------------------------------- */
13743 +
13744 +static struct export_operations aufs_export_op = {
13745 +       .fh_to_dentry           = aufs_fh_to_dentry,
13746 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13747 +       .encode_fh              = aufs_encode_fh,
13748 +       .commit_metadata        = aufs_commit_metadata
13749 +};
13750 +
13751 +void au_export_init(struct super_block *sb)
13752 +{
13753 +       struct au_sbinfo *sbinfo;
13754 +       __u32 u;
13755 +
13756 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13757 +                        && IS_MODULE(CONFIG_EXPORTFS),
13758 +                        AUFS_NAME ": unsupported configuration "
13759 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13760 +
13761 +       sb->s_export_op = &aufs_export_op;
13762 +       sbinfo = au_sbi(sb);
13763 +       sbinfo->si_xigen = NULL;
13764 +       get_random_bytes(&u, sizeof(u));
13765 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13766 +       atomic_set(&sbinfo->si_xigen_next, u);
13767 +}
13768 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13769 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13770 +++ linux/fs/aufs/fhsm.c        2018-08-12 23:43:05.456791299 +0200
13771 @@ -0,0 +1,427 @@
13772 +// SPDX-License-Identifier: GPL-2.0
13773 +/*
13774 + * Copyright (C) 2011-2018 Junjiro R. Okajima
13775 + *
13776 + * This program, aufs is free software; you can redistribute it and/or modify
13777 + * it under the terms of the GNU General Public License as published by
13778 + * the Free Software Foundation; either version 2 of the License, or
13779 + * (at your option) any later version.
13780 + *
13781 + * This program is distributed in the hope that it will be useful,
13782 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13783 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13784 + * GNU General Public License for more details.
13785 + *
13786 + * You should have received a copy of the GNU General Public License
13787 + * along with this program; if not, write to the Free Software
13788 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13789 + */
13790 +
13791 +/*
13792 + * File-based Hierarchy Storage Management
13793 + */
13794 +
13795 +#include <linux/anon_inodes.h>
13796 +#include <linux/poll.h>
13797 +#include <linux/seq_file.h>
13798 +#include <linux/statfs.h>
13799 +#include "aufs.h"
13800 +
13801 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13802 +{
13803 +       struct au_sbinfo *sbinfo;
13804 +       struct au_fhsm *fhsm;
13805 +
13806 +       SiMustAnyLock(sb);
13807 +
13808 +       sbinfo = au_sbi(sb);
13809 +       fhsm = &sbinfo->si_fhsm;
13810 +       AuDebugOn(!fhsm);
13811 +       return fhsm->fhsm_bottom;
13812 +}
13813 +
13814 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13815 +{
13816 +       struct au_sbinfo *sbinfo;
13817 +       struct au_fhsm *fhsm;
13818 +
13819 +       SiMustWriteLock(sb);
13820 +
13821 +       sbinfo = au_sbi(sb);
13822 +       fhsm = &sbinfo->si_fhsm;
13823 +       AuDebugOn(!fhsm);
13824 +       fhsm->fhsm_bottom = bindex;
13825 +}
13826 +
13827 +/* ---------------------------------------------------------------------- */
13828 +
13829 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13830 +{
13831 +       struct au_br_fhsm *bf;
13832 +
13833 +       bf = br->br_fhsm;
13834 +       MtxMustLock(&bf->bf_lock);
13835 +
13836 +       return !bf->bf_readable
13837 +               || time_after(jiffies,
13838 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13839 +}
13840 +
13841 +/* ---------------------------------------------------------------------- */
13842 +
13843 +static void au_fhsm_notify(struct super_block *sb, int val)
13844 +{
13845 +       struct au_sbinfo *sbinfo;
13846 +       struct au_fhsm *fhsm;
13847 +
13848 +       SiMustAnyLock(sb);
13849 +
13850 +       sbinfo = au_sbi(sb);
13851 +       fhsm = &sbinfo->si_fhsm;
13852 +       if (au_fhsm_pid(fhsm)
13853 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13854 +               atomic_set(&fhsm->fhsm_readable, val);
13855 +               if (val)
13856 +                       wake_up(&fhsm->fhsm_wqh);
13857 +       }
13858 +}
13859 +
13860 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13861 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13862 +{
13863 +       int err;
13864 +       struct au_branch *br;
13865 +       struct au_br_fhsm *bf;
13866 +
13867 +       br = au_sbr(sb, bindex);
13868 +       AuDebugOn(au_br_rdonly(br));
13869 +       bf = br->br_fhsm;
13870 +       AuDebugOn(!bf);
13871 +
13872 +       if (do_lock)
13873 +               mutex_lock(&bf->bf_lock);
13874 +       else
13875 +               MtxMustLock(&bf->bf_lock);
13876 +
13877 +       /* sb->s_root for NFS is unreliable */
13878 +       err = au_br_stfs(br, &bf->bf_stfs);
13879 +       if (unlikely(err)) {
13880 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13881 +               goto out;
13882 +       }
13883 +
13884 +       bf->bf_jiffy = jiffies;
13885 +       bf->bf_readable = 1;
13886 +       if (do_notify)
13887 +               au_fhsm_notify(sb, /*val*/1);
13888 +       if (rstfs)
13889 +               *rstfs = bf->bf_stfs;
13890 +
13891 +out:
13892 +       if (do_lock)
13893 +               mutex_unlock(&bf->bf_lock);
13894 +       au_fhsm_notify(sb, /*val*/1);
13895 +
13896 +       return err;
13897 +}
13898 +
13899 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13900 +{
13901 +       int err;
13902 +       struct au_sbinfo *sbinfo;
13903 +       struct au_fhsm *fhsm;
13904 +       struct au_branch *br;
13905 +       struct au_br_fhsm *bf;
13906 +
13907 +       AuDbg("b%d, force %d\n", bindex, force);
13908 +       SiMustAnyLock(sb);
13909 +
13910 +       sbinfo = au_sbi(sb);
13911 +       fhsm = &sbinfo->si_fhsm;
13912 +       if (!au_ftest_si(sbinfo, FHSM)
13913 +           || fhsm->fhsm_bottom == bindex)
13914 +               return;
13915 +
13916 +       br = au_sbr(sb, bindex);
13917 +       bf = br->br_fhsm;
13918 +       AuDebugOn(!bf);
13919 +       mutex_lock(&bf->bf_lock);
13920 +       if (force
13921 +           || au_fhsm_pid(fhsm)
13922 +           || au_fhsm_test_jiffy(sbinfo, br))
13923 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13924 +                                 /*do_notify*/1);
13925 +       mutex_unlock(&bf->bf_lock);
13926 +}
13927 +
13928 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13929 +{
13930 +       aufs_bindex_t bindex, bbot;
13931 +       struct au_branch *br;
13932 +
13933 +       /* exclude the bottom */
13934 +       bbot = au_fhsm_bottom(sb);
13935 +       for (bindex = 0; bindex < bbot; bindex++) {
13936 +               br = au_sbr(sb, bindex);
13937 +               if (au_br_fhsm(br->br_perm))
13938 +                       au_fhsm_wrote(sb, bindex, force);
13939 +       }
13940 +}
13941 +
13942 +/* ---------------------------------------------------------------------- */
13943 +
13944 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13945 +{
13946 +       __poll_t mask;
13947 +       struct au_sbinfo *sbinfo;
13948 +       struct au_fhsm *fhsm;
13949 +
13950 +       mask = 0;
13951 +       sbinfo = file->private_data;
13952 +       fhsm = &sbinfo->si_fhsm;
13953 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13954 +       if (atomic_read(&fhsm->fhsm_readable))
13955 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13956 +
13957 +       if (!mask)
13958 +               AuDbg("mask 0x%x\n", mask);
13959 +       return mask;
13960 +}
13961 +
13962 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13963 +                             struct aufs_stfs *stfs, __s16 brid)
13964 +{
13965 +       int err;
13966 +
13967 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13968 +       if (!err)
13969 +               err = __put_user(brid, &stbr->brid);
13970 +       if (unlikely(err))
13971 +               err = -EFAULT;
13972 +
13973 +       return err;
13974 +}
13975 +
13976 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13977 +                              struct aufs_stbr __user *stbr, size_t count)
13978 +{
13979 +       ssize_t err;
13980 +       int nstbr;
13981 +       aufs_bindex_t bindex, bbot;
13982 +       struct au_branch *br;
13983 +       struct au_br_fhsm *bf;
13984 +
13985 +       /* except the bottom branch */
13986 +       err = 0;
13987 +       nstbr = 0;
13988 +       bbot = au_fhsm_bottom(sb);
13989 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13990 +               br = au_sbr(sb, bindex);
13991 +               if (!au_br_fhsm(br->br_perm))
13992 +                       continue;
13993 +
13994 +               bf = br->br_fhsm;
13995 +               mutex_lock(&bf->bf_lock);
13996 +               if (bf->bf_readable) {
13997 +                       err = -EFAULT;
13998 +                       if (count >= sizeof(*stbr))
13999 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14000 +                                                         br->br_id);
14001 +                       if (!err) {
14002 +                               bf->bf_readable = 0;
14003 +                               count -= sizeof(*stbr);
14004 +                               nstbr++;
14005 +                       }
14006 +               }
14007 +               mutex_unlock(&bf->bf_lock);
14008 +       }
14009 +       if (!err)
14010 +               err = sizeof(*stbr) * nstbr;
14011 +
14012 +       return err;
14013 +}
14014 +
14015 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14016 +                          loff_t *pos)
14017 +{
14018 +       ssize_t err;
14019 +       int readable;
14020 +       aufs_bindex_t nfhsm, bindex, bbot;
14021 +       struct au_sbinfo *sbinfo;
14022 +       struct au_fhsm *fhsm;
14023 +       struct au_branch *br;
14024 +       struct super_block *sb;
14025 +
14026 +       err = 0;
14027 +       sbinfo = file->private_data;
14028 +       fhsm = &sbinfo->si_fhsm;
14029 +need_data:
14030 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
14031 +       if (!atomic_read(&fhsm->fhsm_readable)) {
14032 +               if (vfsub_file_flags(file) & O_NONBLOCK)
14033 +                       err = -EAGAIN;
14034 +               else
14035 +                       err = wait_event_interruptible_locked_irq
14036 +                               (fhsm->fhsm_wqh,
14037 +                                atomic_read(&fhsm->fhsm_readable));
14038 +       }
14039 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14040 +       if (unlikely(err))
14041 +               goto out;
14042 +
14043 +       /* sb may already be dead */
14044 +       au_rw_read_lock(&sbinfo->si_rwsem);
14045 +       readable = atomic_read(&fhsm->fhsm_readable);
14046 +       if (readable > 0) {
14047 +               sb = sbinfo->si_sb;
14048 +               AuDebugOn(!sb);
14049 +               /* exclude the bottom branch */
14050 +               nfhsm = 0;
14051 +               bbot = au_fhsm_bottom(sb);
14052 +               for (bindex = 0; bindex < bbot; bindex++) {
14053 +                       br = au_sbr(sb, bindex);
14054 +                       if (au_br_fhsm(br->br_perm))
14055 +                               nfhsm++;
14056 +               }
14057 +               err = -EMSGSIZE;
14058 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14059 +                       atomic_set(&fhsm->fhsm_readable, 0);
14060 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14061 +                                            count);
14062 +               }
14063 +       }
14064 +       au_rw_read_unlock(&sbinfo->si_rwsem);
14065 +       if (!readable)
14066 +               goto need_data;
14067 +
14068 +out:
14069 +       return err;
14070 +}
14071 +
14072 +static int au_fhsm_release(struct inode *inode, struct file *file)
14073 +{
14074 +       struct au_sbinfo *sbinfo;
14075 +       struct au_fhsm *fhsm;
14076 +
14077 +       /* sb may already be dead */
14078 +       sbinfo = file->private_data;
14079 +       fhsm = &sbinfo->si_fhsm;
14080 +       spin_lock(&fhsm->fhsm_spin);
14081 +       fhsm->fhsm_pid = 0;
14082 +       spin_unlock(&fhsm->fhsm_spin);
14083 +       kobject_put(&sbinfo->si_kobj);
14084 +
14085 +       return 0;
14086 +}
14087 +
14088 +static const struct file_operations au_fhsm_fops = {
14089 +       .owner          = THIS_MODULE,
14090 +       .llseek         = noop_llseek,
14091 +       .read           = au_fhsm_read,
14092 +       .poll           = au_fhsm_poll,
14093 +       .release        = au_fhsm_release
14094 +};
14095 +
14096 +int au_fhsm_fd(struct super_block *sb, int oflags)
14097 +{
14098 +       int err, fd;
14099 +       struct au_sbinfo *sbinfo;
14100 +       struct au_fhsm *fhsm;
14101 +
14102 +       err = -EPERM;
14103 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
14104 +               goto out;
14105 +
14106 +       err = -EINVAL;
14107 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14108 +               goto out;
14109 +
14110 +       err = 0;
14111 +       sbinfo = au_sbi(sb);
14112 +       fhsm = &sbinfo->si_fhsm;
14113 +       spin_lock(&fhsm->fhsm_spin);
14114 +       if (!fhsm->fhsm_pid)
14115 +               fhsm->fhsm_pid = current->pid;
14116 +       else
14117 +               err = -EBUSY;
14118 +       spin_unlock(&fhsm->fhsm_spin);
14119 +       if (unlikely(err))
14120 +               goto out;
14121 +
14122 +       oflags |= O_RDONLY;
14123 +       /* oflags |= FMODE_NONOTIFY; */
14124 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14125 +       err = fd;
14126 +       if (unlikely(fd < 0))
14127 +               goto out_pid;
14128 +
14129 +       /* succeed reglardless 'fhsm' status */
14130 +       kobject_get(&sbinfo->si_kobj);
14131 +       si_noflush_read_lock(sb);
14132 +       if (au_ftest_si(sbinfo, FHSM))
14133 +               au_fhsm_wrote_all(sb, /*force*/0);
14134 +       si_read_unlock(sb);
14135 +       goto out; /* success */
14136 +
14137 +out_pid:
14138 +       spin_lock(&fhsm->fhsm_spin);
14139 +       fhsm->fhsm_pid = 0;
14140 +       spin_unlock(&fhsm->fhsm_spin);
14141 +out:
14142 +       AuTraceErr(err);
14143 +       return err;
14144 +}
14145 +
14146 +/* ---------------------------------------------------------------------- */
14147 +
14148 +int au_fhsm_br_alloc(struct au_branch *br)
14149 +{
14150 +       int err;
14151 +
14152 +       err = 0;
14153 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14154 +       if (br->br_fhsm)
14155 +               au_br_fhsm_init(br->br_fhsm);
14156 +       else
14157 +               err = -ENOMEM;
14158 +
14159 +       return err;
14160 +}
14161 +
14162 +/* ---------------------------------------------------------------------- */
14163 +
14164 +void au_fhsm_fin(struct super_block *sb)
14165 +{
14166 +       au_fhsm_notify(sb, /*val*/-1);
14167 +}
14168 +
14169 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14170 +{
14171 +       struct au_fhsm *fhsm;
14172 +
14173 +       fhsm = &sbinfo->si_fhsm;
14174 +       spin_lock_init(&fhsm->fhsm_spin);
14175 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14176 +       atomic_set(&fhsm->fhsm_readable, 0);
14177 +       fhsm->fhsm_expire
14178 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14179 +       fhsm->fhsm_bottom = -1;
14180 +}
14181 +
14182 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14183 +{
14184 +       sbinfo->si_fhsm.fhsm_expire
14185 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14186 +}
14187 +
14188 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14189 +{
14190 +       unsigned int u;
14191 +
14192 +       if (!au_ftest_si(sbinfo, FHSM))
14193 +               return;
14194 +
14195 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14196 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14197 +               seq_printf(seq, ",fhsm_sec=%u", u);
14198 +}
14199 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14200 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14201 +++ linux/fs/aufs/file.c        2018-08-12 23:43:05.456791299 +0200
14202 @@ -0,0 +1,857 @@
14203 +// SPDX-License-Identifier: GPL-2.0
14204 +/*
14205 + * Copyright (C) 2005-2018 Junjiro R. Okajima
14206 + *
14207 + * This program, aufs is free software; you can redistribute it and/or modify
14208 + * it under the terms of the GNU General Public License as published by
14209 + * the Free Software Foundation; either version 2 of the License, or
14210 + * (at your option) any later version.
14211 + *
14212 + * This program is distributed in the hope that it will be useful,
14213 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14214 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14215 + * GNU General Public License for more details.
14216 + *
14217 + * You should have received a copy of the GNU General Public License
14218 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14219 + */
14220 +
14221 +/*
14222 + * handling file/dir, and address_space operation
14223 + */
14224 +
14225 +#ifdef CONFIG_AUFS_DEBUG
14226 +#include <linux/migrate.h>
14227 +#endif
14228 +#include <linux/pagemap.h>
14229 +#include "aufs.h"
14230 +
14231 +/* drop flags for writing */
14232 +unsigned int au_file_roflags(unsigned int flags)
14233 +{
14234 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14235 +       flags |= O_RDONLY | O_NOATIME;
14236 +       return flags;
14237 +}
14238 +
14239 +/* common functions to regular file and dir */
14240 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14241 +                      struct file *file, int force_wr)
14242 +{
14243 +       struct file *h_file;
14244 +       struct dentry *h_dentry;
14245 +       struct inode *h_inode;
14246 +       struct super_block *sb;
14247 +       struct au_branch *br;
14248 +       struct path h_path;
14249 +       int err;
14250 +
14251 +       /* a race condition can happen between open and unlink/rmdir */
14252 +       h_file = ERR_PTR(-ENOENT);
14253 +       h_dentry = au_h_dptr(dentry, bindex);
14254 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14255 +               goto out;
14256 +       h_inode = d_inode(h_dentry);
14257 +       spin_lock(&h_dentry->d_lock);
14258 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14259 +               /* || !d_inode(dentry)->i_nlink */
14260 +               ;
14261 +       spin_unlock(&h_dentry->d_lock);
14262 +       if (unlikely(err))
14263 +               goto out;
14264 +
14265 +       sb = dentry->d_sb;
14266 +       br = au_sbr(sb, bindex);
14267 +       err = au_br_test_oflag(flags, br);
14268 +       h_file = ERR_PTR(err);
14269 +       if (unlikely(err))
14270 +               goto out;
14271 +
14272 +       /* drop flags for writing */
14273 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14274 +               if (force_wr && !(flags & O_WRONLY))
14275 +                       force_wr = 0;
14276 +               flags = au_file_roflags(flags);
14277 +               if (force_wr) {
14278 +                       h_file = ERR_PTR(-EROFS);
14279 +                       flags = au_file_roflags(flags);
14280 +                       if (unlikely(vfsub_native_ro(h_inode)
14281 +                                    || IS_APPEND(h_inode)))
14282 +                               goto out;
14283 +                       flags &= ~O_ACCMODE;
14284 +                       flags |= O_WRONLY;
14285 +               }
14286 +       }
14287 +       flags &= ~O_CREAT;
14288 +       au_br_get(br);
14289 +       h_path.dentry = h_dentry;
14290 +       h_path.mnt = au_br_mnt(br);
14291 +       h_file = vfsub_dentry_open(&h_path, flags);
14292 +       if (IS_ERR(h_file))
14293 +               goto out_br;
14294 +
14295 +       if (flags & __FMODE_EXEC) {
14296 +               err = deny_write_access(h_file);
14297 +               if (unlikely(err)) {
14298 +                       fput(h_file);
14299 +                       h_file = ERR_PTR(err);
14300 +                       goto out_br;
14301 +               }
14302 +       }
14303 +       fsnotify_open(h_file);
14304 +       goto out; /* success */
14305 +
14306 +out_br:
14307 +       au_br_put(br);
14308 +out:
14309 +       return h_file;
14310 +}
14311 +
14312 +static int au_cmoo(struct dentry *dentry)
14313 +{
14314 +       int err, cmoo, matched;
14315 +       unsigned int udba;
14316 +       struct path h_path;
14317 +       struct au_pin pin;
14318 +       struct au_cp_generic cpg = {
14319 +               .dentry = dentry,
14320 +               .bdst   = -1,
14321 +               .bsrc   = -1,
14322 +               .len    = -1,
14323 +               .pin    = &pin,
14324 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14325 +       };
14326 +       struct inode *delegated;
14327 +       struct super_block *sb;
14328 +       struct au_sbinfo *sbinfo;
14329 +       struct au_fhsm *fhsm;
14330 +       pid_t pid;
14331 +       struct au_branch *br;
14332 +       struct dentry *parent;
14333 +       struct au_hinode *hdir;
14334 +
14335 +       DiMustWriteLock(dentry);
14336 +       IiMustWriteLock(d_inode(dentry));
14337 +
14338 +       err = 0;
14339 +       if (IS_ROOT(dentry))
14340 +               goto out;
14341 +       cpg.bsrc = au_dbtop(dentry);
14342 +       if (!cpg.bsrc)
14343 +               goto out;
14344 +
14345 +       sb = dentry->d_sb;
14346 +       sbinfo = au_sbi(sb);
14347 +       fhsm = &sbinfo->si_fhsm;
14348 +       pid = au_fhsm_pid(fhsm);
14349 +       rcu_read_lock();
14350 +       matched = (pid
14351 +                  && (current->pid == pid
14352 +                      || rcu_dereference(current->real_parent)->pid == pid));
14353 +       rcu_read_unlock();
14354 +       if (matched)
14355 +               goto out;
14356 +
14357 +       br = au_sbr(sb, cpg.bsrc);
14358 +       cmoo = au_br_cmoo(br->br_perm);
14359 +       if (!cmoo)
14360 +               goto out;
14361 +       if (!d_is_reg(dentry))
14362 +               cmoo &= AuBrAttr_COO_ALL;
14363 +       if (!cmoo)
14364 +               goto out;
14365 +
14366 +       parent = dget_parent(dentry);
14367 +       di_write_lock_parent(parent);
14368 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14369 +       cpg.bdst = err;
14370 +       if (unlikely(err < 0)) {
14371 +               err = 0;        /* there is no upper writable branch */
14372 +               goto out_dgrade;
14373 +       }
14374 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14375 +
14376 +       /* do not respect the coo attrib for the target branch */
14377 +       err = au_cpup_dirs(dentry, cpg.bdst);
14378 +       if (unlikely(err))
14379 +               goto out_dgrade;
14380 +
14381 +       di_downgrade_lock(parent, AuLock_IR);
14382 +       udba = au_opt_udba(sb);
14383 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14384 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14385 +       if (unlikely(err))
14386 +               goto out_parent;
14387 +
14388 +       err = au_sio_cpup_simple(&cpg);
14389 +       au_unpin(&pin);
14390 +       if (unlikely(err))
14391 +               goto out_parent;
14392 +       if (!(cmoo & AuBrWAttr_MOO))
14393 +               goto out_parent; /* success */
14394 +
14395 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14396 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14397 +       if (unlikely(err))
14398 +               goto out_parent;
14399 +
14400 +       h_path.mnt = au_br_mnt(br);
14401 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14402 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14403 +       delegated = NULL;
14404 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14405 +       au_unpin(&pin);
14406 +       /* todo: keep h_dentry or not? */
14407 +       if (unlikely(err == -EWOULDBLOCK)) {
14408 +               pr_warn("cannot retry for NFSv4 delegation"
14409 +                       " for an internal unlink\n");
14410 +               iput(delegated);
14411 +       }
14412 +       if (unlikely(err)) {
14413 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14414 +                      dentry, err);
14415 +               err = 0;
14416 +       }
14417 +       goto out_parent; /* success */
14418 +
14419 +out_dgrade:
14420 +       di_downgrade_lock(parent, AuLock_IR);
14421 +out_parent:
14422 +       di_read_unlock(parent, AuLock_IR);
14423 +       dput(parent);
14424 +out:
14425 +       AuTraceErr(err);
14426 +       return err;
14427 +}
14428 +
14429 +int au_do_open(struct file *file, struct au_do_open_args *args)
14430 +{
14431 +       int err, aopen = args->aopen;
14432 +       struct dentry *dentry;
14433 +       struct au_finfo *finfo;
14434 +
14435 +       if (!aopen)
14436 +               err = au_finfo_init(file, args->fidir);
14437 +       else {
14438 +               lockdep_off();
14439 +               err = au_finfo_init(file, args->fidir);
14440 +               lockdep_on();
14441 +       }
14442 +       if (unlikely(err))
14443 +               goto out;
14444 +
14445 +       dentry = file->f_path.dentry;
14446 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14447 +       di_write_lock_child(dentry);
14448 +       err = au_cmoo(dentry);
14449 +       di_downgrade_lock(dentry, AuLock_IR);
14450 +       if (!err) {
14451 +               if (!aopen)
14452 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14453 +               else {
14454 +                       lockdep_off();
14455 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14456 +                       lockdep_on();
14457 +               }
14458 +       }
14459 +       di_read_unlock(dentry, AuLock_IR);
14460 +
14461 +       finfo = au_fi(file);
14462 +       if (!err) {
14463 +               finfo->fi_file = file;
14464 +               au_hbl_add(&finfo->fi_hlist,
14465 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14466 +       }
14467 +       if (!aopen)
14468 +               fi_write_unlock(file);
14469 +       else {
14470 +               lockdep_off();
14471 +               fi_write_unlock(file);
14472 +               lockdep_on();
14473 +       }
14474 +       if (unlikely(err)) {
14475 +               finfo->fi_hdir = NULL;
14476 +               au_finfo_fin(file);
14477 +       }
14478 +
14479 +out:
14480 +       AuTraceErr(err);
14481 +       return err;
14482 +}
14483 +
14484 +int au_reopen_nondir(struct file *file)
14485 +{
14486 +       int err;
14487 +       aufs_bindex_t btop;
14488 +       struct dentry *dentry;
14489 +       struct file *h_file, *h_file_tmp;
14490 +
14491 +       dentry = file->f_path.dentry;
14492 +       btop = au_dbtop(dentry);
14493 +       h_file_tmp = NULL;
14494 +       if (au_fbtop(file) == btop) {
14495 +               h_file = au_hf_top(file);
14496 +               if (file->f_mode == h_file->f_mode)
14497 +                       return 0; /* success */
14498 +               h_file_tmp = h_file;
14499 +               get_file(h_file_tmp);
14500 +               au_set_h_fptr(file, btop, NULL);
14501 +       }
14502 +       AuDebugOn(au_fi(file)->fi_hdir);
14503 +       /*
14504 +        * it can happen
14505 +        * file exists on both of rw and ro
14506 +        * open --> dbtop and fbtop are both 0
14507 +        * prepend a branch as rw, "rw" become ro
14508 +        * remove rw/file
14509 +        * delete the top branch, "rw" becomes rw again
14510 +        *      --> dbtop is 1, fbtop is still 0
14511 +        * write --> fbtop is 0 but dbtop is 1
14512 +        */
14513 +       /* AuDebugOn(au_fbtop(file) < btop); */
14514 +
14515 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14516 +                          file, /*force_wr*/0);
14517 +       err = PTR_ERR(h_file);
14518 +       if (IS_ERR(h_file)) {
14519 +               if (h_file_tmp) {
14520 +                       au_sbr_get(dentry->d_sb, btop);
14521 +                       au_set_h_fptr(file, btop, h_file_tmp);
14522 +                       h_file_tmp = NULL;
14523 +               }
14524 +               goto out; /* todo: close all? */
14525 +       }
14526 +
14527 +       err = 0;
14528 +       au_set_fbtop(file, btop);
14529 +       au_set_h_fptr(file, btop, h_file);
14530 +       au_update_figen(file);
14531 +       /* todo: necessary? */
14532 +       /* file->f_ra = h_file->f_ra; */
14533 +
14534 +out:
14535 +       if (h_file_tmp)
14536 +               fput(h_file_tmp);
14537 +       return err;
14538 +}
14539 +
14540 +/* ---------------------------------------------------------------------- */
14541 +
14542 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14543 +                       struct dentry *hi_wh)
14544 +{
14545 +       int err;
14546 +       aufs_bindex_t btop;
14547 +       struct au_dinfo *dinfo;
14548 +       struct dentry *h_dentry;
14549 +       struct au_hdentry *hdp;
14550 +
14551 +       dinfo = au_di(file->f_path.dentry);
14552 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14553 +
14554 +       btop = dinfo->di_btop;
14555 +       dinfo->di_btop = btgt;
14556 +       hdp = au_hdentry(dinfo, btgt);
14557 +       h_dentry = hdp->hd_dentry;
14558 +       hdp->hd_dentry = hi_wh;
14559 +       err = au_reopen_nondir(file);
14560 +       hdp->hd_dentry = h_dentry;
14561 +       dinfo->di_btop = btop;
14562 +
14563 +       return err;
14564 +}
14565 +
14566 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14567 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14568 +{
14569 +       int err;
14570 +       struct inode *inode, *h_inode;
14571 +       struct dentry *h_dentry, *hi_wh;
14572 +       struct au_cp_generic cpg = {
14573 +               .dentry = file->f_path.dentry,
14574 +               .bdst   = bcpup,
14575 +               .bsrc   = -1,
14576 +               .len    = len,
14577 +               .pin    = pin
14578 +       };
14579 +
14580 +       au_update_dbtop(cpg.dentry);
14581 +       inode = d_inode(cpg.dentry);
14582 +       h_inode = NULL;
14583 +       if (au_dbtop(cpg.dentry) <= bcpup
14584 +           && au_dbbot(cpg.dentry) >= bcpup) {
14585 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14586 +               if (h_dentry && d_is_positive(h_dentry))
14587 +                       h_inode = d_inode(h_dentry);
14588 +       }
14589 +       hi_wh = au_hi_wh(inode, bcpup);
14590 +       if (!hi_wh && !h_inode)
14591 +               err = au_sio_cpup_wh(&cpg, file);
14592 +       else
14593 +               /* already copied-up after unlink */
14594 +               err = au_reopen_wh(file, bcpup, hi_wh);
14595 +
14596 +       if (!err
14597 +           && (inode->i_nlink > 1
14598 +               || (inode->i_state & I_LINKABLE))
14599 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14600 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14601 +
14602 +       return err;
14603 +}
14604 +
14605 +/*
14606 + * prepare the @file for writing.
14607 + */
14608 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14609 +{
14610 +       int err;
14611 +       aufs_bindex_t dbtop;
14612 +       struct dentry *parent;
14613 +       struct inode *inode;
14614 +       struct super_block *sb;
14615 +       struct file *h_file;
14616 +       struct au_cp_generic cpg = {
14617 +               .dentry = file->f_path.dentry,
14618 +               .bdst   = -1,
14619 +               .bsrc   = -1,
14620 +               .len    = len,
14621 +               .pin    = pin,
14622 +               .flags  = AuCpup_DTIME
14623 +       };
14624 +
14625 +       sb = cpg.dentry->d_sb;
14626 +       inode = d_inode(cpg.dentry);
14627 +       cpg.bsrc = au_fbtop(file);
14628 +       err = au_test_ro(sb, cpg.bsrc, inode);
14629 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14630 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14631 +                            /*flags*/0);
14632 +               goto out;
14633 +       }
14634 +
14635 +       /* need to cpup or reopen */
14636 +       parent = dget_parent(cpg.dentry);
14637 +       di_write_lock_parent(parent);
14638 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14639 +       cpg.bdst = err;
14640 +       if (unlikely(err < 0))
14641 +               goto out_dgrade;
14642 +       err = 0;
14643 +
14644 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14645 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14646 +               if (unlikely(err))
14647 +                       goto out_dgrade;
14648 +       }
14649 +
14650 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14651 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14652 +       if (unlikely(err))
14653 +               goto out_dgrade;
14654 +
14655 +       dbtop = au_dbtop(cpg.dentry);
14656 +       if (dbtop <= cpg.bdst)
14657 +               cpg.bsrc = cpg.bdst;
14658 +
14659 +       if (dbtop <= cpg.bdst           /* just reopen */
14660 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14661 +               ) {
14662 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14663 +               if (IS_ERR(h_file))
14664 +                       err = PTR_ERR(h_file);
14665 +               else {
14666 +                       di_downgrade_lock(parent, AuLock_IR);
14667 +                       if (dbtop > cpg.bdst)
14668 +                               err = au_sio_cpup_simple(&cpg);
14669 +                       if (!err)
14670 +                               err = au_reopen_nondir(file);
14671 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14672 +               }
14673 +       } else {                        /* copyup as wh and reopen */
14674 +               /*
14675 +                * since writable hfsplus branch is not supported,
14676 +                * h_open_pre/post() are unnecessary.
14677 +                */
14678 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14679 +               di_downgrade_lock(parent, AuLock_IR);
14680 +       }
14681 +
14682 +       if (!err) {
14683 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14684 +               goto out_dput; /* success */
14685 +       }
14686 +       au_unpin(pin);
14687 +       goto out_unlock;
14688 +
14689 +out_dgrade:
14690 +       di_downgrade_lock(parent, AuLock_IR);
14691 +out_unlock:
14692 +       di_read_unlock(parent, AuLock_IR);
14693 +out_dput:
14694 +       dput(parent);
14695 +out:
14696 +       return err;
14697 +}
14698 +
14699 +/* ---------------------------------------------------------------------- */
14700 +
14701 +int au_do_flush(struct file *file, fl_owner_t id,
14702 +               int (*flush)(struct file *file, fl_owner_t id))
14703 +{
14704 +       int err;
14705 +       struct super_block *sb;
14706 +       struct inode *inode;
14707 +
14708 +       inode = file_inode(file);
14709 +       sb = inode->i_sb;
14710 +       si_noflush_read_lock(sb);
14711 +       fi_read_lock(file);
14712 +       ii_read_lock_child(inode);
14713 +
14714 +       err = flush(file, id);
14715 +       au_cpup_attr_timesizes(inode);
14716 +
14717 +       ii_read_unlock(inode);
14718 +       fi_read_unlock(file);
14719 +       si_read_unlock(sb);
14720 +       return err;
14721 +}
14722 +
14723 +/* ---------------------------------------------------------------------- */
14724 +
14725 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14726 +{
14727 +       int err;
14728 +       struct au_pin pin;
14729 +       struct au_finfo *finfo;
14730 +       struct dentry *parent, *hi_wh;
14731 +       struct inode *inode;
14732 +       struct super_block *sb;
14733 +       struct au_cp_generic cpg = {
14734 +               .dentry = file->f_path.dentry,
14735 +               .bdst   = -1,
14736 +               .bsrc   = -1,
14737 +               .len    = -1,
14738 +               .pin    = &pin,
14739 +               .flags  = AuCpup_DTIME
14740 +       };
14741 +
14742 +       FiMustWriteLock(file);
14743 +
14744 +       err = 0;
14745 +       finfo = au_fi(file);
14746 +       sb = cpg.dentry->d_sb;
14747 +       inode = d_inode(cpg.dentry);
14748 +       cpg.bdst = au_ibtop(inode);
14749 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14750 +               goto out;
14751 +
14752 +       parent = dget_parent(cpg.dentry);
14753 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14754 +               di_read_lock_parent(parent, !AuLock_IR);
14755 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14756 +               cpg.bdst = err;
14757 +               di_read_unlock(parent, !AuLock_IR);
14758 +               if (unlikely(err < 0))
14759 +                       goto out_parent;
14760 +               err = 0;
14761 +       }
14762 +
14763 +       di_read_lock_parent(parent, AuLock_IR);
14764 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14765 +       if (!S_ISDIR(inode->i_mode)
14766 +           && au_opt_test(au_mntflags(sb), PLINK)
14767 +           && au_plink_test(inode)
14768 +           && !d_unhashed(cpg.dentry)
14769 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14770 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14771 +               if (unlikely(err))
14772 +                       goto out_unlock;
14773 +
14774 +               /* always superio. */
14775 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14776 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14777 +               if (!err) {
14778 +                       err = au_sio_cpup_simple(&cpg);
14779 +                       au_unpin(&pin);
14780 +               }
14781 +       } else if (hi_wh) {
14782 +               /* already copied-up after unlink */
14783 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14784 +               *need_reopen = 0;
14785 +       }
14786 +
14787 +out_unlock:
14788 +       di_read_unlock(parent, AuLock_IR);
14789 +out_parent:
14790 +       dput(parent);
14791 +out:
14792 +       return err;
14793 +}
14794 +
14795 +static void au_do_refresh_dir(struct file *file)
14796 +{
14797 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14798 +       struct au_hfile *p, tmp, *q;
14799 +       struct au_finfo *finfo;
14800 +       struct super_block *sb;
14801 +       struct au_fidir *fidir;
14802 +
14803 +       FiMustWriteLock(file);
14804 +
14805 +       sb = file->f_path.dentry->d_sb;
14806 +       finfo = au_fi(file);
14807 +       fidir = finfo->fi_hdir;
14808 +       AuDebugOn(!fidir);
14809 +       p = fidir->fd_hfile + finfo->fi_btop;
14810 +       brid = p->hf_br->br_id;
14811 +       bbot = fidir->fd_bbot;
14812 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14813 +               if (!p->hf_file)
14814 +                       continue;
14815 +
14816 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14817 +               if (new_bindex == bindex)
14818 +                       continue;
14819 +               if (new_bindex < 0) {
14820 +                       au_set_h_fptr(file, bindex, NULL);
14821 +                       continue;
14822 +               }
14823 +
14824 +               /* swap two lower inode, and loop again */
14825 +               q = fidir->fd_hfile + new_bindex;
14826 +               tmp = *q;
14827 +               *q = *p;
14828 +               *p = tmp;
14829 +               if (tmp.hf_file) {
14830 +                       bindex--;
14831 +                       p--;
14832 +               }
14833 +       }
14834 +
14835 +       p = fidir->fd_hfile;
14836 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14837 +               bbot = au_sbbot(sb);
14838 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14839 +                    finfo->fi_btop++, p++)
14840 +                       if (p->hf_file) {
14841 +                               if (file_inode(p->hf_file))
14842 +                                       break;
14843 +                               au_hfput(p, /*execed*/0);
14844 +                       }
14845 +       } else {
14846 +               bbot = au_br_index(sb, brid);
14847 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14848 +                    finfo->fi_btop++, p++)
14849 +                       if (p->hf_file)
14850 +                               au_hfput(p, /*execed*/0);
14851 +               bbot = au_sbbot(sb);
14852 +       }
14853 +
14854 +       p = fidir->fd_hfile + bbot;
14855 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14856 +            fidir->fd_bbot--, p--)
14857 +               if (p->hf_file) {
14858 +                       if (file_inode(p->hf_file))
14859 +                               break;
14860 +                       au_hfput(p, /*execed*/0);
14861 +               }
14862 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14863 +}
14864 +
14865 +/*
14866 + * after branch manipulating, refresh the file.
14867 + */
14868 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14869 +{
14870 +       int err, need_reopen, nbr;
14871 +       aufs_bindex_t bbot, bindex;
14872 +       struct dentry *dentry;
14873 +       struct super_block *sb;
14874 +       struct au_finfo *finfo;
14875 +       struct au_hfile *hfile;
14876 +
14877 +       dentry = file->f_path.dentry;
14878 +       sb = dentry->d_sb;
14879 +       nbr = au_sbbot(sb) + 1;
14880 +       finfo = au_fi(file);
14881 +       if (!finfo->fi_hdir) {
14882 +               hfile = &finfo->fi_htop;
14883 +               AuDebugOn(!hfile->hf_file);
14884 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14885 +               AuDebugOn(bindex < 0);
14886 +               if (bindex != finfo->fi_btop)
14887 +                       au_set_fbtop(file, bindex);
14888 +       } else {
14889 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14890 +               if (unlikely(err))
14891 +                       goto out;
14892 +               au_do_refresh_dir(file);
14893 +       }
14894 +
14895 +       err = 0;
14896 +       need_reopen = 1;
14897 +       if (!au_test_mmapped(file))
14898 +               err = au_file_refresh_by_inode(file, &need_reopen);
14899 +       if (finfo->fi_hdir)
14900 +               /* harmless if err */
14901 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14902 +       if (!err && need_reopen && !d_unlinked(dentry))
14903 +               err = reopen(file);
14904 +       if (!err) {
14905 +               au_update_figen(file);
14906 +               goto out; /* success */
14907 +       }
14908 +
14909 +       /* error, close all lower files */
14910 +       if (finfo->fi_hdir) {
14911 +               bbot = au_fbbot_dir(file);
14912 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14913 +                       au_set_h_fptr(file, bindex, NULL);
14914 +       }
14915 +
14916 +out:
14917 +       return err;
14918 +}
14919 +
14920 +/* common function to regular file and dir */
14921 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14922 +                         int wlock, unsigned int fi_lsc)
14923 +{
14924 +       int err;
14925 +       unsigned int sigen, figen;
14926 +       aufs_bindex_t btop;
14927 +       unsigned char pseudo_link;
14928 +       struct dentry *dentry;
14929 +       struct inode *inode;
14930 +
14931 +       err = 0;
14932 +       dentry = file->f_path.dentry;
14933 +       inode = d_inode(dentry);
14934 +       sigen = au_sigen(dentry->d_sb);
14935 +       fi_write_lock_nested(file, fi_lsc);
14936 +       figen = au_figen(file);
14937 +       if (!fi_lsc)
14938 +               di_write_lock_child(dentry);
14939 +       else
14940 +               di_write_lock_child2(dentry);
14941 +       btop = au_dbtop(dentry);
14942 +       pseudo_link = (btop != au_ibtop(inode));
14943 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14944 +               if (!wlock) {
14945 +                       di_downgrade_lock(dentry, AuLock_IR);
14946 +                       fi_downgrade_lock(file);
14947 +               }
14948 +               goto out; /* success */
14949 +       }
14950 +
14951 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14952 +       if (au_digen_test(dentry, sigen)) {
14953 +               err = au_reval_dpath(dentry, sigen);
14954 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14955 +       }
14956 +
14957 +       if (!err)
14958 +               err = refresh_file(file, reopen);
14959 +       if (!err) {
14960 +               if (!wlock) {
14961 +                       di_downgrade_lock(dentry, AuLock_IR);
14962 +                       fi_downgrade_lock(file);
14963 +               }
14964 +       } else {
14965 +               di_write_unlock(dentry);
14966 +               fi_write_unlock(file);
14967 +       }
14968 +
14969 +out:
14970 +       return err;
14971 +}
14972 +
14973 +/* ---------------------------------------------------------------------- */
14974 +
14975 +/* cf. aufs_nopage() */
14976 +/* for madvise(2) */
14977 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14978 +{
14979 +       unlock_page(page);
14980 +       return 0;
14981 +}
14982 +
14983 +/* it will never be called, but necessary to support O_DIRECT */
14984 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14985 +{ BUG(); return 0; }
14986 +
14987 +/* they will never be called. */
14988 +#ifdef CONFIG_AUFS_DEBUG
14989 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14990 +                           loff_t pos, unsigned len, unsigned flags,
14991 +                           struct page **pagep, void **fsdata)
14992 +{ AuUnsupport(); return 0; }
14993 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14994 +                         loff_t pos, unsigned len, unsigned copied,
14995 +                         struct page *page, void *fsdata)
14996 +{ AuUnsupport(); return 0; }
14997 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14998 +{ AuUnsupport(); return 0; }
14999 +
15000 +static int aufs_set_page_dirty(struct page *page)
15001 +{ AuUnsupport(); return 0; }
15002 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
15003 +                               unsigned int length)
15004 +{ AuUnsupport(); }
15005 +static int aufs_releasepage(struct page *page, gfp_t gfp)
15006 +{ AuUnsupport(); return 0; }
15007 +#if 0 /* called by memory compaction regardless file */
15008 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
15009 +                           struct page *page, enum migrate_mode mode)
15010 +{ AuUnsupport(); return 0; }
15011 +#endif
15012 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15013 +{ AuUnsupport(); return true; }
15014 +static void aufs_putback_page(struct page *page)
15015 +{ AuUnsupport(); }
15016 +static int aufs_launder_page(struct page *page)
15017 +{ AuUnsupport(); return 0; }
15018 +static int aufs_is_partially_uptodate(struct page *page,
15019 +                                     unsigned long from,
15020 +                                     unsigned long count)
15021 +{ AuUnsupport(); return 0; }
15022 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15023 +                                   bool *writeback)
15024 +{ AuUnsupport(); }
15025 +static int aufs_error_remove_page(struct address_space *mapping,
15026 +                                 struct page *page)
15027 +{ AuUnsupport(); return 0; }
15028 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15029 +                             sector_t *span)
15030 +{ AuUnsupport(); return 0; }
15031 +static void aufs_swap_deactivate(struct file *file)
15032 +{ AuUnsupport(); }
15033 +#endif /* CONFIG_AUFS_DEBUG */
15034 +
15035 +const struct address_space_operations aufs_aop = {
15036 +       .readpage               = aufs_readpage,
15037 +       .direct_IO              = aufs_direct_IO,
15038 +#ifdef CONFIG_AUFS_DEBUG
15039 +       .writepage              = aufs_writepage,
15040 +       /* no writepages, because of writepage */
15041 +       .set_page_dirty         = aufs_set_page_dirty,
15042 +       /* no readpages, because of readpage */
15043 +       .write_begin            = aufs_write_begin,
15044 +       .write_end              = aufs_write_end,
15045 +       /* no bmap, no block device */
15046 +       .invalidatepage         = aufs_invalidatepage,
15047 +       .releasepage            = aufs_releasepage,
15048 +       /* is fallback_migrate_page ok? */
15049 +       /* .migratepage         = aufs_migratepage, */
15050 +       .isolate_page           = aufs_isolate_page,
15051 +       .putback_page           = aufs_putback_page,
15052 +       .launder_page           = aufs_launder_page,
15053 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
15054 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
15055 +       .error_remove_page      = aufs_error_remove_page,
15056 +       .swap_activate          = aufs_swap_activate,
15057 +       .swap_deactivate        = aufs_swap_deactivate
15058 +#endif /* CONFIG_AUFS_DEBUG */
15059 +};
15060 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15061 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
15062 +++ linux/fs/aufs/file.h        2018-08-12 23:43:05.456791299 +0200
15063 @@ -0,0 +1,341 @@
15064 +/* SPDX-License-Identifier: GPL-2.0 */
15065 +/*
15066 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15067 + *
15068 + * This program, aufs is free software; you can redistribute it and/or modify
15069 + * it under the terms of the GNU General Public License as published by
15070 + * the Free Software Foundation; either version 2 of the License, or
15071 + * (at your option) any later version.
15072 + *
15073 + * This program is distributed in the hope that it will be useful,
15074 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15075 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15076 + * GNU General Public License for more details.
15077 + *
15078 + * You should have received a copy of the GNU General Public License
15079 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15080 + */
15081 +
15082 +/*
15083 + * file operations
15084 + */
15085 +
15086 +#ifndef __AUFS_FILE_H__
15087 +#define __AUFS_FILE_H__
15088 +
15089 +#ifdef __KERNEL__
15090 +
15091 +#include <linux/file.h>
15092 +#include <linux/fs.h>
15093 +#include <linux/mm_types.h>
15094 +#include <linux/poll.h>
15095 +#include "rwsem.h"
15096 +
15097 +struct au_branch;
15098 +struct au_hfile {
15099 +       struct file             *hf_file;
15100 +       struct au_branch        *hf_br;
15101 +};
15102 +
15103 +struct au_vdir;
15104 +struct au_fidir {
15105 +       aufs_bindex_t           fd_bbot;
15106 +       aufs_bindex_t           fd_nent;
15107 +       struct au_vdir          *fd_vdir_cache;
15108 +       struct au_hfile         fd_hfile[];
15109 +};
15110 +
15111 +static inline int au_fidir_sz(int nent)
15112 +{
15113 +       AuDebugOn(nent < 0);
15114 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15115 +}
15116 +
15117 +struct au_finfo {
15118 +       atomic_t                fi_generation;
15119 +
15120 +       struct au_rwsem         fi_rwsem;
15121 +       aufs_bindex_t           fi_btop;
15122 +
15123 +       /* do not union them */
15124 +       struct {                                /* for non-dir */
15125 +               struct au_hfile                 fi_htop;
15126 +               atomic_t                        fi_mmapped;
15127 +       };
15128 +       struct au_fidir         *fi_hdir;       /* for dir only */
15129 +
15130 +       struct hlist_bl_node    fi_hlist;
15131 +       struct file             *fi_file;       /* very ugly */
15132 +} ____cacheline_aligned_in_smp;
15133 +
15134 +/* ---------------------------------------------------------------------- */
15135 +
15136 +/* file.c */
15137 +extern const struct address_space_operations aufs_aop;
15138 +unsigned int au_file_roflags(unsigned int flags);
15139 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15140 +                      struct file *file, int force_wr);
15141 +struct au_do_open_args {
15142 +       int             aopen;
15143 +       int             (*open)(struct file *file, int flags,
15144 +                               struct file *h_file);
15145 +       struct au_fidir *fidir;
15146 +       struct file     *h_file;
15147 +};
15148 +int au_do_open(struct file *file, struct au_do_open_args *args);
15149 +int au_reopen_nondir(struct file *file);
15150 +struct au_pin;
15151 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15152 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15153 +                         int wlock, unsigned int fi_lsc);
15154 +int au_do_flush(struct file *file, fl_owner_t id,
15155 +               int (*flush)(struct file *file, fl_owner_t id));
15156 +
15157 +/* poll.c */
15158 +#ifdef CONFIG_AUFS_POLL
15159 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15160 +#endif
15161 +
15162 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15163 +/* hfsplus.c */
15164 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15165 +                          int force_wr);
15166 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15167 +                   struct file *h_file);
15168 +#else
15169 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15170 +       aufs_bindex_t bindex, int force_wr)
15171 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15172 +          struct file *h_file);
15173 +#endif
15174 +
15175 +/* f_op.c */
15176 +extern const struct file_operations aufs_file_fop;
15177 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15178 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15179 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15180 +
15181 +/* finfo.c */
15182 +void au_hfput(struct au_hfile *hf, int execed);
15183 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15184 +                  struct file *h_file);
15185 +
15186 +void au_update_figen(struct file *file);
15187 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15188 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15189 +
15190 +void au_fi_init_once(void *_fi);
15191 +void au_finfo_fin(struct file *file);
15192 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15193 +
15194 +/* ioctl.c */
15195 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15196 +#ifdef CONFIG_COMPAT
15197 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15198 +                          unsigned long arg);
15199 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15200 +                             unsigned long arg);
15201 +#endif
15202 +
15203 +/* ---------------------------------------------------------------------- */
15204 +
15205 +static inline struct au_finfo *au_fi(struct file *file)
15206 +{
15207 +       return file->private_data;
15208 +}
15209 +
15210 +/* ---------------------------------------------------------------------- */
15211 +
15212 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15213 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15214 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15215 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15216 +/*
15217 +#define fi_read_trylock_nested(f) \
15218 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15219 +#define fi_write_trylock_nested(f) \
15220 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15221 +*/
15222 +
15223 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15224 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15225 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15226 +
15227 +/* lock subclass for finfo */
15228 +enum {
15229 +       AuLsc_FI_1,
15230 +       AuLsc_FI_2
15231 +};
15232 +
15233 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15234 +{
15235 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15236 +}
15237 +
15238 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15239 +{
15240 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15241 +}
15242 +
15243 +/*
15244 + * fi_read_lock_1, fi_write_lock_1,
15245 + * fi_read_lock_2, fi_write_lock_2
15246 + */
15247 +#define AuReadLockFunc(name) \
15248 +static inline void fi_read_lock_##name(struct file *f) \
15249 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15250 +
15251 +#define AuWriteLockFunc(name) \
15252 +static inline void fi_write_lock_##name(struct file *f) \
15253 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15254 +
15255 +#define AuRWLockFuncs(name) \
15256 +       AuReadLockFunc(name) \
15257 +       AuWriteLockFunc(name)
15258 +
15259 +AuRWLockFuncs(1);
15260 +AuRWLockFuncs(2);
15261 +
15262 +#undef AuReadLockFunc
15263 +#undef AuWriteLockFunc
15264 +#undef AuRWLockFuncs
15265 +
15266 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15267 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15268 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15269 +
15270 +/* ---------------------------------------------------------------------- */
15271 +
15272 +/* todo: hard/soft set? */
15273 +static inline aufs_bindex_t au_fbtop(struct file *file)
15274 +{
15275 +       FiMustAnyLock(file);
15276 +       return au_fi(file)->fi_btop;
15277 +}
15278 +
15279 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15280 +{
15281 +       FiMustAnyLock(file);
15282 +       AuDebugOn(!au_fi(file)->fi_hdir);
15283 +       return au_fi(file)->fi_hdir->fd_bbot;
15284 +}
15285 +
15286 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15287 +{
15288 +       FiMustAnyLock(file);
15289 +       AuDebugOn(!au_fi(file)->fi_hdir);
15290 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15291 +}
15292 +
15293 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15294 +{
15295 +       FiMustWriteLock(file);
15296 +       au_fi(file)->fi_btop = bindex;
15297 +}
15298 +
15299 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15300 +{
15301 +       FiMustWriteLock(file);
15302 +       AuDebugOn(!au_fi(file)->fi_hdir);
15303 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15304 +}
15305 +
15306 +static inline void au_set_fvdir_cache(struct file *file,
15307 +                                     struct au_vdir *vdir_cache)
15308 +{
15309 +       FiMustWriteLock(file);
15310 +       AuDebugOn(!au_fi(file)->fi_hdir);
15311 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15312 +}
15313 +
15314 +static inline struct file *au_hf_top(struct file *file)
15315 +{
15316 +       FiMustAnyLock(file);
15317 +       AuDebugOn(au_fi(file)->fi_hdir);
15318 +       return au_fi(file)->fi_htop.hf_file;
15319 +}
15320 +
15321 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15322 +{
15323 +       FiMustAnyLock(file);
15324 +       AuDebugOn(!au_fi(file)->fi_hdir);
15325 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15326 +}
15327 +
15328 +/* todo: memory barrier? */
15329 +static inline unsigned int au_figen(struct file *f)
15330 +{
15331 +       return atomic_read(&au_fi(f)->fi_generation);
15332 +}
15333 +
15334 +static inline void au_set_mmapped(struct file *f)
15335 +{
15336 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15337 +               return;
15338 +       pr_warn("fi_mmapped wrapped around\n");
15339 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15340 +               ;
15341 +}
15342 +
15343 +static inline void au_unset_mmapped(struct file *f)
15344 +{
15345 +       atomic_dec(&au_fi(f)->fi_mmapped);
15346 +}
15347 +
15348 +static inline int au_test_mmapped(struct file *f)
15349 +{
15350 +       return atomic_read(&au_fi(f)->fi_mmapped);
15351 +}
15352 +
15353 +/* customize vma->vm_file */
15354 +
15355 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15356 +                                      struct file *file)
15357 +{
15358 +       struct file *f;
15359 +
15360 +       f = vma->vm_file;
15361 +       get_file(file);
15362 +       vma->vm_file = file;
15363 +       fput(f);
15364 +}
15365 +
15366 +#ifdef CONFIG_MMU
15367 +#define AuDbgVmRegion(file, vma) do {} while (0)
15368 +
15369 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15370 +                                   struct file *file)
15371 +{
15372 +       au_do_vm_file_reset(vma, file);
15373 +}
15374 +#else
15375 +#define AuDbgVmRegion(file, vma) \
15376 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15377 +
15378 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15379 +                                   struct file *file)
15380 +{
15381 +       struct file *f;
15382 +
15383 +       au_do_vm_file_reset(vma, file);
15384 +       f = vma->vm_region->vm_file;
15385 +       get_file(file);
15386 +       vma->vm_region->vm_file = file;
15387 +       fput(f);
15388 +}
15389 +#endif /* CONFIG_MMU */
15390 +
15391 +/* handle vma->vm_prfile */
15392 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15393 +                                   struct file *file)
15394 +{
15395 +       get_file(file);
15396 +       vma->vm_prfile = file;
15397 +#ifndef CONFIG_MMU
15398 +       get_file(file);
15399 +       vma->vm_region->vm_prfile = file;
15400 +#endif
15401 +}
15402 +
15403 +#endif /* __KERNEL__ */
15404 +#endif /* __AUFS_FILE_H__ */
15405 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15406 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15407 +++ linux/fs/aufs/finfo.c       2018-08-12 23:43:05.456791299 +0200
15408 @@ -0,0 +1,149 @@
15409 +// SPDX-License-Identifier: GPL-2.0
15410 +/*
15411 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15412 + *
15413 + * This program, aufs is free software; you can redistribute it and/or modify
15414 + * it under the terms of the GNU General Public License as published by
15415 + * the Free Software Foundation; either version 2 of the License, or
15416 + * (at your option) any later version.
15417 + *
15418 + * This program is distributed in the hope that it will be useful,
15419 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15420 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15421 + * GNU General Public License for more details.
15422 + *
15423 + * You should have received a copy of the GNU General Public License
15424 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15425 + */
15426 +
15427 +/*
15428 + * file private data
15429 + */
15430 +
15431 +#include "aufs.h"
15432 +
15433 +void au_hfput(struct au_hfile *hf, int execed)
15434 +{
15435 +       if (execed)
15436 +               allow_write_access(hf->hf_file);
15437 +       fput(hf->hf_file);
15438 +       hf->hf_file = NULL;
15439 +       au_br_put(hf->hf_br);
15440 +       hf->hf_br = NULL;
15441 +}
15442 +
15443 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15444 +{
15445 +       struct au_finfo *finfo = au_fi(file);
15446 +       struct au_hfile *hf;
15447 +       struct au_fidir *fidir;
15448 +
15449 +       fidir = finfo->fi_hdir;
15450 +       if (!fidir) {
15451 +               AuDebugOn(finfo->fi_btop != bindex);
15452 +               hf = &finfo->fi_htop;
15453 +       } else
15454 +               hf = fidir->fd_hfile + bindex;
15455 +
15456 +       if (hf && hf->hf_file)
15457 +               au_hfput(hf, vfsub_file_execed(file));
15458 +       if (val) {
15459 +               FiMustWriteLock(file);
15460 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15461 +               hf->hf_file = val;
15462 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15463 +       }
15464 +}
15465 +
15466 +void au_update_figen(struct file *file)
15467 +{
15468 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15469 +       /* smp_mb(); */ /* atomic_set */
15470 +}
15471 +
15472 +/* ---------------------------------------------------------------------- */
15473 +
15474 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15475 +{
15476 +       struct au_fidir *fidir;
15477 +       int nbr;
15478 +
15479 +       nbr = au_sbbot(sb) + 1;
15480 +       if (nbr < 2)
15481 +               nbr = 2; /* initial allocate for 2 branches */
15482 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15483 +       if (fidir) {
15484 +               fidir->fd_bbot = -1;
15485 +               fidir->fd_nent = nbr;
15486 +       }
15487 +
15488 +       return fidir;
15489 +}
15490 +
15491 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15492 +{
15493 +       int err;
15494 +       struct au_fidir *fidir, *p;
15495 +
15496 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15497 +       fidir = finfo->fi_hdir;
15498 +       AuDebugOn(!fidir);
15499 +
15500 +       err = -ENOMEM;
15501 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15502 +                        GFP_NOFS, may_shrink);
15503 +       if (p) {
15504 +               p->fd_nent = nbr;
15505 +               finfo->fi_hdir = p;
15506 +               err = 0;
15507 +       }
15508 +
15509 +       return err;
15510 +}
15511 +
15512 +/* ---------------------------------------------------------------------- */
15513 +
15514 +void au_finfo_fin(struct file *file)
15515 +{
15516 +       struct au_finfo *finfo;
15517 +
15518 +       au_nfiles_dec(file->f_path.dentry->d_sb);
15519 +
15520 +       finfo = au_fi(file);
15521 +       AuDebugOn(finfo->fi_hdir);
15522 +       AuRwDestroy(&finfo->fi_rwsem);
15523 +       au_cache_free_finfo(finfo);
15524 +}
15525 +
15526 +void au_fi_init_once(void *_finfo)
15527 +{
15528 +       struct au_finfo *finfo = _finfo;
15529 +
15530 +       au_rw_init(&finfo->fi_rwsem);
15531 +}
15532 +
15533 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15534 +{
15535 +       int err;
15536 +       struct au_finfo *finfo;
15537 +       struct dentry *dentry;
15538 +
15539 +       err = -ENOMEM;
15540 +       dentry = file->f_path.dentry;
15541 +       finfo = au_cache_alloc_finfo();
15542 +       if (unlikely(!finfo))
15543 +               goto out;
15544 +
15545 +       err = 0;
15546 +       au_nfiles_inc(dentry->d_sb);
15547 +       au_rw_write_lock(&finfo->fi_rwsem);
15548 +       finfo->fi_btop = -1;
15549 +       finfo->fi_hdir = fidir;
15550 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15551 +       /* smp_mb(); */ /* atomic_set */
15552 +
15553 +       file->private_data = finfo;
15554 +
15555 +out:
15556 +       return err;
15557 +}
15558 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15559 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15560 +++ linux/fs/aufs/f_op.c        2018-08-12 23:43:05.456791299 +0200
15561 @@ -0,0 +1,818 @@
15562 +// SPDX-License-Identifier: GPL-2.0
15563 +/*
15564 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15565 + *
15566 + * This program, aufs is free software; you can redistribute it and/or modify
15567 + * it under the terms of the GNU General Public License as published by
15568 + * the Free Software Foundation; either version 2 of the License, or
15569 + * (at your option) any later version.
15570 + *
15571 + * This program is distributed in the hope that it will be useful,
15572 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15573 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15574 + * GNU General Public License for more details.
15575 + *
15576 + * You should have received a copy of the GNU General Public License
15577 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15578 + */
15579 +
15580 +/*
15581 + * file and vm operations
15582 + */
15583 +
15584 +#include <linux/aio.h>
15585 +#include <linux/fs_stack.h>
15586 +#include <linux/mman.h>
15587 +#include <linux/security.h>
15588 +#include "aufs.h"
15589 +
15590 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15591 +{
15592 +       int err;
15593 +       aufs_bindex_t bindex;
15594 +       struct dentry *dentry, *h_dentry;
15595 +       struct au_finfo *finfo;
15596 +       struct inode *h_inode;
15597 +
15598 +       FiMustWriteLock(file);
15599 +
15600 +       err = 0;
15601 +       dentry = file->f_path.dentry;
15602 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15603 +       finfo = au_fi(file);
15604 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15605 +       atomic_set(&finfo->fi_mmapped, 0);
15606 +       bindex = au_dbtop(dentry);
15607 +       if (!h_file) {
15608 +               h_dentry = au_h_dptr(dentry, bindex);
15609 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15610 +               if (unlikely(err))
15611 +                       goto out;
15612 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15613 +       } else {
15614 +               h_dentry = h_file->f_path.dentry;
15615 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15616 +               if (unlikely(err))
15617 +                       goto out;
15618 +               get_file(h_file);
15619 +       }
15620 +       if (IS_ERR(h_file))
15621 +               err = PTR_ERR(h_file);
15622 +       else {
15623 +               if ((flags & __O_TMPFILE)
15624 +                   && !(flags & O_EXCL)) {
15625 +                       h_inode = file_inode(h_file);
15626 +                       spin_lock(&h_inode->i_lock);
15627 +                       h_inode->i_state |= I_LINKABLE;
15628 +                       spin_unlock(&h_inode->i_lock);
15629 +               }
15630 +               au_set_fbtop(file, bindex);
15631 +               au_set_h_fptr(file, bindex, h_file);
15632 +               au_update_figen(file);
15633 +               /* todo: necessary? */
15634 +               /* file->f_ra = h_file->f_ra; */
15635 +       }
15636 +
15637 +out:
15638 +       return err;
15639 +}
15640 +
15641 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15642 +                           struct file *file)
15643 +{
15644 +       int err;
15645 +       struct super_block *sb;
15646 +       struct au_do_open_args args = {
15647 +               .open   = au_do_open_nondir
15648 +       };
15649 +
15650 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15651 +             file, vfsub_file_flags(file), file->f_mode);
15652 +
15653 +       sb = file->f_path.dentry->d_sb;
15654 +       si_read_lock(sb, AuLock_FLUSH);
15655 +       err = au_do_open(file, &args);
15656 +       si_read_unlock(sb);
15657 +       return err;
15658 +}
15659 +
15660 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15661 +{
15662 +       struct au_finfo *finfo;
15663 +       aufs_bindex_t bindex;
15664 +
15665 +       finfo = au_fi(file);
15666 +       au_hbl_del(&finfo->fi_hlist,
15667 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15668 +       bindex = finfo->fi_btop;
15669 +       if (bindex >= 0)
15670 +               au_set_h_fptr(file, bindex, NULL);
15671 +
15672 +       au_finfo_fin(file);
15673 +       return 0;
15674 +}
15675 +
15676 +/* ---------------------------------------------------------------------- */
15677 +
15678 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15679 +{
15680 +       int err;
15681 +       struct file *h_file;
15682 +
15683 +       err = 0;
15684 +       h_file = au_hf_top(file);
15685 +       if (h_file)
15686 +               err = vfsub_flush(h_file, id);
15687 +       return err;
15688 +}
15689 +
15690 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15691 +{
15692 +       return au_do_flush(file, id, au_do_flush_nondir);
15693 +}
15694 +
15695 +/* ---------------------------------------------------------------------- */
15696 +/*
15697 + * read and write functions acquire [fdi]_rwsem once, but release before
15698 + * mmap_sem. This is because to stop a race condition between mmap(2).
15699 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
15700 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15701 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15702 + */
15703 +
15704 +/* Callers should call au_read_post() or fput() in the end */
15705 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15706 +{
15707 +       struct file *h_file;
15708 +       int err;
15709 +
15710 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15711 +       if (!err) {
15712 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15713 +               h_file = au_hf_top(file);
15714 +               get_file(h_file);
15715 +               if (!keep_fi)
15716 +                       fi_read_unlock(file);
15717 +       } else
15718 +               h_file = ERR_PTR(err);
15719 +
15720 +       return h_file;
15721 +}
15722 +
15723 +static void au_read_post(struct inode *inode, struct file *h_file)
15724 +{
15725 +       /* update without lock, I don't think it a problem */
15726 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15727 +       fput(h_file);
15728 +}
15729 +
15730 +struct au_write_pre {
15731 +       /* input */
15732 +       unsigned int lsc;
15733 +
15734 +       /* output */
15735 +       blkcnt_t blks;
15736 +       aufs_bindex_t btop;
15737 +};
15738 +
15739 +/*
15740 + * return with iinfo is write-locked
15741 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15742 + * end
15743 + */
15744 +static struct file *au_write_pre(struct file *file, int do_ready,
15745 +                                struct au_write_pre *wpre)
15746 +{
15747 +       struct file *h_file;
15748 +       struct dentry *dentry;
15749 +       int err;
15750 +       unsigned int lsc;
15751 +       struct au_pin pin;
15752 +
15753 +       lsc = 0;
15754 +       if (wpre)
15755 +               lsc = wpre->lsc;
15756 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15757 +       h_file = ERR_PTR(err);
15758 +       if (unlikely(err))
15759 +               goto out;
15760 +
15761 +       dentry = file->f_path.dentry;
15762 +       if (do_ready) {
15763 +               err = au_ready_to_write(file, -1, &pin);
15764 +               if (unlikely(err)) {
15765 +                       h_file = ERR_PTR(err);
15766 +                       di_write_unlock(dentry);
15767 +                       goto out_fi;
15768 +               }
15769 +       }
15770 +
15771 +       di_downgrade_lock(dentry, /*flags*/0);
15772 +       if (wpre)
15773 +               wpre->btop = au_fbtop(file);
15774 +       h_file = au_hf_top(file);
15775 +       get_file(h_file);
15776 +       if (wpre)
15777 +               wpre->blks = file_inode(h_file)->i_blocks;
15778 +       if (do_ready)
15779 +               au_unpin(&pin);
15780 +       di_read_unlock(dentry, /*flags*/0);
15781 +
15782 +out_fi:
15783 +       fi_write_unlock(file);
15784 +out:
15785 +       return h_file;
15786 +}
15787 +
15788 +static void au_write_post(struct inode *inode, struct file *h_file,
15789 +                         struct au_write_pre *wpre, ssize_t written)
15790 +{
15791 +       struct inode *h_inode;
15792 +
15793 +       au_cpup_attr_timesizes(inode);
15794 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15795 +       h_inode = file_inode(h_file);
15796 +       inode->i_mode = h_inode->i_mode;
15797 +       ii_write_unlock(inode);
15798 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15799 +       if (written > 0)
15800 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15801 +                             /*force*/h_inode->i_blocks > wpre->blks);
15802 +       fput(h_file);
15803 +}
15804 +
15805 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15806 +                        loff_t *ppos)
15807 +{
15808 +       ssize_t err;
15809 +       struct inode *inode;
15810 +       struct file *h_file;
15811 +       struct super_block *sb;
15812 +
15813 +       inode = file_inode(file);
15814 +       sb = inode->i_sb;
15815 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15816 +
15817 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15818 +       err = PTR_ERR(h_file);
15819 +       if (IS_ERR(h_file))
15820 +               goto out;
15821 +
15822 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
15823 +       err = vfsub_read_u(h_file, buf, count, ppos);
15824 +       /* todo: necessary? */
15825 +       /* file->f_ra = h_file->f_ra; */
15826 +       au_read_post(inode, h_file);
15827 +
15828 +out:
15829 +       si_read_unlock(sb);
15830 +       return err;
15831 +}
15832 +
15833 +/*
15834 + * todo: very ugly
15835 + * it locks both of i_mutex and si_rwsem for read in safe.
15836 + * if the plink maintenance mode continues forever (that is the problem),
15837 + * may loop forever.
15838 + */
15839 +static void au_mtx_and_read_lock(struct inode *inode)
15840 +{
15841 +       int err;
15842 +       struct super_block *sb = inode->i_sb;
15843 +
15844 +       while (1) {
15845 +               inode_lock(inode);
15846 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15847 +               if (!err)
15848 +                       break;
15849 +               inode_unlock(inode);
15850 +               si_read_lock(sb, AuLock_NOPLMW);
15851 +               si_read_unlock(sb);
15852 +       }
15853 +}
15854 +
15855 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15856 +                         size_t count, loff_t *ppos)
15857 +{
15858 +       ssize_t err;
15859 +       struct au_write_pre wpre;
15860 +       struct inode *inode;
15861 +       struct file *h_file;
15862 +       char __user *buf = (char __user *)ubuf;
15863 +
15864 +       inode = file_inode(file);
15865 +       au_mtx_and_read_lock(inode);
15866 +
15867 +       wpre.lsc = 0;
15868 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15869 +       err = PTR_ERR(h_file);
15870 +       if (IS_ERR(h_file))
15871 +               goto out;
15872 +
15873 +       err = vfsub_write_u(h_file, buf, count, ppos);
15874 +       au_write_post(inode, h_file, &wpre, err);
15875 +
15876 +out:
15877 +       si_read_unlock(inode->i_sb);
15878 +       inode_unlock(inode);
15879 +       return err;
15880 +}
15881 +
15882 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15883 +                         struct iov_iter *iov_iter)
15884 +{
15885 +       ssize_t err;
15886 +       struct file *file;
15887 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15888 +
15889 +       err = security_file_permission(h_file, rw);
15890 +       if (unlikely(err))
15891 +               goto out;
15892 +
15893 +       err = -ENOSYS;
15894 +       iter = NULL;
15895 +       if (rw == MAY_READ)
15896 +               iter = h_file->f_op->read_iter;
15897 +       else if (rw == MAY_WRITE)
15898 +               iter = h_file->f_op->write_iter;
15899 +
15900 +       file = kio->ki_filp;
15901 +       kio->ki_filp = h_file;
15902 +       if (iter) {
15903 +               lockdep_off();
15904 +               err = iter(kio, iov_iter);
15905 +               lockdep_on();
15906 +       } else
15907 +               /* currently there is no such fs */
15908 +               WARN_ON_ONCE(1);
15909 +       kio->ki_filp = file;
15910 +
15911 +out:
15912 +       return err;
15913 +}
15914 +
15915 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15916 +{
15917 +       ssize_t err;
15918 +       struct file *file, *h_file;
15919 +       struct inode *inode;
15920 +       struct super_block *sb;
15921 +
15922 +       file = kio->ki_filp;
15923 +       inode = file_inode(file);
15924 +       sb = inode->i_sb;
15925 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15926 +
15927 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15928 +       err = PTR_ERR(h_file);
15929 +       if (IS_ERR(h_file))
15930 +               goto out;
15931 +
15932 +       if (au_test_loopback_kthread()) {
15933 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15934 +               if (file->f_mapping != h_file->f_mapping) {
15935 +                       file->f_mapping = h_file->f_mapping;
15936 +                       smp_mb(); /* unnecessary? */
15937 +               }
15938 +       }
15939 +       fi_read_unlock(file);
15940 +
15941 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15942 +       /* todo: necessary? */
15943 +       /* file->f_ra = h_file->f_ra; */
15944 +       au_read_post(inode, h_file);
15945 +
15946 +out:
15947 +       si_read_unlock(sb);
15948 +       return err;
15949 +}
15950 +
15951 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15952 +{
15953 +       ssize_t err;
15954 +       struct au_write_pre wpre;
15955 +       struct inode *inode;
15956 +       struct file *file, *h_file;
15957 +
15958 +       file = kio->ki_filp;
15959 +       inode = file_inode(file);
15960 +       au_mtx_and_read_lock(inode);
15961 +
15962 +       wpre.lsc = 0;
15963 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15964 +       err = PTR_ERR(h_file);
15965 +       if (IS_ERR(h_file))
15966 +               goto out;
15967 +
15968 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15969 +       au_write_post(inode, h_file, &wpre, err);
15970 +
15971 +out:
15972 +       si_read_unlock(inode->i_sb);
15973 +       inode_unlock(inode);
15974 +       return err;
15975 +}
15976 +
15977 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15978 +                               struct pipe_inode_info *pipe, size_t len,
15979 +                               unsigned int flags)
15980 +{
15981 +       ssize_t err;
15982 +       struct file *h_file;
15983 +       struct inode *inode;
15984 +       struct super_block *sb;
15985 +
15986 +       inode = file_inode(file);
15987 +       sb = inode->i_sb;
15988 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15989 +
15990 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15991 +       err = PTR_ERR(h_file);
15992 +       if (IS_ERR(h_file))
15993 +               goto out;
15994 +
15995 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15996 +       /* todo: necessasry? */
15997 +       /* file->f_ra = h_file->f_ra; */
15998 +       au_read_post(inode, h_file);
15999 +
16000 +out:
16001 +       si_read_unlock(sb);
16002 +       return err;
16003 +}
16004 +
16005 +static ssize_t
16006 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16007 +                 size_t len, unsigned int flags)
16008 +{
16009 +       ssize_t err;
16010 +       struct au_write_pre wpre;
16011 +       struct inode *inode;
16012 +       struct file *h_file;
16013 +
16014 +       inode = file_inode(file);
16015 +       au_mtx_and_read_lock(inode);
16016 +
16017 +       wpre.lsc = 0;
16018 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16019 +       err = PTR_ERR(h_file);
16020 +       if (IS_ERR(h_file))
16021 +               goto out;
16022 +
16023 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
16024 +       au_write_post(inode, h_file, &wpre, err);
16025 +
16026 +out:
16027 +       si_read_unlock(inode->i_sb);
16028 +       inode_unlock(inode);
16029 +       return err;
16030 +}
16031 +
16032 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16033 +                          loff_t len)
16034 +{
16035 +       long err;
16036 +       struct au_write_pre wpre;
16037 +       struct inode *inode;
16038 +       struct file *h_file;
16039 +
16040 +       inode = file_inode(file);
16041 +       au_mtx_and_read_lock(inode);
16042 +
16043 +       wpre.lsc = 0;
16044 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16045 +       err = PTR_ERR(h_file);
16046 +       if (IS_ERR(h_file))
16047 +               goto out;
16048 +
16049 +       lockdep_off();
16050 +       err = vfs_fallocate(h_file, mode, offset, len);
16051 +       lockdep_on();
16052 +       au_write_post(inode, h_file, &wpre, /*written*/1);
16053 +
16054 +out:
16055 +       si_read_unlock(inode->i_sb);
16056 +       inode_unlock(inode);
16057 +       return err;
16058 +}
16059 +
16060 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16061 +                                   struct file *dst, loff_t dst_pos,
16062 +                                   size_t len, unsigned int flags)
16063 +{
16064 +       ssize_t err;
16065 +       struct au_write_pre wpre;
16066 +       enum { SRC, DST };
16067 +       struct {
16068 +               struct inode *inode;
16069 +               struct file *h_file;
16070 +               struct super_block *h_sb;
16071 +       } a[2];
16072 +#define a_src  a[SRC]
16073 +#define a_dst  a[DST]
16074 +
16075 +       err = -EINVAL;
16076 +       a_src.inode = file_inode(src);
16077 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16078 +               goto out;
16079 +       a_dst.inode = file_inode(dst);
16080 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16081 +               goto out;
16082 +
16083 +       au_mtx_and_read_lock(a_dst.inode);
16084 +       /*
16085 +        * in order to match the order in di_write_lock2_{child,parent}(),
16086 +        * use f_path.dentry for this comparision.
16087 +        */
16088 +       if (src->f_path.dentry < dst->f_path.dentry) {
16089 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16090 +               err = PTR_ERR(a_src.h_file);
16091 +               if (IS_ERR(a_src.h_file))
16092 +                       goto out_si;
16093 +
16094 +               wpre.lsc = AuLsc_FI_2;
16095 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16096 +               err = PTR_ERR(a_dst.h_file);
16097 +               if (IS_ERR(a_dst.h_file)) {
16098 +                       au_read_post(a_src.inode, a_src.h_file);
16099 +                       goto out_si;
16100 +               }
16101 +       } else {
16102 +               wpre.lsc = AuLsc_FI_1;
16103 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16104 +               err = PTR_ERR(a_dst.h_file);
16105 +               if (IS_ERR(a_dst.h_file))
16106 +                       goto out_si;
16107 +
16108 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16109 +               err = PTR_ERR(a_src.h_file);
16110 +               if (IS_ERR(a_src.h_file)) {
16111 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16112 +                                     /*written*/0);
16113 +                       goto out_si;
16114 +               }
16115 +       }
16116 +
16117 +       err = -EXDEV;
16118 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16119 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16120 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16121 +               AuDbgFile(src);
16122 +               AuDbgFile(dst);
16123 +               goto out_file;
16124 +       }
16125 +
16126 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16127 +                                   dst_pos, len, flags);
16128 +
16129 +out_file:
16130 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16131 +       fi_read_unlock(src);
16132 +       au_read_post(a_src.inode, a_src.h_file);
16133 +out_si:
16134 +       si_read_unlock(a_dst.inode->i_sb);
16135 +       inode_unlock(a_dst.inode);
16136 +out:
16137 +       return err;
16138 +#undef a_src
16139 +#undef a_dst
16140 +}
16141 +
16142 +/* ---------------------------------------------------------------------- */
16143 +
16144 +/*
16145 + * The locking order around current->mmap_sem.
16146 + * - in most and regular cases
16147 + *   file I/O syscall -- aufs_read() or something
16148 + *     -- si_rwsem for read -- mmap_sem
16149 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
16150 + * - in mmap case
16151 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16152 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
16153 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
16154 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16155 + * It means that when aufs acquires si_rwsem for write, the process should never
16156 + * acquire mmap_sem.
16157 + *
16158 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16159 + * problem either since any directory is not able to be mmap-ed.
16160 + * The similar scenario is applied to aufs_readlink() too.
16161 + */
16162 +
16163 +#if 0 /* stop calling security_file_mmap() */
16164 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16165 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16166 +
16167 +static unsigned long au_arch_prot_conv(unsigned long flags)
16168 +{
16169 +       /* currently ppc64 only */
16170 +#ifdef CONFIG_PPC64
16171 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16172 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16173 +       return AuConv_VM_PROT(flags, SAO);
16174 +#else
16175 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16176 +       return 0;
16177 +#endif
16178 +}
16179 +
16180 +static unsigned long au_prot_conv(unsigned long flags)
16181 +{
16182 +       return AuConv_VM_PROT(flags, READ)
16183 +               | AuConv_VM_PROT(flags, WRITE)
16184 +               | AuConv_VM_PROT(flags, EXEC)
16185 +               | au_arch_prot_conv(flags);
16186 +}
16187 +
16188 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16189 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16190 +
16191 +static unsigned long au_flag_conv(unsigned long flags)
16192 +{
16193 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16194 +               | AuConv_VM_MAP(flags, DENYWRITE)
16195 +               | AuConv_VM_MAP(flags, LOCKED);
16196 +}
16197 +#endif
16198 +
16199 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16200 +{
16201 +       int err;
16202 +       const unsigned char wlock
16203 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16204 +       struct super_block *sb;
16205 +       struct file *h_file;
16206 +       struct inode *inode;
16207 +
16208 +       AuDbgVmRegion(file, vma);
16209 +
16210 +       inode = file_inode(file);
16211 +       sb = inode->i_sb;
16212 +       lockdep_off();
16213 +       si_read_lock(sb, AuLock_NOPLMW);
16214 +
16215 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16216 +       lockdep_on();
16217 +       err = PTR_ERR(h_file);
16218 +       if (IS_ERR(h_file))
16219 +               goto out;
16220 +
16221 +       err = 0;
16222 +       au_set_mmapped(file);
16223 +       au_vm_file_reset(vma, h_file);
16224 +       /*
16225 +        * we cannot call security_mmap_file() here since it may acquire
16226 +        * mmap_sem or i_mutex.
16227 +        *
16228 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16229 +        *                       au_flag_conv(vma->vm_flags));
16230 +        */
16231 +       if (!err)
16232 +               err = call_mmap(h_file, vma);
16233 +       if (!err) {
16234 +               au_vm_prfile_set(vma, file);
16235 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16236 +               goto out_fput; /* success */
16237 +       }
16238 +       au_unset_mmapped(file);
16239 +       au_vm_file_reset(vma, file);
16240 +
16241 +out_fput:
16242 +       lockdep_off();
16243 +       ii_write_unlock(inode);
16244 +       lockdep_on();
16245 +       fput(h_file);
16246 +out:
16247 +       lockdep_off();
16248 +       si_read_unlock(sb);
16249 +       lockdep_on();
16250 +       AuTraceErr(err);
16251 +       return err;
16252 +}
16253 +
16254 +/* ---------------------------------------------------------------------- */
16255 +
16256 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16257 +                            int datasync)
16258 +{
16259 +       int err;
16260 +       struct au_write_pre wpre;
16261 +       struct inode *inode;
16262 +       struct file *h_file;
16263 +
16264 +       err = 0; /* -EBADF; */ /* posix? */
16265 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16266 +               goto out;
16267 +
16268 +       inode = file_inode(file);
16269 +       au_mtx_and_read_lock(inode);
16270 +
16271 +       wpre.lsc = 0;
16272 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16273 +       err = PTR_ERR(h_file);
16274 +       if (IS_ERR(h_file))
16275 +               goto out_unlock;
16276 +
16277 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16278 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16279 +
16280 +out_unlock:
16281 +       si_read_unlock(inode->i_sb);
16282 +       inode_unlock(inode);
16283 +out:
16284 +       return err;
16285 +}
16286 +
16287 +static int aufs_fasync(int fd, struct file *file, int flag)
16288 +{
16289 +       int err;
16290 +       struct file *h_file;
16291 +       struct super_block *sb;
16292 +
16293 +       sb = file->f_path.dentry->d_sb;
16294 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16295 +
16296 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16297 +       err = PTR_ERR(h_file);
16298 +       if (IS_ERR(h_file))
16299 +               goto out;
16300 +
16301 +       if (h_file->f_op->fasync)
16302 +               err = h_file->f_op->fasync(fd, h_file, flag);
16303 +       fput(h_file); /* instead of au_read_post() */
16304 +
16305 +out:
16306 +       si_read_unlock(sb);
16307 +       return err;
16308 +}
16309 +
16310 +static int aufs_setfl(struct file *file, unsigned long arg)
16311 +{
16312 +       int err;
16313 +       struct file *h_file;
16314 +       struct super_block *sb;
16315 +
16316 +       sb = file->f_path.dentry->d_sb;
16317 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16318 +
16319 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16320 +       err = PTR_ERR(h_file);
16321 +       if (IS_ERR(h_file))
16322 +               goto out;
16323 +
16324 +       /* stop calling h_file->fasync */
16325 +       arg |= vfsub_file_flags(file) & FASYNC;
16326 +       err = setfl(/*unused fd*/-1, h_file, arg);
16327 +       fput(h_file); /* instead of au_read_post() */
16328 +
16329 +out:
16330 +       si_read_unlock(sb);
16331 +       return err;
16332 +}
16333 +
16334 +/* ---------------------------------------------------------------------- */
16335 +
16336 +/* no one supports this operation, currently */
16337 +#if 0
16338 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16339 +                            size_t len, loff_t *pos, int more)
16340 +{
16341 +}
16342 +#endif
16343 +
16344 +/* ---------------------------------------------------------------------- */
16345 +
16346 +const struct file_operations aufs_file_fop = {
16347 +       .owner          = THIS_MODULE,
16348 +
16349 +       .llseek         = default_llseek,
16350 +
16351 +       .read           = aufs_read,
16352 +       .write          = aufs_write,
16353 +       .read_iter      = aufs_read_iter,
16354 +       .write_iter     = aufs_write_iter,
16355 +
16356 +#ifdef CONFIG_AUFS_POLL
16357 +       .poll           = aufs_poll,
16358 +#endif
16359 +       .unlocked_ioctl = aufs_ioctl_nondir,
16360 +#ifdef CONFIG_COMPAT
16361 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16362 +#endif
16363 +       .mmap           = aufs_mmap,
16364 +       .open           = aufs_open_nondir,
16365 +       .flush          = aufs_flush_nondir,
16366 +       .release        = aufs_release_nondir,
16367 +       .fsync          = aufs_fsync_nondir,
16368 +       .fasync         = aufs_fasync,
16369 +       /* .sendpage    = aufs_sendpage, */
16370 +       .setfl          = aufs_setfl,
16371 +       .splice_write   = aufs_splice_write,
16372 +       .splice_read    = aufs_splice_read,
16373 +#if 0
16374 +       .aio_splice_write = aufs_aio_splice_write,
16375 +       .aio_splice_read  = aufs_aio_splice_read,
16376 +#endif
16377 +       .fallocate      = aufs_fallocate,
16378 +       .copy_file_range = aufs_copy_file_range
16379 +};
16380 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16381 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16382 +++ linux/fs/aufs/fstype.h      2018-08-12 23:43:05.456791299 +0200
16383 @@ -0,0 +1,401 @@
16384 +/* SPDX-License-Identifier: GPL-2.0 */
16385 +/*
16386 + * Copyright (C) 2005-2018 Junjiro R. Okajima
16387 + *
16388 + * This program, aufs is free software; you can redistribute it and/or modify
16389 + * it under the terms of the GNU General Public License as published by
16390 + * the Free Software Foundation; either version 2 of the License, or
16391 + * (at your option) any later version.
16392 + *
16393 + * This program is distributed in the hope that it will be useful,
16394 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16395 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16396 + * GNU General Public License for more details.
16397 + *
16398 + * You should have received a copy of the GNU General Public License
16399 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16400 + */
16401 +
16402 +/*
16403 + * judging filesystem type
16404 + */
16405 +
16406 +#ifndef __AUFS_FSTYPE_H__
16407 +#define __AUFS_FSTYPE_H__
16408 +
16409 +#ifdef __KERNEL__
16410 +
16411 +#include <linux/fs.h>
16412 +#include <linux/magic.h>
16413 +#include <linux/nfs_fs.h>
16414 +#include <linux/romfs_fs.h>
16415 +
16416 +static inline int au_test_aufs(struct super_block *sb)
16417 +{
16418 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16419 +}
16420 +
16421 +static inline const char *au_sbtype(struct super_block *sb)
16422 +{
16423 +       return sb->s_type->name;
16424 +}
16425 +
16426 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16427 +{
16428 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16429 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16430 +#else
16431 +       return 0;
16432 +#endif
16433 +}
16434 +
16435 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16436 +{
16437 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16438 +       return sb->s_magic == ROMFS_MAGIC;
16439 +#else
16440 +       return 0;
16441 +#endif
16442 +}
16443 +
16444 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16445 +{
16446 +#if IS_ENABLED(CONFIG_CRAMFS)
16447 +       return sb->s_magic == CRAMFS_MAGIC;
16448 +#endif
16449 +       return 0;
16450 +}
16451 +
16452 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16453 +{
16454 +#if IS_ENABLED(CONFIG_NFS_FS)
16455 +       return sb->s_magic == NFS_SUPER_MAGIC;
16456 +#else
16457 +       return 0;
16458 +#endif
16459 +}
16460 +
16461 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16462 +{
16463 +#if IS_ENABLED(CONFIG_FUSE_FS)
16464 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16465 +#else
16466 +       return 0;
16467 +#endif
16468 +}
16469 +
16470 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16471 +{
16472 +#if IS_ENABLED(CONFIG_XFS_FS)
16473 +       return sb->s_magic == XFS_SB_MAGIC;
16474 +#else
16475 +       return 0;
16476 +#endif
16477 +}
16478 +
16479 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16480 +{
16481 +#ifdef CONFIG_TMPFS
16482 +       return sb->s_magic == TMPFS_MAGIC;
16483 +#else
16484 +       return 0;
16485 +#endif
16486 +}
16487 +
16488 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16489 +{
16490 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16491 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16492 +#else
16493 +       return 0;
16494 +#endif
16495 +}
16496 +
16497 +static inline int au_test_ramfs(struct super_block *sb)
16498 +{
16499 +       return sb->s_magic == RAMFS_MAGIC;
16500 +}
16501 +
16502 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16503 +{
16504 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16505 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16506 +#else
16507 +       return 0;
16508 +#endif
16509 +}
16510 +
16511 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16512 +{
16513 +#ifdef CONFIG_PROC_FS
16514 +       return sb->s_magic == PROC_SUPER_MAGIC;
16515 +#else
16516 +       return 0;
16517 +#endif
16518 +}
16519 +
16520 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16521 +{
16522 +#ifdef CONFIG_SYSFS
16523 +       return sb->s_magic == SYSFS_MAGIC;
16524 +#else
16525 +       return 0;
16526 +#endif
16527 +}
16528 +
16529 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16530 +{
16531 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16532 +       return sb->s_magic == CONFIGFS_MAGIC;
16533 +#else
16534 +       return 0;
16535 +#endif
16536 +}
16537 +
16538 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16539 +{
16540 +#if IS_ENABLED(CONFIG_MINIX_FS)
16541 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16542 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16543 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16544 +               || sb->s_magic == MINIX_SUPER_MAGIC
16545 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16546 +#else
16547 +       return 0;
16548 +#endif
16549 +}
16550 +
16551 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16552 +{
16553 +#if IS_ENABLED(CONFIG_FAT_FS)
16554 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16555 +#else
16556 +       return 0;
16557 +#endif
16558 +}
16559 +
16560 +static inline int au_test_msdos(struct super_block *sb)
16561 +{
16562 +       return au_test_fat(sb);
16563 +}
16564 +
16565 +static inline int au_test_vfat(struct super_block *sb)
16566 +{
16567 +       return au_test_fat(sb);
16568 +}
16569 +
16570 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16571 +{
16572 +#ifdef CONFIG_SECURITYFS
16573 +       return sb->s_magic == SECURITYFS_MAGIC;
16574 +#else
16575 +       return 0;
16576 +#endif
16577 +}
16578 +
16579 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16580 +{
16581 +#if IS_ENABLED(CONFIG_SQUASHFS)
16582 +       return sb->s_magic == SQUASHFS_MAGIC;
16583 +#else
16584 +       return 0;
16585 +#endif
16586 +}
16587 +
16588 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16589 +{
16590 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16591 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16592 +#else
16593 +       return 0;
16594 +#endif
16595 +}
16596 +
16597 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16598 +{
16599 +#if IS_ENABLED(CONFIG_XENFS)
16600 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16601 +#else
16602 +       return 0;
16603 +#endif
16604 +}
16605 +
16606 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16607 +{
16608 +#ifdef CONFIG_DEBUG_FS
16609 +       return sb->s_magic == DEBUGFS_MAGIC;
16610 +#else
16611 +       return 0;
16612 +#endif
16613 +}
16614 +
16615 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16616 +{
16617 +#if IS_ENABLED(CONFIG_NILFS)
16618 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16619 +#else
16620 +       return 0;
16621 +#endif
16622 +}
16623 +
16624 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16625 +{
16626 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16627 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16628 +#else
16629 +       return 0;
16630 +#endif
16631 +}
16632 +
16633 +/* ---------------------------------------------------------------------- */
16634 +/*
16635 + * they can't be an aufs branch.
16636 + */
16637 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16638 +{
16639 +       return
16640 +#ifndef CONFIG_AUFS_BR_RAMFS
16641 +               au_test_ramfs(sb) ||
16642 +#endif
16643 +               au_test_procfs(sb)
16644 +               || au_test_sysfs(sb)
16645 +               || au_test_configfs(sb)
16646 +               || au_test_debugfs(sb)
16647 +               || au_test_securityfs(sb)
16648 +               || au_test_xenfs(sb)
16649 +               || au_test_ecryptfs(sb)
16650 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16651 +               || au_test_aufs(sb); /* will be supported in next version */
16652 +}
16653 +
16654 +static inline int au_test_fs_remote(struct super_block *sb)
16655 +{
16656 +       return !au_test_tmpfs(sb)
16657 +#ifdef CONFIG_AUFS_BR_RAMFS
16658 +               && !au_test_ramfs(sb)
16659 +#endif
16660 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16661 +}
16662 +
16663 +/* ---------------------------------------------------------------------- */
16664 +
16665 +/*
16666 + * Note: these functions (below) are created after reading ->getattr() in all
16667 + * filesystems under linux/fs. it means we have to do so in every update...
16668 + */
16669 +
16670 +/*
16671 + * some filesystems require getattr to refresh the inode attributes before
16672 + * referencing.
16673 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16674 + * and leave the work for d_revalidate()
16675 + */
16676 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16677 +{
16678 +       return au_test_nfs(sb)
16679 +               || au_test_fuse(sb)
16680 +               /* || au_test_btrfs(sb) */      /* untested */
16681 +               ;
16682 +}
16683 +
16684 +/*
16685 + * filesystems which don't maintain i_size or i_blocks.
16686 + */
16687 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16688 +{
16689 +       return au_test_xfs(sb)
16690 +               || au_test_btrfs(sb)
16691 +               || au_test_ubifs(sb)
16692 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16693 +               /* || au_test_minix(sb) */      /* untested */
16694 +               ;
16695 +}
16696 +
16697 +/*
16698 + * filesystems which don't store the correct value in some of their inode
16699 + * attributes.
16700 + */
16701 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16702 +{
16703 +       return au_test_fs_bad_iattr_size(sb)
16704 +               || au_test_fat(sb)
16705 +               || au_test_msdos(sb)
16706 +               || au_test_vfat(sb);
16707 +}
16708 +
16709 +/* they don't check i_nlink in link(2) */
16710 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16711 +{
16712 +       return au_test_tmpfs(sb)
16713 +#ifdef CONFIG_AUFS_BR_RAMFS
16714 +               || au_test_ramfs(sb)
16715 +#endif
16716 +               || au_test_ubifs(sb)
16717 +               || au_test_hfsplus(sb);
16718 +}
16719 +
16720 +/*
16721 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16722 + */
16723 +static inline int au_test_fs_notime(struct super_block *sb)
16724 +{
16725 +       return au_test_nfs(sb)
16726 +               || au_test_fuse(sb)
16727 +               || au_test_ubifs(sb)
16728 +               ;
16729 +}
16730 +
16731 +/* temporary support for i#1 in cramfs */
16732 +static inline int au_test_fs_unique_ino(struct inode *inode)
16733 +{
16734 +       if (au_test_cramfs(inode->i_sb))
16735 +               return inode->i_ino != 1;
16736 +       return 1;
16737 +}
16738 +
16739 +/* ---------------------------------------------------------------------- */
16740 +
16741 +/*
16742 + * the filesystem where the xino files placed must support i/o after unlink and
16743 + * maintain i_size and i_blocks.
16744 + */
16745 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16746 +{
16747 +       return au_test_fs_remote(sb)
16748 +               || au_test_fs_bad_iattr_size(sb)
16749 +               /* don't want unnecessary work for xino */
16750 +               || au_test_aufs(sb)
16751 +               || au_test_ecryptfs(sb)
16752 +               || au_test_nilfs(sb);
16753 +}
16754 +
16755 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16756 +{
16757 +       return au_test_tmpfs(sb)
16758 +               || au_test_ramfs(sb);
16759 +}
16760 +
16761 +/*
16762 + * test if the @sb is real-readonly.
16763 + */
16764 +static inline int au_test_fs_rr(struct super_block *sb)
16765 +{
16766 +       return au_test_squashfs(sb)
16767 +               || au_test_iso9660(sb)
16768 +               || au_test_cramfs(sb)
16769 +               || au_test_romfs(sb);
16770 +}
16771 +
16772 +/*
16773 + * test if the @inode is nfs with 'noacl' option
16774 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16775 + */
16776 +static inline int au_test_nfs_noacl(struct inode *inode)
16777 +{
16778 +       return au_test_nfs(inode->i_sb)
16779 +               /* && IS_POSIXACL(inode) */
16780 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16781 +}
16782 +
16783 +#endif /* __KERNEL__ */
16784 +#endif /* __AUFS_FSTYPE_H__ */
16785 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16786 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16787 +++ linux/fs/aufs/hbl.h 2018-08-12 23:43:05.456791299 +0200
16788 @@ -0,0 +1,65 @@
16789 +/* SPDX-License-Identifier: GPL-2.0 */
16790 +/*
16791 + * Copyright (C) 2017-2018 Junjiro R. Okajima
16792 + *
16793 + * This program, aufs is free software; you can redistribute it and/or modify
16794 + * it under the terms of the GNU General Public License as published by
16795 + * the Free Software Foundation; either version 2 of the License, or
16796 + * (at your option) any later version.
16797 + *
16798 + * This program is distributed in the hope that it will be useful,
16799 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16800 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16801 + * GNU General Public License for more details.
16802 + *
16803 + * You should have received a copy of the GNU General Public License
16804 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16805 + */
16806 +
16807 +/*
16808 + * helpers for hlist_bl.h
16809 + */
16810 +
16811 +#ifndef __AUFS_HBL_H__
16812 +#define __AUFS_HBL_H__
16813 +
16814 +#ifdef __KERNEL__
16815 +
16816 +#include <linux/list_bl.h>
16817 +
16818 +static inline void au_hbl_add(struct hlist_bl_node *node,
16819 +                             struct hlist_bl_head *hbl)
16820 +{
16821 +       hlist_bl_lock(hbl);
16822 +       hlist_bl_add_head(node, hbl);
16823 +       hlist_bl_unlock(hbl);
16824 +}
16825 +
16826 +static inline void au_hbl_del(struct hlist_bl_node *node,
16827 +                             struct hlist_bl_head *hbl)
16828 +{
16829 +       hlist_bl_lock(hbl);
16830 +       hlist_bl_del(node);
16831 +       hlist_bl_unlock(hbl);
16832 +}
16833 +
16834 +#define au_hbl_for_each(pos, head)                                     \
16835 +       for (pos = hlist_bl_first(head);                                \
16836 +            pos;                                                       \
16837 +            pos = pos->next)
16838 +
16839 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16840 +{
16841 +       unsigned long cnt;
16842 +       struct hlist_bl_node *pos;
16843 +
16844 +       cnt = 0;
16845 +       hlist_bl_lock(hbl);
16846 +       au_hbl_for_each(pos, hbl)
16847 +               cnt++;
16848 +       hlist_bl_unlock(hbl);
16849 +       return cnt;
16850 +}
16851 +
16852 +#endif /* __KERNEL__ */
16853 +#endif /* __AUFS_HBL_H__ */
16854 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16855 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16856 +++ linux/fs/aufs/hfsnotify.c   2018-08-12 23:43:05.456791299 +0200
16857 @@ -0,0 +1,290 @@
16858 +// SPDX-License-Identifier: GPL-2.0
16859 +/*
16860 + * Copyright (C) 2005-2018 Junjiro R. Okajima
16861 + *
16862 + * This program, aufs is free software; you can redistribute it and/or modify
16863 + * it under the terms of the GNU General Public License as published by
16864 + * the Free Software Foundation; either version 2 of the License, or
16865 + * (at your option) any later version.
16866 + *
16867 + * This program is distributed in the hope that it will be useful,
16868 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16869 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16870 + * GNU General Public License for more details.
16871 + *
16872 + * You should have received a copy of the GNU General Public License
16873 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16874 + */
16875 +
16876 +/*
16877 + * fsnotify for the lower directories
16878 + */
16879 +
16880 +#include "aufs.h"
16881 +
16882 +/* FS_IN_IGNORED is unnecessary */
16883 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16884 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16885 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16886 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16887 +
16888 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16889 +{
16890 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16891 +                                            hn_mark);
16892 +       /* AuDbg("here\n"); */
16893 +       au_cache_free_hnotify(hn);
16894 +       smp_mb__before_atomic(); /* for atomic64_dec */
16895 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16896 +               wake_up(&au_hfsn_wq);
16897 +}
16898 +
16899 +static int au_hfsn_alloc(struct au_hinode *hinode)
16900 +{
16901 +       int err;
16902 +       struct au_hnotify *hn;
16903 +       struct super_block *sb;
16904 +       struct au_branch *br;
16905 +       struct fsnotify_mark *mark;
16906 +       aufs_bindex_t bindex;
16907 +
16908 +       hn = hinode->hi_notify;
16909 +       sb = hn->hn_aufs_inode->i_sb;
16910 +       bindex = au_br_index(sb, hinode->hi_id);
16911 +       br = au_sbr(sb, bindex);
16912 +       AuDebugOn(!br->br_hfsn);
16913 +
16914 +       mark = &hn->hn_mark;
16915 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16916 +       mark->mask = AuHfsnMask;
16917 +       /*
16918 +        * by udba rename or rmdir, aufs assign a new inode to the known
16919 +        * h_inode, so specify 1 to allow dups.
16920 +        */
16921 +       lockdep_off();
16922 +       err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
16923 +                               /*allow_dups*/1);
16924 +       lockdep_on();
16925 +
16926 +       return err;
16927 +}
16928 +
16929 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16930 +{
16931 +       struct fsnotify_mark *mark;
16932 +       unsigned long long ull;
16933 +       struct fsnotify_group *group;
16934 +
16935 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16936 +       BUG_ON(!ull);
16937 +
16938 +       mark = &hn->hn_mark;
16939 +       spin_lock(&mark->lock);
16940 +       group = mark->group;
16941 +       fsnotify_get_group(group);
16942 +       spin_unlock(&mark->lock);
16943 +       lockdep_off();
16944 +       fsnotify_destroy_mark(mark, group);
16945 +       fsnotify_put_mark(mark);
16946 +       fsnotify_put_group(group);
16947 +       lockdep_on();
16948 +
16949 +       /* free hn by myself */
16950 +       return 0;
16951 +}
16952 +
16953 +/* ---------------------------------------------------------------------- */
16954 +
16955 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16956 +{
16957 +       struct fsnotify_mark *mark;
16958 +
16959 +       mark = &hinode->hi_notify->hn_mark;
16960 +       spin_lock(&mark->lock);
16961 +       if (do_set) {
16962 +               AuDebugOn(mark->mask & AuHfsnMask);
16963 +               mark->mask |= AuHfsnMask;
16964 +       } else {
16965 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16966 +               mark->mask &= ~AuHfsnMask;
16967 +       }
16968 +       spin_unlock(&mark->lock);
16969 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16970 +}
16971 +
16972 +/* ---------------------------------------------------------------------- */
16973 +
16974 +/* #define AuDbgHnotify */
16975 +#ifdef AuDbgHnotify
16976 +static char *au_hfsn_name(u32 mask)
16977 +{
16978 +#ifdef CONFIG_AUFS_DEBUG
16979 +#define test_ret(flag)                         \
16980 +       do {                                    \
16981 +               if (mask & flag)                \
16982 +                       return #flag;           \
16983 +       } while (0)
16984 +       test_ret(FS_ACCESS);
16985 +       test_ret(FS_MODIFY);
16986 +       test_ret(FS_ATTRIB);
16987 +       test_ret(FS_CLOSE_WRITE);
16988 +       test_ret(FS_CLOSE_NOWRITE);
16989 +       test_ret(FS_OPEN);
16990 +       test_ret(FS_MOVED_FROM);
16991 +       test_ret(FS_MOVED_TO);
16992 +       test_ret(FS_CREATE);
16993 +       test_ret(FS_DELETE);
16994 +       test_ret(FS_DELETE_SELF);
16995 +       test_ret(FS_MOVE_SELF);
16996 +       test_ret(FS_UNMOUNT);
16997 +       test_ret(FS_Q_OVERFLOW);
16998 +       test_ret(FS_IN_IGNORED);
16999 +       test_ret(FS_ISDIR);
17000 +       test_ret(FS_IN_ONESHOT);
17001 +       test_ret(FS_EVENT_ON_CHILD);
17002 +       return "";
17003 +#undef test_ret
17004 +#else
17005 +       return "??";
17006 +#endif
17007 +}
17008 +#endif
17009 +
17010 +/* ---------------------------------------------------------------------- */
17011 +
17012 +static void au_hfsn_free_group(struct fsnotify_group *group)
17013 +{
17014 +       struct au_br_hfsnotify *hfsn = group->private;
17015 +
17016 +       /* AuDbg("here\n"); */
17017 +       kfree(hfsn);
17018 +}
17019 +
17020 +static int au_hfsn_handle_event(struct fsnotify_group *group,
17021 +                               struct inode *inode,
17022 +                               u32 mask, const void *data, int data_type,
17023 +                               const unsigned char *file_name, u32 cookie,
17024 +                               struct fsnotify_iter_info *iter_info)
17025 +{
17026 +       int err;
17027 +       struct au_hnotify *hnotify;
17028 +       struct inode *h_dir, *h_inode;
17029 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
17030 +       struct fsnotify_mark *inode_mark;
17031 +
17032 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
17033 +
17034 +       err = 0;
17035 +       /* if FS_UNMOUNT happens, there must be another bug */
17036 +       AuDebugOn(mask & FS_UNMOUNT);
17037 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
17038 +               goto out;
17039 +
17040 +       h_dir = inode;
17041 +       h_inode = NULL;
17042 +#ifdef AuDbgHnotify
17043 +       au_debug_on();
17044 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17045 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17046 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17047 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
17048 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17049 +               /* WARN_ON(1); */
17050 +       }
17051 +       au_debug_off();
17052 +#endif
17053 +
17054 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
17055 +       AuDebugOn(!inode_mark);
17056 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17057 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
17058 +
17059 +out:
17060 +       return err;
17061 +}
17062 +
17063 +static struct fsnotify_ops au_hfsn_ops = {
17064 +       .handle_event           = au_hfsn_handle_event,
17065 +       .free_group_priv        = au_hfsn_free_group,
17066 +       .free_mark              = au_hfsn_free_mark
17067 +};
17068 +
17069 +/* ---------------------------------------------------------------------- */
17070 +
17071 +static void au_hfsn_fin_br(struct au_branch *br)
17072 +{
17073 +       struct au_br_hfsnotify *hfsn;
17074 +
17075 +       hfsn = br->br_hfsn;
17076 +       if (hfsn) {
17077 +               lockdep_off();
17078 +               fsnotify_put_group(hfsn->hfsn_group);
17079 +               lockdep_on();
17080 +       }
17081 +}
17082 +
17083 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17084 +{
17085 +       int err;
17086 +       struct fsnotify_group *group;
17087 +       struct au_br_hfsnotify *hfsn;
17088 +
17089 +       err = 0;
17090 +       br->br_hfsn = NULL;
17091 +       if (!au_br_hnotifyable(perm))
17092 +               goto out;
17093 +
17094 +       err = -ENOMEM;
17095 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17096 +       if (unlikely(!hfsn))
17097 +               goto out;
17098 +
17099 +       err = 0;
17100 +       group = fsnotify_alloc_group(&au_hfsn_ops);
17101 +       if (IS_ERR(group)) {
17102 +               err = PTR_ERR(group);
17103 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
17104 +               goto out_hfsn;
17105 +       }
17106 +
17107 +       group->private = hfsn;
17108 +       hfsn->hfsn_group = group;
17109 +       br->br_hfsn = hfsn;
17110 +       goto out; /* success */
17111 +
17112 +out_hfsn:
17113 +       kfree(hfsn);
17114 +out:
17115 +       return err;
17116 +}
17117 +
17118 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17119 +{
17120 +       int err;
17121 +
17122 +       err = 0;
17123 +       if (!br->br_hfsn)
17124 +               err = au_hfsn_init_br(br, perm);
17125 +
17126 +       return err;
17127 +}
17128 +
17129 +/* ---------------------------------------------------------------------- */
17130 +
17131 +static void au_hfsn_fin(void)
17132 +{
17133 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17134 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17135 +}
17136 +
17137 +const struct au_hnotify_op au_hnotify_op = {
17138 +       .ctl            = au_hfsn_ctl,
17139 +       .alloc          = au_hfsn_alloc,
17140 +       .free           = au_hfsn_free,
17141 +
17142 +       .fin            = au_hfsn_fin,
17143 +
17144 +       .reset_br       = au_hfsn_reset_br,
17145 +       .fin_br         = au_hfsn_fin_br,
17146 +       .init_br        = au_hfsn_init_br
17147 +};
17148 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17149 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
17150 +++ linux/fs/aufs/hfsplus.c     2018-08-12 23:43:05.456791299 +0200
17151 @@ -0,0 +1,57 @@
17152 +// SPDX-License-Identifier: GPL-2.0
17153 +/*
17154 + * Copyright (C) 2010-2018 Junjiro R. Okajima
17155 + *
17156 + * This program, aufs is free software; you can redistribute it and/or modify
17157 + * it under the terms of the GNU General Public License as published by
17158 + * the Free Software Foundation; either version 2 of the License, or
17159 + * (at your option) any later version.
17160 + *
17161 + * This program is distributed in the hope that it will be useful,
17162 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17163 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17164 + * GNU General Public License for more details.
17165 + *
17166 + * You should have received a copy of the GNU General Public License
17167 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17168 + */
17169 +
17170 +/*
17171 + * special support for filesystems which aqucires an inode mutex
17172 + * at final closing a file, eg, hfsplus.
17173 + *
17174 + * This trick is very simple and stupid, just to open the file before really
17175 + * neceeary open to tell hfsplus that this is not the final closing.
17176 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17177 + * and au_h_open_post() after releasing it.
17178 + */
17179 +
17180 +#include "aufs.h"
17181 +
17182 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17183 +                          int force_wr)
17184 +{
17185 +       struct file *h_file;
17186 +       struct dentry *h_dentry;
17187 +
17188 +       h_dentry = au_h_dptr(dentry, bindex);
17189 +       AuDebugOn(!h_dentry);
17190 +       AuDebugOn(d_is_negative(h_dentry));
17191 +
17192 +       h_file = NULL;
17193 +       if (au_test_hfsplus(h_dentry->d_sb)
17194 +           && d_is_reg(h_dentry))
17195 +               h_file = au_h_open(dentry, bindex,
17196 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17197 +                                  /*file*/NULL, force_wr);
17198 +       return h_file;
17199 +}
17200 +
17201 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17202 +                   struct file *h_file)
17203 +{
17204 +       if (h_file) {
17205 +               fput(h_file);
17206 +               au_sbr_put(dentry->d_sb, bindex);
17207 +       }
17208 +}
17209 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17210 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17211 +++ linux/fs/aufs/hnotify.c     2018-08-12 23:43:05.456791299 +0200
17212 @@ -0,0 +1,720 @@
17213 +// SPDX-License-Identifier: GPL-2.0
17214 +/*
17215 + * Copyright (C) 2005-2018 Junjiro R. Okajima
17216 + *
17217 + * This program, aufs is free software; you can redistribute it and/or modify
17218 + * it under the terms of the GNU General Public License as published by
17219 + * the Free Software Foundation; either version 2 of the License, or
17220 + * (at your option) any later version.
17221 + *
17222 + * This program is distributed in the hope that it will be useful,
17223 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17224 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17225 + * GNU General Public License for more details.
17226 + *
17227 + * You should have received a copy of the GNU General Public License
17228 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17229 + */
17230 +
17231 +/*
17232 + * abstraction to notify the direct changes on lower directories
17233 + */
17234 +
17235 +#include "aufs.h"
17236 +
17237 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17238 +{
17239 +       int err;
17240 +       struct au_hnotify *hn;
17241 +
17242 +       err = -ENOMEM;
17243 +       hn = au_cache_alloc_hnotify();
17244 +       if (hn) {
17245 +               hn->hn_aufs_inode = inode;
17246 +               hinode->hi_notify = hn;
17247 +               err = au_hnotify_op.alloc(hinode);
17248 +               AuTraceErr(err);
17249 +               if (unlikely(err)) {
17250 +                       hinode->hi_notify = NULL;
17251 +                       au_cache_free_hnotify(hn);
17252 +                       /*
17253 +                        * The upper dir was removed by udba, but the same named
17254 +                        * dir left. In this case, aufs assignes a new inode
17255 +                        * number and set the monitor again.
17256 +                        * For the lower dir, the old monitnor is still left.
17257 +                        */
17258 +                       if (err == -EEXIST)
17259 +                               err = 0;
17260 +               }
17261 +       }
17262 +
17263 +       AuTraceErr(err);
17264 +       return err;
17265 +}
17266 +
17267 +void au_hn_free(struct au_hinode *hinode)
17268 +{
17269 +       struct au_hnotify *hn;
17270 +
17271 +       hn = hinode->hi_notify;
17272 +       if (hn) {
17273 +               hinode->hi_notify = NULL;
17274 +               if (au_hnotify_op.free(hinode, hn))
17275 +                       au_cache_free_hnotify(hn);
17276 +       }
17277 +}
17278 +
17279 +/* ---------------------------------------------------------------------- */
17280 +
17281 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17282 +{
17283 +       if (hinode->hi_notify)
17284 +               au_hnotify_op.ctl(hinode, do_set);
17285 +}
17286 +
17287 +void au_hn_reset(struct inode *inode, unsigned int flags)
17288 +{
17289 +       aufs_bindex_t bindex, bbot;
17290 +       struct inode *hi;
17291 +       struct dentry *iwhdentry;
17292 +
17293 +       bbot = au_ibbot(inode);
17294 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17295 +               hi = au_h_iptr(inode, bindex);
17296 +               if (!hi)
17297 +                       continue;
17298 +
17299 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17300 +               iwhdentry = au_hi_wh(inode, bindex);
17301 +               if (iwhdentry)
17302 +                       dget(iwhdentry);
17303 +               au_igrab(hi);
17304 +               au_set_h_iptr(inode, bindex, NULL, 0);
17305 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17306 +                             flags & ~AuHi_XINO);
17307 +               iput(hi);
17308 +               dput(iwhdentry);
17309 +               /* inode_unlock(hi); */
17310 +       }
17311 +}
17312 +
17313 +/* ---------------------------------------------------------------------- */
17314 +
17315 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17316 +{
17317 +       int err;
17318 +       aufs_bindex_t bindex, bbot, bfound, btop;
17319 +       struct inode *h_i;
17320 +
17321 +       err = 0;
17322 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17323 +               pr_warn("branch root dir was changed\n");
17324 +               goto out;
17325 +       }
17326 +
17327 +       bfound = -1;
17328 +       bbot = au_ibbot(inode);
17329 +       btop = au_ibtop(inode);
17330 +#if 0 /* reserved for future use */
17331 +       if (bindex == bbot) {
17332 +               /* keep this ino in rename case */
17333 +               goto out;
17334 +       }
17335 +#endif
17336 +       for (bindex = btop; bindex <= bbot; bindex++)
17337 +               if (au_h_iptr(inode, bindex) == h_inode) {
17338 +                       bfound = bindex;
17339 +                       break;
17340 +               }
17341 +       if (bfound < 0)
17342 +               goto out;
17343 +
17344 +       for (bindex = btop; bindex <= bbot; bindex++) {
17345 +               h_i = au_h_iptr(inode, bindex);
17346 +               if (!h_i)
17347 +                       continue;
17348 +
17349 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17350 +               /* ignore this error */
17351 +               /* bad action? */
17352 +       }
17353 +
17354 +       /* children inode number will be broken */
17355 +
17356 +out:
17357 +       AuTraceErr(err);
17358 +       return err;
17359 +}
17360 +
17361 +static int hn_gen_tree(struct dentry *dentry)
17362 +{
17363 +       int err, i, j, ndentry;
17364 +       struct au_dcsub_pages dpages;
17365 +       struct au_dpage *dpage;
17366 +       struct dentry **dentries;
17367 +
17368 +       err = au_dpages_init(&dpages, GFP_NOFS);
17369 +       if (unlikely(err))
17370 +               goto out;
17371 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17372 +       if (unlikely(err))
17373 +               goto out_dpages;
17374 +
17375 +       for (i = 0; i < dpages.ndpage; i++) {
17376 +               dpage = dpages.dpages + i;
17377 +               dentries = dpage->dentries;
17378 +               ndentry = dpage->ndentry;
17379 +               for (j = 0; j < ndentry; j++) {
17380 +                       struct dentry *d;
17381 +
17382 +                       d = dentries[j];
17383 +                       if (IS_ROOT(d))
17384 +                               continue;
17385 +
17386 +                       au_digen_dec(d);
17387 +                       if (d_really_is_positive(d))
17388 +                               /* todo: reset children xino?
17389 +                                  cached children only? */
17390 +                               au_iigen_dec(d_inode(d));
17391 +               }
17392 +       }
17393 +
17394 +out_dpages:
17395 +       au_dpages_free(&dpages);
17396 +
17397 +#if 0
17398 +       /* discard children */
17399 +       dentry_unhash(dentry);
17400 +       dput(dentry);
17401 +#endif
17402 +out:
17403 +       return err;
17404 +}
17405 +
17406 +/*
17407 + * return 0 if processed.
17408 + */
17409 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17410 +                          const unsigned int isdir)
17411 +{
17412 +       int err;
17413 +       struct dentry *d;
17414 +       struct qstr *dname;
17415 +
17416 +       err = 1;
17417 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17418 +               pr_warn("branch root dir was changed\n");
17419 +               err = 0;
17420 +               goto out;
17421 +       }
17422 +
17423 +       if (!isdir) {
17424 +               AuDebugOn(!name);
17425 +               au_iigen_dec(inode);
17426 +               spin_lock(&inode->i_lock);
17427 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17428 +                       spin_lock(&d->d_lock);
17429 +                       dname = &d->d_name;
17430 +                       if (dname->len != nlen
17431 +                           && memcmp(dname->name, name, nlen)) {
17432 +                               spin_unlock(&d->d_lock);
17433 +                               continue;
17434 +                       }
17435 +                       err = 0;
17436 +                       au_digen_dec(d);
17437 +                       spin_unlock(&d->d_lock);
17438 +                       break;
17439 +               }
17440 +               spin_unlock(&inode->i_lock);
17441 +       } else {
17442 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17443 +               d = d_find_any_alias(inode);
17444 +               if (!d) {
17445 +                       au_iigen_dec(inode);
17446 +                       goto out;
17447 +               }
17448 +
17449 +               spin_lock(&d->d_lock);
17450 +               dname = &d->d_name;
17451 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17452 +                       spin_unlock(&d->d_lock);
17453 +                       err = hn_gen_tree(d);
17454 +                       spin_lock(&d->d_lock);
17455 +               }
17456 +               spin_unlock(&d->d_lock);
17457 +               dput(d);
17458 +       }
17459 +
17460 +out:
17461 +       AuTraceErr(err);
17462 +       return err;
17463 +}
17464 +
17465 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17466 +{
17467 +       int err;
17468 +
17469 +       if (IS_ROOT(dentry)) {
17470 +               pr_warn("branch root dir was changed\n");
17471 +               return 0;
17472 +       }
17473 +
17474 +       err = 0;
17475 +       if (!isdir) {
17476 +               au_digen_dec(dentry);
17477 +               if (d_really_is_positive(dentry))
17478 +                       au_iigen_dec(d_inode(dentry));
17479 +       } else {
17480 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17481 +               if (d_really_is_positive(dentry))
17482 +                       err = hn_gen_tree(dentry);
17483 +       }
17484 +
17485 +       AuTraceErr(err);
17486 +       return err;
17487 +}
17488 +
17489 +/* ---------------------------------------------------------------------- */
17490 +
17491 +/* hnotify job flags */
17492 +#define AuHnJob_XINO0          1
17493 +#define AuHnJob_GEN            (1 << 1)
17494 +#define AuHnJob_DIRENT         (1 << 2)
17495 +#define AuHnJob_ISDIR          (1 << 3)
17496 +#define AuHnJob_TRYXINO0       (1 << 4)
17497 +#define AuHnJob_MNTPNT         (1 << 5)
17498 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17499 +#define au_fset_hnjob(flags, name) \
17500 +       do { (flags) |= AuHnJob_##name; } while (0)
17501 +#define au_fclr_hnjob(flags, name) \
17502 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17503 +
17504 +enum {
17505 +       AuHn_CHILD,
17506 +       AuHn_PARENT,
17507 +       AuHnLast
17508 +};
17509 +
17510 +struct au_hnotify_args {
17511 +       struct inode *h_dir, *dir, *h_child_inode;
17512 +       u32 mask;
17513 +       unsigned int flags[AuHnLast];
17514 +       unsigned int h_child_nlen;
17515 +       char h_child_name[];
17516 +};
17517 +
17518 +struct hn_job_args {
17519 +       unsigned int flags;
17520 +       struct inode *inode, *h_inode, *dir, *h_dir;
17521 +       struct dentry *dentry;
17522 +       char *h_name;
17523 +       int h_nlen;
17524 +};
17525 +
17526 +static int hn_job(struct hn_job_args *a)
17527 +{
17528 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17529 +       int e;
17530 +
17531 +       /* reset xino */
17532 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17533 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17534 +
17535 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17536 +           && a->inode
17537 +           && a->h_inode) {
17538 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17539 +               if (!a->h_inode->i_nlink
17540 +                   && !(a->h_inode->i_state & I_LINKABLE))
17541 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17542 +               inode_unlock_shared(a->h_inode);
17543 +       }
17544 +
17545 +       /* make the generation obsolete */
17546 +       if (au_ftest_hnjob(a->flags, GEN)) {
17547 +               e = -1;
17548 +               if (a->inode)
17549 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17550 +                                             isdir);
17551 +               if (e && a->dentry)
17552 +                       hn_gen_by_name(a->dentry, isdir);
17553 +               /* ignore this error */
17554 +       }
17555 +
17556 +       /* make dir entries obsolete */
17557 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17558 +               struct au_vdir *vdir;
17559 +
17560 +               vdir = au_ivdir(a->inode);
17561 +               if (vdir)
17562 +                       vdir->vd_jiffy = 0;
17563 +               /* IMustLock(a->inode); */
17564 +               /* inode_inc_iversion(a->inode); */
17565 +       }
17566 +
17567 +       /* can do nothing but warn */
17568 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17569 +           && a->dentry
17570 +           && d_mountpoint(a->dentry))
17571 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17572 +
17573 +       return 0;
17574 +}
17575 +
17576 +/* ---------------------------------------------------------------------- */
17577 +
17578 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17579 +                                          struct inode *dir)
17580 +{
17581 +       struct dentry *dentry, *d, *parent;
17582 +       struct qstr *dname;
17583 +
17584 +       parent = d_find_any_alias(dir);
17585 +       if (!parent)
17586 +               return NULL;
17587 +
17588 +       dentry = NULL;
17589 +       spin_lock(&parent->d_lock);
17590 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17591 +               /* AuDbg("%pd\n", d); */
17592 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17593 +               dname = &d->d_name;
17594 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17595 +                       goto cont_unlock;
17596 +               if (au_di(d))
17597 +                       au_digen_dec(d);
17598 +               else
17599 +                       goto cont_unlock;
17600 +               if (au_dcount(d) > 0) {
17601 +                       dentry = dget_dlock(d);
17602 +                       spin_unlock(&d->d_lock);
17603 +                       break;
17604 +               }
17605 +
17606 +cont_unlock:
17607 +               spin_unlock(&d->d_lock);
17608 +       }
17609 +       spin_unlock(&parent->d_lock);
17610 +       dput(parent);
17611 +
17612 +       if (dentry)
17613 +               di_write_lock_child(dentry);
17614 +
17615 +       return dentry;
17616 +}
17617 +
17618 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17619 +                                        aufs_bindex_t bindex, ino_t h_ino)
17620 +{
17621 +       struct inode *inode;
17622 +       ino_t ino;
17623 +       int err;
17624 +
17625 +       inode = NULL;
17626 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17627 +       if (!err && ino)
17628 +               inode = ilookup(sb, ino);
17629 +       if (!inode)
17630 +               goto out;
17631 +
17632 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17633 +               pr_warn("wrong root branch\n");
17634 +               iput(inode);
17635 +               inode = NULL;
17636 +               goto out;
17637 +       }
17638 +
17639 +       ii_write_lock_child(inode);
17640 +
17641 +out:
17642 +       return inode;
17643 +}
17644 +
17645 +static void au_hn_bh(void *_args)
17646 +{
17647 +       struct au_hnotify_args *a = _args;
17648 +       struct super_block *sb;
17649 +       aufs_bindex_t bindex, bbot, bfound;
17650 +       unsigned char xino, try_iput;
17651 +       int err;
17652 +       struct inode *inode;
17653 +       ino_t h_ino;
17654 +       struct hn_job_args args;
17655 +       struct dentry *dentry;
17656 +       struct au_sbinfo *sbinfo;
17657 +
17658 +       AuDebugOn(!_args);
17659 +       AuDebugOn(!a->h_dir);
17660 +       AuDebugOn(!a->dir);
17661 +       AuDebugOn(!a->mask);
17662 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17663 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17664 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17665 +
17666 +       inode = NULL;
17667 +       dentry = NULL;
17668 +       /*
17669 +        * do not lock a->dir->i_mutex here
17670 +        * because of d_revalidate() may cause a deadlock.
17671 +        */
17672 +       sb = a->dir->i_sb;
17673 +       AuDebugOn(!sb);
17674 +       sbinfo = au_sbi(sb);
17675 +       AuDebugOn(!sbinfo);
17676 +       si_write_lock(sb, AuLock_NOPLMW);
17677 +
17678 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17679 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17680 +               case FS_MOVED_FROM:
17681 +               case FS_MOVED_TO:
17682 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17683 +                               "for the direct rename(2)\n");
17684 +               }
17685 +
17686 +       ii_read_lock_parent(a->dir);
17687 +       bfound = -1;
17688 +       bbot = au_ibbot(a->dir);
17689 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17690 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17691 +                       bfound = bindex;
17692 +                       break;
17693 +               }
17694 +       ii_read_unlock(a->dir);
17695 +       if (unlikely(bfound < 0))
17696 +               goto out;
17697 +
17698 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17699 +       h_ino = 0;
17700 +       if (a->h_child_inode)
17701 +               h_ino = a->h_child_inode->i_ino;
17702 +
17703 +       if (a->h_child_nlen
17704 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17705 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17706 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17707 +                                             a->dir);
17708 +       try_iput = 0;
17709 +       if (dentry && d_really_is_positive(dentry))
17710 +               inode = d_inode(dentry);
17711 +       if (xino && !inode && h_ino
17712 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17713 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17714 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17715 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17716 +               try_iput = 1;
17717 +       }
17718 +
17719 +       args.flags = a->flags[AuHn_CHILD];
17720 +       args.dentry = dentry;
17721 +       args.inode = inode;
17722 +       args.h_inode = a->h_child_inode;
17723 +       args.dir = a->dir;
17724 +       args.h_dir = a->h_dir;
17725 +       args.h_name = a->h_child_name;
17726 +       args.h_nlen = a->h_child_nlen;
17727 +       err = hn_job(&args);
17728 +       if (dentry) {
17729 +               if (au_di(dentry))
17730 +                       di_write_unlock(dentry);
17731 +               dput(dentry);
17732 +       }
17733 +       if (inode && try_iput) {
17734 +               ii_write_unlock(inode);
17735 +               iput(inode);
17736 +       }
17737 +
17738 +       ii_write_lock_parent(a->dir);
17739 +       args.flags = a->flags[AuHn_PARENT];
17740 +       args.dentry = NULL;
17741 +       args.inode = a->dir;
17742 +       args.h_inode = a->h_dir;
17743 +       args.dir = NULL;
17744 +       args.h_dir = NULL;
17745 +       args.h_name = NULL;
17746 +       args.h_nlen = 0;
17747 +       err = hn_job(&args);
17748 +       ii_write_unlock(a->dir);
17749 +
17750 +out:
17751 +       iput(a->h_child_inode);
17752 +       iput(a->h_dir);
17753 +       iput(a->dir);
17754 +       si_write_unlock(sb);
17755 +       au_nwt_done(&sbinfo->si_nowait);
17756 +       kfree(a);
17757 +}
17758 +
17759 +/* ---------------------------------------------------------------------- */
17760 +
17761 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17762 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
17763 +{
17764 +       int err, len;
17765 +       unsigned int flags[AuHnLast], f;
17766 +       unsigned char isdir, isroot, wh;
17767 +       struct inode *dir;
17768 +       struct au_hnotify_args *args;
17769 +       char *p, *h_child_name;
17770 +
17771 +       err = 0;
17772 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17773 +       dir = igrab(hnotify->hn_aufs_inode);
17774 +       if (!dir)
17775 +               goto out;
17776 +
17777 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17778 +       wh = 0;
17779 +       h_child_name = (void *)h_child_qstr->name;
17780 +       len = h_child_qstr->len;
17781 +       if (h_child_name) {
17782 +               if (len > AUFS_WH_PFX_LEN
17783 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17784 +                       h_child_name += AUFS_WH_PFX_LEN;
17785 +                       len -= AUFS_WH_PFX_LEN;
17786 +                       wh = 1;
17787 +               }
17788 +       }
17789 +
17790 +       isdir = 0;
17791 +       if (h_child_inode)
17792 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17793 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17794 +       flags[AuHn_CHILD] = 0;
17795 +       if (isdir)
17796 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17797 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17798 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17799 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17800 +       case FS_MOVED_FROM:
17801 +       case FS_MOVED_TO:
17802 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17803 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17804 +               /*FALLTHROUGH*/
17805 +       case FS_CREATE:
17806 +               AuDebugOn(!h_child_name);
17807 +               break;
17808 +
17809 +       case FS_DELETE:
17810 +               /*
17811 +                * aufs never be able to get this child inode.
17812 +                * revalidation should be in d_revalidate()
17813 +                * by checking i_nlink, i_generation or d_unhashed().
17814 +                */
17815 +               AuDebugOn(!h_child_name);
17816 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17817 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17818 +               break;
17819 +
17820 +       default:
17821 +               AuDebugOn(1);
17822 +       }
17823 +
17824 +       if (wh)
17825 +               h_child_inode = NULL;
17826 +
17827 +       err = -ENOMEM;
17828 +       /* iput() and kfree() will be called in au_hnotify() */
17829 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17830 +       if (unlikely(!args)) {
17831 +               AuErr1("no memory\n");
17832 +               iput(dir);
17833 +               goto out;
17834 +       }
17835 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17836 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17837 +       args->mask = mask;
17838 +       args->dir = dir;
17839 +       args->h_dir = igrab(h_dir);
17840 +       if (h_child_inode)
17841 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17842 +       args->h_child_inode = h_child_inode;
17843 +       args->h_child_nlen = len;
17844 +       if (len) {
17845 +               p = (void *)args;
17846 +               p += sizeof(*args);
17847 +               memcpy(p, h_child_name, len);
17848 +               p[len] = 0;
17849 +       }
17850 +
17851 +       /* NFS fires the event for silly-renamed one from kworker */
17852 +       f = 0;
17853 +       if (!dir->i_nlink
17854 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17855 +               f = AuWkq_NEST;
17856 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17857 +       if (unlikely(err)) {
17858 +               pr_err("wkq %d\n", err);
17859 +               iput(args->h_child_inode);
17860 +               iput(args->h_dir);
17861 +               iput(args->dir);
17862 +               kfree(args);
17863 +       }
17864 +
17865 +out:
17866 +       return err;
17867 +}
17868 +
17869 +/* ---------------------------------------------------------------------- */
17870 +
17871 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17872 +{
17873 +       int err;
17874 +
17875 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17876 +
17877 +       err = 0;
17878 +       if (au_hnotify_op.reset_br)
17879 +               err = au_hnotify_op.reset_br(udba, br, perm);
17880 +
17881 +       return err;
17882 +}
17883 +
17884 +int au_hnotify_init_br(struct au_branch *br, int perm)
17885 +{
17886 +       int err;
17887 +
17888 +       err = 0;
17889 +       if (au_hnotify_op.init_br)
17890 +               err = au_hnotify_op.init_br(br, perm);
17891 +
17892 +       return err;
17893 +}
17894 +
17895 +void au_hnotify_fin_br(struct au_branch *br)
17896 +{
17897 +       if (au_hnotify_op.fin_br)
17898 +               au_hnotify_op.fin_br(br);
17899 +}
17900 +
17901 +static void au_hn_destroy_cache(void)
17902 +{
17903 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17904 +       au_cache[AuCache_HNOTIFY] = NULL;
17905 +}
17906 +
17907 +int __init au_hnotify_init(void)
17908 +{
17909 +       int err;
17910 +
17911 +       err = -ENOMEM;
17912 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17913 +       if (au_cache[AuCache_HNOTIFY]) {
17914 +               err = 0;
17915 +               if (au_hnotify_op.init)
17916 +                       err = au_hnotify_op.init();
17917 +               if (unlikely(err))
17918 +                       au_hn_destroy_cache();
17919 +       }
17920 +       AuTraceErr(err);
17921 +       return err;
17922 +}
17923 +
17924 +void au_hnotify_fin(void)
17925 +{
17926 +       if (au_hnotify_op.fin)
17927 +               au_hnotify_op.fin();
17928 +
17929 +       /* cf. au_cache_fin() */
17930 +       if (au_cache[AuCache_HNOTIFY])
17931 +               au_hn_destroy_cache();
17932 +}
17933 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17934 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17935 +++ linux/fs/aufs/iinfo.c       2018-08-12 23:43:05.460124736 +0200
17936 @@ -0,0 +1,286 @@
17937 +// SPDX-License-Identifier: GPL-2.0
17938 +/*
17939 + * Copyright (C) 2005-2018 Junjiro R. Okajima
17940 + *
17941 + * This program, aufs is free software; you can redistribute it and/or modify
17942 + * it under the terms of the GNU General Public License as published by
17943 + * the Free Software Foundation; either version 2 of the License, or
17944 + * (at your option) any later version.
17945 + *
17946 + * This program is distributed in the hope that it will be useful,
17947 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17948 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17949 + * GNU General Public License for more details.
17950 + *
17951 + * You should have received a copy of the GNU General Public License
17952 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17953 + */
17954 +
17955 +/*
17956 + * inode private data
17957 + */
17958 +
17959 +#include "aufs.h"
17960 +
17961 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17962 +{
17963 +       struct inode *h_inode;
17964 +       struct au_hinode *hinode;
17965 +
17966 +       IiMustAnyLock(inode);
17967 +
17968 +       hinode = au_hinode(au_ii(inode), bindex);
17969 +       h_inode = hinode->hi_inode;
17970 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17971 +       return h_inode;
17972 +}
17973 +
17974 +/* todo: hard/soft set? */
17975 +void au_hiput(struct au_hinode *hinode)
17976 +{
17977 +       au_hn_free(hinode);
17978 +       dput(hinode->hi_whdentry);
17979 +       iput(hinode->hi_inode);
17980 +}
17981 +
17982 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17983 +{
17984 +       unsigned int flags;
17985 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17986 +
17987 +       flags = 0;
17988 +       if (au_opt_test(mnt_flags, XINO))
17989 +               au_fset_hi(flags, XINO);
17990 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17991 +               au_fset_hi(flags, HNOTIFY);
17992 +       return flags;
17993 +}
17994 +
17995 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17996 +                  struct inode *h_inode, unsigned int flags)
17997 +{
17998 +       struct au_hinode *hinode;
17999 +       struct inode *hi;
18000 +       struct au_iinfo *iinfo = au_ii(inode);
18001 +
18002 +       IiMustWriteLock(inode);
18003 +
18004 +       hinode = au_hinode(iinfo, bindex);
18005 +       hi = hinode->hi_inode;
18006 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18007 +
18008 +       if (hi)
18009 +               au_hiput(hinode);
18010 +       hinode->hi_inode = h_inode;
18011 +       if (h_inode) {
18012 +               int err;
18013 +               struct super_block *sb = inode->i_sb;
18014 +               struct au_branch *br;
18015 +
18016 +               AuDebugOn(inode->i_mode
18017 +                         && (h_inode->i_mode & S_IFMT)
18018 +                         != (inode->i_mode & S_IFMT));
18019 +               if (bindex == iinfo->ii_btop)
18020 +                       au_cpup_igen(inode, h_inode);
18021 +               br = au_sbr(sb, bindex);
18022 +               hinode->hi_id = br->br_id;
18023 +               if (au_ftest_hi(flags, XINO)) {
18024 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
18025 +                                           inode->i_ino);
18026 +                       if (unlikely(err))
18027 +                               AuIOErr1("failed au_xino_write() %d\n", err);
18028 +               }
18029 +
18030 +               if (au_ftest_hi(flags, HNOTIFY)
18031 +                   && au_br_hnotifyable(br->br_perm)) {
18032 +                       err = au_hn_alloc(hinode, inode);
18033 +                       if (unlikely(err))
18034 +                               AuIOErr1("au_hn_alloc() %d\n", err);
18035 +               }
18036 +       }
18037 +}
18038 +
18039 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18040 +                 struct dentry *h_wh)
18041 +{
18042 +       struct au_hinode *hinode;
18043 +
18044 +       IiMustWriteLock(inode);
18045 +
18046 +       hinode = au_hinode(au_ii(inode), bindex);
18047 +       AuDebugOn(hinode->hi_whdentry);
18048 +       hinode->hi_whdentry = h_wh;
18049 +}
18050 +
18051 +void au_update_iigen(struct inode *inode, int half)
18052 +{
18053 +       struct au_iinfo *iinfo;
18054 +       struct au_iigen *iigen;
18055 +       unsigned int sigen;
18056 +
18057 +       sigen = au_sigen(inode->i_sb);
18058 +       iinfo = au_ii(inode);
18059 +       iigen = &iinfo->ii_generation;
18060 +       spin_lock(&iigen->ig_spin);
18061 +       iigen->ig_generation = sigen;
18062 +       if (half)
18063 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18064 +       else
18065 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
18066 +       spin_unlock(&iigen->ig_spin);
18067 +}
18068 +
18069 +/* it may be called at remount time, too */
18070 +void au_update_ibrange(struct inode *inode, int do_put_zero)
18071 +{
18072 +       struct au_iinfo *iinfo;
18073 +       aufs_bindex_t bindex, bbot;
18074 +
18075 +       AuDebugOn(au_is_bad_inode(inode));
18076 +       IiMustWriteLock(inode);
18077 +
18078 +       iinfo = au_ii(inode);
18079 +       if (do_put_zero && iinfo->ii_btop >= 0) {
18080 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18081 +                    bindex++) {
18082 +                       struct inode *h_i;
18083 +
18084 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
18085 +                       if (h_i
18086 +                           && !h_i->i_nlink
18087 +                           && !(h_i->i_state & I_LINKABLE))
18088 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18089 +               }
18090 +       }
18091 +
18092 +       iinfo->ii_btop = -1;
18093 +       iinfo->ii_bbot = -1;
18094 +       bbot = au_sbbot(inode->i_sb);
18095 +       for (bindex = 0; bindex <= bbot; bindex++)
18096 +               if (au_hinode(iinfo, bindex)->hi_inode) {
18097 +                       iinfo->ii_btop = bindex;
18098 +                       break;
18099 +               }
18100 +       if (iinfo->ii_btop >= 0)
18101 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18102 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
18103 +                               iinfo->ii_bbot = bindex;
18104 +                               break;
18105 +                       }
18106 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
18107 +}
18108 +
18109 +/* ---------------------------------------------------------------------- */
18110 +
18111 +void au_icntnr_init_once(void *_c)
18112 +{
18113 +       struct au_icntnr *c = _c;
18114 +       struct au_iinfo *iinfo = &c->iinfo;
18115 +
18116 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
18117 +       au_rw_init(&iinfo->ii_rwsem);
18118 +       inode_init_once(&c->vfs_inode);
18119 +}
18120 +
18121 +void au_hinode_init(struct au_hinode *hinode)
18122 +{
18123 +       hinode->hi_inode = NULL;
18124 +       hinode->hi_id = -1;
18125 +       au_hn_init(hinode);
18126 +       hinode->hi_whdentry = NULL;
18127 +}
18128 +
18129 +int au_iinfo_init(struct inode *inode)
18130 +{
18131 +       struct au_iinfo *iinfo;
18132 +       struct super_block *sb;
18133 +       struct au_hinode *hi;
18134 +       int nbr, i;
18135 +
18136 +       sb = inode->i_sb;
18137 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18138 +       nbr = au_sbbot(sb) + 1;
18139 +       if (unlikely(nbr <= 0))
18140 +               nbr = 1;
18141 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18142 +       if (hi) {
18143 +               au_ninodes_inc(sb);
18144 +
18145 +               iinfo->ii_hinode = hi;
18146 +               for (i = 0; i < nbr; i++, hi++)
18147 +                       au_hinode_init(hi);
18148 +
18149 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
18150 +               iinfo->ii_btop = -1;
18151 +               iinfo->ii_bbot = -1;
18152 +               iinfo->ii_vdir = NULL;
18153 +               return 0;
18154 +       }
18155 +       return -ENOMEM;
18156 +}
18157 +
18158 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
18159 +{
18160 +       int err, i;
18161 +       struct au_hinode *hip;
18162 +
18163 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18164 +
18165 +       err = -ENOMEM;
18166 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18167 +                         may_shrink);
18168 +       if (hip) {
18169 +               iinfo->ii_hinode = hip;
18170 +               i = iinfo->ii_bbot + 1;
18171 +               hip += i;
18172 +               for (; i < nbr; i++, hip++)
18173 +                       au_hinode_init(hip);
18174 +               err = 0;
18175 +       }
18176 +
18177 +       return err;
18178 +}
18179 +
18180 +void au_iinfo_fin(struct inode *inode)
18181 +{
18182 +       struct au_iinfo *iinfo;
18183 +       struct au_hinode *hi;
18184 +       struct super_block *sb;
18185 +       aufs_bindex_t bindex, bbot;
18186 +       const unsigned char unlinked = !inode->i_nlink;
18187 +
18188 +       AuDebugOn(au_is_bad_inode(inode));
18189 +
18190 +       sb = inode->i_sb;
18191 +       au_ninodes_dec(sb);
18192 +       if (si_pid_test(sb))
18193 +               au_xino_delete_inode(inode, unlinked);
18194 +       else {
18195 +               /*
18196 +                * it is safe to hide the dependency between sbinfo and
18197 +                * sb->s_umount.
18198 +                */
18199 +               lockdep_off();
18200 +               si_noflush_read_lock(sb);
18201 +               au_xino_delete_inode(inode, unlinked);
18202 +               si_read_unlock(sb);
18203 +               lockdep_on();
18204 +       }
18205 +
18206 +       iinfo = au_ii(inode);
18207 +       if (iinfo->ii_vdir)
18208 +               au_vdir_free(iinfo->ii_vdir);
18209 +
18210 +       bindex = iinfo->ii_btop;
18211 +       if (bindex >= 0) {
18212 +               hi = au_hinode(iinfo, bindex);
18213 +               bbot = iinfo->ii_bbot;
18214 +               while (bindex++ <= bbot) {
18215 +                       if (hi->hi_inode)
18216 +                               au_hiput(hi);
18217 +                       hi++;
18218 +               }
18219 +       }
18220 +       kfree(iinfo->ii_hinode);
18221 +       AuRwDestroy(&iinfo->ii_rwsem);
18222 +}
18223 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18224 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18225 +++ linux/fs/aufs/inode.c       2018-08-12 23:43:05.460124736 +0200
18226 @@ -0,0 +1,528 @@
18227 +// SPDX-License-Identifier: GPL-2.0
18228 +/*
18229 + * Copyright (C) 2005-2018 Junjiro R. Okajima
18230 + *
18231 + * This program, aufs is free software; you can redistribute it and/or modify
18232 + * it under the terms of the GNU General Public License as published by
18233 + * the Free Software Foundation; either version 2 of the License, or
18234 + * (at your option) any later version.
18235 + *
18236 + * This program is distributed in the hope that it will be useful,
18237 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18238 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18239 + * GNU General Public License for more details.
18240 + *
18241 + * You should have received a copy of the GNU General Public License
18242 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18243 + */
18244 +
18245 +/*
18246 + * inode functions
18247 + */
18248 +
18249 +#include "aufs.h"
18250 +
18251 +struct inode *au_igrab(struct inode *inode)
18252 +{
18253 +       if (inode) {
18254 +               AuDebugOn(!atomic_read(&inode->i_count));
18255 +               ihold(inode);
18256 +       }
18257 +       return inode;
18258 +}
18259 +
18260 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18261 +{
18262 +       au_cpup_attr_all(inode, /*force*/0);
18263 +       au_update_iigen(inode, /*half*/1);
18264 +       if (do_version)
18265 +               inode_inc_iversion(inode);
18266 +}
18267 +
18268 +static int au_ii_refresh(struct inode *inode, int *update)
18269 +{
18270 +       int err, e, nbr;
18271 +       umode_t type;
18272 +       aufs_bindex_t bindex, new_bindex;
18273 +       struct super_block *sb;
18274 +       struct au_iinfo *iinfo;
18275 +       struct au_hinode *p, *q, tmp;
18276 +
18277 +       AuDebugOn(au_is_bad_inode(inode));
18278 +       IiMustWriteLock(inode);
18279 +
18280 +       *update = 0;
18281 +       sb = inode->i_sb;
18282 +       nbr = au_sbbot(sb) + 1;
18283 +       type = inode->i_mode & S_IFMT;
18284 +       iinfo = au_ii(inode);
18285 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18286 +       if (unlikely(err))
18287 +               goto out;
18288 +
18289 +       AuDebugOn(iinfo->ii_btop < 0);
18290 +       p = au_hinode(iinfo, iinfo->ii_btop);
18291 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18292 +            bindex++, p++) {
18293 +               if (!p->hi_inode)
18294 +                       continue;
18295 +
18296 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18297 +               new_bindex = au_br_index(sb, p->hi_id);
18298 +               if (new_bindex == bindex)
18299 +                       continue;
18300 +
18301 +               if (new_bindex < 0) {
18302 +                       *update = 1;
18303 +                       au_hiput(p);
18304 +                       p->hi_inode = NULL;
18305 +                       continue;
18306 +               }
18307 +
18308 +               if (new_bindex < iinfo->ii_btop)
18309 +                       iinfo->ii_btop = new_bindex;
18310 +               if (iinfo->ii_bbot < new_bindex)
18311 +                       iinfo->ii_bbot = new_bindex;
18312 +               /* swap two lower inode, and loop again */
18313 +               q = au_hinode(iinfo, new_bindex);
18314 +               tmp = *q;
18315 +               *q = *p;
18316 +               *p = tmp;
18317 +               if (tmp.hi_inode) {
18318 +                       bindex--;
18319 +                       p--;
18320 +               }
18321 +       }
18322 +       au_update_ibrange(inode, /*do_put_zero*/0);
18323 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18324 +       e = au_dy_irefresh(inode);
18325 +       if (unlikely(e && !err))
18326 +               err = e;
18327 +
18328 +out:
18329 +       AuTraceErr(err);
18330 +       return err;
18331 +}
18332 +
18333 +void au_refresh_iop(struct inode *inode, int force_getattr)
18334 +{
18335 +       int type;
18336 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18337 +       const struct inode_operations *iop
18338 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18339 +
18340 +       if (inode->i_op == iop)
18341 +               return;
18342 +
18343 +       switch (inode->i_mode & S_IFMT) {
18344 +       case S_IFDIR:
18345 +               type = AuIop_DIR;
18346 +               break;
18347 +       case S_IFLNK:
18348 +               type = AuIop_SYMLINK;
18349 +               break;
18350 +       default:
18351 +               type = AuIop_OTHER;
18352 +               break;
18353 +       }
18354 +
18355 +       inode->i_op = iop + type;
18356 +       /* unnecessary smp_wmb() */
18357 +}
18358 +
18359 +int au_refresh_hinode_self(struct inode *inode)
18360 +{
18361 +       int err, update;
18362 +
18363 +       err = au_ii_refresh(inode, &update);
18364 +       if (!err)
18365 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18366 +
18367 +       AuTraceErr(err);
18368 +       return err;
18369 +}
18370 +
18371 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18372 +{
18373 +       int err, e, update;
18374 +       unsigned int flags;
18375 +       umode_t mode;
18376 +       aufs_bindex_t bindex, bbot;
18377 +       unsigned char isdir;
18378 +       struct au_hinode *p;
18379 +       struct au_iinfo *iinfo;
18380 +
18381 +       err = au_ii_refresh(inode, &update);
18382 +       if (unlikely(err))
18383 +               goto out;
18384 +
18385 +       update = 0;
18386 +       iinfo = au_ii(inode);
18387 +       p = au_hinode(iinfo, iinfo->ii_btop);
18388 +       mode = (inode->i_mode & S_IFMT);
18389 +       isdir = S_ISDIR(mode);
18390 +       flags = au_hi_flags(inode, isdir);
18391 +       bbot = au_dbbot(dentry);
18392 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18393 +               struct inode *h_i, *h_inode;
18394 +               struct dentry *h_d;
18395 +
18396 +               h_d = au_h_dptr(dentry, bindex);
18397 +               if (!h_d || d_is_negative(h_d))
18398 +                       continue;
18399 +
18400 +               h_inode = d_inode(h_d);
18401 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18402 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18403 +                       h_i = au_h_iptr(inode, bindex);
18404 +                       if (h_i) {
18405 +                               if (h_i == h_inode)
18406 +                                       continue;
18407 +                               err = -EIO;
18408 +                               break;
18409 +                       }
18410 +               }
18411 +               if (bindex < iinfo->ii_btop)
18412 +                       iinfo->ii_btop = bindex;
18413 +               if (iinfo->ii_bbot < bindex)
18414 +                       iinfo->ii_bbot = bindex;
18415 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18416 +               update = 1;
18417 +       }
18418 +       au_update_ibrange(inode, /*do_put_zero*/0);
18419 +       e = au_dy_irefresh(inode);
18420 +       if (unlikely(e && !err))
18421 +               err = e;
18422 +       if (!err)
18423 +               au_refresh_hinode_attr(inode, update && isdir);
18424 +
18425 +out:
18426 +       AuTraceErr(err);
18427 +       return err;
18428 +}
18429 +
18430 +static int set_inode(struct inode *inode, struct dentry *dentry)
18431 +{
18432 +       int err;
18433 +       unsigned int flags;
18434 +       umode_t mode;
18435 +       aufs_bindex_t bindex, btop, btail;
18436 +       unsigned char isdir;
18437 +       struct dentry *h_dentry;
18438 +       struct inode *h_inode;
18439 +       struct au_iinfo *iinfo;
18440 +       struct inode_operations *iop;
18441 +
18442 +       IiMustWriteLock(inode);
18443 +
18444 +       err = 0;
18445 +       isdir = 0;
18446 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18447 +       btop = au_dbtop(dentry);
18448 +       h_dentry = au_h_dptr(dentry, btop);
18449 +       h_inode = d_inode(h_dentry);
18450 +       mode = h_inode->i_mode;
18451 +       switch (mode & S_IFMT) {
18452 +       case S_IFREG:
18453 +               btail = au_dbtail(dentry);
18454 +               inode->i_op = iop + AuIop_OTHER;
18455 +               inode->i_fop = &aufs_file_fop;
18456 +               err = au_dy_iaop(inode, btop, h_inode);
18457 +               if (unlikely(err))
18458 +                       goto out;
18459 +               break;
18460 +       case S_IFDIR:
18461 +               isdir = 1;
18462 +               btail = au_dbtaildir(dentry);
18463 +               inode->i_op = iop + AuIop_DIR;
18464 +               inode->i_fop = &aufs_dir_fop;
18465 +               break;
18466 +       case S_IFLNK:
18467 +               btail = au_dbtail(dentry);
18468 +               inode->i_op = iop + AuIop_SYMLINK;
18469 +               break;
18470 +       case S_IFBLK:
18471 +       case S_IFCHR:
18472 +       case S_IFIFO:
18473 +       case S_IFSOCK:
18474 +               btail = au_dbtail(dentry);
18475 +               inode->i_op = iop + AuIop_OTHER;
18476 +               init_special_inode(inode, mode, h_inode->i_rdev);
18477 +               break;
18478 +       default:
18479 +               AuIOErr("Unknown file type 0%o\n", mode);
18480 +               err = -EIO;
18481 +               goto out;
18482 +       }
18483 +
18484 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18485 +       flags = au_hi_flags(inode, isdir);
18486 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18487 +           && au_ftest_hi(flags, HNOTIFY)
18488 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18489 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18490 +               au_fclr_hi(flags, HNOTIFY);
18491 +       iinfo = au_ii(inode);
18492 +       iinfo->ii_btop = btop;
18493 +       iinfo->ii_bbot = btail;
18494 +       for (bindex = btop; bindex <= btail; bindex++) {
18495 +               h_dentry = au_h_dptr(dentry, bindex);
18496 +               if (h_dentry)
18497 +                       au_set_h_iptr(inode, bindex,
18498 +                                     au_igrab(d_inode(h_dentry)), flags);
18499 +       }
18500 +       au_cpup_attr_all(inode, /*force*/1);
18501 +       /*
18502 +        * to force calling aufs_get_acl() every time,
18503 +        * do not call cache_no_acl() for aufs inode.
18504 +        */
18505 +
18506 +out:
18507 +       return err;
18508 +}
18509 +
18510 +/*
18511 + * successful returns with iinfo write_locked
18512 + * minus: errno
18513 + * zero: success, matched
18514 + * plus: no error, but unmatched
18515 + */
18516 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18517 +{
18518 +       int err;
18519 +       unsigned int gen, igflags;
18520 +       aufs_bindex_t bindex, bbot;
18521 +       struct inode *h_inode, *h_dinode;
18522 +       struct dentry *h_dentry;
18523 +
18524 +       /*
18525 +        * before this function, if aufs got any iinfo lock, it must be only
18526 +        * one, the parent dir.
18527 +        * it can happen by UDBA and the obsoleted inode number.
18528 +        */
18529 +       err = -EIO;
18530 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18531 +               goto out;
18532 +
18533 +       err = 1;
18534 +       ii_write_lock_new_child(inode);
18535 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18536 +       h_dinode = d_inode(h_dentry);
18537 +       bbot = au_ibbot(inode);
18538 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18539 +               h_inode = au_h_iptr(inode, bindex);
18540 +               if (!h_inode || h_inode != h_dinode)
18541 +                       continue;
18542 +
18543 +               err = 0;
18544 +               gen = au_iigen(inode, &igflags);
18545 +               if (gen == au_digen(dentry)
18546 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18547 +                       break;
18548 +
18549 +               /* fully refresh inode using dentry */
18550 +               err = au_refresh_hinode(inode, dentry);
18551 +               if (!err)
18552 +                       au_update_iigen(inode, /*half*/0);
18553 +               break;
18554 +       }
18555 +
18556 +       if (unlikely(err))
18557 +               ii_write_unlock(inode);
18558 +out:
18559 +       return err;
18560 +}
18561 +
18562 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18563 +          unsigned int d_type, ino_t *ino)
18564 +{
18565 +       int err, idx;
18566 +       const int isnondir = d_type != DT_DIR;
18567 +
18568 +       /* prevent hardlinked inode number from race condition */
18569 +       if (isnondir) {
18570 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18571 +               if (unlikely(err))
18572 +                       goto out;
18573 +       }
18574 +
18575 +       err = au_xino_read(sb, bindex, h_ino, ino);
18576 +       if (unlikely(err))
18577 +               goto out_xinondir;
18578 +
18579 +       if (!*ino) {
18580 +               err = -EIO;
18581 +               *ino = au_xino_new_ino(sb);
18582 +               if (unlikely(!*ino))
18583 +                       goto out_xinondir;
18584 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18585 +               if (unlikely(err))
18586 +                       goto out_xinondir;
18587 +       }
18588 +
18589 +out_xinondir:
18590 +       if (isnondir && idx >= 0)
18591 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18592 +out:
18593 +       return err;
18594 +}
18595 +
18596 +/* successful returns with iinfo write_locked */
18597 +/* todo: return with unlocked? */
18598 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18599 +{
18600 +       struct inode *inode, *h_inode;
18601 +       struct dentry *h_dentry;
18602 +       struct super_block *sb;
18603 +       ino_t h_ino, ino;
18604 +       int err, idx, hlinked;
18605 +       aufs_bindex_t btop;
18606 +
18607 +       sb = dentry->d_sb;
18608 +       btop = au_dbtop(dentry);
18609 +       h_dentry = au_h_dptr(dentry, btop);
18610 +       h_inode = d_inode(h_dentry);
18611 +       h_ino = h_inode->i_ino;
18612 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18613 +
18614 +new_ino:
18615 +       /*
18616 +        * stop 'race'-ing between hardlinks under different
18617 +        * parents.
18618 +        */
18619 +       if (hlinked) {
18620 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18621 +               inode = ERR_PTR(err);
18622 +               if (unlikely(err))
18623 +                       goto out;
18624 +       }
18625 +
18626 +       err = au_xino_read(sb, btop, h_ino, &ino);
18627 +       inode = ERR_PTR(err);
18628 +       if (unlikely(err))
18629 +               goto out_xinondir;
18630 +
18631 +       if (!ino) {
18632 +               ino = au_xino_new_ino(sb);
18633 +               if (unlikely(!ino)) {
18634 +                       inode = ERR_PTR(-EIO);
18635 +                       goto out_xinondir;
18636 +               }
18637 +       }
18638 +
18639 +       AuDbg("i%lu\n", (unsigned long)ino);
18640 +       inode = au_iget_locked(sb, ino);
18641 +       err = PTR_ERR(inode);
18642 +       if (IS_ERR(inode))
18643 +               goto out_xinondir;
18644 +
18645 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18646 +       if (inode->i_state & I_NEW) {
18647 +               ii_write_lock_new_child(inode);
18648 +               err = set_inode(inode, dentry);
18649 +               if (!err) {
18650 +                       unlock_new_inode(inode);
18651 +                       goto out_xinondir; /* success */
18652 +               }
18653 +
18654 +               /*
18655 +                * iget_failed() calls iput(), but we need to call
18656 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18657 +                * i_count.
18658 +                */
18659 +               atomic_inc(&inode->i_count);
18660 +               iget_failed(inode);
18661 +               ii_write_unlock(inode);
18662 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18663 +               /* ignore this error */
18664 +               goto out_iput;
18665 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18666 +               /*
18667 +                * horrible race condition between lookup, readdir and copyup
18668 +                * (or something).
18669 +                */
18670 +               if (hlinked && idx >= 0)
18671 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18672 +               err = reval_inode(inode, dentry);
18673 +               if (unlikely(err < 0)) {
18674 +                       hlinked = 0;
18675 +                       goto out_iput;
18676 +               }
18677 +               if (!err)
18678 +                       goto out; /* success */
18679 +               else if (hlinked && idx >= 0) {
18680 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18681 +                       if (unlikely(err)) {
18682 +                               iput(inode);
18683 +                               inode = ERR_PTR(err);
18684 +                               goto out;
18685 +                       }
18686 +               }
18687 +       }
18688 +
18689 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18690 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18691 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18692 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18693 +                       (unsigned long)h_ino, (unsigned long)ino);
18694 +       ino = 0;
18695 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18696 +       if (!err) {
18697 +               iput(inode);
18698 +               if (hlinked && idx >= 0)
18699 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18700 +               goto new_ino;
18701 +       }
18702 +
18703 +out_iput:
18704 +       iput(inode);
18705 +       inode = ERR_PTR(err);
18706 +out_xinondir:
18707 +       if (hlinked && idx >= 0)
18708 +               au_xinondir_leave(sb, btop, h_ino, idx);
18709 +out:
18710 +       return inode;
18711 +}
18712 +
18713 +/* ---------------------------------------------------------------------- */
18714 +
18715 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18716 +              struct inode *inode)
18717 +{
18718 +       int err;
18719 +       struct inode *hi;
18720 +
18721 +       err = au_br_rdonly(au_sbr(sb, bindex));
18722 +
18723 +       /* pseudo-link after flushed may happen out of bounds */
18724 +       if (!err
18725 +           && inode
18726 +           && au_ibtop(inode) <= bindex
18727 +           && bindex <= au_ibbot(inode)) {
18728 +               /*
18729 +                * permission check is unnecessary since vfsub routine
18730 +                * will be called later
18731 +                */
18732 +               hi = au_h_iptr(inode, bindex);
18733 +               if (hi)
18734 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18735 +       }
18736 +
18737 +       return err;
18738 +}
18739 +
18740 +int au_test_h_perm(struct inode *h_inode, int mask)
18741 +{
18742 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18743 +               return 0;
18744 +       return inode_permission(h_inode, mask);
18745 +}
18746 +
18747 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18748 +{
18749 +       if (au_test_nfs(h_inode->i_sb)
18750 +           && (mask & MAY_WRITE)
18751 +           && S_ISDIR(h_inode->i_mode))
18752 +               mask |= MAY_READ; /* force permission check */
18753 +       return au_test_h_perm(h_inode, mask);
18754 +}
18755 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18756 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18757 +++ linux/fs/aufs/inode.h       2018-08-12 23:43:05.460124736 +0200
18758 @@ -0,0 +1,696 @@
18759 +/* SPDX-License-Identifier: GPL-2.0 */
18760 +/*
18761 + * Copyright (C) 2005-2018 Junjiro R. Okajima
18762 + *
18763 + * This program, aufs is free software; you can redistribute it and/or modify
18764 + * it under the terms of the GNU General Public License as published by
18765 + * the Free Software Foundation; either version 2 of the License, or
18766 + * (at your option) any later version.
18767 + *
18768 + * This program is distributed in the hope that it will be useful,
18769 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18770 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18771 + * GNU General Public License for more details.
18772 + *
18773 + * You should have received a copy of the GNU General Public License
18774 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18775 + */
18776 +
18777 +/*
18778 + * inode operations
18779 + */
18780 +
18781 +#ifndef __AUFS_INODE_H__
18782 +#define __AUFS_INODE_H__
18783 +
18784 +#ifdef __KERNEL__
18785 +
18786 +#include <linux/fsnotify.h>
18787 +#include "rwsem.h"
18788 +
18789 +struct vfsmount;
18790 +
18791 +struct au_hnotify {
18792 +#ifdef CONFIG_AUFS_HNOTIFY
18793 +#ifdef CONFIG_AUFS_HFSNOTIFY
18794 +       /* never use fsnotify_add_vfsmount_mark() */
18795 +       struct fsnotify_mark            hn_mark;
18796 +#endif
18797 +       struct inode            *hn_aufs_inode; /* no get/put */
18798 +#endif
18799 +} ____cacheline_aligned_in_smp;
18800 +
18801 +struct au_hinode {
18802 +       struct inode            *hi_inode;
18803 +       aufs_bindex_t           hi_id;
18804 +#ifdef CONFIG_AUFS_HNOTIFY
18805 +       struct au_hnotify       *hi_notify;
18806 +#endif
18807 +
18808 +       /* reference to the copied-up whiteout with get/put */
18809 +       struct dentry           *hi_whdentry;
18810 +};
18811 +
18812 +/* ig_flags */
18813 +#define AuIG_HALF_REFRESHED            1
18814 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18815 +#define au_ig_fset(flags, name) \
18816 +       do { (flags) |= AuIG_##name; } while (0)
18817 +#define au_ig_fclr(flags, name) \
18818 +       do { (flags) &= ~AuIG_##name; } while (0)
18819 +
18820 +struct au_iigen {
18821 +       spinlock_t      ig_spin;
18822 +       __u32           ig_generation, ig_flags;
18823 +};
18824 +
18825 +struct au_vdir;
18826 +struct au_iinfo {
18827 +       struct au_iigen         ii_generation;
18828 +       struct super_block      *ii_hsb1;       /* no get/put */
18829 +
18830 +       struct au_rwsem         ii_rwsem;
18831 +       aufs_bindex_t           ii_btop, ii_bbot;
18832 +       __u32                   ii_higen;
18833 +       struct au_hinode        *ii_hinode;
18834 +       struct au_vdir          *ii_vdir;
18835 +};
18836 +
18837 +struct au_icntnr {
18838 +       struct au_iinfo iinfo;
18839 +       struct inode vfs_inode;
18840 +       struct hlist_bl_node plink;
18841 +} ____cacheline_aligned_in_smp;
18842 +
18843 +/* au_pin flags */
18844 +#define AuPin_DI_LOCKED                1
18845 +#define AuPin_MNT_WRITE                (1 << 1)
18846 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18847 +#define au_fset_pin(flags, name) \
18848 +       do { (flags) |= AuPin_##name; } while (0)
18849 +#define au_fclr_pin(flags, name) \
18850 +       do { (flags) &= ~AuPin_##name; } while (0)
18851 +
18852 +struct au_pin {
18853 +       /* input */
18854 +       struct dentry *dentry;
18855 +       unsigned int udba;
18856 +       unsigned char lsc_di, lsc_hi, flags;
18857 +       aufs_bindex_t bindex;
18858 +
18859 +       /* output */
18860 +       struct dentry *parent;
18861 +       struct au_hinode *hdir;
18862 +       struct vfsmount *h_mnt;
18863 +
18864 +       /* temporary unlock/relock for copyup */
18865 +       struct dentry *h_dentry, *h_parent;
18866 +       struct au_branch *br;
18867 +       struct task_struct *task;
18868 +};
18869 +
18870 +void au_pin_hdir_unlock(struct au_pin *p);
18871 +int au_pin_hdir_lock(struct au_pin *p);
18872 +int au_pin_hdir_relock(struct au_pin *p);
18873 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18874 +void au_pin_hdir_release(struct au_pin *p);
18875 +
18876 +/* ---------------------------------------------------------------------- */
18877 +
18878 +static inline struct au_iinfo *au_ii(struct inode *inode)
18879 +{
18880 +       BUG_ON(is_bad_inode(inode));
18881 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18882 +}
18883 +
18884 +/* ---------------------------------------------------------------------- */
18885 +
18886 +/* inode.c */
18887 +struct inode *au_igrab(struct inode *inode);
18888 +void au_refresh_iop(struct inode *inode, int force_getattr);
18889 +int au_refresh_hinode_self(struct inode *inode);
18890 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18891 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18892 +          unsigned int d_type, ino_t *ino);
18893 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18894 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18895 +              struct inode *inode);
18896 +int au_test_h_perm(struct inode *h_inode, int mask);
18897 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18898 +
18899 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18900 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18901 +{
18902 +#ifdef CONFIG_AUFS_SHWH
18903 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18904 +#else
18905 +       return 0;
18906 +#endif
18907 +}
18908 +
18909 +/* i_op.c */
18910 +enum {
18911 +       AuIop_SYMLINK,
18912 +       AuIop_DIR,
18913 +       AuIop_OTHER,
18914 +       AuIop_Last
18915 +};
18916 +extern struct inode_operations aufs_iop[AuIop_Last],
18917 +       aufs_iop_nogetattr[AuIop_Last];
18918 +
18919 +/* au_wr_dir flags */
18920 +#define AuWrDir_ADD_ENTRY      1
18921 +#define AuWrDir_ISDIR          (1 << 1)
18922 +#define AuWrDir_TMPFILE                (1 << 2)
18923 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18924 +#define au_fset_wrdir(flags, name) \
18925 +       do { (flags) |= AuWrDir_##name; } while (0)
18926 +#define au_fclr_wrdir(flags, name) \
18927 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18928 +
18929 +struct au_wr_dir_args {
18930 +       aufs_bindex_t force_btgt;
18931 +       unsigned char flags;
18932 +};
18933 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18934 +             struct au_wr_dir_args *args);
18935 +
18936 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18937 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18938 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18939 +                unsigned int udba, unsigned char flags);
18940 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18941 +          unsigned int udba, unsigned char flags) __must_check;
18942 +int au_do_pin(struct au_pin *pin) __must_check;
18943 +void au_unpin(struct au_pin *pin);
18944 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18945 +
18946 +#define AuIcpup_DID_CPUP       1
18947 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18948 +#define au_fset_icpup(flags, name) \
18949 +       do { (flags) |= AuIcpup_##name; } while (0)
18950 +#define au_fclr_icpup(flags, name) \
18951 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18952 +
18953 +struct au_icpup_args {
18954 +       unsigned char flags;
18955 +       unsigned char pin_flags;
18956 +       aufs_bindex_t btgt;
18957 +       unsigned int udba;
18958 +       struct au_pin pin;
18959 +       struct path h_path;
18960 +       struct inode *h_inode;
18961 +};
18962 +
18963 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18964 +                    struct au_icpup_args *a);
18965 +
18966 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18967 +                     int locked);
18968 +
18969 +/* i_op_add.c */
18970 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18971 +              struct dentry *h_parent, int isdir);
18972 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18973 +              dev_t dev);
18974 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18975 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18976 +               bool want_excl);
18977 +struct vfsub_aopen_args;
18978 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18979 +                      struct vfsub_aopen_args *args);
18980 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18981 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18982 +             struct dentry *dentry);
18983 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18984 +
18985 +/* i_op_del.c */
18986 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18987 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18988 +              struct dentry *h_parent, int isdir);
18989 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18990 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18991 +
18992 +/* i_op_ren.c */
18993 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18994 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18995 +               struct inode *dir, struct dentry *dentry,
18996 +               unsigned int flags);
18997 +
18998 +/* iinfo.c */
18999 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19000 +void au_hiput(struct au_hinode *hinode);
19001 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19002 +                 struct dentry *h_wh);
19003 +unsigned int au_hi_flags(struct inode *inode, int isdir);
19004 +
19005 +/* hinode flags */
19006 +#define AuHi_XINO      1
19007 +#define AuHi_HNOTIFY   (1 << 1)
19008 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
19009 +#define au_fset_hi(flags, name) \
19010 +       do { (flags) |= AuHi_##name; } while (0)
19011 +#define au_fclr_hi(flags, name) \
19012 +       do { (flags) &= ~AuHi_##name; } while (0)
19013 +
19014 +#ifndef CONFIG_AUFS_HNOTIFY
19015 +#undef AuHi_HNOTIFY
19016 +#define AuHi_HNOTIFY   0
19017 +#endif
19018 +
19019 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19020 +                  struct inode *h_inode, unsigned int flags);
19021 +
19022 +void au_update_iigen(struct inode *inode, int half);
19023 +void au_update_ibrange(struct inode *inode, int do_put_zero);
19024 +
19025 +void au_icntnr_init_once(void *_c);
19026 +void au_hinode_init(struct au_hinode *hinode);
19027 +int au_iinfo_init(struct inode *inode);
19028 +void au_iinfo_fin(struct inode *inode);
19029 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
19030 +
19031 +#ifdef CONFIG_PROC_FS
19032 +/* plink.c */
19033 +int au_plink_maint(struct super_block *sb, int flags);
19034 +struct au_sbinfo;
19035 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19036 +int au_plink_maint_enter(struct super_block *sb);
19037 +#ifdef CONFIG_AUFS_DEBUG
19038 +void au_plink_list(struct super_block *sb);
19039 +#else
19040 +AuStubVoid(au_plink_list, struct super_block *sb)
19041 +#endif
19042 +int au_plink_test(struct inode *inode);
19043 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19044 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19045 +                    struct dentry *h_dentry);
19046 +void au_plink_put(struct super_block *sb, int verbose);
19047 +void au_plink_clean(struct super_block *sb, int verbose);
19048 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
19049 +#else
19050 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19051 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19052 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19053 +AuStubVoid(au_plink_list, struct super_block *sb);
19054 +AuStubInt0(au_plink_test, struct inode *inode);
19055 +AuStub(struct dentry *, au_plink_lkup, return NULL,
19056 +       struct inode *inode, aufs_bindex_t bindex);
19057 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19058 +          struct dentry *h_dentry);
19059 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19060 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19061 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19062 +#endif /* CONFIG_PROC_FS */
19063 +
19064 +#ifdef CONFIG_AUFS_XATTR
19065 +/* xattr.c */
19066 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19067 +                 unsigned int verbose);
19068 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
19069 +void au_xattr_init(struct super_block *sb);
19070 +#else
19071 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
19072 +          int ignore_flags, unsigned int verbose);
19073 +AuStubVoid(au_xattr_init, struct super_block *sb);
19074 +#endif
19075 +
19076 +#ifdef CONFIG_FS_POSIX_ACL
19077 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19078 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19079 +#endif
19080 +
19081 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19082 +enum {
19083 +       AU_XATTR_SET,
19084 +       AU_ACL_SET
19085 +};
19086 +
19087 +struct au_sxattr {
19088 +       int type;
19089 +       union {
19090 +               struct {
19091 +                       const char      *name;
19092 +                       const void      *value;
19093 +                       size_t          size;
19094 +                       int             flags;
19095 +               } set;
19096 +               struct {
19097 +                       struct posix_acl *acl;
19098 +                       int             type;
19099 +               } acl_set;
19100 +       } u;
19101 +};
19102 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19103 +                 struct au_sxattr *arg);
19104 +#endif
19105 +
19106 +/* ---------------------------------------------------------------------- */
19107 +
19108 +/* lock subclass for iinfo */
19109 +enum {
19110 +       AuLsc_II_CHILD,         /* child first */
19111 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
19112 +       AuLsc_II_CHILD3,        /* copyup dirs */
19113 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
19114 +       AuLsc_II_PARENT2,
19115 +       AuLsc_II_PARENT3,       /* copyup dirs */
19116 +       AuLsc_II_NEW_CHILD
19117 +};
19118 +
19119 +/*
19120 + * ii_read_lock_child, ii_write_lock_child,
19121 + * ii_read_lock_child2, ii_write_lock_child2,
19122 + * ii_read_lock_child3, ii_write_lock_child3,
19123 + * ii_read_lock_parent, ii_write_lock_parent,
19124 + * ii_read_lock_parent2, ii_write_lock_parent2,
19125 + * ii_read_lock_parent3, ii_write_lock_parent3,
19126 + * ii_read_lock_new_child, ii_write_lock_new_child,
19127 + */
19128 +#define AuReadLockFunc(name, lsc) \
19129 +static inline void ii_read_lock_##name(struct inode *i) \
19130 +{ \
19131 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19132 +}
19133 +
19134 +#define AuWriteLockFunc(name, lsc) \
19135 +static inline void ii_write_lock_##name(struct inode *i) \
19136 +{ \
19137 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19138 +}
19139 +
19140 +#define AuRWLockFuncs(name, lsc) \
19141 +       AuReadLockFunc(name, lsc) \
19142 +       AuWriteLockFunc(name, lsc)
19143 +
19144 +AuRWLockFuncs(child, CHILD);
19145 +AuRWLockFuncs(child2, CHILD2);
19146 +AuRWLockFuncs(child3, CHILD3);
19147 +AuRWLockFuncs(parent, PARENT);
19148 +AuRWLockFuncs(parent2, PARENT2);
19149 +AuRWLockFuncs(parent3, PARENT3);
19150 +AuRWLockFuncs(new_child, NEW_CHILD);
19151 +
19152 +#undef AuReadLockFunc
19153 +#undef AuWriteLockFunc
19154 +#undef AuRWLockFuncs
19155 +
19156 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19157 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19158 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19159 +
19160 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19161 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19162 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19163 +
19164 +/* ---------------------------------------------------------------------- */
19165 +
19166 +static inline void au_icntnr_init(struct au_icntnr *c)
19167 +{
19168 +#ifdef CONFIG_AUFS_DEBUG
19169 +       c->vfs_inode.i_mode = 0;
19170 +#endif
19171 +}
19172 +
19173 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19174 +{
19175 +       unsigned int gen;
19176 +       struct au_iinfo *iinfo;
19177 +       struct au_iigen *iigen;
19178 +
19179 +       iinfo = au_ii(inode);
19180 +       iigen = &iinfo->ii_generation;
19181 +       spin_lock(&iigen->ig_spin);
19182 +       if (igflags)
19183 +               *igflags = iigen->ig_flags;
19184 +       gen = iigen->ig_generation;
19185 +       spin_unlock(&iigen->ig_spin);
19186 +
19187 +       return gen;
19188 +}
19189 +
19190 +/* tiny test for inode number */
19191 +/* tmpfs generation is too rough */
19192 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19193 +{
19194 +       struct au_iinfo *iinfo;
19195 +
19196 +       iinfo = au_ii(inode);
19197 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19198 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19199 +                && iinfo->ii_higen == h_inode->i_generation);
19200 +}
19201 +
19202 +static inline void au_iigen_dec(struct inode *inode)
19203 +{
19204 +       struct au_iinfo *iinfo;
19205 +       struct au_iigen *iigen;
19206 +
19207 +       iinfo = au_ii(inode);
19208 +       iigen = &iinfo->ii_generation;
19209 +       spin_lock(&iigen->ig_spin);
19210 +       iigen->ig_generation--;
19211 +       spin_unlock(&iigen->ig_spin);
19212 +}
19213 +
19214 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19215 +{
19216 +       int err;
19217 +
19218 +       err = 0;
19219 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19220 +               err = -EIO;
19221 +
19222 +       return err;
19223 +}
19224 +
19225 +/* ---------------------------------------------------------------------- */
19226 +
19227 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19228 +                                         aufs_bindex_t bindex)
19229 +{
19230 +       return iinfo->ii_hinode + bindex;
19231 +}
19232 +
19233 +static inline int au_is_bad_inode(struct inode *inode)
19234 +{
19235 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19236 +}
19237 +
19238 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19239 +                                       aufs_bindex_t bindex)
19240 +{
19241 +       IiMustAnyLock(inode);
19242 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19243 +}
19244 +
19245 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19246 +{
19247 +       IiMustAnyLock(inode);
19248 +       return au_ii(inode)->ii_btop;
19249 +}
19250 +
19251 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19252 +{
19253 +       IiMustAnyLock(inode);
19254 +       return au_ii(inode)->ii_bbot;
19255 +}
19256 +
19257 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19258 +{
19259 +       IiMustAnyLock(inode);
19260 +       return au_ii(inode)->ii_vdir;
19261 +}
19262 +
19263 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19264 +{
19265 +       IiMustAnyLock(inode);
19266 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19267 +}
19268 +
19269 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19270 +{
19271 +       IiMustWriteLock(inode);
19272 +       au_ii(inode)->ii_btop = bindex;
19273 +}
19274 +
19275 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19276 +{
19277 +       IiMustWriteLock(inode);
19278 +       au_ii(inode)->ii_bbot = bindex;
19279 +}
19280 +
19281 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19282 +{
19283 +       IiMustWriteLock(inode);
19284 +       au_ii(inode)->ii_vdir = vdir;
19285 +}
19286 +
19287 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19288 +{
19289 +       IiMustAnyLock(inode);
19290 +       return au_hinode(au_ii(inode), bindex);
19291 +}
19292 +
19293 +/* ---------------------------------------------------------------------- */
19294 +
19295 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19296 +{
19297 +       if (pin)
19298 +               return pin->parent;
19299 +       return NULL;
19300 +}
19301 +
19302 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19303 +{
19304 +       if (pin && pin->hdir)
19305 +               return pin->hdir->hi_inode;
19306 +       return NULL;
19307 +}
19308 +
19309 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19310 +{
19311 +       if (pin)
19312 +               return pin->hdir;
19313 +       return NULL;
19314 +}
19315 +
19316 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19317 +{
19318 +       if (pin)
19319 +               pin->dentry = dentry;
19320 +}
19321 +
19322 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19323 +                                          unsigned char lflag)
19324 +{
19325 +       if (pin) {
19326 +               if (lflag)
19327 +                       au_fset_pin(pin->flags, DI_LOCKED);
19328 +               else
19329 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19330 +       }
19331 +}
19332 +
19333 +#if 0 /* reserved */
19334 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19335 +{
19336 +       if (pin) {
19337 +               dput(pin->parent);
19338 +               pin->parent = dget(parent);
19339 +       }
19340 +}
19341 +#endif
19342 +
19343 +/* ---------------------------------------------------------------------- */
19344 +
19345 +struct au_branch;
19346 +#ifdef CONFIG_AUFS_HNOTIFY
19347 +struct au_hnotify_op {
19348 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19349 +       int (*alloc)(struct au_hinode *hinode);
19350 +
19351 +       /*
19352 +        * if it returns true, the the caller should free hinode->hi_notify,
19353 +        * otherwise ->free() frees it.
19354 +        */
19355 +       int (*free)(struct au_hinode *hinode,
19356 +                   struct au_hnotify *hn) __must_check;
19357 +
19358 +       void (*fin)(void);
19359 +       int (*init)(void);
19360 +
19361 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19362 +       void (*fin_br)(struct au_branch *br);
19363 +       int (*init_br)(struct au_branch *br, int perm);
19364 +};
19365 +
19366 +/* hnotify.c */
19367 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19368 +void au_hn_free(struct au_hinode *hinode);
19369 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19370 +void au_hn_reset(struct inode *inode, unsigned int flags);
19371 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19372 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
19373 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19374 +int au_hnotify_init_br(struct au_branch *br, int perm);
19375 +void au_hnotify_fin_br(struct au_branch *br);
19376 +int __init au_hnotify_init(void);
19377 +void au_hnotify_fin(void);
19378 +
19379 +/* hfsnotify.c */
19380 +extern const struct au_hnotify_op au_hnotify_op;
19381 +
19382 +static inline
19383 +void au_hn_init(struct au_hinode *hinode)
19384 +{
19385 +       hinode->hi_notify = NULL;
19386 +}
19387 +
19388 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19389 +{
19390 +       return hinode->hi_notify;
19391 +}
19392 +
19393 +#else
19394 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19395 +       struct au_hinode *hinode __maybe_unused,
19396 +       struct inode *inode __maybe_unused)
19397 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19398 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19399 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19400 +          int do_set __maybe_unused)
19401 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19402 +          unsigned int flags __maybe_unused)
19403 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19404 +          struct au_branch *br __maybe_unused,
19405 +          int perm __maybe_unused)
19406 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19407 +          int perm __maybe_unused)
19408 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19409 +AuStubInt0(__init au_hnotify_init, void)
19410 +AuStubVoid(au_hnotify_fin, void)
19411 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19412 +#endif /* CONFIG_AUFS_HNOTIFY */
19413 +
19414 +static inline void au_hn_suspend(struct au_hinode *hdir)
19415 +{
19416 +       au_hn_ctl(hdir, /*do_set*/0);
19417 +}
19418 +
19419 +static inline void au_hn_resume(struct au_hinode *hdir)
19420 +{
19421 +       au_hn_ctl(hdir, /*do_set*/1);
19422 +}
19423 +
19424 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19425 +{
19426 +       inode_lock(hdir->hi_inode);
19427 +       au_hn_suspend(hdir);
19428 +}
19429 +
19430 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19431 +                                         unsigned int sc __maybe_unused)
19432 +{
19433 +       inode_lock_nested(hdir->hi_inode, sc);
19434 +       au_hn_suspend(hdir);
19435 +}
19436 +
19437 +#if 0 /* unused */
19438 +#include "vfsub.h"
19439 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19440 +                                                 unsigned int sc)
19441 +{
19442 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19443 +       au_hn_suspend(hdir);
19444 +}
19445 +#endif
19446 +
19447 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19448 +{
19449 +       au_hn_resume(hdir);
19450 +       inode_unlock(hdir->hi_inode);
19451 +}
19452 +
19453 +#endif /* __KERNEL__ */
19454 +#endif /* __AUFS_INODE_H__ */
19455 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19456 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19457 +++ linux/fs/aufs/ioctl.c       2018-08-12 23:43:05.460124736 +0200
19458 @@ -0,0 +1,220 @@
19459 +// SPDX-License-Identifier: GPL-2.0
19460 +/*
19461 + * Copyright (C) 2005-2018 Junjiro R. Okajima
19462 + *
19463 + * This program, aufs is free software; you can redistribute it and/or modify
19464 + * it under the terms of the GNU General Public License as published by
19465 + * the Free Software Foundation; either version 2 of the License, or
19466 + * (at your option) any later version.
19467 + *
19468 + * This program is distributed in the hope that it will be useful,
19469 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19470 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19471 + * GNU General Public License for more details.
19472 + *
19473 + * You should have received a copy of the GNU General Public License
19474 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19475 + */
19476 +
19477 +/*
19478 + * ioctl
19479 + * plink-management and readdir in userspace.
19480 + * assist the pathconf(3) wrapper library.
19481 + * move-down
19482 + * File-based Hierarchical Storage Management.
19483 + */
19484 +
19485 +#include <linux/compat.h>
19486 +#include <linux/file.h>
19487 +#include "aufs.h"
19488 +
19489 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19490 +{
19491 +       int err, fd;
19492 +       aufs_bindex_t wbi, bindex, bbot;
19493 +       struct file *h_file;
19494 +       struct super_block *sb;
19495 +       struct dentry *root;
19496 +       struct au_branch *br;
19497 +       struct aufs_wbr_fd wbrfd = {
19498 +               .oflags = au_dir_roflags,
19499 +               .brid   = -1
19500 +       };
19501 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19502 +               | O_NOATIME | O_CLOEXEC;
19503 +
19504 +       AuDebugOn(wbrfd.oflags & ~valid);
19505 +
19506 +       if (arg) {
19507 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19508 +               if (unlikely(err)) {
19509 +                       err = -EFAULT;
19510 +                       goto out;
19511 +               }
19512 +
19513 +               err = -EINVAL;
19514 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19515 +               wbrfd.oflags |= au_dir_roflags;
19516 +               AuDbg("0%o\n", wbrfd.oflags);
19517 +               if (unlikely(wbrfd.oflags & ~valid))
19518 +                       goto out;
19519 +       }
19520 +
19521 +       fd = get_unused_fd_flags(0);
19522 +       err = fd;
19523 +       if (unlikely(fd < 0))
19524 +               goto out;
19525 +
19526 +       h_file = ERR_PTR(-EINVAL);
19527 +       wbi = 0;
19528 +       br = NULL;
19529 +       sb = path->dentry->d_sb;
19530 +       root = sb->s_root;
19531 +       aufs_read_lock(root, AuLock_IR);
19532 +       bbot = au_sbbot(sb);
19533 +       if (wbrfd.brid >= 0) {
19534 +               wbi = au_br_index(sb, wbrfd.brid);
19535 +               if (unlikely(wbi < 0 || wbi > bbot))
19536 +                       goto out_unlock;
19537 +       }
19538 +
19539 +       h_file = ERR_PTR(-ENOENT);
19540 +       br = au_sbr(sb, wbi);
19541 +       if (!au_br_writable(br->br_perm)) {
19542 +               if (arg)
19543 +                       goto out_unlock;
19544 +
19545 +               bindex = wbi + 1;
19546 +               wbi = -1;
19547 +               for (; bindex <= bbot; bindex++) {
19548 +                       br = au_sbr(sb, bindex);
19549 +                       if (au_br_writable(br->br_perm)) {
19550 +                               wbi = bindex;
19551 +                               br = au_sbr(sb, wbi);
19552 +                               break;
19553 +                       }
19554 +               }
19555 +       }
19556 +       AuDbg("wbi %d\n", wbi);
19557 +       if (wbi >= 0)
19558 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19559 +                                  /*force_wr*/0);
19560 +
19561 +out_unlock:
19562 +       aufs_read_unlock(root, AuLock_IR);
19563 +       err = PTR_ERR(h_file);
19564 +       if (IS_ERR(h_file))
19565 +               goto out_fd;
19566 +
19567 +       au_br_put(br); /* cf. au_h_open() */
19568 +       fd_install(fd, h_file);
19569 +       err = fd;
19570 +       goto out; /* success */
19571 +
19572 +out_fd:
19573 +       put_unused_fd(fd);
19574 +out:
19575 +       AuTraceErr(err);
19576 +       return err;
19577 +}
19578 +
19579 +/* ---------------------------------------------------------------------- */
19580 +
19581 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19582 +{
19583 +       long err;
19584 +       struct dentry *dentry;
19585 +
19586 +       switch (cmd) {
19587 +       case AUFS_CTL_RDU:
19588 +       case AUFS_CTL_RDU_INO:
19589 +               err = au_rdu_ioctl(file, cmd, arg);
19590 +               break;
19591 +
19592 +       case AUFS_CTL_WBR_FD:
19593 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19594 +               break;
19595 +
19596 +       case AUFS_CTL_IBUSY:
19597 +               err = au_ibusy_ioctl(file, arg);
19598 +               break;
19599 +
19600 +       case AUFS_CTL_BRINFO:
19601 +               err = au_brinfo_ioctl(file, arg);
19602 +               break;
19603 +
19604 +       case AUFS_CTL_FHSM_FD:
19605 +               dentry = file->f_path.dentry;
19606 +               if (IS_ROOT(dentry))
19607 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19608 +               else
19609 +                       err = -ENOTTY;
19610 +               break;
19611 +
19612 +       default:
19613 +               /* do not call the lower */
19614 +               AuDbg("0x%x\n", cmd);
19615 +               err = -ENOTTY;
19616 +       }
19617 +
19618 +       AuTraceErr(err);
19619 +       return err;
19620 +}
19621 +
19622 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19623 +{
19624 +       long err;
19625 +
19626 +       switch (cmd) {
19627 +       case AUFS_CTL_MVDOWN:
19628 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19629 +               break;
19630 +
19631 +       case AUFS_CTL_WBR_FD:
19632 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19633 +               break;
19634 +
19635 +       default:
19636 +               /* do not call the lower */
19637 +               AuDbg("0x%x\n", cmd);
19638 +               err = -ENOTTY;
19639 +       }
19640 +
19641 +       AuTraceErr(err);
19642 +       return err;
19643 +}
19644 +
19645 +#ifdef CONFIG_COMPAT
19646 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19647 +                          unsigned long arg)
19648 +{
19649 +       long err;
19650 +
19651 +       switch (cmd) {
19652 +       case AUFS_CTL_RDU:
19653 +       case AUFS_CTL_RDU_INO:
19654 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19655 +               break;
19656 +
19657 +       case AUFS_CTL_IBUSY:
19658 +               err = au_ibusy_compat_ioctl(file, arg);
19659 +               break;
19660 +
19661 +       case AUFS_CTL_BRINFO:
19662 +               err = au_brinfo_compat_ioctl(file, arg);
19663 +               break;
19664 +
19665 +       default:
19666 +               err = aufs_ioctl_dir(file, cmd, arg);
19667 +       }
19668 +
19669 +       AuTraceErr(err);
19670 +       return err;
19671 +}
19672 +
19673 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19674 +                             unsigned long arg)
19675 +{
19676 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19677 +}
19678 +#endif
19679 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19680 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19681 +++ linux/fs/aufs/i_op_add.c    2018-08-12 23:43:05.456791299 +0200
19682 @@ -0,0 +1,921 @@
19683 +// SPDX-License-Identifier: GPL-2.0
19684 +/*
19685 + * Copyright (C) 2005-2018 Junjiro R. Okajima
19686 + *
19687 + * This program, aufs is free software; you can redistribute it and/or modify
19688 + * it under the terms of the GNU General Public License as published by
19689 + * the Free Software Foundation; either version 2 of the License, or
19690 + * (at your option) any later version.
19691 + *
19692 + * This program is distributed in the hope that it will be useful,
19693 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19694 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19695 + * GNU General Public License for more details.
19696 + *
19697 + * You should have received a copy of the GNU General Public License
19698 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19699 + */
19700 +
19701 +/*
19702 + * inode operations (add entry)
19703 + */
19704 +
19705 +#include "aufs.h"
19706 +
19707 +/*
19708 + * final procedure of adding a new entry, except link(2).
19709 + * remove whiteout, instantiate, copyup the parent dir's times and size
19710 + * and update version.
19711 + * if it failed, re-create the removed whiteout.
19712 + */
19713 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19714 +                 struct dentry *wh_dentry, struct dentry *dentry)
19715 +{
19716 +       int err, rerr;
19717 +       aufs_bindex_t bwh;
19718 +       struct path h_path;
19719 +       struct super_block *sb;
19720 +       struct inode *inode, *h_dir;
19721 +       struct dentry *wh;
19722 +
19723 +       bwh = -1;
19724 +       sb = dir->i_sb;
19725 +       if (wh_dentry) {
19726 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19727 +               IMustLock(h_dir);
19728 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19729 +               bwh = au_dbwh(dentry);
19730 +               h_path.dentry = wh_dentry;
19731 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19732 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19733 +                                         dentry);
19734 +               if (unlikely(err))
19735 +                       goto out;
19736 +       }
19737 +
19738 +       inode = au_new_inode(dentry, /*must_new*/1);
19739 +       if (!IS_ERR(inode)) {
19740 +               d_instantiate(dentry, inode);
19741 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19742 +               IMustLock(dir);
19743 +               au_dir_ts(dir, bindex);
19744 +               inode_inc_iversion(dir);
19745 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19746 +               return 0; /* success */
19747 +       }
19748 +
19749 +       err = PTR_ERR(inode);
19750 +       if (!wh_dentry)
19751 +               goto out;
19752 +
19753 +       /* revert */
19754 +       /* dir inode is locked */
19755 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19756 +       rerr = PTR_ERR(wh);
19757 +       if (IS_ERR(wh)) {
19758 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19759 +                       dentry, err, rerr);
19760 +               err = -EIO;
19761 +       } else
19762 +               dput(wh);
19763 +
19764 +out:
19765 +       return err;
19766 +}
19767 +
19768 +static int au_d_may_add(struct dentry *dentry)
19769 +{
19770 +       int err;
19771 +
19772 +       err = 0;
19773 +       if (unlikely(d_unhashed(dentry)))
19774 +               err = -ENOENT;
19775 +       if (unlikely(d_really_is_positive(dentry)))
19776 +               err = -EEXIST;
19777 +       return err;
19778 +}
19779 +
19780 +/*
19781 + * simple tests for the adding inode operations.
19782 + * following the checks in vfs, plus the parent-child relationship.
19783 + */
19784 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19785 +              struct dentry *h_parent, int isdir)
19786 +{
19787 +       int err;
19788 +       umode_t h_mode;
19789 +       struct dentry *h_dentry;
19790 +       struct inode *h_inode;
19791 +
19792 +       err = -ENAMETOOLONG;
19793 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19794 +               goto out;
19795 +
19796 +       h_dentry = au_h_dptr(dentry, bindex);
19797 +       if (d_really_is_negative(dentry)) {
19798 +               err = -EEXIST;
19799 +               if (unlikely(d_is_positive(h_dentry)))
19800 +                       goto out;
19801 +       } else {
19802 +               /* rename(2) case */
19803 +               err = -EIO;
19804 +               if (unlikely(d_is_negative(h_dentry)))
19805 +                       goto out;
19806 +               h_inode = d_inode(h_dentry);
19807 +               if (unlikely(!h_inode->i_nlink))
19808 +                       goto out;
19809 +
19810 +               h_mode = h_inode->i_mode;
19811 +               if (!isdir) {
19812 +                       err = -EISDIR;
19813 +                       if (unlikely(S_ISDIR(h_mode)))
19814 +                               goto out;
19815 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19816 +                       err = -ENOTDIR;
19817 +                       goto out;
19818 +               }
19819 +       }
19820 +
19821 +       err = 0;
19822 +       /* expected parent dir is locked */
19823 +       if (unlikely(h_parent != h_dentry->d_parent))
19824 +               err = -EIO;
19825 +
19826 +out:
19827 +       AuTraceErr(err);
19828 +       return err;
19829 +}
19830 +
19831 +/*
19832 + * initial procedure of adding a new entry.
19833 + * prepare writable branch and the parent dir, lock it,
19834 + * and lookup whiteout for the new entry.
19835 + */
19836 +static struct dentry*
19837 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19838 +                 struct dentry *src_dentry, struct au_pin *pin,
19839 +                 struct au_wr_dir_args *wr_dir_args)
19840 +{
19841 +       struct dentry *wh_dentry, *h_parent;
19842 +       struct super_block *sb;
19843 +       struct au_branch *br;
19844 +       int err;
19845 +       unsigned int udba;
19846 +       aufs_bindex_t bcpup;
19847 +
19848 +       AuDbg("%pd\n", dentry);
19849 +
19850 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19851 +       bcpup = err;
19852 +       wh_dentry = ERR_PTR(err);
19853 +       if (unlikely(err < 0))
19854 +               goto out;
19855 +
19856 +       sb = dentry->d_sb;
19857 +       udba = au_opt_udba(sb);
19858 +       err = au_pin(pin, dentry, bcpup, udba,
19859 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19860 +       wh_dentry = ERR_PTR(err);
19861 +       if (unlikely(err))
19862 +               goto out;
19863 +
19864 +       h_parent = au_pinned_h_parent(pin);
19865 +       if (udba != AuOpt_UDBA_NONE
19866 +           && au_dbtop(dentry) == bcpup)
19867 +               err = au_may_add(dentry, bcpup, h_parent,
19868 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19869 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19870 +               err = -ENAMETOOLONG;
19871 +       wh_dentry = ERR_PTR(err);
19872 +       if (unlikely(err))
19873 +               goto out_unpin;
19874 +
19875 +       br = au_sbr(sb, bcpup);
19876 +       if (dt) {
19877 +               struct path tmp = {
19878 +                       .dentry = h_parent,
19879 +                       .mnt    = au_br_mnt(br)
19880 +               };
19881 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19882 +       }
19883 +
19884 +       wh_dentry = NULL;
19885 +       if (bcpup != au_dbwh(dentry))
19886 +               goto out; /* success */
19887 +
19888 +       /*
19889 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19890 +        * would not be able to removed in the future. So we don't allow such
19891 +        * name here and we don't handle ENAMETOOLONG differently here.
19892 +        */
19893 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19894 +
19895 +out_unpin:
19896 +       if (IS_ERR(wh_dentry))
19897 +               au_unpin(pin);
19898 +out:
19899 +       return wh_dentry;
19900 +}
19901 +
19902 +/* ---------------------------------------------------------------------- */
19903 +
19904 +enum { Mknod, Symlink, Creat };
19905 +struct simple_arg {
19906 +       int type;
19907 +       union {
19908 +               struct {
19909 +                       umode_t                 mode;
19910 +                       bool                    want_excl;
19911 +                       bool                    try_aopen;
19912 +                       struct vfsub_aopen_args *aopen;
19913 +               } c;
19914 +               struct {
19915 +                       const char *symname;
19916 +               } s;
19917 +               struct {
19918 +                       umode_t mode;
19919 +                       dev_t dev;
19920 +               } m;
19921 +       } u;
19922 +};
19923 +
19924 +static int add_simple(struct inode *dir, struct dentry *dentry,
19925 +                     struct simple_arg *arg)
19926 +{
19927 +       int err, rerr;
19928 +       aufs_bindex_t btop;
19929 +       unsigned char created;
19930 +       const unsigned char try_aopen
19931 +               = (arg->type == Creat && arg->u.c.try_aopen);
19932 +       struct dentry *wh_dentry, *parent;
19933 +       struct inode *h_dir;
19934 +       struct super_block *sb;
19935 +       struct au_branch *br;
19936 +       /* to reuduce stack size */
19937 +       struct {
19938 +               struct au_dtime dt;
19939 +               struct au_pin pin;
19940 +               struct path h_path;
19941 +               struct au_wr_dir_args wr_dir_args;
19942 +       } *a;
19943 +
19944 +       AuDbg("%pd\n", dentry);
19945 +       IMustLock(dir);
19946 +
19947 +       err = -ENOMEM;
19948 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19949 +       if (unlikely(!a))
19950 +               goto out;
19951 +       a->wr_dir_args.force_btgt = -1;
19952 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19953 +
19954 +       parent = dentry->d_parent; /* dir inode is locked */
19955 +       if (!try_aopen) {
19956 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19957 +               if (unlikely(err))
19958 +                       goto out_free;
19959 +       }
19960 +       err = au_d_may_add(dentry);
19961 +       if (unlikely(err))
19962 +               goto out_unlock;
19963 +       if (!try_aopen)
19964 +               di_write_lock_parent(parent);
19965 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19966 +                                     &a->pin, &a->wr_dir_args);
19967 +       err = PTR_ERR(wh_dentry);
19968 +       if (IS_ERR(wh_dentry))
19969 +               goto out_parent;
19970 +
19971 +       btop = au_dbtop(dentry);
19972 +       sb = dentry->d_sb;
19973 +       br = au_sbr(sb, btop);
19974 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19975 +       a->h_path.mnt = au_br_mnt(br);
19976 +       h_dir = au_pinned_h_dir(&a->pin);
19977 +       switch (arg->type) {
19978 +       case Creat:
19979 +               err = 0;
19980 +               if (!try_aopen || !h_dir->i_op->atomic_open)
19981 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19982 +                                          arg->u.c.want_excl);
19983 +               else
19984 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
19985 +                                               arg->u.c.aopen, br);
19986 +               break;
19987 +       case Symlink:
19988 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19989 +               break;
19990 +       case Mknod:
19991 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19992 +                                 arg->u.m.dev);
19993 +               break;
19994 +       default:
19995 +               BUG();
19996 +       }
19997 +       created = !err;
19998 +       if (!err)
19999 +               err = epilog(dir, btop, wh_dentry, dentry);
20000 +
20001 +       /* revert */
20002 +       if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
20003 +               /* no delegation since it is just created */
20004 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20005 +                                   /*force*/0);
20006 +               if (rerr) {
20007 +                       AuIOErr("%pd revert failure(%d, %d)\n",
20008 +                               dentry, err, rerr);
20009 +                       err = -EIO;
20010 +               }
20011 +               au_dtime_revert(&a->dt);
20012 +       }
20013 +
20014 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
20015 +               *arg->u.c.aopen->opened |= FILE_CREATED;
20016 +
20017 +       au_unpin(&a->pin);
20018 +       dput(wh_dentry);
20019 +
20020 +out_parent:
20021 +       if (!try_aopen)
20022 +               di_write_unlock(parent);
20023 +out_unlock:
20024 +       if (unlikely(err)) {
20025 +               au_update_dbtop(dentry);
20026 +               d_drop(dentry);
20027 +       }
20028 +       if (!try_aopen)
20029 +               aufs_read_unlock(dentry, AuLock_DW);
20030 +out_free:
20031 +       kfree(a);
20032 +out:
20033 +       return err;
20034 +}
20035 +
20036 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20037 +              dev_t dev)
20038 +{
20039 +       struct simple_arg arg = {
20040 +               .type = Mknod,
20041 +               .u.m = {
20042 +                       .mode   = mode,
20043 +                       .dev    = dev
20044 +               }
20045 +       };
20046 +       return add_simple(dir, dentry, &arg);
20047 +}
20048 +
20049 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20050 +{
20051 +       struct simple_arg arg = {
20052 +               .type = Symlink,
20053 +               .u.s.symname = symname
20054 +       };
20055 +       return add_simple(dir, dentry, &arg);
20056 +}
20057 +
20058 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20059 +               bool want_excl)
20060 +{
20061 +       struct simple_arg arg = {
20062 +               .type = Creat,
20063 +               .u.c = {
20064 +                       .mode           = mode,
20065 +                       .want_excl      = want_excl
20066 +               }
20067 +       };
20068 +       return add_simple(dir, dentry, &arg);
20069 +}
20070 +
20071 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20072 +                      struct vfsub_aopen_args *aopen_args)
20073 +{
20074 +       struct simple_arg arg = {
20075 +               .type = Creat,
20076 +               .u.c = {
20077 +                       .mode           = aopen_args->create_mode,
20078 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
20079 +                       .try_aopen      = true,
20080 +                       .aopen          = aopen_args
20081 +               }
20082 +       };
20083 +       return add_simple(dir, dentry, &arg);
20084 +}
20085 +
20086 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20087 +{
20088 +       int err;
20089 +       aufs_bindex_t bindex;
20090 +       struct super_block *sb;
20091 +       struct dentry *parent, *h_parent, *h_dentry;
20092 +       struct inode *h_dir, *inode;
20093 +       struct vfsmount *h_mnt;
20094 +       struct au_wr_dir_args wr_dir_args = {
20095 +               .force_btgt     = -1,
20096 +               .flags          = AuWrDir_TMPFILE
20097 +       };
20098 +
20099 +       /* copy-up may happen */
20100 +       inode_lock(dir);
20101 +
20102 +       sb = dir->i_sb;
20103 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20104 +       if (unlikely(err))
20105 +               goto out;
20106 +
20107 +       err = au_di_init(dentry);
20108 +       if (unlikely(err))
20109 +               goto out_si;
20110 +
20111 +       err = -EBUSY;
20112 +       parent = d_find_any_alias(dir);
20113 +       AuDebugOn(!parent);
20114 +       di_write_lock_parent(parent);
20115 +       if (unlikely(d_inode(parent) != dir))
20116 +               goto out_parent;
20117 +
20118 +       err = au_digen_test(parent, au_sigen(sb));
20119 +       if (unlikely(err))
20120 +               goto out_parent;
20121 +
20122 +       bindex = au_dbtop(parent);
20123 +       au_set_dbtop(dentry, bindex);
20124 +       au_set_dbbot(dentry, bindex);
20125 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20126 +       bindex = err;
20127 +       if (unlikely(err < 0))
20128 +               goto out_parent;
20129 +
20130 +       err = -EOPNOTSUPP;
20131 +       h_dir = au_h_iptr(dir, bindex);
20132 +       if (unlikely(!h_dir->i_op->tmpfile))
20133 +               goto out_parent;
20134 +
20135 +       h_mnt = au_sbr_mnt(sb, bindex);
20136 +       err = vfsub_mnt_want_write(h_mnt);
20137 +       if (unlikely(err))
20138 +               goto out_parent;
20139 +
20140 +       h_parent = au_h_dptr(parent, bindex);
20141 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20142 +       if (IS_ERR(h_dentry)) {
20143 +               err = PTR_ERR(h_dentry);
20144 +               goto out_mnt;
20145 +       }
20146 +
20147 +       au_set_dbtop(dentry, bindex);
20148 +       au_set_dbbot(dentry, bindex);
20149 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20150 +       inode = au_new_inode(dentry, /*must_new*/1);
20151 +       if (IS_ERR(inode)) {
20152 +               err = PTR_ERR(inode);
20153 +               au_set_h_dptr(dentry, bindex, NULL);
20154 +               au_set_dbtop(dentry, -1);
20155 +               au_set_dbbot(dentry, -1);
20156 +       } else {
20157 +               if (!inode->i_nlink)
20158 +                       set_nlink(inode, 1);
20159 +               d_tmpfile(dentry, inode);
20160 +               au_di(dentry)->di_tmpfile = 1;
20161 +
20162 +               /* update without i_mutex */
20163 +               if (au_ibtop(dir) == au_dbtop(dentry))
20164 +                       au_cpup_attr_timesizes(dir);
20165 +       }
20166 +       dput(h_dentry);
20167 +
20168 +out_mnt:
20169 +       vfsub_mnt_drop_write(h_mnt);
20170 +out_parent:
20171 +       di_write_unlock(parent);
20172 +       dput(parent);
20173 +       di_write_unlock(dentry);
20174 +       if (unlikely(err)) {
20175 +               au_di_fin(dentry);
20176 +               dentry->d_fsdata = NULL;
20177 +       }
20178 +out_si:
20179 +       si_read_unlock(sb);
20180 +out:
20181 +       inode_unlock(dir);
20182 +       return err;
20183 +}
20184 +
20185 +/* ---------------------------------------------------------------------- */
20186 +
20187 +struct au_link_args {
20188 +       aufs_bindex_t bdst, bsrc;
20189 +       struct au_pin pin;
20190 +       struct path h_path;
20191 +       struct dentry *src_parent, *parent;
20192 +};
20193 +
20194 +static int au_cpup_before_link(struct dentry *src_dentry,
20195 +                              struct au_link_args *a)
20196 +{
20197 +       int err;
20198 +       struct dentry *h_src_dentry;
20199 +       struct au_cp_generic cpg = {
20200 +               .dentry = src_dentry,
20201 +               .bdst   = a->bdst,
20202 +               .bsrc   = a->bsrc,
20203 +               .len    = -1,
20204 +               .pin    = &a->pin,
20205 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20206 +       };
20207 +
20208 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20209 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20210 +       if (unlikely(err))
20211 +               goto out;
20212 +
20213 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20214 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20215 +                    au_opt_udba(src_dentry->d_sb),
20216 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20217 +       if (unlikely(err))
20218 +               goto out;
20219 +
20220 +       err = au_sio_cpup_simple(&cpg);
20221 +       au_unpin(&a->pin);
20222 +
20223 +out:
20224 +       di_read_unlock(a->src_parent, AuLock_IR);
20225 +       return err;
20226 +}
20227 +
20228 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20229 +                          struct au_link_args *a)
20230 +{
20231 +       int err;
20232 +       unsigned char plink;
20233 +       aufs_bindex_t bbot;
20234 +       struct dentry *h_src_dentry;
20235 +       struct inode *h_inode, *inode, *delegated;
20236 +       struct super_block *sb;
20237 +       struct file *h_file;
20238 +
20239 +       plink = 0;
20240 +       h_inode = NULL;
20241 +       sb = src_dentry->d_sb;
20242 +       inode = d_inode(src_dentry);
20243 +       if (au_ibtop(inode) <= a->bdst)
20244 +               h_inode = au_h_iptr(inode, a->bdst);
20245 +       if (!h_inode || !h_inode->i_nlink) {
20246 +               /* copyup src_dentry as the name of dentry. */
20247 +               bbot = au_dbbot(dentry);
20248 +               if (bbot < a->bsrc)
20249 +                       au_set_dbbot(dentry, a->bsrc);
20250 +               au_set_h_dptr(dentry, a->bsrc,
20251 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20252 +               dget(a->h_path.dentry);
20253 +               au_set_h_dptr(dentry, a->bdst, NULL);
20254 +               AuDbg("temporary d_inode...\n");
20255 +               spin_lock(&dentry->d_lock);
20256 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20257 +               spin_unlock(&dentry->d_lock);
20258 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20259 +               if (IS_ERR(h_file))
20260 +                       err = PTR_ERR(h_file);
20261 +               else {
20262 +                       struct au_cp_generic cpg = {
20263 +                               .dentry = dentry,
20264 +                               .bdst   = a->bdst,
20265 +                               .bsrc   = -1,
20266 +                               .len    = -1,
20267 +                               .pin    = &a->pin,
20268 +                               .flags  = AuCpup_KEEPLINO
20269 +                       };
20270 +                       err = au_sio_cpup_simple(&cpg);
20271 +                       au_h_open_post(dentry, a->bsrc, h_file);
20272 +                       if (!err) {
20273 +                               dput(a->h_path.dentry);
20274 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20275 +                       } else
20276 +                               au_set_h_dptr(dentry, a->bdst,
20277 +                                             a->h_path.dentry);
20278 +               }
20279 +               spin_lock(&dentry->d_lock);
20280 +               dentry->d_inode = NULL; /* restore */
20281 +               spin_unlock(&dentry->d_lock);
20282 +               AuDbg("temporary d_inode...done\n");
20283 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20284 +               au_set_dbbot(dentry, bbot);
20285 +       } else {
20286 +               /* the inode of src_dentry already exists on a.bdst branch */
20287 +               h_src_dentry = d_find_alias(h_inode);
20288 +               if (!h_src_dentry && au_plink_test(inode)) {
20289 +                       plink = 1;
20290 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20291 +                       err = PTR_ERR(h_src_dentry);
20292 +                       if (IS_ERR(h_src_dentry))
20293 +                               goto out;
20294 +
20295 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20296 +                               dput(h_src_dentry);
20297 +                               h_src_dentry = NULL;
20298 +                       }
20299 +
20300 +               }
20301 +               if (h_src_dentry) {
20302 +                       delegated = NULL;
20303 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20304 +                                        &a->h_path, &delegated);
20305 +                       if (unlikely(err == -EWOULDBLOCK)) {
20306 +                               pr_warn("cannot retry for NFSv4 delegation"
20307 +                                       " for an internal link\n");
20308 +                               iput(delegated);
20309 +                       }
20310 +                       dput(h_src_dentry);
20311 +               } else {
20312 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20313 +                               h_inode->i_ino, a->bdst);
20314 +                       err = -EIO;
20315 +               }
20316 +       }
20317 +
20318 +       if (!err && !plink)
20319 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20320 +
20321 +out:
20322 +       AuTraceErr(err);
20323 +       return err;
20324 +}
20325 +
20326 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20327 +             struct dentry *dentry)
20328 +{
20329 +       int err, rerr;
20330 +       struct au_dtime dt;
20331 +       struct au_link_args *a;
20332 +       struct dentry *wh_dentry, *h_src_dentry;
20333 +       struct inode *inode, *delegated;
20334 +       struct super_block *sb;
20335 +       struct au_wr_dir_args wr_dir_args = {
20336 +               /* .force_btgt  = -1, */
20337 +               .flags          = AuWrDir_ADD_ENTRY
20338 +       };
20339 +
20340 +       IMustLock(dir);
20341 +       inode = d_inode(src_dentry);
20342 +       IMustLock(inode);
20343 +
20344 +       err = -ENOMEM;
20345 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20346 +       if (unlikely(!a))
20347 +               goto out;
20348 +
20349 +       a->parent = dentry->d_parent; /* dir inode is locked */
20350 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20351 +                                       AuLock_NOPLM | AuLock_GEN);
20352 +       if (unlikely(err))
20353 +               goto out_kfree;
20354 +       err = au_d_linkable(src_dentry);
20355 +       if (unlikely(err))
20356 +               goto out_unlock;
20357 +       err = au_d_may_add(dentry);
20358 +       if (unlikely(err))
20359 +               goto out_unlock;
20360 +
20361 +       a->src_parent = dget_parent(src_dentry);
20362 +       wr_dir_args.force_btgt = au_ibtop(inode);
20363 +
20364 +       di_write_lock_parent(a->parent);
20365 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20366 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20367 +                                     &wr_dir_args);
20368 +       err = PTR_ERR(wh_dentry);
20369 +       if (IS_ERR(wh_dentry))
20370 +               goto out_parent;
20371 +
20372 +       err = 0;
20373 +       sb = dentry->d_sb;
20374 +       a->bdst = au_dbtop(dentry);
20375 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20376 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20377 +       a->bsrc = au_ibtop(inode);
20378 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20379 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20380 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20381 +       if (!h_src_dentry) {
20382 +               a->bsrc = au_dbtop(src_dentry);
20383 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20384 +               AuDebugOn(!h_src_dentry);
20385 +       } else if (IS_ERR(h_src_dentry)) {
20386 +               err = PTR_ERR(h_src_dentry);
20387 +               goto out_parent;
20388 +       }
20389 +
20390 +       /*
20391 +        * aufs doesn't touch the credential so
20392 +        * security_dentry_create_files_as() is unnecrssary.
20393 +        */
20394 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20395 +               if (a->bdst < a->bsrc
20396 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20397 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20398 +               else {
20399 +                       delegated = NULL;
20400 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20401 +                                        &a->h_path, &delegated);
20402 +                       if (unlikely(err == -EWOULDBLOCK)) {
20403 +                               pr_warn("cannot retry for NFSv4 delegation"
20404 +                                       " for an internal link\n");
20405 +                               iput(delegated);
20406 +                       }
20407 +               }
20408 +               dput(h_src_dentry);
20409 +       } else {
20410 +               /*
20411 +                * copyup src_dentry to the branch we process,
20412 +                * and then link(2) to it.
20413 +                */
20414 +               dput(h_src_dentry);
20415 +               if (a->bdst < a->bsrc
20416 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20417 +                       au_unpin(&a->pin);
20418 +                       di_write_unlock(a->parent);
20419 +                       err = au_cpup_before_link(src_dentry, a);
20420 +                       di_write_lock_parent(a->parent);
20421 +                       if (!err)
20422 +                               err = au_pin(&a->pin, dentry, a->bdst,
20423 +                                            au_opt_udba(sb),
20424 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20425 +                       if (unlikely(err))
20426 +                               goto out_wh;
20427 +               }
20428 +               if (!err) {
20429 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20430 +                       err = -ENOENT;
20431 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20432 +                               delegated = NULL;
20433 +                               err = vfsub_link(h_src_dentry,
20434 +                                                au_pinned_h_dir(&a->pin),
20435 +                                                &a->h_path, &delegated);
20436 +                               if (unlikely(err == -EWOULDBLOCK)) {
20437 +                                       pr_warn("cannot retry"
20438 +                                               " for NFSv4 delegation"
20439 +                                               " for an internal link\n");
20440 +                                       iput(delegated);
20441 +                               }
20442 +                       }
20443 +               }
20444 +       }
20445 +       if (unlikely(err))
20446 +               goto out_unpin;
20447 +
20448 +       if (wh_dentry) {
20449 +               a->h_path.dentry = wh_dentry;
20450 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20451 +                                         dentry);
20452 +               if (unlikely(err))
20453 +                       goto out_revert;
20454 +       }
20455 +
20456 +       au_dir_ts(dir, a->bdst);
20457 +       inode_inc_iversion(dir);
20458 +       inc_nlink(inode);
20459 +       inode->i_ctime = dir->i_ctime;
20460 +       d_instantiate(dentry, au_igrab(inode));
20461 +       if (d_unhashed(a->h_path.dentry))
20462 +               /* some filesystem calls d_drop() */
20463 +               d_drop(dentry);
20464 +       /* some filesystems consume an inode even hardlink */
20465 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20466 +       goto out_unpin; /* success */
20467 +
20468 +out_revert:
20469 +       /* no delegation since it is just created */
20470 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20471 +                           /*delegated*/NULL, /*force*/0);
20472 +       if (unlikely(rerr)) {
20473 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20474 +               err = -EIO;
20475 +       }
20476 +       au_dtime_revert(&dt);
20477 +out_unpin:
20478 +       au_unpin(&a->pin);
20479 +out_wh:
20480 +       dput(wh_dentry);
20481 +out_parent:
20482 +       di_write_unlock(a->parent);
20483 +       dput(a->src_parent);
20484 +out_unlock:
20485 +       if (unlikely(err)) {
20486 +               au_update_dbtop(dentry);
20487 +               d_drop(dentry);
20488 +       }
20489 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20490 +out_kfree:
20491 +       kfree(a);
20492 +out:
20493 +       AuTraceErr(err);
20494 +       return err;
20495 +}
20496 +
20497 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20498 +{
20499 +       int err, rerr;
20500 +       aufs_bindex_t bindex;
20501 +       unsigned char diropq;
20502 +       struct path h_path;
20503 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20504 +       struct inode *h_inode;
20505 +       struct super_block *sb;
20506 +       struct {
20507 +               struct au_pin pin;
20508 +               struct au_dtime dt;
20509 +       } *a; /* reduce the stack usage */
20510 +       struct au_wr_dir_args wr_dir_args = {
20511 +               .force_btgt     = -1,
20512 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20513 +       };
20514 +
20515 +       IMustLock(dir);
20516 +
20517 +       err = -ENOMEM;
20518 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20519 +       if (unlikely(!a))
20520 +               goto out;
20521 +
20522 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20523 +       if (unlikely(err))
20524 +               goto out_free;
20525 +       err = au_d_may_add(dentry);
20526 +       if (unlikely(err))
20527 +               goto out_unlock;
20528 +
20529 +       parent = dentry->d_parent; /* dir inode is locked */
20530 +       di_write_lock_parent(parent);
20531 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20532 +                                     &a->pin, &wr_dir_args);
20533 +       err = PTR_ERR(wh_dentry);
20534 +       if (IS_ERR(wh_dentry))
20535 +               goto out_parent;
20536 +
20537 +       sb = dentry->d_sb;
20538 +       bindex = au_dbtop(dentry);
20539 +       h_path.dentry = au_h_dptr(dentry, bindex);
20540 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20541 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20542 +       if (unlikely(err))
20543 +               goto out_unpin;
20544 +
20545 +       /* make the dir opaque */
20546 +       diropq = 0;
20547 +       h_inode = d_inode(h_path.dentry);
20548 +       if (wh_dentry
20549 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20550 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20551 +               opq_dentry = au_diropq_create(dentry, bindex);
20552 +               inode_unlock(h_inode);
20553 +               err = PTR_ERR(opq_dentry);
20554 +               if (IS_ERR(opq_dentry))
20555 +                       goto out_dir;
20556 +               dput(opq_dentry);
20557 +               diropq = 1;
20558 +       }
20559 +
20560 +       err = epilog(dir, bindex, wh_dentry, dentry);
20561 +       if (!err) {
20562 +               inc_nlink(dir);
20563 +               goto out_unpin; /* success */
20564 +       }
20565 +
20566 +       /* revert */
20567 +       if (diropq) {
20568 +               AuLabel(revert opq);
20569 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20570 +               rerr = au_diropq_remove(dentry, bindex);
20571 +               inode_unlock(h_inode);
20572 +               if (rerr) {
20573 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20574 +                               dentry, err, rerr);
20575 +                       err = -EIO;
20576 +               }
20577 +       }
20578 +
20579 +out_dir:
20580 +       AuLabel(revert dir);
20581 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20582 +       if (rerr) {
20583 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20584 +                       dentry, err, rerr);
20585 +               err = -EIO;
20586 +       }
20587 +       au_dtime_revert(&a->dt);
20588 +out_unpin:
20589 +       au_unpin(&a->pin);
20590 +       dput(wh_dentry);
20591 +out_parent:
20592 +       di_write_unlock(parent);
20593 +out_unlock:
20594 +       if (unlikely(err)) {
20595 +               au_update_dbtop(dentry);
20596 +               d_drop(dentry);
20597 +       }
20598 +       aufs_read_unlock(dentry, AuLock_DW);
20599 +out_free:
20600 +       kfree(a);
20601 +out:
20602 +       return err;
20603 +}
20604 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20605 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20606 +++ linux/fs/aufs/i_op.c        2018-08-12 23:43:05.456791299 +0200
20607 @@ -0,0 +1,1460 @@
20608 +// SPDX-License-Identifier: GPL-2.0
20609 +/*
20610 + * Copyright (C) 2005-2018 Junjiro R. Okajima
20611 + *
20612 + * This program, aufs is free software; you can redistribute it and/or modify
20613 + * it under the terms of the GNU General Public License as published by
20614 + * the Free Software Foundation; either version 2 of the License, or
20615 + * (at your option) any later version.
20616 + *
20617 + * This program is distributed in the hope that it will be useful,
20618 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20619 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20620 + * GNU General Public License for more details.
20621 + *
20622 + * You should have received a copy of the GNU General Public License
20623 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20624 + */
20625 +
20626 +/*
20627 + * inode operations (except add/del/rename)
20628 + */
20629 +
20630 +#include <linux/device_cgroup.h>
20631 +#include <linux/fs_stack.h>
20632 +#include <linux/namei.h>
20633 +#include <linux/security.h>
20634 +#include "aufs.h"
20635 +
20636 +static int h_permission(struct inode *h_inode, int mask,
20637 +                       struct path *h_path, int brperm)
20638 +{
20639 +       int err;
20640 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20641 +
20642 +       err = -EPERM;
20643 +       if (write_mask && IS_IMMUTABLE(h_inode))
20644 +               goto out;
20645 +
20646 +       err = -EACCES;
20647 +       if (((mask & MAY_EXEC)
20648 +            && S_ISREG(h_inode->i_mode)
20649 +            && (path_noexec(h_path)
20650 +                || !(h_inode->i_mode & 0111))))
20651 +               goto out;
20652 +
20653 +       /*
20654 +        * - skip the lower fs test in the case of write to ro branch.
20655 +        * - nfs dir permission write check is optimized, but a policy for
20656 +        *   link/rename requires a real check.
20657 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20658 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20659 +        */
20660 +       if ((write_mask && !au_br_writable(brperm))
20661 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20662 +               && write_mask && !(mask & MAY_READ))
20663 +           || !h_inode->i_op->permission) {
20664 +               /* AuLabel(generic_permission); */
20665 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20666 +               err = generic_permission(h_inode, mask);
20667 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20668 +                       err = h_inode->i_op->permission(h_inode, mask);
20669 +               AuTraceErr(err);
20670 +       } else {
20671 +               /* AuLabel(h_inode->permission); */
20672 +               err = h_inode->i_op->permission(h_inode, mask);
20673 +               AuTraceErr(err);
20674 +       }
20675 +
20676 +       if (!err)
20677 +               err = devcgroup_inode_permission(h_inode, mask);
20678 +       if (!err)
20679 +               err = security_inode_permission(h_inode, mask);
20680 +
20681 +#if 0
20682 +       if (!err) {
20683 +               /* todo: do we need to call ima_path_check()? */
20684 +               struct path h_path = {
20685 +                       .dentry =
20686 +                       .mnt    = h_mnt
20687 +               };
20688 +               err = ima_path_check(&h_path,
20689 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20690 +                                    IMA_COUNT_LEAVE);
20691 +       }
20692 +#endif
20693 +
20694 +out:
20695 +       return err;
20696 +}
20697 +
20698 +static int aufs_permission(struct inode *inode, int mask)
20699 +{
20700 +       int err;
20701 +       aufs_bindex_t bindex, bbot;
20702 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20703 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20704 +       struct inode *h_inode;
20705 +       struct super_block *sb;
20706 +       struct au_branch *br;
20707 +
20708 +       /* todo: support rcu-walk? */
20709 +       if (mask & MAY_NOT_BLOCK)
20710 +               return -ECHILD;
20711 +
20712 +       sb = inode->i_sb;
20713 +       si_read_lock(sb, AuLock_FLUSH);
20714 +       ii_read_lock_child(inode);
20715 +#if 0
20716 +       err = au_iigen_test(inode, au_sigen(sb));
20717 +       if (unlikely(err))
20718 +               goto out;
20719 +#endif
20720 +
20721 +       if (!isdir
20722 +           || write_mask
20723 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20724 +               err = au_busy_or_stale();
20725 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20726 +               if (unlikely(!h_inode
20727 +                            || (h_inode->i_mode & S_IFMT)
20728 +                            != (inode->i_mode & S_IFMT)))
20729 +                       goto out;
20730 +
20731 +               err = 0;
20732 +               bindex = au_ibtop(inode);
20733 +               br = au_sbr(sb, bindex);
20734 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20735 +               if (write_mask
20736 +                   && !err
20737 +                   && !special_file(h_inode->i_mode)) {
20738 +                       /* test whether the upper writable branch exists */
20739 +                       err = -EROFS;
20740 +                       for (; bindex >= 0; bindex--)
20741 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20742 +                                       err = 0;
20743 +                                       break;
20744 +                               }
20745 +               }
20746 +               goto out;
20747 +       }
20748 +
20749 +       /* non-write to dir */
20750 +       err = 0;
20751 +       bbot = au_ibbot(inode);
20752 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20753 +               h_inode = au_h_iptr(inode, bindex);
20754 +               if (h_inode) {
20755 +                       err = au_busy_or_stale();
20756 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20757 +                               break;
20758 +
20759 +                       br = au_sbr(sb, bindex);
20760 +                       err = h_permission(h_inode, mask, &br->br_path,
20761 +                                          br->br_perm);
20762 +               }
20763 +       }
20764 +
20765 +out:
20766 +       ii_read_unlock(inode);
20767 +       si_read_unlock(sb);
20768 +       return err;
20769 +}
20770 +
20771 +/* ---------------------------------------------------------------------- */
20772 +
20773 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20774 +                                 unsigned int flags)
20775 +{
20776 +       struct dentry *ret, *parent;
20777 +       struct inode *inode;
20778 +       struct super_block *sb;
20779 +       int err, npositive;
20780 +
20781 +       IMustLock(dir);
20782 +
20783 +       /* todo: support rcu-walk? */
20784 +       ret = ERR_PTR(-ECHILD);
20785 +       if (flags & LOOKUP_RCU)
20786 +               goto out;
20787 +
20788 +       ret = ERR_PTR(-ENAMETOOLONG);
20789 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20790 +               goto out;
20791 +
20792 +       sb = dir->i_sb;
20793 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20794 +       ret = ERR_PTR(err);
20795 +       if (unlikely(err))
20796 +               goto out;
20797 +
20798 +       err = au_di_init(dentry);
20799 +       ret = ERR_PTR(err);
20800 +       if (unlikely(err))
20801 +               goto out_si;
20802 +
20803 +       inode = NULL;
20804 +       npositive = 0; /* suppress a warning */
20805 +       parent = dentry->d_parent; /* dir inode is locked */
20806 +       di_read_lock_parent(parent, AuLock_IR);
20807 +       err = au_alive_dir(parent);
20808 +       if (!err)
20809 +               err = au_digen_test(parent, au_sigen(sb));
20810 +       if (!err) {
20811 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20812 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20813 +                                          AuLkup_ALLOW_NEG);
20814 +               err = npositive;
20815 +       }
20816 +       di_read_unlock(parent, AuLock_IR);
20817 +       ret = ERR_PTR(err);
20818 +       if (unlikely(err < 0))
20819 +               goto out_unlock;
20820 +
20821 +       if (npositive) {
20822 +               inode = au_new_inode(dentry, /*must_new*/0);
20823 +               if (IS_ERR(inode)) {
20824 +                       ret = (void *)inode;
20825 +                       inode = NULL;
20826 +                       goto out_unlock;
20827 +               }
20828 +       }
20829 +
20830 +       if (inode)
20831 +               atomic_inc(&inode->i_count);
20832 +       ret = d_splice_alias(inode, dentry);
20833 +#if 0
20834 +       if (unlikely(d_need_lookup(dentry))) {
20835 +               spin_lock(&dentry->d_lock);
20836 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20837 +               spin_unlock(&dentry->d_lock);
20838 +       } else
20839 +#endif
20840 +       if (inode) {
20841 +               if (!IS_ERR(ret)) {
20842 +                       iput(inode);
20843 +                       if (ret && ret != dentry)
20844 +                               ii_write_unlock(inode);
20845 +               } else {
20846 +                       ii_write_unlock(inode);
20847 +                       iput(inode);
20848 +                       inode = NULL;
20849 +               }
20850 +       }
20851 +
20852 +out_unlock:
20853 +       di_write_unlock(dentry);
20854 +out_si:
20855 +       si_read_unlock(sb);
20856 +out:
20857 +       return ret;
20858 +}
20859 +
20860 +/* ---------------------------------------------------------------------- */
20861 +
20862 +struct aopen_node {
20863 +       struct hlist_bl_node hblist;
20864 +       struct file *file, *h_file;
20865 +};
20866 +
20867 +static int au_do_aopen(struct inode *inode, struct file *file)
20868 +{
20869 +       struct hlist_bl_head *aopen;
20870 +       struct hlist_bl_node *pos;
20871 +       struct aopen_node *node;
20872 +       struct au_do_open_args args = {
20873 +               .aopen  = 1,
20874 +               .open   = au_do_open_nondir
20875 +       };
20876 +
20877 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20878 +       hlist_bl_lock(aopen);
20879 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20880 +               if (node->file == file) {
20881 +                       args.h_file = node->h_file;
20882 +                       break;
20883 +               }
20884 +       hlist_bl_unlock(aopen);
20885 +       /* AuDebugOn(!args.h_file); */
20886 +
20887 +       return au_do_open(file, &args);
20888 +}
20889 +
20890 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20891 +                           struct file *file, unsigned int open_flag,
20892 +                           umode_t create_mode, int *opened)
20893 +{
20894 +       int err, unlocked, h_opened = *opened;
20895 +       unsigned int lkup_flags;
20896 +       struct dentry *parent, *d;
20897 +       struct hlist_bl_head *aopen;
20898 +       struct vfsub_aopen_args args = {
20899 +               .open_flag      = open_flag,
20900 +               .create_mode    = create_mode,
20901 +               .opened         = &h_opened
20902 +       };
20903 +       struct aopen_node aopen_node = {
20904 +               .file   = file
20905 +       };
20906 +
20907 +       IMustLock(dir);
20908 +       AuDbg("open_flag 0%o\n", open_flag);
20909 +       AuDbgDentry(dentry);
20910 +
20911 +       err = 0;
20912 +       if (!au_di(dentry)) {
20913 +               lkup_flags = LOOKUP_OPEN;
20914 +               if (open_flag & O_CREAT)
20915 +                       lkup_flags |= LOOKUP_CREATE;
20916 +               d = aufs_lookup(dir, dentry, lkup_flags);
20917 +               if (IS_ERR(d)) {
20918 +                       err = PTR_ERR(d);
20919 +                       AuTraceErr(err);
20920 +                       goto out;
20921 +               } else if (d) {
20922 +                       /*
20923 +                        * obsoleted dentry found.
20924 +                        * another error will be returned later.
20925 +                        */
20926 +                       d_drop(d);
20927 +                       AuDbgDentry(d);
20928 +                       dput(d);
20929 +               }
20930 +               AuDbgDentry(dentry);
20931 +       }
20932 +
20933 +       if (d_is_positive(dentry)
20934 +           || d_unhashed(dentry)
20935 +           || d_unlinked(dentry)
20936 +           || !(open_flag & O_CREAT))
20937 +               goto out_no_open;
20938 +
20939 +       unlocked = 0;
20940 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20941 +       if (unlikely(err))
20942 +               goto out;
20943 +
20944 +       parent = dentry->d_parent;      /* dir is locked */
20945 +       di_write_lock_parent(parent);
20946 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20947 +       if (unlikely(err))
20948 +               goto out_unlock;
20949 +
20950 +       AuDbgDentry(dentry);
20951 +       if (d_is_positive(dentry))
20952 +               goto out_unlock;
20953 +
20954 +       args.file = get_empty_filp();
20955 +       err = PTR_ERR(args.file);
20956 +       if (IS_ERR(args.file))
20957 +               goto out_unlock;
20958 +
20959 +       args.file->f_flags = file->f_flags;
20960 +       err = au_aopen_or_create(dir, dentry, &args);
20961 +       AuTraceErr(err);
20962 +       AuDbgFile(args.file);
20963 +       if (unlikely(err < 0)) {
20964 +               if (h_opened & FILE_OPENED)
20965 +                       fput(args.file);
20966 +               else
20967 +                       put_filp(args.file);
20968 +               goto out_unlock;
20969 +       }
20970 +       di_write_unlock(parent);
20971 +       di_write_unlock(dentry);
20972 +       unlocked = 1;
20973 +
20974 +       /* some filesystems don't set FILE_CREATED while succeeded? */
20975 +       *opened |= FILE_CREATED;
20976 +       if (h_opened & FILE_OPENED)
20977 +               aopen_node.h_file = args.file;
20978 +       else {
20979 +               put_filp(args.file);
20980 +               args.file = NULL;
20981 +       }
20982 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
20983 +       au_hbl_add(&aopen_node.hblist, aopen);
20984 +       err = finish_open(file, dentry, au_do_aopen, opened);
20985 +       au_hbl_del(&aopen_node.hblist, aopen);
20986 +       AuTraceErr(err);
20987 +       AuDbgFile(file);
20988 +       if (aopen_node.h_file)
20989 +               fput(aopen_node.h_file);
20990 +
20991 +out_unlock:
20992 +       if (unlocked)
20993 +               si_read_unlock(dentry->d_sb);
20994 +       else {
20995 +               di_write_unlock(parent);
20996 +               aufs_read_unlock(dentry, AuLock_DW);
20997 +       }
20998 +       AuDbgDentry(dentry);
20999 +       if (unlikely(err < 0))
21000 +               goto out;
21001 +out_no_open:
21002 +       if (err >= 0 && !(*opened & FILE_CREATED)) {
21003 +               AuLabel(out_no_open);
21004 +               dget(dentry);
21005 +               err = finish_no_open(file, dentry);
21006 +       }
21007 +out:
21008 +       AuDbg("%pd%s%s\n", dentry,
21009 +             (*opened & FILE_CREATED) ? " created" : "",
21010 +             (*opened & FILE_OPENED) ? " opened" : "");
21011 +       AuTraceErr(err);
21012 +       return err;
21013 +}
21014 +
21015 +
21016 +/* ---------------------------------------------------------------------- */
21017 +
21018 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21019 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
21020 +                         aufs_bindex_t btop)
21021 +{
21022 +       int err;
21023 +       struct dentry *h_parent;
21024 +       struct inode *h_dir;
21025 +
21026 +       if (add_entry)
21027 +               IMustLock(d_inode(parent));
21028 +       else
21029 +               di_write_lock_parent(parent);
21030 +
21031 +       err = 0;
21032 +       if (!au_h_dptr(parent, bcpup)) {
21033 +               if (btop > bcpup)
21034 +                       err = au_cpup_dirs(dentry, bcpup);
21035 +               else if (btop < bcpup)
21036 +                       err = au_cpdown_dirs(dentry, bcpup);
21037 +               else
21038 +                       BUG();
21039 +       }
21040 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
21041 +               h_parent = au_h_dptr(parent, bcpup);
21042 +               h_dir = d_inode(h_parent);
21043 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
21044 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
21045 +               /* todo: no unlock here */
21046 +               inode_unlock_shared(h_dir);
21047 +
21048 +               AuDbg("bcpup %d\n", bcpup);
21049 +               if (!err) {
21050 +                       if (d_really_is_negative(dentry))
21051 +                               au_set_h_dptr(dentry, btop, NULL);
21052 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
21053 +               }
21054 +       }
21055 +
21056 +       if (!add_entry)
21057 +               di_write_unlock(parent);
21058 +       if (!err)
21059 +               err = bcpup; /* success */
21060 +
21061 +       AuTraceErr(err);
21062 +       return err;
21063 +}
21064 +
21065 +/*
21066 + * decide the branch and the parent dir where we will create a new entry.
21067 + * returns new bindex or an error.
21068 + * copyup the parent dir if needed.
21069 + */
21070 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21071 +             struct au_wr_dir_args *args)
21072 +{
21073 +       int err;
21074 +       unsigned int flags;
21075 +       aufs_bindex_t bcpup, btop, src_btop;
21076 +       const unsigned char add_entry
21077 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
21078 +               | au_ftest_wrdir(args->flags, TMPFILE);
21079 +       struct super_block *sb;
21080 +       struct dentry *parent;
21081 +       struct au_sbinfo *sbinfo;
21082 +
21083 +       sb = dentry->d_sb;
21084 +       sbinfo = au_sbi(sb);
21085 +       parent = dget_parent(dentry);
21086 +       btop = au_dbtop(dentry);
21087 +       bcpup = btop;
21088 +       if (args->force_btgt < 0) {
21089 +               if (src_dentry) {
21090 +                       src_btop = au_dbtop(src_dentry);
21091 +                       if (src_btop < btop)
21092 +                               bcpup = src_btop;
21093 +               } else if (add_entry) {
21094 +                       flags = 0;
21095 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21096 +                               au_fset_wbr(flags, DIR);
21097 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21098 +                       bcpup = err;
21099 +               }
21100 +
21101 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21102 +                       if (add_entry)
21103 +                               err = AuWbrCopyup(sbinfo, dentry);
21104 +                       else {
21105 +                               if (!IS_ROOT(dentry)) {
21106 +                                       di_read_lock_parent(parent, !AuLock_IR);
21107 +                                       err = AuWbrCopyup(sbinfo, dentry);
21108 +                                       di_read_unlock(parent, !AuLock_IR);
21109 +                               } else
21110 +                                       err = AuWbrCopyup(sbinfo, dentry);
21111 +                       }
21112 +                       bcpup = err;
21113 +                       if (unlikely(err < 0))
21114 +                               goto out;
21115 +               }
21116 +       } else {
21117 +               bcpup = args->force_btgt;
21118 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21119 +       }
21120 +
21121 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21122 +       err = bcpup;
21123 +       if (bcpup == btop)
21124 +               goto out; /* success */
21125 +
21126 +       /* copyup the new parent into the branch we process */
21127 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21128 +       if (err >= 0) {
21129 +               if (d_really_is_negative(dentry)) {
21130 +                       au_set_h_dptr(dentry, btop, NULL);
21131 +                       au_set_dbtop(dentry, bcpup);
21132 +                       au_set_dbbot(dentry, bcpup);
21133 +               }
21134 +               AuDebugOn(add_entry
21135 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21136 +                         && !au_h_dptr(dentry, bcpup));
21137 +       }
21138 +
21139 +out:
21140 +       dput(parent);
21141 +       return err;
21142 +}
21143 +
21144 +/* ---------------------------------------------------------------------- */
21145 +
21146 +void au_pin_hdir_unlock(struct au_pin *p)
21147 +{
21148 +       if (p->hdir)
21149 +               au_hn_inode_unlock(p->hdir);
21150 +}
21151 +
21152 +int au_pin_hdir_lock(struct au_pin *p)
21153 +{
21154 +       int err;
21155 +
21156 +       err = 0;
21157 +       if (!p->hdir)
21158 +               goto out;
21159 +
21160 +       /* even if an error happens later, keep this lock */
21161 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21162 +
21163 +       err = -EBUSY;
21164 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21165 +               goto out;
21166 +
21167 +       err = 0;
21168 +       if (p->h_dentry)
21169 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21170 +                                 p->h_parent, p->br);
21171 +
21172 +out:
21173 +       return err;
21174 +}
21175 +
21176 +int au_pin_hdir_relock(struct au_pin *p)
21177 +{
21178 +       int err, i;
21179 +       struct inode *h_i;
21180 +       struct dentry *h_d[] = {
21181 +               p->h_dentry,
21182 +               p->h_parent
21183 +       };
21184 +
21185 +       err = au_pin_hdir_lock(p);
21186 +       if (unlikely(err))
21187 +               goto out;
21188 +
21189 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21190 +               if (!h_d[i])
21191 +                       continue;
21192 +               if (d_is_positive(h_d[i])) {
21193 +                       h_i = d_inode(h_d[i]);
21194 +                       err = !h_i->i_nlink;
21195 +               }
21196 +       }
21197 +
21198 +out:
21199 +       return err;
21200 +}
21201 +
21202 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21203 +{
21204 +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21205 +       p->hdir->hi_inode->i_rwsem.owner = task;
21206 +#endif
21207 +}
21208 +
21209 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21210 +{
21211 +       if (p->hdir) {
21212 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21213 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21214 +               au_pin_hdir_set_owner(p, current);
21215 +       }
21216 +}
21217 +
21218 +void au_pin_hdir_release(struct au_pin *p)
21219 +{
21220 +       if (p->hdir) {
21221 +               au_pin_hdir_set_owner(p, p->task);
21222 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
21223 +       }
21224 +}
21225 +
21226 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21227 +{
21228 +       if (pin && pin->parent)
21229 +               return au_h_dptr(pin->parent, pin->bindex);
21230 +       return NULL;
21231 +}
21232 +
21233 +void au_unpin(struct au_pin *p)
21234 +{
21235 +       if (p->hdir)
21236 +               au_pin_hdir_unlock(p);
21237 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21238 +               vfsub_mnt_drop_write(p->h_mnt);
21239 +       if (!p->hdir)
21240 +               return;
21241 +
21242 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21243 +               di_read_unlock(p->parent, AuLock_IR);
21244 +       iput(p->hdir->hi_inode);
21245 +       dput(p->parent);
21246 +       p->parent = NULL;
21247 +       p->hdir = NULL;
21248 +       p->h_mnt = NULL;
21249 +       /* do not clear p->task */
21250 +}
21251 +
21252 +int au_do_pin(struct au_pin *p)
21253 +{
21254 +       int err;
21255 +       struct super_block *sb;
21256 +       struct inode *h_dir;
21257 +
21258 +       err = 0;
21259 +       sb = p->dentry->d_sb;
21260 +       p->br = au_sbr(sb, p->bindex);
21261 +       if (IS_ROOT(p->dentry)) {
21262 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21263 +                       p->h_mnt = au_br_mnt(p->br);
21264 +                       err = vfsub_mnt_want_write(p->h_mnt);
21265 +                       if (unlikely(err)) {
21266 +                               au_fclr_pin(p->flags, MNT_WRITE);
21267 +                               goto out_err;
21268 +                       }
21269 +               }
21270 +               goto out;
21271 +       }
21272 +
21273 +       p->h_dentry = NULL;
21274 +       if (p->bindex <= au_dbbot(p->dentry))
21275 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21276 +
21277 +       p->parent = dget_parent(p->dentry);
21278 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21279 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21280 +
21281 +       h_dir = NULL;
21282 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21283 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21284 +       if (p->hdir)
21285 +               h_dir = p->hdir->hi_inode;
21286 +
21287 +       /*
21288 +        * udba case, or
21289 +        * if DI_LOCKED is not set, then p->parent may be different
21290 +        * and h_parent can be NULL.
21291 +        */
21292 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21293 +               err = -EBUSY;
21294 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21295 +                       di_read_unlock(p->parent, AuLock_IR);
21296 +               dput(p->parent);
21297 +               p->parent = NULL;
21298 +               goto out_err;
21299 +       }
21300 +
21301 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21302 +               p->h_mnt = au_br_mnt(p->br);
21303 +               err = vfsub_mnt_want_write(p->h_mnt);
21304 +               if (unlikely(err)) {
21305 +                       au_fclr_pin(p->flags, MNT_WRITE);
21306 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21307 +                               di_read_unlock(p->parent, AuLock_IR);
21308 +                       dput(p->parent);
21309 +                       p->parent = NULL;
21310 +                       goto out_err;
21311 +               }
21312 +       }
21313 +
21314 +       au_igrab(h_dir);
21315 +       err = au_pin_hdir_lock(p);
21316 +       if (!err)
21317 +               goto out; /* success */
21318 +
21319 +       au_unpin(p);
21320 +
21321 +out_err:
21322 +       pr_err("err %d\n", err);
21323 +       err = au_busy_or_stale();
21324 +out:
21325 +       return err;
21326 +}
21327 +
21328 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21329 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21330 +                unsigned int udba, unsigned char flags)
21331 +{
21332 +       p->dentry = dentry;
21333 +       p->udba = udba;
21334 +       p->lsc_di = lsc_di;
21335 +       p->lsc_hi = lsc_hi;
21336 +       p->flags = flags;
21337 +       p->bindex = bindex;
21338 +
21339 +       p->parent = NULL;
21340 +       p->hdir = NULL;
21341 +       p->h_mnt = NULL;
21342 +
21343 +       p->h_dentry = NULL;
21344 +       p->h_parent = NULL;
21345 +       p->br = NULL;
21346 +       p->task = current;
21347 +}
21348 +
21349 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21350 +          unsigned int udba, unsigned char flags)
21351 +{
21352 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21353 +                   udba, flags);
21354 +       return au_do_pin(pin);
21355 +}
21356 +
21357 +/* ---------------------------------------------------------------------- */
21358 +
21359 +/*
21360 + * ->setattr() and ->getattr() are called in various cases.
21361 + * chmod, stat: dentry is revalidated.
21362 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21363 + *               unhashed.
21364 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21365 + */
21366 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21367 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21368 +{
21369 +       int err;
21370 +       struct dentry *parent;
21371 +
21372 +       err = 0;
21373 +       if (au_digen_test(dentry, sigen)) {
21374 +               parent = dget_parent(dentry);
21375 +               di_read_lock_parent(parent, AuLock_IR);
21376 +               err = au_refresh_dentry(dentry, parent);
21377 +               di_read_unlock(parent, AuLock_IR);
21378 +               dput(parent);
21379 +       }
21380 +
21381 +       AuTraceErr(err);
21382 +       return err;
21383 +}
21384 +
21385 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21386 +                    struct au_icpup_args *a)
21387 +{
21388 +       int err;
21389 +       loff_t sz;
21390 +       aufs_bindex_t btop, ibtop;
21391 +       struct dentry *hi_wh, *parent;
21392 +       struct inode *inode;
21393 +       struct au_wr_dir_args wr_dir_args = {
21394 +               .force_btgt     = -1,
21395 +               .flags          = 0
21396 +       };
21397 +
21398 +       if (d_is_dir(dentry))
21399 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21400 +       /* plink or hi_wh() case */
21401 +       btop = au_dbtop(dentry);
21402 +       inode = d_inode(dentry);
21403 +       ibtop = au_ibtop(inode);
21404 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21405 +               wr_dir_args.force_btgt = ibtop;
21406 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21407 +       if (unlikely(err < 0))
21408 +               goto out;
21409 +       a->btgt = err;
21410 +       if (err != btop)
21411 +               au_fset_icpup(a->flags, DID_CPUP);
21412 +
21413 +       err = 0;
21414 +       a->pin_flags = AuPin_MNT_WRITE;
21415 +       parent = NULL;
21416 +       if (!IS_ROOT(dentry)) {
21417 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21418 +               parent = dget_parent(dentry);
21419 +               di_write_lock_parent(parent);
21420 +       }
21421 +
21422 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21423 +       if (unlikely(err))
21424 +               goto out_parent;
21425 +
21426 +       sz = -1;
21427 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21428 +       a->h_inode = d_inode(a->h_path.dentry);
21429 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21430 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21431 +               if (ia->ia_size < i_size_read(a->h_inode))
21432 +                       sz = ia->ia_size;
21433 +               inode_unlock_shared(a->h_inode);
21434 +       }
21435 +
21436 +       hi_wh = NULL;
21437 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21438 +               hi_wh = au_hi_wh(inode, a->btgt);
21439 +               if (!hi_wh) {
21440 +                       struct au_cp_generic cpg = {
21441 +                               .dentry = dentry,
21442 +                               .bdst   = a->btgt,
21443 +                               .bsrc   = -1,
21444 +                               .len    = sz,
21445 +                               .pin    = &a->pin
21446 +                       };
21447 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21448 +                       if (unlikely(err))
21449 +                               goto out_unlock;
21450 +                       hi_wh = au_hi_wh(inode, a->btgt);
21451 +                       /* todo: revalidate hi_wh? */
21452 +               }
21453 +       }
21454 +
21455 +       if (parent) {
21456 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21457 +               di_downgrade_lock(parent, AuLock_IR);
21458 +               dput(parent);
21459 +               parent = NULL;
21460 +       }
21461 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21462 +               goto out; /* success */
21463 +
21464 +       if (!d_unhashed(dentry)) {
21465 +               struct au_cp_generic cpg = {
21466 +                       .dentry = dentry,
21467 +                       .bdst   = a->btgt,
21468 +                       .bsrc   = btop,
21469 +                       .len    = sz,
21470 +                       .pin    = &a->pin,
21471 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21472 +               };
21473 +               err = au_sio_cpup_simple(&cpg);
21474 +               if (!err)
21475 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21476 +       } else if (!hi_wh)
21477 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21478 +       else
21479 +               a->h_path.dentry = hi_wh; /* do not dget here */
21480 +
21481 +out_unlock:
21482 +       a->h_inode = d_inode(a->h_path.dentry);
21483 +       if (!err)
21484 +               goto out; /* success */
21485 +       au_unpin(&a->pin);
21486 +out_parent:
21487 +       if (parent) {
21488 +               di_write_unlock(parent);
21489 +               dput(parent);
21490 +       }
21491 +out:
21492 +       if (!err)
21493 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21494 +       return err;
21495 +}
21496 +
21497 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21498 +{
21499 +       int err;
21500 +       struct inode *inode, *delegated;
21501 +       struct super_block *sb;
21502 +       struct file *file;
21503 +       struct au_icpup_args *a;
21504 +
21505 +       inode = d_inode(dentry);
21506 +       IMustLock(inode);
21507 +
21508 +       err = setattr_prepare(dentry, ia);
21509 +       if (unlikely(err))
21510 +               goto out;
21511 +
21512 +       err = -ENOMEM;
21513 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21514 +       if (unlikely(!a))
21515 +               goto out;
21516 +
21517 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21518 +               ia->ia_valid &= ~ATTR_MODE;
21519 +
21520 +       file = NULL;
21521 +       sb = dentry->d_sb;
21522 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21523 +       if (unlikely(err))
21524 +               goto out_kfree;
21525 +
21526 +       if (ia->ia_valid & ATTR_FILE) {
21527 +               /* currently ftruncate(2) only */
21528 +               AuDebugOn(!d_is_reg(dentry));
21529 +               file = ia->ia_file;
21530 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21531 +                                           /*fi_lsc*/0);
21532 +               if (unlikely(err))
21533 +                       goto out_si;
21534 +               ia->ia_file = au_hf_top(file);
21535 +               a->udba = AuOpt_UDBA_NONE;
21536 +       } else {
21537 +               /* fchmod() doesn't pass ia_file */
21538 +               a->udba = au_opt_udba(sb);
21539 +               di_write_lock_child(dentry);
21540 +               /* no d_unlinked(), to set UDBA_NONE for root */
21541 +               if (d_unhashed(dentry))
21542 +                       a->udba = AuOpt_UDBA_NONE;
21543 +               if (a->udba != AuOpt_UDBA_NONE) {
21544 +                       AuDebugOn(IS_ROOT(dentry));
21545 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21546 +                       if (unlikely(err))
21547 +                               goto out_dentry;
21548 +               }
21549 +       }
21550 +
21551 +       err = au_pin_and_icpup(dentry, ia, a);
21552 +       if (unlikely(err < 0))
21553 +               goto out_dentry;
21554 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21555 +               ia->ia_file = NULL;
21556 +               ia->ia_valid &= ~ATTR_FILE;
21557 +       }
21558 +
21559 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21560 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21561 +           == (ATTR_MODE | ATTR_CTIME)) {
21562 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21563 +               if (unlikely(err))
21564 +                       goto out_unlock;
21565 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21566 +                  && (ia->ia_valid & ATTR_CTIME)) {
21567 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21568 +               if (unlikely(err))
21569 +                       goto out_unlock;
21570 +       }
21571 +
21572 +       if (ia->ia_valid & ATTR_SIZE) {
21573 +               struct file *f;
21574 +
21575 +               if (ia->ia_size < i_size_read(inode))
21576 +                       /* unmap only */
21577 +                       truncate_setsize(inode, ia->ia_size);
21578 +
21579 +               f = NULL;
21580 +               if (ia->ia_valid & ATTR_FILE)
21581 +                       f = ia->ia_file;
21582 +               inode_unlock(a->h_inode);
21583 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21584 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21585 +       } else {
21586 +               delegated = NULL;
21587 +               while (1) {
21588 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21589 +                       if (delegated) {
21590 +                               err = break_deleg_wait(&delegated);
21591 +                               if (!err)
21592 +                                       continue;
21593 +                       }
21594 +                       break;
21595 +               }
21596 +       }
21597 +       /*
21598 +        * regardless aufs 'acl' option setting.
21599 +        * why don't all acl-aware fs call this func from their ->setattr()?
21600 +        */
21601 +       if (!err && (ia->ia_valid & ATTR_MODE))
21602 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21603 +       if (!err)
21604 +               au_cpup_attr_changeable(inode);
21605 +
21606 +out_unlock:
21607 +       inode_unlock(a->h_inode);
21608 +       au_unpin(&a->pin);
21609 +       if (unlikely(err))
21610 +               au_update_dbtop(dentry);
21611 +out_dentry:
21612 +       di_write_unlock(dentry);
21613 +       if (file) {
21614 +               fi_write_unlock(file);
21615 +               ia->ia_file = file;
21616 +               ia->ia_valid |= ATTR_FILE;
21617 +       }
21618 +out_si:
21619 +       si_read_unlock(sb);
21620 +out_kfree:
21621 +       kfree(a);
21622 +out:
21623 +       AuTraceErr(err);
21624 +       return err;
21625 +}
21626 +
21627 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21628 +static int au_h_path_to_set_attr(struct dentry *dentry,
21629 +                                struct au_icpup_args *a, struct path *h_path)
21630 +{
21631 +       int err;
21632 +       struct super_block *sb;
21633 +
21634 +       sb = dentry->d_sb;
21635 +       a->udba = au_opt_udba(sb);
21636 +       /* no d_unlinked(), to set UDBA_NONE for root */
21637 +       if (d_unhashed(dentry))
21638 +               a->udba = AuOpt_UDBA_NONE;
21639 +       if (a->udba != AuOpt_UDBA_NONE) {
21640 +               AuDebugOn(IS_ROOT(dentry));
21641 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21642 +               if (unlikely(err))
21643 +                       goto out;
21644 +       }
21645 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21646 +       if (unlikely(err < 0))
21647 +               goto out;
21648 +
21649 +       h_path->dentry = a->h_path.dentry;
21650 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21651 +
21652 +out:
21653 +       return err;
21654 +}
21655 +
21656 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21657 +                 struct au_sxattr *arg)
21658 +{
21659 +       int err;
21660 +       struct path h_path;
21661 +       struct super_block *sb;
21662 +       struct au_icpup_args *a;
21663 +       struct inode *h_inode;
21664 +
21665 +       IMustLock(inode);
21666 +
21667 +       err = -ENOMEM;
21668 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21669 +       if (unlikely(!a))
21670 +               goto out;
21671 +
21672 +       sb = dentry->d_sb;
21673 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21674 +       if (unlikely(err))
21675 +               goto out_kfree;
21676 +
21677 +       h_path.dentry = NULL;   /* silence gcc */
21678 +       di_write_lock_child(dentry);
21679 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21680 +       if (unlikely(err))
21681 +               goto out_di;
21682 +
21683 +       inode_unlock(a->h_inode);
21684 +       switch (arg->type) {
21685 +       case AU_XATTR_SET:
21686 +               AuDebugOn(d_is_negative(h_path.dentry));
21687 +               err = vfsub_setxattr(h_path.dentry,
21688 +                                    arg->u.set.name, arg->u.set.value,
21689 +                                    arg->u.set.size, arg->u.set.flags);
21690 +               break;
21691 +       case AU_ACL_SET:
21692 +               err = -EOPNOTSUPP;
21693 +               h_inode = d_inode(h_path.dentry);
21694 +               if (h_inode->i_op->set_acl)
21695 +                       /* this will call posix_acl_update_mode */
21696 +                       err = h_inode->i_op->set_acl(h_inode,
21697 +                                                    arg->u.acl_set.acl,
21698 +                                                    arg->u.acl_set.type);
21699 +               break;
21700 +       }
21701 +       if (!err)
21702 +               au_cpup_attr_timesizes(inode);
21703 +
21704 +       au_unpin(&a->pin);
21705 +       if (unlikely(err))
21706 +               au_update_dbtop(dentry);
21707 +
21708 +out_di:
21709 +       di_write_unlock(dentry);
21710 +       si_read_unlock(sb);
21711 +out_kfree:
21712 +       kfree(a);
21713 +out:
21714 +       AuTraceErr(err);
21715 +       return err;
21716 +}
21717 +#endif
21718 +
21719 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21720 +                            unsigned int nlink)
21721 +{
21722 +       unsigned int n;
21723 +
21724 +       inode->i_mode = st->mode;
21725 +       /* don't i_[ug]id_write() here */
21726 +       inode->i_uid = st->uid;
21727 +       inode->i_gid = st->gid;
21728 +       inode->i_atime = st->atime;
21729 +       inode->i_mtime = st->mtime;
21730 +       inode->i_ctime = st->ctime;
21731 +
21732 +       au_cpup_attr_nlink(inode, /*force*/0);
21733 +       if (S_ISDIR(inode->i_mode)) {
21734 +               n = inode->i_nlink;
21735 +               n -= nlink;
21736 +               n += st->nlink;
21737 +               smp_mb(); /* for i_nlink */
21738 +               /* 0 can happen */
21739 +               set_nlink(inode, n);
21740 +       }
21741 +
21742 +       spin_lock(&inode->i_lock);
21743 +       inode->i_blocks = st->blocks;
21744 +       i_size_write(inode, st->size);
21745 +       spin_unlock(&inode->i_lock);
21746 +}
21747 +
21748 +/*
21749 + * common routine for aufs_getattr() and au_getxattr().
21750 + * returns zero or negative (an error).
21751 + * @dentry will be read-locked in success.
21752 + */
21753 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21754 +                     int locked)
21755 +{
21756 +       int err;
21757 +       unsigned int mnt_flags, sigen;
21758 +       unsigned char udba_none;
21759 +       aufs_bindex_t bindex;
21760 +       struct super_block *sb, *h_sb;
21761 +       struct inode *inode;
21762 +
21763 +       h_path->mnt = NULL;
21764 +       h_path->dentry = NULL;
21765 +
21766 +       err = 0;
21767 +       sb = dentry->d_sb;
21768 +       mnt_flags = au_mntflags(sb);
21769 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21770 +
21771 +       if (unlikely(locked))
21772 +               goto body; /* skip locking dinfo */
21773 +
21774 +       /* support fstat(2) */
21775 +       if (!d_unlinked(dentry) && !udba_none) {
21776 +               sigen = au_sigen(sb);
21777 +               err = au_digen_test(dentry, sigen);
21778 +               if (!err) {
21779 +                       di_read_lock_child(dentry, AuLock_IR);
21780 +                       err = au_dbrange_test(dentry);
21781 +                       if (unlikely(err)) {
21782 +                               di_read_unlock(dentry, AuLock_IR);
21783 +                               goto out;
21784 +                       }
21785 +               } else {
21786 +                       AuDebugOn(IS_ROOT(dentry));
21787 +                       di_write_lock_child(dentry);
21788 +                       err = au_dbrange_test(dentry);
21789 +                       if (!err)
21790 +                               err = au_reval_for_attr(dentry, sigen);
21791 +                       if (!err)
21792 +                               di_downgrade_lock(dentry, AuLock_IR);
21793 +                       else {
21794 +                               di_write_unlock(dentry);
21795 +                               goto out;
21796 +                       }
21797 +               }
21798 +       } else
21799 +               di_read_lock_child(dentry, AuLock_IR);
21800 +
21801 +body:
21802 +       inode = d_inode(dentry);
21803 +       bindex = au_ibtop(inode);
21804 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21805 +       h_sb = h_path->mnt->mnt_sb;
21806 +       if (!force
21807 +           && !au_test_fs_bad_iattr(h_sb)
21808 +           && udba_none)
21809 +               goto out; /* success */
21810 +
21811 +       if (au_dbtop(dentry) == bindex)
21812 +               h_path->dentry = au_h_dptr(dentry, bindex);
21813 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21814 +               h_path->dentry = au_plink_lkup(inode, bindex);
21815 +               if (IS_ERR(h_path->dentry))
21816 +                       /* pretending success */
21817 +                       h_path->dentry = NULL;
21818 +               else
21819 +                       dput(h_path->dentry);
21820 +       }
21821 +
21822 +out:
21823 +       return err;
21824 +}
21825 +
21826 +static int aufs_getattr(const struct path *path, struct kstat *st,
21827 +                       u32 request, unsigned int query)
21828 +{
21829 +       int err;
21830 +       unsigned char positive;
21831 +       struct path h_path;
21832 +       struct dentry *dentry;
21833 +       struct inode *inode;
21834 +       struct super_block *sb;
21835 +
21836 +       dentry = path->dentry;
21837 +       inode = d_inode(dentry);
21838 +       sb = dentry->d_sb;
21839 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21840 +       if (unlikely(err))
21841 +               goto out;
21842 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
21843 +       if (unlikely(err))
21844 +               goto out_si;
21845 +       if (unlikely(!h_path.dentry))
21846 +               /* illegally overlapped or something */
21847 +               goto out_fill; /* pretending success */
21848 +
21849 +       positive = d_is_positive(h_path.dentry);
21850 +       if (positive)
21851 +               /* no vfsub version */
21852 +               err = vfs_getattr(&h_path, st, request, query);
21853 +       if (!err) {
21854 +               if (positive)
21855 +                       au_refresh_iattr(inode, st,
21856 +                                        d_inode(h_path.dentry)->i_nlink);
21857 +               goto out_fill; /* success */
21858 +       }
21859 +       AuTraceErr(err);
21860 +       goto out_di;
21861 +
21862 +out_fill:
21863 +       generic_fillattr(inode, st);
21864 +out_di:
21865 +       di_read_unlock(dentry, AuLock_IR);
21866 +out_si:
21867 +       si_read_unlock(sb);
21868 +out:
21869 +       AuTraceErr(err);
21870 +       return err;
21871 +}
21872 +
21873 +/* ---------------------------------------------------------------------- */
21874 +
21875 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21876 +                                struct delayed_call *done)
21877 +{
21878 +       const char *ret;
21879 +       struct dentry *h_dentry;
21880 +       struct inode *h_inode;
21881 +       int err;
21882 +       aufs_bindex_t bindex;
21883 +
21884 +       ret = NULL; /* suppress a warning */
21885 +       err = -ECHILD;
21886 +       if (!dentry)
21887 +               goto out;
21888 +
21889 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21890 +       if (unlikely(err))
21891 +               goto out;
21892 +
21893 +       err = au_d_hashed_positive(dentry);
21894 +       if (unlikely(err))
21895 +               goto out_unlock;
21896 +
21897 +       err = -EINVAL;
21898 +       inode = d_inode(dentry);
21899 +       bindex = au_ibtop(inode);
21900 +       h_inode = au_h_iptr(inode, bindex);
21901 +       if (unlikely(!h_inode->i_op->get_link))
21902 +               goto out_unlock;
21903 +
21904 +       err = -EBUSY;
21905 +       h_dentry = NULL;
21906 +       if (au_dbtop(dentry) <= bindex) {
21907 +               h_dentry = au_h_dptr(dentry, bindex);
21908 +               if (h_dentry)
21909 +                       dget(h_dentry);
21910 +       }
21911 +       if (!h_dentry) {
21912 +               h_dentry = d_find_any_alias(h_inode);
21913 +               if (IS_ERR(h_dentry)) {
21914 +                       err = PTR_ERR(h_dentry);
21915 +                       goto out_unlock;
21916 +               }
21917 +       }
21918 +       if (unlikely(!h_dentry))
21919 +               goto out_unlock;
21920 +
21921 +       err = 0;
21922 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21923 +       AuDbgDentry(h_dentry);
21924 +       ret = vfs_get_link(h_dentry, done);
21925 +       dput(h_dentry);
21926 +       if (IS_ERR(ret))
21927 +               err = PTR_ERR(ret);
21928 +
21929 +out_unlock:
21930 +       aufs_read_unlock(dentry, AuLock_IR);
21931 +out:
21932 +       if (unlikely(err))
21933 +               ret = ERR_PTR(err);
21934 +       AuTraceErrPtr(ret);
21935 +       return ret;
21936 +}
21937 +
21938 +/* ---------------------------------------------------------------------- */
21939 +
21940 +static int au_is_special(struct inode *inode)
21941 +{
21942 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21943 +}
21944 +
21945 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts, int flags)
21946 +{
21947 +       int err;
21948 +       aufs_bindex_t bindex;
21949 +       struct super_block *sb;
21950 +       struct inode *h_inode;
21951 +       struct vfsmount *h_mnt;
21952 +
21953 +       sb = inode->i_sb;
21954 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21955 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21956 +
21957 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21958 +       lockdep_off();
21959 +       si_read_lock(sb, AuLock_FLUSH);
21960 +       ii_write_lock_child(inode);
21961 +
21962 +       err = 0;
21963 +       bindex = au_ibtop(inode);
21964 +       h_inode = au_h_iptr(inode, bindex);
21965 +       if (!au_test_ro(sb, bindex, inode)) {
21966 +               h_mnt = au_sbr_mnt(sb, bindex);
21967 +               err = vfsub_mnt_want_write(h_mnt);
21968 +               if (!err) {
21969 +                       err = vfsub_update_time(h_inode, ts, flags);
21970 +                       vfsub_mnt_drop_write(h_mnt);
21971 +               }
21972 +       } else if (au_is_special(h_inode)) {
21973 +               /*
21974 +                * Never copy-up here.
21975 +                * These special files may already be opened and used for
21976 +                * communicating. If we copied it up, then the communication
21977 +                * would be corrupted.
21978 +                */
21979 +               AuWarn1("timestamps for i%lu are ignored "
21980 +                       "since it is on readonly branch (hi%lu).\n",
21981 +                       inode->i_ino, h_inode->i_ino);
21982 +       } else if (flags & ~S_ATIME) {
21983 +               err = -EIO;
21984 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21985 +               AuDebugOn(1);
21986 +       }
21987 +
21988 +       if (!err)
21989 +               au_cpup_attr_timesizes(inode);
21990 +       ii_write_unlock(inode);
21991 +       si_read_unlock(sb);
21992 +       lockdep_on();
21993 +
21994 +       if (!err && (flags & S_VERSION))
21995 +               inode_inc_iversion(inode);
21996 +
21997 +       return err;
21998 +}
21999 +
22000 +/* ---------------------------------------------------------------------- */
22001 +
22002 +/* no getattr version will be set by module.c:aufs_init() */
22003 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22004 +       aufs_iop[] = {
22005 +       [AuIop_SYMLINK] = {
22006 +               .permission     = aufs_permission,
22007 +#ifdef CONFIG_FS_POSIX_ACL
22008 +               .get_acl        = aufs_get_acl,
22009 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
22010 +#endif
22011 +
22012 +               .setattr        = aufs_setattr,
22013 +               .getattr        = aufs_getattr,
22014 +
22015 +#ifdef CONFIG_AUFS_XATTR
22016 +               .listxattr      = aufs_listxattr,
22017 +#endif
22018 +
22019 +               .get_link       = aufs_get_link,
22020 +
22021 +               /* .update_time = aufs_update_time */
22022 +       },
22023 +       [AuIop_DIR] = {
22024 +               .create         = aufs_create,
22025 +               .lookup         = aufs_lookup,
22026 +               .link           = aufs_link,
22027 +               .unlink         = aufs_unlink,
22028 +               .symlink        = aufs_symlink,
22029 +               .mkdir          = aufs_mkdir,
22030 +               .rmdir          = aufs_rmdir,
22031 +               .mknod          = aufs_mknod,
22032 +               .rename         = aufs_rename,
22033 +
22034 +               .permission     = aufs_permission,
22035 +#ifdef CONFIG_FS_POSIX_ACL
22036 +               .get_acl        = aufs_get_acl,
22037 +               .set_acl        = aufs_set_acl,
22038 +#endif
22039 +
22040 +               .setattr        = aufs_setattr,
22041 +               .getattr        = aufs_getattr,
22042 +
22043 +#ifdef CONFIG_AUFS_XATTR
22044 +               .listxattr      = aufs_listxattr,
22045 +#endif
22046 +
22047 +               .update_time    = aufs_update_time,
22048 +               .atomic_open    = aufs_atomic_open,
22049 +               .tmpfile        = aufs_tmpfile
22050 +       },
22051 +       [AuIop_OTHER] = {
22052 +               .permission     = aufs_permission,
22053 +#ifdef CONFIG_FS_POSIX_ACL
22054 +               .get_acl        = aufs_get_acl,
22055 +               .set_acl        = aufs_set_acl,
22056 +#endif
22057 +
22058 +               .setattr        = aufs_setattr,
22059 +               .getattr        = aufs_getattr,
22060 +
22061 +#ifdef CONFIG_AUFS_XATTR
22062 +               .listxattr      = aufs_listxattr,
22063 +#endif
22064 +
22065 +               .update_time    = aufs_update_time
22066 +       }
22067 +};
22068 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22069 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22070 +++ linux/fs/aufs/i_op_del.c    2018-08-12 23:43:05.456791299 +0200
22071 @@ -0,0 +1,512 @@
22072 +// SPDX-License-Identifier: GPL-2.0
22073 +/*
22074 + * Copyright (C) 2005-2018 Junjiro R. Okajima
22075 + *
22076 + * This program, aufs is free software; you can redistribute it and/or modify
22077 + * it under the terms of the GNU General Public License as published by
22078 + * the Free Software Foundation; either version 2 of the License, or
22079 + * (at your option) any later version.
22080 + *
22081 + * This program is distributed in the hope that it will be useful,
22082 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22083 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22084 + * GNU General Public License for more details.
22085 + *
22086 + * You should have received a copy of the GNU General Public License
22087 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22088 + */
22089 +
22090 +/*
22091 + * inode operations (del entry)
22092 + */
22093 +
22094 +#include "aufs.h"
22095 +
22096 +/*
22097 + * decide if a new whiteout for @dentry is necessary or not.
22098 + * when it is necessary, prepare the parent dir for the upper branch whose
22099 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22100 + * be done by caller.
22101 + * return value:
22102 + * 0: wh is unnecessary
22103 + * plus: wh is necessary
22104 + * minus: error
22105 + */
22106 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22107 +{
22108 +       int need_wh, err;
22109 +       aufs_bindex_t btop;
22110 +       struct super_block *sb;
22111 +
22112 +       sb = dentry->d_sb;
22113 +       btop = au_dbtop(dentry);
22114 +       if (*bcpup < 0) {
22115 +               *bcpup = btop;
22116 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22117 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22118 +                       *bcpup = err;
22119 +                       if (unlikely(err < 0))
22120 +                               goto out;
22121 +               }
22122 +       } else
22123 +               AuDebugOn(btop < *bcpup
22124 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22125 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22126 +
22127 +       if (*bcpup != btop) {
22128 +               err = au_cpup_dirs(dentry, *bcpup);
22129 +               if (unlikely(err))
22130 +                       goto out;
22131 +               need_wh = 1;
22132 +       } else {
22133 +               struct au_dinfo *dinfo, *tmp;
22134 +
22135 +               need_wh = -ENOMEM;
22136 +               dinfo = au_di(dentry);
22137 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22138 +               if (tmp) {
22139 +                       au_di_cp(tmp, dinfo);
22140 +                       au_di_swap(tmp, dinfo);
22141 +                       /* returns the number of positive dentries */
22142 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22143 +                                                /* AuLkup_IGNORE_PERM */ 0);
22144 +                       au_di_swap(tmp, dinfo);
22145 +                       au_rw_write_unlock(&tmp->di_rwsem);
22146 +                       au_di_free(tmp);
22147 +               }
22148 +       }
22149 +       AuDbg("need_wh %d\n", need_wh);
22150 +       err = need_wh;
22151 +
22152 +out:
22153 +       return err;
22154 +}
22155 +
22156 +/*
22157 + * simple tests for the del-entry operations.
22158 + * following the checks in vfs, plus the parent-child relationship.
22159 + */
22160 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22161 +              struct dentry *h_parent, int isdir)
22162 +{
22163 +       int err;
22164 +       umode_t h_mode;
22165 +       struct dentry *h_dentry, *h_latest;
22166 +       struct inode *h_inode;
22167 +
22168 +       h_dentry = au_h_dptr(dentry, bindex);
22169 +       if (d_really_is_positive(dentry)) {
22170 +               err = -ENOENT;
22171 +               if (unlikely(d_is_negative(h_dentry)))
22172 +                       goto out;
22173 +               h_inode = d_inode(h_dentry);
22174 +               if (unlikely(!h_inode->i_nlink))
22175 +                       goto out;
22176 +
22177 +               h_mode = h_inode->i_mode;
22178 +               if (!isdir) {
22179 +                       err = -EISDIR;
22180 +                       if (unlikely(S_ISDIR(h_mode)))
22181 +                               goto out;
22182 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22183 +                       err = -ENOTDIR;
22184 +                       goto out;
22185 +               }
22186 +       } else {
22187 +               /* rename(2) case */
22188 +               err = -EIO;
22189 +               if (unlikely(d_is_positive(h_dentry)))
22190 +                       goto out;
22191 +       }
22192 +
22193 +       err = -ENOENT;
22194 +       /* expected parent dir is locked */
22195 +       if (unlikely(h_parent != h_dentry->d_parent))
22196 +               goto out;
22197 +       err = 0;
22198 +
22199 +       /*
22200 +        * rmdir a dir may break the consistency on some filesystem.
22201 +        * let's try heavy test.
22202 +        */
22203 +       err = -EACCES;
22204 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22205 +                    && au_test_h_perm(d_inode(h_parent),
22206 +                                      MAY_EXEC | MAY_WRITE)))
22207 +               goto out;
22208 +
22209 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22210 +       err = -EIO;
22211 +       if (IS_ERR(h_latest))
22212 +               goto out;
22213 +       if (h_latest == h_dentry)
22214 +               err = 0;
22215 +       dput(h_latest);
22216 +
22217 +out:
22218 +       return err;
22219 +}
22220 +
22221 +/*
22222 + * decide the branch where we operate for @dentry. the branch index will be set
22223 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
22224 + * dir for reverting.
22225 + * when a new whiteout is necessary, create it.
22226 + */
22227 +static struct dentry*
22228 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22229 +                   struct au_dtime *dt, struct au_pin *pin)
22230 +{
22231 +       struct dentry *wh_dentry;
22232 +       struct super_block *sb;
22233 +       struct path h_path;
22234 +       int err, need_wh;
22235 +       unsigned int udba;
22236 +       aufs_bindex_t bcpup;
22237 +
22238 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22239 +       wh_dentry = ERR_PTR(need_wh);
22240 +       if (unlikely(need_wh < 0))
22241 +               goto out;
22242 +
22243 +       sb = dentry->d_sb;
22244 +       udba = au_opt_udba(sb);
22245 +       bcpup = *rbcpup;
22246 +       err = au_pin(pin, dentry, bcpup, udba,
22247 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22248 +       wh_dentry = ERR_PTR(err);
22249 +       if (unlikely(err))
22250 +               goto out;
22251 +
22252 +       h_path.dentry = au_pinned_h_parent(pin);
22253 +       if (udba != AuOpt_UDBA_NONE
22254 +           && au_dbtop(dentry) == bcpup) {
22255 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22256 +               wh_dentry = ERR_PTR(err);
22257 +               if (unlikely(err))
22258 +                       goto out_unpin;
22259 +       }
22260 +
22261 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22262 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22263 +       wh_dentry = NULL;
22264 +       if (!need_wh)
22265 +               goto out; /* success, no need to create whiteout */
22266 +
22267 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22268 +       if (IS_ERR(wh_dentry))
22269 +               goto out_unpin;
22270 +
22271 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22272 +       goto out; /* success */
22273 +
22274 +out_unpin:
22275 +       au_unpin(pin);
22276 +out:
22277 +       return wh_dentry;
22278 +}
22279 +
22280 +/*
22281 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22282 + * in order to be revertible and save time for removing many child whiteouts
22283 + * under the dir.
22284 + * returns 1 when there are too many child whiteout and caller should remove
22285 + * them asynchronously. returns 0 when the number of children is enough small to
22286 + * remove now or the branch fs is a remote fs.
22287 + * otherwise return an error.
22288 + */
22289 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22290 +                          struct au_nhash *whlist, struct inode *dir)
22291 +{
22292 +       int rmdir_later, err, dirwh;
22293 +       struct dentry *h_dentry;
22294 +       struct super_block *sb;
22295 +       struct inode *inode;
22296 +
22297 +       sb = dentry->d_sb;
22298 +       SiMustAnyLock(sb);
22299 +       h_dentry = au_h_dptr(dentry, bindex);
22300 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22301 +       if (unlikely(err))
22302 +               goto out;
22303 +
22304 +       /* stop monitoring */
22305 +       inode = d_inode(dentry);
22306 +       au_hn_free(au_hi(inode, bindex));
22307 +
22308 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22309 +               dirwh = au_sbi(sb)->si_dirwh;
22310 +               rmdir_later = (dirwh <= 1);
22311 +               if (!rmdir_later)
22312 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22313 +                                                             dirwh);
22314 +               if (rmdir_later)
22315 +                       return rmdir_later;
22316 +       }
22317 +
22318 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22319 +       if (unlikely(err)) {
22320 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22321 +                       h_dentry, bindex, err);
22322 +               err = 0;
22323 +       }
22324 +
22325 +out:
22326 +       AuTraceErr(err);
22327 +       return err;
22328 +}
22329 +
22330 +/*
22331 + * final procedure for deleting a entry.
22332 + * maintain dentry and iattr.
22333 + */
22334 +static void epilog(struct inode *dir, struct dentry *dentry,
22335 +                  aufs_bindex_t bindex)
22336 +{
22337 +       struct inode *inode;
22338 +
22339 +       inode = d_inode(dentry);
22340 +       d_drop(dentry);
22341 +       inode->i_ctime = dir->i_ctime;
22342 +
22343 +       au_dir_ts(dir, bindex);
22344 +       inode_inc_iversion(dir);
22345 +}
22346 +
22347 +/*
22348 + * when an error happened, remove the created whiteout and revert everything.
22349 + */
22350 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22351 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22352 +                    struct dentry *dentry, struct au_dtime *dt)
22353 +{
22354 +       int rerr;
22355 +       struct path h_path = {
22356 +               .dentry = wh_dentry,
22357 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22358 +       };
22359 +
22360 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22361 +       if (!rerr) {
22362 +               au_set_dbwh(dentry, bwh);
22363 +               au_dtime_revert(dt);
22364 +               return 0;
22365 +       }
22366 +
22367 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22368 +       return -EIO;
22369 +}
22370 +
22371 +/* ---------------------------------------------------------------------- */
22372 +
22373 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22374 +{
22375 +       int err;
22376 +       aufs_bindex_t bwh, bindex, btop;
22377 +       struct inode *inode, *h_dir, *delegated;
22378 +       struct dentry *parent, *wh_dentry;
22379 +       /* to reuduce stack size */
22380 +       struct {
22381 +               struct au_dtime dt;
22382 +               struct au_pin pin;
22383 +               struct path h_path;
22384 +       } *a;
22385 +
22386 +       IMustLock(dir);
22387 +
22388 +       err = -ENOMEM;
22389 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22390 +       if (unlikely(!a))
22391 +               goto out;
22392 +
22393 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22394 +       if (unlikely(err))
22395 +               goto out_free;
22396 +       err = au_d_hashed_positive(dentry);
22397 +       if (unlikely(err))
22398 +               goto out_unlock;
22399 +       inode = d_inode(dentry);
22400 +       IMustLock(inode);
22401 +       err = -EISDIR;
22402 +       if (unlikely(d_is_dir(dentry)))
22403 +               goto out_unlock; /* possible? */
22404 +
22405 +       btop = au_dbtop(dentry);
22406 +       bwh = au_dbwh(dentry);
22407 +       bindex = -1;
22408 +       parent = dentry->d_parent; /* dir inode is locked */
22409 +       di_write_lock_parent(parent);
22410 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22411 +                                       &a->pin);
22412 +       err = PTR_ERR(wh_dentry);
22413 +       if (IS_ERR(wh_dentry))
22414 +               goto out_parent;
22415 +
22416 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22417 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22418 +       dget(a->h_path.dentry);
22419 +       if (bindex == btop) {
22420 +               h_dir = au_pinned_h_dir(&a->pin);
22421 +               delegated = NULL;
22422 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22423 +               if (unlikely(err == -EWOULDBLOCK)) {
22424 +                       pr_warn("cannot retry for NFSv4 delegation"
22425 +                               " for an internal unlink\n");
22426 +                       iput(delegated);
22427 +               }
22428 +       } else {
22429 +               /* dir inode is locked */
22430 +               h_dir = d_inode(wh_dentry->d_parent);
22431 +               IMustLock(h_dir);
22432 +               err = 0;
22433 +       }
22434 +
22435 +       if (!err) {
22436 +               vfsub_drop_nlink(inode);
22437 +               epilog(dir, dentry, bindex);
22438 +
22439 +               /* update target timestamps */
22440 +               if (bindex == btop) {
22441 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22442 +                       /*ignore*/
22443 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22444 +               } else
22445 +                       /* todo: this timestamp may be reverted later */
22446 +                       inode->i_ctime = h_dir->i_ctime;
22447 +               goto out_unpin; /* success */
22448 +       }
22449 +
22450 +       /* revert */
22451 +       if (wh_dentry) {
22452 +               int rerr;
22453 +
22454 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22455 +                                &a->dt);
22456 +               if (rerr)
22457 +                       err = rerr;
22458 +       }
22459 +
22460 +out_unpin:
22461 +       au_unpin(&a->pin);
22462 +       dput(wh_dentry);
22463 +       dput(a->h_path.dentry);
22464 +out_parent:
22465 +       di_write_unlock(parent);
22466 +out_unlock:
22467 +       aufs_read_unlock(dentry, AuLock_DW);
22468 +out_free:
22469 +       kfree(a);
22470 +out:
22471 +       return err;
22472 +}
22473 +
22474 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22475 +{
22476 +       int err, rmdir_later;
22477 +       aufs_bindex_t bwh, bindex, btop;
22478 +       struct inode *inode;
22479 +       struct dentry *parent, *wh_dentry, *h_dentry;
22480 +       struct au_whtmp_rmdir *args;
22481 +       /* to reuduce stack size */
22482 +       struct {
22483 +               struct au_dtime dt;
22484 +               struct au_pin pin;
22485 +       } *a;
22486 +
22487 +       IMustLock(dir);
22488 +
22489 +       err = -ENOMEM;
22490 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22491 +       if (unlikely(!a))
22492 +               goto out;
22493 +
22494 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22495 +       if (unlikely(err))
22496 +               goto out_free;
22497 +       err = au_alive_dir(dentry);
22498 +       if (unlikely(err))
22499 +               goto out_unlock;
22500 +       inode = d_inode(dentry);
22501 +       IMustLock(inode);
22502 +       err = -ENOTDIR;
22503 +       if (unlikely(!d_is_dir(dentry)))
22504 +               goto out_unlock; /* possible? */
22505 +
22506 +       err = -ENOMEM;
22507 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22508 +       if (unlikely(!args))
22509 +               goto out_unlock;
22510 +
22511 +       parent = dentry->d_parent; /* dir inode is locked */
22512 +       di_write_lock_parent(parent);
22513 +       err = au_test_empty(dentry, &args->whlist);
22514 +       if (unlikely(err))
22515 +               goto out_parent;
22516 +
22517 +       btop = au_dbtop(dentry);
22518 +       bwh = au_dbwh(dentry);
22519 +       bindex = -1;
22520 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22521 +                                       &a->pin);
22522 +       err = PTR_ERR(wh_dentry);
22523 +       if (IS_ERR(wh_dentry))
22524 +               goto out_parent;
22525 +
22526 +       h_dentry = au_h_dptr(dentry, btop);
22527 +       dget(h_dentry);
22528 +       rmdir_later = 0;
22529 +       if (bindex == btop) {
22530 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22531 +               if (err > 0) {
22532 +                       rmdir_later = err;
22533 +                       err = 0;
22534 +               }
22535 +       } else {
22536 +               /* stop monitoring */
22537 +               au_hn_free(au_hi(inode, btop));
22538 +
22539 +               /* dir inode is locked */
22540 +               IMustLock(d_inode(wh_dentry->d_parent));
22541 +               err = 0;
22542 +       }
22543 +
22544 +       if (!err) {
22545 +               vfsub_dead_dir(inode);
22546 +               au_set_dbdiropq(dentry, -1);
22547 +               epilog(dir, dentry, bindex);
22548 +
22549 +               if (rmdir_later) {
22550 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22551 +                       args = NULL;
22552 +               }
22553 +
22554 +               goto out_unpin; /* success */
22555 +       }
22556 +
22557 +       /* revert */
22558 +       AuLabel(revert);
22559 +       if (wh_dentry) {
22560 +               int rerr;
22561 +
22562 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22563 +                                &a->dt);
22564 +               if (rerr)
22565 +                       err = rerr;
22566 +       }
22567 +
22568 +out_unpin:
22569 +       au_unpin(&a->pin);
22570 +       dput(wh_dentry);
22571 +       dput(h_dentry);
22572 +out_parent:
22573 +       di_write_unlock(parent);
22574 +       if (args)
22575 +               au_whtmp_rmdir_free(args);
22576 +out_unlock:
22577 +       aufs_read_unlock(dentry, AuLock_DW);
22578 +out_free:
22579 +       kfree(a);
22580 +out:
22581 +       AuTraceErr(err);
22582 +       return err;
22583 +}
22584 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22585 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22586 +++ linux/fs/aufs/i_op_ren.c    2018-08-12 23:43:05.460124736 +0200
22587 @@ -0,0 +1,1247 @@
22588 +// SPDX-License-Identifier: GPL-2.0
22589 +/*
22590 + * Copyright (C) 2005-2018 Junjiro R. Okajima
22591 + *
22592 + * This program, aufs is free software; you can redistribute it and/or modify
22593 + * it under the terms of the GNU General Public License as published by
22594 + * the Free Software Foundation; either version 2 of the License, or
22595 + * (at your option) any later version.
22596 + *
22597 + * This program is distributed in the hope that it will be useful,
22598 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22599 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22600 + * GNU General Public License for more details.
22601 + *
22602 + * You should have received a copy of the GNU General Public License
22603 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22604 + */
22605 +
22606 +/*
22607 + * inode operation (rename entry)
22608 + * todo: this is crazy monster
22609 + */
22610 +
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
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 exitsts
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 +       a->exchange = _flags & RENAME_EXCHANGE;
23568 +       a->src_dir = _src_dir;
23569 +       a->src_dentry = _src_dentry;
23570 +       a->src_inode = NULL;
23571 +       if (d_really_is_positive(a->src_dentry))
23572 +               a->src_inode = d_inode(a->src_dentry);
23573 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23574 +       a->dst_dir = _dst_dir;
23575 +       a->dst_dentry = _dst_dentry;
23576 +       a->dst_inode = NULL;
23577 +       if (d_really_is_positive(a->dst_dentry))
23578 +               a->dst_inode = d_inode(a->dst_dentry);
23579 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23580 +       if (a->dst_inode) {
23581 +               /*
23582 +                * if EXCHANGE && src is non-dir && dst is dir,
23583 +                * dst is not locked.
23584 +                */
23585 +               /* IMustLock(a->dst_inode); */
23586 +               au_igrab(a->dst_inode);
23587 +       }
23588 +
23589 +       err = -ENOTDIR;
23590 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23591 +       if (d_is_dir(a->src_dentry)) {
23592 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23593 +               if (unlikely(!a->exchange
23594 +                            && d_really_is_positive(a->dst_dentry)
23595 +                            && !d_is_dir(a->dst_dentry)))
23596 +                       goto out_free;
23597 +               lock_flags |= AuLock_DIRS;
23598 +       }
23599 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23600 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23601 +               if (unlikely(!a->exchange
23602 +                            && d_really_is_positive(a->src_dentry)
23603 +                            && !d_is_dir(a->src_dentry)))
23604 +                       goto out_free;
23605 +               lock_flags |= AuLock_DIRS;
23606 +       }
23607 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23608 +                                       lock_flags);
23609 +       if (unlikely(err))
23610 +               goto out_free;
23611 +
23612 +       err = au_d_hashed_positive(a->src_dentry);
23613 +       if (unlikely(err))
23614 +               goto out_unlock;
23615 +       err = -ENOENT;
23616 +       if (a->dst_inode) {
23617 +               /*
23618 +                * If it is a dir, VFS unhash it before this
23619 +                * function. It means we cannot rely upon d_unhashed().
23620 +                */
23621 +               if (unlikely(!a->dst_inode->i_nlink))
23622 +                       goto out_unlock;
23623 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23624 +                       err = au_d_hashed_positive(a->dst_dentry);
23625 +                       if (unlikely(err && !a->exchange))
23626 +                               goto out_unlock;
23627 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23628 +                       goto out_unlock;
23629 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23630 +               goto out_unlock;
23631 +
23632 +       /*
23633 +        * is it possible?
23634 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23635 +        * there may exist a problem somewhere else.
23636 +        */
23637 +       err = -EINVAL;
23638 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23639 +               goto out_unlock;
23640 +
23641 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23642 +       di_write_lock_parent(a->dst_parent);
23643 +
23644 +       /* which branch we process */
23645 +       err = au_ren_wbr(a);
23646 +       if (unlikely(err < 0))
23647 +               goto out_parent;
23648 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23649 +       a->h_path.mnt = au_br_mnt(a->br);
23650 +
23651 +       /* are they available to be renamed */
23652 +       err = au_ren_may_dir(a);
23653 +       if (unlikely(err))
23654 +               goto out_children;
23655 +
23656 +       /* prepare the writable parent dir on the same branch */
23657 +       if (a->dst_btop == a->btgt) {
23658 +               au_fset_ren(a->auren_flags, WHDST);
23659 +       } else {
23660 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23661 +               if (unlikely(err))
23662 +                       goto out_children;
23663 +       }
23664 +
23665 +       err = 0;
23666 +       if (!a->exchange) {
23667 +               if (a->src_dir != a->dst_dir) {
23668 +                       /*
23669 +                        * this temporary unlock is safe,
23670 +                        * because both dir->i_mutex are locked.
23671 +                        */
23672 +                       di_write_unlock(a->dst_parent);
23673 +                       di_write_lock_parent(a->src_parent);
23674 +                       err = au_wr_dir_need_wh(a->src_dentry,
23675 +                                               au_ftest_ren(a->auren_flags,
23676 +                                                            ISDIR_SRC),
23677 +                                               &a->btgt);
23678 +                       di_write_unlock(a->src_parent);
23679 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23680 +                                             /*isdir*/1);
23681 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23682 +               } else
23683 +                       err = au_wr_dir_need_wh(a->src_dentry,
23684 +                                               au_ftest_ren(a->auren_flags,
23685 +                                                            ISDIR_SRC),
23686 +                                               &a->btgt);
23687 +       }
23688 +       if (unlikely(err < 0))
23689 +               goto out_children;
23690 +       if (err)
23691 +               au_fset_ren(a->auren_flags, WHSRC);
23692 +
23693 +       /* cpup src */
23694 +       if (a->src_btop != a->btgt) {
23695 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23696 +                            au_opt_udba(a->src_dentry->d_sb),
23697 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23698 +               if (!err) {
23699 +                       struct au_cp_generic cpg = {
23700 +                               .dentry = a->src_dentry,
23701 +                               .bdst   = a->btgt,
23702 +                               .bsrc   = a->src_btop,
23703 +                               .len    = -1,
23704 +                               .pin    = &pin,
23705 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23706 +                       };
23707 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23708 +                       err = au_sio_cpup_simple(&cpg);
23709 +                       au_unpin(&pin);
23710 +               }
23711 +               if (unlikely(err))
23712 +                       goto out_children;
23713 +               a->src_btop = a->btgt;
23714 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23715 +               if (!a->exchange)
23716 +                       au_fset_ren(a->auren_flags, WHSRC);
23717 +       }
23718 +
23719 +       /* cpup dst */
23720 +       if (a->exchange && a->dst_inode
23721 +           && a->dst_btop != a->btgt) {
23722 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23723 +                            au_opt_udba(a->dst_dentry->d_sb),
23724 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23725 +               if (!err) {
23726 +                       struct au_cp_generic cpg = {
23727 +                               .dentry = a->dst_dentry,
23728 +                               .bdst   = a->btgt,
23729 +                               .bsrc   = a->dst_btop,
23730 +                               .len    = -1,
23731 +                               .pin    = &pin,
23732 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23733 +                       };
23734 +                       err = au_sio_cpup_simple(&cpg);
23735 +                       au_unpin(&pin);
23736 +               }
23737 +               if (unlikely(err))
23738 +                       goto out_children;
23739 +               a->dst_btop = a->btgt;
23740 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23741 +       }
23742 +
23743 +       /* lock them all */
23744 +       err = au_ren_lock(a);
23745 +       if (unlikely(err))
23746 +               /* leave the copied-up one */
23747 +               goto out_children;
23748 +
23749 +       if (!a->exchange) {
23750 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23751 +                       err = au_may_ren(a);
23752 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23753 +                       err = -ENAMETOOLONG;
23754 +               if (unlikely(err))
23755 +                       goto out_hdir;
23756 +       }
23757 +
23758 +       /* store timestamps to be revertible */
23759 +       au_ren_dt(a);
23760 +
23761 +       /* store dirren info */
23762 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23763 +               err = au_dr_rename(a->src_dentry, a->btgt,
23764 +                                  &a->dst_dentry->d_name, &rev);
23765 +               AuTraceErr(err);
23766 +               if (unlikely(err))
23767 +                       goto out_dt;
23768 +       }
23769 +
23770 +       /* here we go */
23771 +       err = do_rename(a);
23772 +       if (unlikely(err))
23773 +               goto out_dirren;
23774 +
23775 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23776 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23777 +
23778 +       /* update dir attributes */
23779 +       au_ren_refresh_dir(a);
23780 +
23781 +       /* dput/iput all lower dentries */
23782 +       au_ren_refresh(a);
23783 +
23784 +       goto out_hdir; /* success */
23785 +
23786 +out_dirren:
23787 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23788 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23789 +out_dt:
23790 +       au_ren_rev_dt(err, a);
23791 +out_hdir:
23792 +       au_ren_unlock(a);
23793 +out_children:
23794 +       au_nhash_wh_free(&a->whlist);
23795 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23796 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23797 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23798 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23799 +       }
23800 +out_parent:
23801 +       if (!err) {
23802 +               if (d_unhashed(a->src_dentry))
23803 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23804 +               if (d_unhashed(a->dst_dentry))
23805 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23806 +               if (!a->exchange)
23807 +                       d_move(a->src_dentry, a->dst_dentry);
23808 +               else {
23809 +                       d_exchange(a->src_dentry, a->dst_dentry);
23810 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23811 +                               d_drop(a->dst_dentry);
23812 +               }
23813 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23814 +                       d_drop(a->src_dentry);
23815 +       } else {
23816 +               au_update_dbtop(a->dst_dentry);
23817 +               if (!a->dst_inode)
23818 +                       d_drop(a->dst_dentry);
23819 +       }
23820 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23821 +               di_write_unlock(a->dst_parent);
23822 +       else
23823 +               di_write_unlock2(a->src_parent, a->dst_parent);
23824 +out_unlock:
23825 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23826 +out_free:
23827 +       iput(a->dst_inode);
23828 +       if (a->thargs)
23829 +               au_whtmp_rmdir_free(a->thargs);
23830 +       kfree(a);
23831 +out:
23832 +       AuTraceErr(err);
23833 +       return err;
23834 +}
23835 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23836 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23837 +++ linux/fs/aufs/Kconfig       2018-06-04 09:08:09.181412645 +0200
23838 @@ -0,0 +1,199 @@
23839 +# SPDX-License-Identifier: GPL-2.0
23840 +config AUFS_FS
23841 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23842 +       help
23843 +       Aufs is a stackable unification filesystem such as Unionfs,
23844 +       which unifies several directories and provides a merged single
23845 +       directory.
23846 +       In the early days, aufs was entirely re-designed and
23847 +       re-implemented Unionfs Version 1.x series. Introducing many
23848 +       original ideas, approaches and improvements, it becomes totally
23849 +       different from Unionfs while keeping the basic features.
23850 +
23851 +if AUFS_FS
23852 +choice
23853 +       prompt "Maximum number of branches"
23854 +       default AUFS_BRANCH_MAX_127
23855 +       help
23856 +       Specifies the maximum number of branches (or member directories)
23857 +       in a single aufs. The larger value consumes more system
23858 +       resources and has a minor impact to performance.
23859 +config AUFS_BRANCH_MAX_127
23860 +       bool "127"
23861 +       help
23862 +       Specifies the maximum number of branches (or member directories)
23863 +       in a single aufs. The larger value consumes more system
23864 +       resources and has a minor impact to performance.
23865 +config AUFS_BRANCH_MAX_511
23866 +       bool "511"
23867 +       help
23868 +       Specifies the maximum number of branches (or member directories)
23869 +       in a single aufs. The larger value consumes more system
23870 +       resources and has a minor impact to performance.
23871 +config AUFS_BRANCH_MAX_1023
23872 +       bool "1023"
23873 +       help
23874 +       Specifies the maximum number of branches (or member directories)
23875 +       in a single aufs. The larger value consumes more system
23876 +       resources and has a minor impact to performance.
23877 +config AUFS_BRANCH_MAX_32767
23878 +       bool "32767"
23879 +       help
23880 +       Specifies the maximum number of branches (or member directories)
23881 +       in a single aufs. The larger value consumes more system
23882 +       resources and has a minor impact to performance.
23883 +endchoice
23884 +
23885 +config AUFS_SBILIST
23886 +       bool
23887 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23888 +       default y
23889 +       help
23890 +       Automatic configuration for internal use.
23891 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23892 +
23893 +config AUFS_HNOTIFY
23894 +       bool "Detect direct branch access (bypassing aufs)"
23895 +       help
23896 +       If you want to modify files on branches directly, eg. bypassing aufs,
23897 +       and want aufs to detect the changes of them fully, then enable this
23898 +       option and use 'udba=notify' mount option.
23899 +       Currently there is only one available configuration, "fsnotify".
23900 +       It will have a negative impact to the performance.
23901 +       See detail in aufs.5.
23902 +
23903 +choice
23904 +       prompt "method" if AUFS_HNOTIFY
23905 +       default AUFS_HFSNOTIFY
23906 +config AUFS_HFSNOTIFY
23907 +       bool "fsnotify"
23908 +       select FSNOTIFY
23909 +endchoice
23910 +
23911 +config AUFS_EXPORT
23912 +       bool "NFS-exportable aufs"
23913 +       depends on EXPORTFS
23914 +       help
23915 +       If you want to export your mounted aufs via NFS, then enable this
23916 +       option. There are several requirements for this configuration.
23917 +       See detail in aufs.5.
23918 +
23919 +config AUFS_INO_T_64
23920 +       bool
23921 +       depends on AUFS_EXPORT
23922 +       depends on 64BIT && !(ALPHA || S390)
23923 +       default y
23924 +       help
23925 +       Automatic configuration for internal use.
23926 +       /* typedef unsigned long/int __kernel_ino_t */
23927 +       /* alpha and s390x are int */
23928 +
23929 +config AUFS_XATTR
23930 +       bool "support for XATTR/EA (including Security Labels)"
23931 +       help
23932 +       If your branch fs supports XATTR/EA and you want to make them
23933 +       available in aufs too, then enable this opsion and specify the
23934 +       branch attributes for EA.
23935 +       See detail in aufs.5.
23936 +
23937 +config AUFS_FHSM
23938 +       bool "File-based Hierarchical Storage Management"
23939 +       help
23940 +       Hierarchical Storage Management (or HSM) is a well-known feature
23941 +       in the storage world. Aufs provides this feature as file-based.
23942 +       with multiple branches.
23943 +       These multiple branches are prioritized, ie. the topmost one
23944 +       should be the fastest drive and be used heavily.
23945 +
23946 +config AUFS_RDU
23947 +       bool "Readdir in userspace"
23948 +       help
23949 +       Aufs has two methods to provide a merged view for a directory,
23950 +       by a user-space library and by kernel-space natively. The latter
23951 +       is always enabled but sometimes large and slow.
23952 +       If you enable this option, install the library in aufs2-util
23953 +       package, and set some environment variables for your readdir(3),
23954 +       then the work will be handled in user-space which generally
23955 +       shows better performance in most cases.
23956 +       See detail in aufs.5.
23957 +
23958 +config AUFS_DIRREN
23959 +       bool "Workaround for rename(2)-ing a directory"
23960 +       help
23961 +       By default, aufs returns EXDEV error in renameing a dir who has
23962 +       his child on the lower branch, since it is a bad idea to issue
23963 +       rename(2) internally for every lower branch. But user may not
23964 +       accept this behaviour. So here is a workaround to allow such
23965 +       rename(2) and store some extra infromation on the writable
23966 +       branch. Obviously this costs high (and I don't like it).
23967 +       To use this feature, you need to enable this configuration AND
23968 +       to specify the mount option `dirren.'
23969 +       See details in aufs.5 and the design documents.
23970 +
23971 +config AUFS_SHWH
23972 +       bool "Show whiteouts"
23973 +       help
23974 +       If you want to make the whiteouts in aufs visible, then enable
23975 +       this option and specify 'shwh' mount option. Although it may
23976 +       sounds like philosophy or something, but in technically it
23977 +       simply shows the name of whiteout with keeping its behaviour.
23978 +
23979 +config AUFS_BR_RAMFS
23980 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23981 +       help
23982 +       If you want to use ramfs as an aufs branch fs, then enable this
23983 +       option. Generally tmpfs is recommended.
23984 +       Aufs prohibited them to be a branch fs by default, because
23985 +       initramfs becomes unusable after switch_root or something
23986 +       generally. If you sets initramfs as an aufs branch and boot your
23987 +       system by switch_root, you will meet a problem easily since the
23988 +       files in initramfs may be inaccessible.
23989 +       Unless you are going to use ramfs as an aufs branch fs without
23990 +       switch_root or something, leave it N.
23991 +
23992 +config AUFS_BR_FUSE
23993 +       bool "Fuse fs as an aufs branch"
23994 +       depends on FUSE_FS
23995 +       select AUFS_POLL
23996 +       help
23997 +       If you want to use fuse-based userspace filesystem as an aufs
23998 +       branch fs, then enable this option.
23999 +       It implements the internal poll(2) operation which is
24000 +       implemented by fuse only (curretnly).
24001 +
24002 +config AUFS_POLL
24003 +       bool
24004 +       help
24005 +       Automatic configuration for internal use.
24006 +
24007 +config AUFS_BR_HFSPLUS
24008 +       bool "Hfsplus as an aufs branch"
24009 +       depends on HFSPLUS_FS
24010 +       default y
24011 +       help
24012 +       If you want to use hfsplus fs as an aufs branch fs, then enable
24013 +       this option. This option introduces a small overhead at
24014 +       copying-up a file on hfsplus.
24015 +
24016 +config AUFS_BDEV_LOOP
24017 +       bool
24018 +       depends on BLK_DEV_LOOP
24019 +       default y
24020 +       help
24021 +       Automatic configuration for internal use.
24022 +       Convert =[ym] into =y.
24023 +
24024 +config AUFS_DEBUG
24025 +       bool "Debug aufs"
24026 +       help
24027 +       Enable this to compile aufs internal debug code.
24028 +       It will have a negative impact to the performance.
24029 +
24030 +config AUFS_MAGIC_SYSRQ
24031 +       bool
24032 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
24033 +       default y
24034 +       help
24035 +       Automatic configuration for internal use.
24036 +       When aufs supports Magic SysRq, enabled automatically.
24037 +endif
24038 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24039 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24040 +++ linux/fs/aufs/loop.c        2018-08-12 23:43:05.460124736 +0200
24041 @@ -0,0 +1,148 @@
24042 +// SPDX-License-Identifier: GPL-2.0
24043 +/*
24044 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24045 + *
24046 + * This program, aufs is free software; you can redistribute it and/or modify
24047 + * it under the terms of the GNU General Public License as published by
24048 + * the Free Software Foundation; either version 2 of the License, or
24049 + * (at your option) any later version.
24050 + *
24051 + * This program is distributed in the hope that it will be useful,
24052 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24053 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24054 + * GNU General Public License for more details.
24055 + *
24056 + * You should have received a copy of the GNU General Public License
24057 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24058 + */
24059 +
24060 +/*
24061 + * support for loopback block device as a branch
24062 + */
24063 +
24064 +#include "aufs.h"
24065 +
24066 +/* added into drivers/block/loop.c */
24067 +static struct file *(*backing_file_func)(struct super_block *sb);
24068 +
24069 +/*
24070 + * test if two lower dentries have overlapping branches.
24071 + */
24072 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24073 +{
24074 +       struct super_block *h_sb;
24075 +       struct file *backing_file;
24076 +
24077 +       if (unlikely(!backing_file_func)) {
24078 +               /* don't load "loop" module here */
24079 +               backing_file_func = symbol_get(loop_backing_file);
24080 +               if (unlikely(!backing_file_func))
24081 +                       /* "loop" module is not loaded */
24082 +                       return 0;
24083 +       }
24084 +
24085 +       h_sb = h_adding->d_sb;
24086 +       backing_file = backing_file_func(h_sb);
24087 +       if (!backing_file)
24088 +               return 0;
24089 +
24090 +       h_adding = backing_file->f_path.dentry;
24091 +       /*
24092 +        * h_adding can be local NFS.
24093 +        * in this case aufs cannot detect the loop.
24094 +        */
24095 +       if (unlikely(h_adding->d_sb == sb))
24096 +               return 1;
24097 +       return !!au_test_subdir(h_adding, sb->s_root);
24098 +}
24099 +
24100 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24101 +int au_test_loopback_kthread(void)
24102 +{
24103 +       int ret;
24104 +       struct task_struct *tsk = current;
24105 +       char c, comm[sizeof(tsk->comm)];
24106 +
24107 +       ret = 0;
24108 +       if (tsk->flags & PF_KTHREAD) {
24109 +               get_task_comm(comm, tsk);
24110 +               c = comm[4];
24111 +               ret = ('0' <= c && c <= '9'
24112 +                      && !strncmp(comm, "loop", 4));
24113 +       }
24114 +
24115 +       return ret;
24116 +}
24117 +
24118 +/* ---------------------------------------------------------------------- */
24119 +
24120 +#define au_warn_loopback_step  16
24121 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24122 +static unsigned long *au_warn_loopback_array;
24123 +
24124 +void au_warn_loopback(struct super_block *h_sb)
24125 +{
24126 +       int i, new_nelem;
24127 +       unsigned long *a, magic;
24128 +       static DEFINE_SPINLOCK(spin);
24129 +
24130 +       magic = h_sb->s_magic;
24131 +       spin_lock(&spin);
24132 +       a = au_warn_loopback_array;
24133 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24134 +               if (a[i] == magic) {
24135 +                       spin_unlock(&spin);
24136 +                       return;
24137 +               }
24138 +
24139 +       /* h_sb is new to us, print it */
24140 +       if (i < au_warn_loopback_nelem) {
24141 +               a[i] = magic;
24142 +               goto pr;
24143 +       }
24144 +
24145 +       /* expand the array */
24146 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24147 +       a = au_kzrealloc(au_warn_loopback_array,
24148 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24149 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24150 +                        /*may_shrink*/0);
24151 +       if (a) {
24152 +               au_warn_loopback_nelem = new_nelem;
24153 +               au_warn_loopback_array = a;
24154 +               a[i] = magic;
24155 +               goto pr;
24156 +       }
24157 +
24158 +       spin_unlock(&spin);
24159 +       AuWarn1("realloc failed, ignored\n");
24160 +       return;
24161 +
24162 +pr:
24163 +       spin_unlock(&spin);
24164 +       pr_warn("you may want to try another patch for loopback file "
24165 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24166 +}
24167 +
24168 +int au_loopback_init(void)
24169 +{
24170 +       int err;
24171 +       struct super_block *sb __maybe_unused;
24172 +
24173 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
24174 +
24175 +       err = 0;
24176 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24177 +                                        sizeof(unsigned long), GFP_NOFS);
24178 +       if (unlikely(!au_warn_loopback_array))
24179 +               err = -ENOMEM;
24180 +
24181 +       return err;
24182 +}
24183 +
24184 +void au_loopback_fin(void)
24185 +{
24186 +       if (backing_file_func)
24187 +               symbol_put(loop_backing_file);
24188 +       kfree(au_warn_loopback_array);
24189 +}
24190 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24191 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24192 +++ linux/fs/aufs/loop.h        2018-08-12 23:43:05.460124736 +0200
24193 @@ -0,0 +1,53 @@
24194 +/* SPDX-License-Identifier: GPL-2.0 */
24195 +/*
24196 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24197 + *
24198 + * This program, aufs is free software; you can redistribute it and/or modify
24199 + * it under the terms of the GNU General Public License as published by
24200 + * the Free Software Foundation; either version 2 of the License, or
24201 + * (at your option) any later version.
24202 + *
24203 + * This program is distributed in the hope that it will be useful,
24204 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24205 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24206 + * GNU General Public License for more details.
24207 + *
24208 + * You should have received a copy of the GNU General Public License
24209 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24210 + */
24211 +
24212 +/*
24213 + * support for loopback mount as a branch
24214 + */
24215 +
24216 +#ifndef __AUFS_LOOP_H__
24217 +#define __AUFS_LOOP_H__
24218 +
24219 +#ifdef __KERNEL__
24220 +
24221 +struct dentry;
24222 +struct super_block;
24223 +
24224 +#ifdef CONFIG_AUFS_BDEV_LOOP
24225 +/* drivers/block/loop.c */
24226 +struct file *loop_backing_file(struct super_block *sb);
24227 +
24228 +/* loop.c */
24229 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24230 +int au_test_loopback_kthread(void);
24231 +void au_warn_loopback(struct super_block *h_sb);
24232 +
24233 +int au_loopback_init(void);
24234 +void au_loopback_fin(void);
24235 +#else
24236 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24237 +          struct dentry *h_adding)
24238 +AuStubInt0(au_test_loopback_kthread, void)
24239 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24240 +
24241 +AuStubInt0(au_loopback_init, void)
24242 +AuStubVoid(au_loopback_fin, void)
24243 +#endif /* BLK_DEV_LOOP */
24244 +
24245 +#endif /* __KERNEL__ */
24246 +#endif /* __AUFS_LOOP_H__ */
24247 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24248 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24249 +++ linux/fs/aufs/magic.mk      2018-06-04 09:08:09.188079511 +0200
24250 @@ -0,0 +1,31 @@
24251 +# SPDX-License-Identifier: GPL-2.0
24252 +
24253 +# defined in ${srctree}/fs/fuse/inode.c
24254 +# tristate
24255 +ifdef CONFIG_FUSE_FS
24256 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24257 +endif
24258 +
24259 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24260 +# tristate
24261 +ifdef CONFIG_XFS_FS
24262 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24263 +endif
24264 +
24265 +# defined in ${srctree}/fs/configfs/mount.c
24266 +# tristate
24267 +ifdef CONFIG_CONFIGFS_FS
24268 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24269 +endif
24270 +
24271 +# defined in ${srctree}/fs/ubifs/ubifs.h
24272 +# tristate
24273 +ifdef CONFIG_UBIFS_FS
24274 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24275 +endif
24276 +
24277 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24278 +# tristate
24279 +ifdef CONFIG_HFSPLUS_FS
24280 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24281 +endif
24282 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24283 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24284 +++ linux/fs/aufs/Makefile      2018-06-04 09:08:09.181412645 +0200
24285 @@ -0,0 +1,46 @@
24286 +# SPDX-License-Identifier: GPL-2.0
24287 +
24288 +include ${src}/magic.mk
24289 +ifeq (${CONFIG_AUFS_FS},m)
24290 +include ${src}/conf.mk
24291 +endif
24292 +-include ${src}/priv_def.mk
24293 +
24294 +# cf. include/linux/kernel.h
24295 +# enable pr_debug
24296 +ccflags-y += -DDEBUG
24297 +# sparse requires the full pathname
24298 +ifdef M
24299 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24300 +else
24301 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24302 +endif
24303 +
24304 +obj-$(CONFIG_AUFS_FS) += aufs.o
24305 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24306 +       wkq.o vfsub.o dcsub.o \
24307 +       cpup.o whout.o wbr_policy.o \
24308 +       dinfo.o dentry.o \
24309 +       dynop.o \
24310 +       finfo.o file.o f_op.o \
24311 +       dir.o vdir.o \
24312 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24313 +       mvdown.o ioctl.o
24314 +
24315 +# all are boolean
24316 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24317 +aufs-$(CONFIG_SYSFS) += sysfs.o
24318 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24319 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24320 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24321 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24322 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24323 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24324 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24325 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24326 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24327 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24328 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24329 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24330 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24331 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24332 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24333 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24334 +++ linux/fs/aufs/module.c      2018-08-12 23:43:05.460124736 +0200
24335 @@ -0,0 +1,273 @@
24336 +// SPDX-License-Identifier: GPL-2.0
24337 +/*
24338 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24339 + *
24340 + * This program, aufs is free software; you can redistribute it and/or modify
24341 + * it under the terms of the GNU General Public License as published by
24342 + * the Free Software Foundation; either version 2 of the License, or
24343 + * (at your option) any later version.
24344 + *
24345 + * This program is distributed in the hope that it will be useful,
24346 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24347 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24348 + * GNU General Public License for more details.
24349 + *
24350 + * You should have received a copy of the GNU General Public License
24351 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24352 + */
24353 +
24354 +/*
24355 + * module global variables and operations
24356 + */
24357 +
24358 +#include <linux/module.h>
24359 +#include <linux/seq_file.h>
24360 +#include "aufs.h"
24361 +
24362 +/* shrinkable realloc */
24363 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24364 +{
24365 +       size_t sz;
24366 +       int diff;
24367 +
24368 +       sz = 0;
24369 +       diff = -1;
24370 +       if (p) {
24371 +#if 0 /* unused */
24372 +               if (!new_sz) {
24373 +                       kfree(p);
24374 +                       p = NULL;
24375 +                       goto out;
24376 +               }
24377 +#else
24378 +               AuDebugOn(!new_sz);
24379 +#endif
24380 +               sz = ksize(p);
24381 +               diff = au_kmidx_sub(sz, new_sz);
24382 +       }
24383 +       if (sz && !diff)
24384 +               goto out;
24385 +
24386 +       if (sz < new_sz)
24387 +               /* expand or SLOB */
24388 +               p = krealloc(p, new_sz, gfp);
24389 +       else if (new_sz < sz && may_shrink) {
24390 +               /* shrink */
24391 +               void *q;
24392 +
24393 +               q = kmalloc(new_sz, gfp);
24394 +               if (q) {
24395 +                       if (p) {
24396 +                               memcpy(q, p, new_sz);
24397 +                               kfree(p);
24398 +                       }
24399 +                       p = q;
24400 +               } else
24401 +                       p = NULL;
24402 +       }
24403 +
24404 +out:
24405 +       return p;
24406 +}
24407 +
24408 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24409 +                  int may_shrink)
24410 +{
24411 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24412 +       if (p && new_sz > nused)
24413 +               memset(p + nused, 0, new_sz - nused);
24414 +       return p;
24415 +}
24416 +
24417 +/* ---------------------------------------------------------------------- */
24418 +/*
24419 + * aufs caches
24420 + */
24421 +struct kmem_cache *au_cache[AuCache_Last];
24422 +
24423 +static void au_cache_fin(void)
24424 +{
24425 +       int i;
24426 +
24427 +       /*
24428 +        * Make sure all delayed rcu free inodes are flushed before we
24429 +        * destroy cache.
24430 +        */
24431 +       rcu_barrier();
24432 +
24433 +       /* excluding AuCache_HNOTIFY */
24434 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24435 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24436 +               kmem_cache_destroy(au_cache[i]);
24437 +               au_cache[i] = NULL;
24438 +       }
24439 +}
24440 +
24441 +static int __init au_cache_init(void)
24442 +{
24443 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24444 +       if (au_cache[AuCache_DINFO])
24445 +               /* SLAB_DESTROY_BY_RCU */
24446 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24447 +                                                      au_icntnr_init_once);
24448 +       if (au_cache[AuCache_ICNTNR])
24449 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24450 +                                                     au_fi_init_once);
24451 +       if (au_cache[AuCache_FINFO])
24452 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24453 +       if (au_cache[AuCache_VDIR])
24454 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24455 +       if (au_cache[AuCache_DEHSTR])
24456 +               return 0;
24457 +
24458 +       au_cache_fin();
24459 +       return -ENOMEM;
24460 +}
24461 +
24462 +/* ---------------------------------------------------------------------- */
24463 +
24464 +int au_dir_roflags;
24465 +
24466 +#ifdef CONFIG_AUFS_SBILIST
24467 +/*
24468 + * iterate_supers_type() doesn't protect us from
24469 + * remounting (branch management)
24470 + */
24471 +struct hlist_bl_head au_sbilist;
24472 +#endif
24473 +
24474 +/*
24475 + * functions for module interface.
24476 + */
24477 +MODULE_LICENSE("GPL");
24478 +/* MODULE_LICENSE("GPL v2"); */
24479 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24480 +MODULE_DESCRIPTION(AUFS_NAME
24481 +       " -- Advanced multi layered unification filesystem");
24482 +MODULE_VERSION(AUFS_VERSION);
24483 +MODULE_ALIAS_FS(AUFS_NAME);
24484 +
24485 +/* this module parameter has no meaning when SYSFS is disabled */
24486 +int sysaufs_brs = 1;
24487 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24488 +module_param_named(brs, sysaufs_brs, int, 0444);
24489 +
24490 +/* this module parameter has no meaning when USER_NS is disabled */
24491 +bool au_userns;
24492 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24493 +module_param_named(allow_userns, au_userns, bool, 0444);
24494 +
24495 +/* ---------------------------------------------------------------------- */
24496 +
24497 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24498 +
24499 +int au_seq_path(struct seq_file *seq, struct path *path)
24500 +{
24501 +       int err;
24502 +
24503 +       err = seq_path(seq, path, au_esc_chars);
24504 +       if (err >= 0)
24505 +               err = 0;
24506 +       else
24507 +               err = -ENOMEM;
24508 +
24509 +       return err;
24510 +}
24511 +
24512 +/* ---------------------------------------------------------------------- */
24513 +
24514 +static int __init aufs_init(void)
24515 +{
24516 +       int err, i;
24517 +       char *p;
24518 +
24519 +       p = au_esc_chars;
24520 +       for (i = 1; i <= ' '; i++)
24521 +               *p++ = i;
24522 +       *p++ = '\\';
24523 +       *p++ = '\x7f';
24524 +       *p = 0;
24525 +
24526 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24527 +
24528 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24529 +       for (i = 0; i < AuIop_Last; i++)
24530 +               aufs_iop_nogetattr[i].getattr = NULL;
24531 +
24532 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24533 +
24534 +       au_sbilist_init();
24535 +       sysaufs_brs_init();
24536 +       au_debug_init();
24537 +       au_dy_init();
24538 +       err = sysaufs_init();
24539 +       if (unlikely(err))
24540 +               goto out;
24541 +       err = dbgaufs_init();
24542 +       if (unlikely(err))
24543 +               goto out_sysaufs;
24544 +       err = au_procfs_init();
24545 +       if (unlikely(err))
24546 +               goto out_dbgaufs;
24547 +       err = au_wkq_init();
24548 +       if (unlikely(err))
24549 +               goto out_procfs;
24550 +       err = au_loopback_init();
24551 +       if (unlikely(err))
24552 +               goto out_wkq;
24553 +       err = au_hnotify_init();
24554 +       if (unlikely(err))
24555 +               goto out_loopback;
24556 +       err = au_sysrq_init();
24557 +       if (unlikely(err))
24558 +               goto out_hin;
24559 +       err = au_cache_init();
24560 +       if (unlikely(err))
24561 +               goto out_sysrq;
24562 +
24563 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24564 +       err = register_filesystem(&aufs_fs_type);
24565 +       if (unlikely(err))
24566 +               goto out_cache;
24567 +
24568 +       /* since we define pr_fmt, call printk directly */
24569 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24570 +       goto out; /* success */
24571 +
24572 +out_cache:
24573 +       au_cache_fin();
24574 +out_sysrq:
24575 +       au_sysrq_fin();
24576 +out_hin:
24577 +       au_hnotify_fin();
24578 +out_loopback:
24579 +       au_loopback_fin();
24580 +out_wkq:
24581 +       au_wkq_fin();
24582 +out_procfs:
24583 +       au_procfs_fin();
24584 +out_dbgaufs:
24585 +       dbgaufs_fin();
24586 +out_sysaufs:
24587 +       sysaufs_fin();
24588 +       au_dy_fin();
24589 +out:
24590 +       return err;
24591 +}
24592 +
24593 +static void __exit aufs_exit(void)
24594 +{
24595 +       unregister_filesystem(&aufs_fs_type);
24596 +       au_cache_fin();
24597 +       au_sysrq_fin();
24598 +       au_hnotify_fin();
24599 +       au_loopback_fin();
24600 +       au_wkq_fin();
24601 +       au_procfs_fin();
24602 +       dbgaufs_fin();
24603 +       sysaufs_fin();
24604 +       au_dy_fin();
24605 +}
24606 +
24607 +module_init(aufs_init);
24608 +module_exit(aufs_exit);
24609 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24610 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24611 +++ linux/fs/aufs/module.h      2018-08-12 23:43:05.460124736 +0200
24612 @@ -0,0 +1,102 @@
24613 +/* SPDX-License-Identifier: GPL-2.0 */
24614 +/*
24615 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24616 + *
24617 + * This program, aufs is free software; you can redistribute it and/or modify
24618 + * it under the terms of the GNU General Public License as published by
24619 + * the Free Software Foundation; either version 2 of the License, or
24620 + * (at your option) any later version.
24621 + *
24622 + * This program is distributed in the hope that it will be useful,
24623 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24624 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24625 + * GNU General Public License for more details.
24626 + *
24627 + * You should have received a copy of the GNU General Public License
24628 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24629 + */
24630 +
24631 +/*
24632 + * module initialization and module-global
24633 + */
24634 +
24635 +#ifndef __AUFS_MODULE_H__
24636 +#define __AUFS_MODULE_H__
24637 +
24638 +#ifdef __KERNEL__
24639 +
24640 +#include <linux/slab.h>
24641 +
24642 +struct path;
24643 +struct seq_file;
24644 +
24645 +/* module parameters */
24646 +extern int sysaufs_brs;
24647 +extern bool au_userns;
24648 +
24649 +/* ---------------------------------------------------------------------- */
24650 +
24651 +extern int au_dir_roflags;
24652 +
24653 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24654 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24655 +                  int may_shrink);
24656 +
24657 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24658 +{
24659 +#ifndef CONFIG_SLOB
24660 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24661 +#else
24662 +       return -1; /* SLOB is untested */
24663 +#endif
24664 +}
24665 +
24666 +int au_seq_path(struct seq_file *seq, struct path *path);
24667 +
24668 +#ifdef CONFIG_PROC_FS
24669 +/* procfs.c */
24670 +int __init au_procfs_init(void);
24671 +void au_procfs_fin(void);
24672 +#else
24673 +AuStubInt0(au_procfs_init, void);
24674 +AuStubVoid(au_procfs_fin, void);
24675 +#endif
24676 +
24677 +/* ---------------------------------------------------------------------- */
24678 +
24679 +/* kmem cache */
24680 +enum {
24681 +       AuCache_DINFO,
24682 +       AuCache_ICNTNR,
24683 +       AuCache_FINFO,
24684 +       AuCache_VDIR,
24685 +       AuCache_DEHSTR,
24686 +       AuCache_HNOTIFY, /* must be last */
24687 +       AuCache_Last
24688 +};
24689 +
24690 +extern struct kmem_cache *au_cache[AuCache_Last];
24691 +
24692 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24693 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24694 +#define AuCacheCtor(type, ctor)        \
24695 +       kmem_cache_create(#type, sizeof(struct type), \
24696 +                         __alignof__(struct type), AuCacheFlags, ctor)
24697 +
24698 +#define AuCacheFuncs(name, index) \
24699 +static inline struct au_##name *au_cache_alloc_##name(void) \
24700 +{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24701 +static inline void au_cache_free_##name(struct au_##name *p) \
24702 +{ kmem_cache_free(au_cache[AuCache_##index], p); }
24703 +
24704 +AuCacheFuncs(dinfo, DINFO);
24705 +AuCacheFuncs(icntnr, ICNTNR);
24706 +AuCacheFuncs(finfo, FINFO);
24707 +AuCacheFuncs(vdir, VDIR);
24708 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24709 +#ifdef CONFIG_AUFS_HNOTIFY
24710 +AuCacheFuncs(hnotify, HNOTIFY);
24711 +#endif
24712 +
24713 +#endif /* __KERNEL__ */
24714 +#endif /* __AUFS_MODULE_H__ */
24715 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24716 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24717 +++ linux/fs/aufs/mvdown.c      2018-08-12 23:43:05.460124736 +0200
24718 @@ -0,0 +1,705 @@
24719 +// SPDX-License-Identifier: GPL-2.0
24720 +/*
24721 + * Copyright (C) 2011-2018 Junjiro R. Okajima
24722 + *
24723 + * This program, aufs is free software; you can redistribute it and/or modify
24724 + * it under the terms of the GNU General Public License as published by
24725 + * the Free Software Foundation; either version 2 of the License, or
24726 + * (at your option) any later version.
24727 + *
24728 + * This program is distributed in the hope that it will be useful,
24729 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24730 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24731 + * GNU General Public License for more details.
24732 + *
24733 + * You should have received a copy of the GNU General Public License
24734 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24735 + */
24736 +
24737 +/*
24738 + * move-down, opposite of copy-up
24739 + */
24740 +
24741 +#include "aufs.h"
24742 +
24743 +struct au_mvd_args {
24744 +       struct {
24745 +               struct super_block *h_sb;
24746 +               struct dentry *h_parent;
24747 +               struct au_hinode *hdir;
24748 +               struct inode *h_dir, *h_inode;
24749 +               struct au_pin pin;
24750 +       } info[AUFS_MVDOWN_NARRAY];
24751 +
24752 +       struct aufs_mvdown mvdown;
24753 +       struct dentry *dentry, *parent;
24754 +       struct inode *inode, *dir;
24755 +       struct super_block *sb;
24756 +       aufs_bindex_t bopq, bwh, bfound;
24757 +       unsigned char rename_lock;
24758 +};
24759 +
24760 +#define mvd_errno              mvdown.au_errno
24761 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24762 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24763 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24764 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24765 +
24766 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
24767 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
24768 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
24769 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
24770 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
24771 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
24772 +
24773 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
24774 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
24775 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
24776 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
24777 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
24778 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
24779 +
24780 +#define AU_MVD_PR(flag, ...) do {                      \
24781 +               if (flag)                               \
24782 +                       pr_err(__VA_ARGS__);            \
24783 +       } while (0)
24784 +
24785 +static int find_lower_writable(struct au_mvd_args *a)
24786 +{
24787 +       struct super_block *sb;
24788 +       aufs_bindex_t bindex, bbot;
24789 +       struct au_branch *br;
24790 +
24791 +       sb = a->sb;
24792 +       bindex = a->mvd_bsrc;
24793 +       bbot = au_sbbot(sb);
24794 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24795 +               for (bindex++; bindex <= bbot; bindex++) {
24796 +                       br = au_sbr(sb, bindex);
24797 +                       if (au_br_fhsm(br->br_perm)
24798 +                           && !sb_rdonly(au_br_sb(br)))
24799 +                               return bindex;
24800 +               }
24801 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24802 +               for (bindex++; bindex <= bbot; bindex++) {
24803 +                       br = au_sbr(sb, bindex);
24804 +                       if (!au_br_rdonly(br))
24805 +                               return bindex;
24806 +               }
24807 +       else
24808 +               for (bindex++; bindex <= bbot; bindex++) {
24809 +                       br = au_sbr(sb, bindex);
24810 +                       if (!sb_rdonly(au_br_sb(br))) {
24811 +                               if (au_br_rdonly(br))
24812 +                                       a->mvdown.flags
24813 +                                               |= AUFS_MVDOWN_ROLOWER_R;
24814 +                               return bindex;
24815 +                       }
24816 +               }
24817 +
24818 +       return -1;
24819 +}
24820 +
24821 +/* make the parent dir on bdst */
24822 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24823 +{
24824 +       int err;
24825 +
24826 +       err = 0;
24827 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24828 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24829 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24830 +       a->mvd_h_dst_parent = NULL;
24831 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
24832 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24833 +       if (!a->mvd_h_dst_parent) {
24834 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24835 +               if (unlikely(err)) {
24836 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
24837 +                       goto out;
24838 +               }
24839 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24840 +       }
24841 +
24842 +out:
24843 +       AuTraceErr(err);
24844 +       return err;
24845 +}
24846 +
24847 +/* lock them all */
24848 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
24849 +{
24850 +       int err;
24851 +       struct dentry *h_trap;
24852 +
24853 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24854 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
24855 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24856 +                    au_opt_udba(a->sb),
24857 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24858 +       AuTraceErr(err);
24859 +       if (unlikely(err)) {
24860 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
24861 +               goto out;
24862 +       }
24863 +
24864 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24865 +               a->rename_lock = 0;
24866 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24867 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24868 +                           au_opt_udba(a->sb),
24869 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24870 +               err = au_do_pin(&a->mvd_pin_src);
24871 +               AuTraceErr(err);
24872 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
24873 +               if (unlikely(err)) {
24874 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
24875 +                       goto out_dst;
24876 +               }
24877 +               goto out; /* success */
24878 +       }
24879 +
24880 +       a->rename_lock = 1;
24881 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
24882 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24883 +                    au_opt_udba(a->sb),
24884 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24885 +       AuTraceErr(err);
24886 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
24887 +       if (unlikely(err)) {
24888 +               AU_MVD_PR(dmsg, "pin_src failed\n");
24889 +               au_pin_hdir_lock(&a->mvd_pin_dst);
24890 +               goto out_dst;
24891 +       }
24892 +       au_pin_hdir_unlock(&a->mvd_pin_src);
24893 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24894 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
24895 +       if (h_trap) {
24896 +               err = (h_trap != a->mvd_h_src_parent);
24897 +               if (err)
24898 +                       err = (h_trap != a->mvd_h_dst_parent);
24899 +       }
24900 +       BUG_ON(err); /* it should never happen */
24901 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
24902 +               err = -EBUSY;
24903 +               AuTraceErr(err);
24904 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24905 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
24906 +               au_pin_hdir_lock(&a->mvd_pin_src);
24907 +               au_unpin(&a->mvd_pin_src);
24908 +               au_pin_hdir_lock(&a->mvd_pin_dst);
24909 +               goto out_dst;
24910 +       }
24911 +       goto out; /* success */
24912 +
24913 +out_dst:
24914 +       au_unpin(&a->mvd_pin_dst);
24915 +out:
24916 +       AuTraceErr(err);
24917 +       return err;
24918 +}
24919 +
24920 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
24921 +{
24922 +       if (!a->rename_lock)
24923 +               au_unpin(&a->mvd_pin_src);
24924 +       else {
24925 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24926 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
24927 +               au_pin_hdir_lock(&a->mvd_pin_src);
24928 +               au_unpin(&a->mvd_pin_src);
24929 +               au_pin_hdir_lock(&a->mvd_pin_dst);
24930 +       }
24931 +       au_unpin(&a->mvd_pin_dst);
24932 +}
24933 +
24934 +/* copy-down the file */
24935 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
24936 +{
24937 +       int err;
24938 +       struct au_cp_generic cpg = {
24939 +               .dentry = a->dentry,
24940 +               .bdst   = a->mvd_bdst,
24941 +               .bsrc   = a->mvd_bsrc,
24942 +               .len    = -1,
24943 +               .pin    = &a->mvd_pin_dst,
24944 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24945 +       };
24946 +
24947 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
24948 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
24949 +               au_fset_cpup(cpg.flags, OVERWRITE);
24950 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
24951 +               au_fset_cpup(cpg.flags, RWDST);
24952 +       err = au_sio_cpdown_simple(&cpg);
24953 +       if (unlikely(err))
24954 +               AU_MVD_PR(dmsg, "cpdown failed\n");
24955 +
24956 +       AuTraceErr(err);
24957 +       return err;
24958 +}
24959 +
24960 +/*
24961 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
24962 + * were sleeping
24963 + */
24964 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
24965 +{
24966 +       int err;
24967 +       struct path h_path;
24968 +       struct au_branch *br;
24969 +       struct inode *delegated;
24970 +
24971 +       br = au_sbr(a->sb, a->mvd_bdst);
24972 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
24973 +       err = PTR_ERR(h_path.dentry);
24974 +       if (IS_ERR(h_path.dentry)) {
24975 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
24976 +               goto out;
24977 +       }
24978 +
24979 +       err = 0;
24980 +       if (d_is_positive(h_path.dentry)) {
24981 +               h_path.mnt = au_br_mnt(br);
24982 +               delegated = NULL;
24983 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
24984 +                                  &delegated, /*force*/0);
24985 +               if (unlikely(err == -EWOULDBLOCK)) {
24986 +                       pr_warn("cannot retry for NFSv4 delegation"
24987 +                               " for an internal unlink\n");
24988 +                       iput(delegated);
24989 +               }
24990 +               if (unlikely(err))
24991 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
24992 +       }
24993 +       dput(h_path.dentry);
24994 +
24995 +out:
24996 +       AuTraceErr(err);
24997 +       return err;
24998 +}
24999 +
25000 +/*
25001 + * unlink the topmost h_dentry
25002 + */
25003 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25004 +{
25005 +       int err;
25006 +       struct path h_path;
25007 +       struct inode *delegated;
25008 +
25009 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25010 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25011 +       delegated = NULL;
25012 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25013 +       if (unlikely(err == -EWOULDBLOCK)) {
25014 +               pr_warn("cannot retry for NFSv4 delegation"
25015 +                       " for an internal unlink\n");
25016 +               iput(delegated);
25017 +       }
25018 +       if (unlikely(err))
25019 +               AU_MVD_PR(dmsg, "unlink failed\n");
25020 +
25021 +       AuTraceErr(err);
25022 +       return err;
25023 +}
25024 +
25025 +/* Since mvdown succeeded, we ignore an error of this function */
25026 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25027 +{
25028 +       int err;
25029 +       struct au_branch *br;
25030 +
25031 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25032 +       br = au_sbr(a->sb, a->mvd_bsrc);
25033 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25034 +       if (!err) {
25035 +               br = au_sbr(a->sb, a->mvd_bdst);
25036 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25037 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25038 +       }
25039 +       if (!err)
25040 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25041 +       else
25042 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25043 +}
25044 +
25045 +/*
25046 + * copy-down the file and unlink the bsrc file.
25047 + * - unlink the bdst whout if exist
25048 + * - copy-down the file (with whtmp name and rename)
25049 + * - unlink the bsrc file
25050 + */
25051 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25052 +{
25053 +       int err;
25054 +
25055 +       err = au_do_mkdir(dmsg, a);
25056 +       if (!err)
25057 +               err = au_do_lock(dmsg, a);
25058 +       if (unlikely(err))
25059 +               goto out;
25060 +
25061 +       /*
25062 +        * do not revert the activities we made on bdst since they should be
25063 +        * harmless in aufs.
25064 +        */
25065 +
25066 +       err = au_do_cpdown(dmsg, a);
25067 +       if (!err)
25068 +               err = au_do_unlink_wh(dmsg, a);
25069 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25070 +               err = au_do_unlink(dmsg, a);
25071 +       if (unlikely(err))
25072 +               goto out_unlock;
25073 +
25074 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25075 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25076 +       if (find_lower_writable(a) < 0)
25077 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25078 +
25079 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25080 +               au_do_stfs(dmsg, a);
25081 +
25082 +       /* maintain internal array */
25083 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25084 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25085 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25086 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25087 +               au_set_ibtop(a->inode, a->mvd_bdst);
25088 +       } else {
25089 +               /* hide the lower */
25090 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25091 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25092 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25093 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25094 +       }
25095 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25096 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25097 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25098 +               au_set_ibbot(a->inode, a->mvd_bdst);
25099 +
25100 +out_unlock:
25101 +       au_do_unlock(dmsg, a);
25102 +out:
25103 +       AuTraceErr(err);
25104 +       return err;
25105 +}
25106 +
25107 +/* ---------------------------------------------------------------------- */
25108 +
25109 +/* make sure the file is idle */
25110 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25111 +{
25112 +       int err, plinked;
25113 +
25114 +       err = 0;
25115 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25116 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25117 +           && au_dcount(a->dentry) == 1
25118 +           && atomic_read(&a->inode->i_count) == 1
25119 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25120 +           && (!plinked || !au_plink_test(a->inode))
25121 +           && a->inode->i_nlink == 1)
25122 +               goto out;
25123 +
25124 +       err = -EBUSY;
25125 +       AU_MVD_PR(dmsg,
25126 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25127 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25128 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25129 +                 a->mvd_h_src_inode->i_nlink,
25130 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25131 +
25132 +out:
25133 +       AuTraceErr(err);
25134 +       return err;
25135 +}
25136 +
25137 +/* make sure the parent dir is fine */
25138 +static int au_mvd_args_parent(const unsigned char dmsg,
25139 +                             struct au_mvd_args *a)
25140 +{
25141 +       int err;
25142 +       aufs_bindex_t bindex;
25143 +
25144 +       err = 0;
25145 +       if (unlikely(au_alive_dir(a->parent))) {
25146 +               err = -ENOENT;
25147 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25148 +               goto out;
25149 +       }
25150 +
25151 +       a->bopq = au_dbdiropq(a->parent);
25152 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25153 +       AuDbg("b%d\n", bindex);
25154 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25155 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25156 +               err = -EINVAL;
25157 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25158 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25159 +                         a->bopq, a->mvd_bdst);
25160 +       }
25161 +
25162 +out:
25163 +       AuTraceErr(err);
25164 +       return err;
25165 +}
25166 +
25167 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25168 +                                   struct au_mvd_args *a)
25169 +{
25170 +       int err;
25171 +       struct au_dinfo *dinfo, *tmp;
25172 +
25173 +       /* lookup the next lower positive entry */
25174 +       err = -ENOMEM;
25175 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25176 +       if (unlikely(!tmp))
25177 +               goto out;
25178 +
25179 +       a->bfound = -1;
25180 +       a->bwh = -1;
25181 +       dinfo = au_di(a->dentry);
25182 +       au_di_cp(tmp, dinfo);
25183 +       au_di_swap(tmp, dinfo);
25184 +
25185 +       /* returns the number of positive dentries */
25186 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25187 +                            /* AuLkup_IGNORE_PERM */ 0);
25188 +       if (!err)
25189 +               a->bwh = au_dbwh(a->dentry);
25190 +       else if (err > 0)
25191 +               a->bfound = au_dbtop(a->dentry);
25192 +
25193 +       au_di_swap(tmp, dinfo);
25194 +       au_rw_write_unlock(&tmp->di_rwsem);
25195 +       au_di_free(tmp);
25196 +       if (unlikely(err < 0))
25197 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25198 +
25199 +       /*
25200 +        * here, we have these cases.
25201 +        * bfound == -1
25202 +        *      no positive dentry under bsrc. there are more sub-cases.
25203 +        *      bwh < 0
25204 +        *              there no whiteout, we can safely move-down.
25205 +        *      bwh <= bsrc
25206 +        *              impossible
25207 +        *      bsrc < bwh && bwh < bdst
25208 +        *              there is a whiteout on RO branch. cannot proceed.
25209 +        *      bwh == bdst
25210 +        *              there is a whiteout on the RW target branch. it should
25211 +        *              be removed.
25212 +        *      bdst < bwh
25213 +        *              there is a whiteout somewhere unrelated branch.
25214 +        * -1 < bfound && bfound <= bsrc
25215 +        *      impossible.
25216 +        * bfound < bdst
25217 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25218 +        *      proceed.
25219 +        * bfound == bdst
25220 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25221 +        *      error.
25222 +        * bdst < bfound
25223 +        *      found, after we create the file on bdst, it will be hidden.
25224 +        */
25225 +
25226 +       AuDebugOn(a->bfound == -1
25227 +                 && a->bwh != -1
25228 +                 && a->bwh <= a->mvd_bsrc);
25229 +       AuDebugOn(-1 < a->bfound
25230 +                 && a->bfound <= a->mvd_bsrc);
25231 +
25232 +       err = -EINVAL;
25233 +       if (a->bfound == -1
25234 +           && a->mvd_bsrc < a->bwh
25235 +           && a->bwh != -1
25236 +           && a->bwh < a->mvd_bdst) {
25237 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25238 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25239 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25240 +               goto out;
25241 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25242 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25243 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25244 +                         a->mvd_bdst, a->bfound);
25245 +               goto out;
25246 +       }
25247 +
25248 +       err = 0; /* success */
25249 +
25250 +out:
25251 +       AuTraceErr(err);
25252 +       return err;
25253 +}
25254 +
25255 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25256 +{
25257 +       int err;
25258 +
25259 +       err = 0;
25260 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25261 +           && a->bfound == a->mvd_bdst)
25262 +               err = -EEXIST;
25263 +       AuTraceErr(err);
25264 +       return err;
25265 +}
25266 +
25267 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25268 +{
25269 +       int err;
25270 +       struct au_branch *br;
25271 +
25272 +       err = -EISDIR;
25273 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25274 +               goto out;
25275 +
25276 +       err = -EINVAL;
25277 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25278 +               a->mvd_bsrc = au_ibtop(a->inode);
25279 +       else {
25280 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25281 +               if (unlikely(a->mvd_bsrc < 0
25282 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25283 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25284 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25285 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25286 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25287 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25288 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25289 +                       AU_MVD_PR(dmsg, "no upper\n");
25290 +                       goto out;
25291 +               }
25292 +       }
25293 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25294 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25295 +               AU_MVD_PR(dmsg, "on the bottom\n");
25296 +               goto out;
25297 +       }
25298 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25299 +       br = au_sbr(a->sb, a->mvd_bsrc);
25300 +       err = au_br_rdonly(br);
25301 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25302 +               if (unlikely(err))
25303 +                       goto out;
25304 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25305 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25306 +               if (err)
25307 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25308 +               /* go on */
25309 +       } else
25310 +               goto out;
25311 +
25312 +       err = -EINVAL;
25313 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25314 +               a->mvd_bdst = find_lower_writable(a);
25315 +               if (unlikely(a->mvd_bdst < 0)) {
25316 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25317 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25318 +                       goto out;
25319 +               }
25320 +       } else {
25321 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25322 +               if (unlikely(a->mvd_bdst < 0
25323 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25324 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25325 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25326 +                       goto out;
25327 +               }
25328 +       }
25329 +
25330 +       err = au_mvd_args_busy(dmsg, a);
25331 +       if (!err)
25332 +               err = au_mvd_args_parent(dmsg, a);
25333 +       if (!err)
25334 +               err = au_mvd_args_intermediate(dmsg, a);
25335 +       if (!err)
25336 +               err = au_mvd_args_exist(dmsg, a);
25337 +       if (!err)
25338 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25339 +
25340 +out:
25341 +       AuTraceErr(err);
25342 +       return err;
25343 +}
25344 +
25345 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25346 +{
25347 +       int err, e;
25348 +       unsigned char dmsg;
25349 +       struct au_mvd_args *args;
25350 +       struct inode *inode;
25351 +
25352 +       inode = d_inode(dentry);
25353 +       err = -EPERM;
25354 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25355 +               goto out;
25356 +
25357 +       err = -ENOMEM;
25358 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25359 +       if (unlikely(!args))
25360 +               goto out;
25361 +
25362 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25363 +       if (!err)
25364 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
25365 +       if (unlikely(err)) {
25366 +               err = -EFAULT;
25367 +               AuTraceErr(err);
25368 +               goto out_free;
25369 +       }
25370 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25371 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25372 +       args->mvdown.au_errno = 0;
25373 +       args->dentry = dentry;
25374 +       args->inode = inode;
25375 +       args->sb = dentry->d_sb;
25376 +
25377 +       err = -ENOENT;
25378 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25379 +       args->parent = dget_parent(dentry);
25380 +       args->dir = d_inode(args->parent);
25381 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25382 +       dput(args->parent);
25383 +       if (unlikely(args->parent != dentry->d_parent)) {
25384 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25385 +               goto out_dir;
25386 +       }
25387 +
25388 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25389 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25390 +       if (unlikely(err))
25391 +               goto out_inode;
25392 +
25393 +       di_write_lock_parent(args->parent);
25394 +       err = au_mvd_args(dmsg, args);
25395 +       if (unlikely(err))
25396 +               goto out_parent;
25397 +
25398 +       err = au_do_mvdown(dmsg, args);
25399 +       if (unlikely(err))
25400 +               goto out_parent;
25401 +
25402 +       au_cpup_attr_timesizes(args->dir);
25403 +       au_cpup_attr_timesizes(inode);
25404 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25405 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25406 +       /* au_digen_dec(dentry); */
25407 +
25408 +out_parent:
25409 +       di_write_unlock(args->parent);
25410 +       aufs_read_unlock(dentry, AuLock_DW);
25411 +out_inode:
25412 +       inode_unlock(inode);
25413 +out_dir:
25414 +       inode_unlock(args->dir);
25415 +out_free:
25416 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25417 +       if (unlikely(e))
25418 +               err = -EFAULT;
25419 +       kfree(args);
25420 +out:
25421 +       AuTraceErr(err);
25422 +       return err;
25423 +}
25424 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25425 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25426 +++ linux/fs/aufs/opts.c        2018-08-12 23:43:05.460124736 +0200
25427 @@ -0,0 +1,1891 @@
25428 +// SPDX-License-Identifier: GPL-2.0
25429 +/*
25430 + * Copyright (C) 2005-2018 Junjiro R. Okajima
25431 + *
25432 + * This program, aufs is free software; you can redistribute it and/or modify
25433 + * it under the terms of the GNU General Public License as published by
25434 + * the Free Software Foundation; either version 2 of the License, or
25435 + * (at your option) any later version.
25436 + *
25437 + * This program is distributed in the hope that it will be useful,
25438 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25439 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25440 + * GNU General Public License for more details.
25441 + *
25442 + * You should have received a copy of the GNU General Public License
25443 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25444 + */
25445 +
25446 +/*
25447 + * mount options/flags
25448 + */
25449 +
25450 +#include <linux/namei.h>
25451 +#include <linux/types.h> /* a distribution requires */
25452 +#include <linux/parser.h>
25453 +#include "aufs.h"
25454 +
25455 +/* ---------------------------------------------------------------------- */
25456 +
25457 +enum {
25458 +       Opt_br,
25459 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25460 +       Opt_idel, Opt_imod,
25461 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25462 +       Opt_rdblk_def, Opt_rdhash_def,
25463 +       Opt_xino, Opt_noxino,
25464 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25465 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25466 +       Opt_trunc_xib, Opt_notrunc_xib,
25467 +       Opt_shwh, Opt_noshwh,
25468 +       Opt_plink, Opt_noplink, Opt_list_plink,
25469 +       Opt_udba,
25470 +       Opt_dio, Opt_nodio,
25471 +       Opt_diropq_a, Opt_diropq_w,
25472 +       Opt_warn_perm, Opt_nowarn_perm,
25473 +       Opt_wbr_copyup, Opt_wbr_create,
25474 +       Opt_fhsm_sec,
25475 +       Opt_verbose, Opt_noverbose,
25476 +       Opt_sum, Opt_nosum, Opt_wsum,
25477 +       Opt_dirperm1, Opt_nodirperm1,
25478 +       Opt_dirren, Opt_nodirren,
25479 +       Opt_acl, Opt_noacl,
25480 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25481 +};
25482 +
25483 +static match_table_t options = {
25484 +       {Opt_br, "br=%s"},
25485 +       {Opt_br, "br:%s"},
25486 +
25487 +       {Opt_add, "add=%d:%s"},
25488 +       {Opt_add, "add:%d:%s"},
25489 +       {Opt_add, "ins=%d:%s"},
25490 +       {Opt_add, "ins:%d:%s"},
25491 +       {Opt_append, "append=%s"},
25492 +       {Opt_append, "append:%s"},
25493 +       {Opt_prepend, "prepend=%s"},
25494 +       {Opt_prepend, "prepend:%s"},
25495 +
25496 +       {Opt_del, "del=%s"},
25497 +       {Opt_del, "del:%s"},
25498 +       /* {Opt_idel, "idel:%d"}, */
25499 +       {Opt_mod, "mod=%s"},
25500 +       {Opt_mod, "mod:%s"},
25501 +       /* {Opt_imod, "imod:%d:%s"}, */
25502 +
25503 +       {Opt_dirwh, "dirwh=%d"},
25504 +
25505 +       {Opt_xino, "xino=%s"},
25506 +       {Opt_noxino, "noxino"},
25507 +       {Opt_trunc_xino, "trunc_xino"},
25508 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25509 +       {Opt_notrunc_xino, "notrunc_xino"},
25510 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25511 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25512 +       /* {Opt_zxino, "zxino=%s"}, */
25513 +       {Opt_trunc_xib, "trunc_xib"},
25514 +       {Opt_notrunc_xib, "notrunc_xib"},
25515 +
25516 +#ifdef CONFIG_PROC_FS
25517 +       {Opt_plink, "plink"},
25518 +#else
25519 +       {Opt_ignore_silent, "plink"},
25520 +#endif
25521 +
25522 +       {Opt_noplink, "noplink"},
25523 +
25524 +#ifdef CONFIG_AUFS_DEBUG
25525 +       {Opt_list_plink, "list_plink"},
25526 +#endif
25527 +
25528 +       {Opt_udba, "udba=%s"},
25529 +
25530 +       {Opt_dio, "dio"},
25531 +       {Opt_nodio, "nodio"},
25532 +
25533 +#ifdef CONFIG_AUFS_DIRREN
25534 +       {Opt_dirren, "dirren"},
25535 +       {Opt_nodirren, "nodirren"},
25536 +#else
25537 +       {Opt_ignore, "dirren"},
25538 +       {Opt_ignore_silent, "nodirren"},
25539 +#endif
25540 +
25541 +#ifdef CONFIG_AUFS_FHSM
25542 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25543 +#else
25544 +       {Opt_ignore, "fhsm_sec=%d"},
25545 +#endif
25546 +
25547 +       {Opt_diropq_a, "diropq=always"},
25548 +       {Opt_diropq_a, "diropq=a"},
25549 +       {Opt_diropq_w, "diropq=whiteouted"},
25550 +       {Opt_diropq_w, "diropq=w"},
25551 +
25552 +       {Opt_warn_perm, "warn_perm"},
25553 +       {Opt_nowarn_perm, "nowarn_perm"},
25554 +
25555 +       /* keep them temporary */
25556 +       {Opt_ignore_silent, "nodlgt"},
25557 +       {Opt_ignore, "clean_plink"},
25558 +
25559 +#ifdef CONFIG_AUFS_SHWH
25560 +       {Opt_shwh, "shwh"},
25561 +#endif
25562 +       {Opt_noshwh, "noshwh"},
25563 +
25564 +       {Opt_dirperm1, "dirperm1"},
25565 +       {Opt_nodirperm1, "nodirperm1"},
25566 +
25567 +       {Opt_verbose, "verbose"},
25568 +       {Opt_verbose, "v"},
25569 +       {Opt_noverbose, "noverbose"},
25570 +       {Opt_noverbose, "quiet"},
25571 +       {Opt_noverbose, "q"},
25572 +       {Opt_noverbose, "silent"},
25573 +
25574 +       {Opt_sum, "sum"},
25575 +       {Opt_nosum, "nosum"},
25576 +       {Opt_wsum, "wsum"},
25577 +
25578 +       {Opt_rdcache, "rdcache=%d"},
25579 +       {Opt_rdblk, "rdblk=%d"},
25580 +       {Opt_rdblk_def, "rdblk=def"},
25581 +       {Opt_rdhash, "rdhash=%d"},
25582 +       {Opt_rdhash_def, "rdhash=def"},
25583 +
25584 +       {Opt_wbr_create, "create=%s"},
25585 +       {Opt_wbr_create, "create_policy=%s"},
25586 +       {Opt_wbr_copyup, "cpup=%s"},
25587 +       {Opt_wbr_copyup, "copyup=%s"},
25588 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25589 +
25590 +       /* generic VFS flag */
25591 +#ifdef CONFIG_FS_POSIX_ACL
25592 +       {Opt_acl, "acl"},
25593 +       {Opt_noacl, "noacl"},
25594 +#else
25595 +       {Opt_ignore, "acl"},
25596 +       {Opt_ignore_silent, "noacl"},
25597 +#endif
25598 +
25599 +       /* internal use for the scripts */
25600 +       {Opt_ignore_silent, "si=%s"},
25601 +
25602 +       {Opt_br, "dirs=%s"},
25603 +       {Opt_ignore, "debug=%d"},
25604 +       {Opt_ignore, "delete=whiteout"},
25605 +       {Opt_ignore, "delete=all"},
25606 +       {Opt_ignore, "imap=%s"},
25607 +
25608 +       /* temporary workaround, due to old mount(8)? */
25609 +       {Opt_ignore_silent, "relatime"},
25610 +
25611 +       {Opt_err, NULL}
25612 +};
25613 +
25614 +/* ---------------------------------------------------------------------- */
25615 +
25616 +static const char *au_parser_pattern(int val, match_table_t tbl)
25617 +{
25618 +       struct match_token *p;
25619 +
25620 +       p = tbl;
25621 +       while (p->pattern) {
25622 +               if (p->token == val)
25623 +                       return p->pattern;
25624 +               p++;
25625 +       }
25626 +       BUG();
25627 +       return "??";
25628 +}
25629 +
25630 +static const char *au_optstr(int *val, match_table_t tbl)
25631 +{
25632 +       struct match_token *p;
25633 +       int v;
25634 +
25635 +       v = *val;
25636 +       if (!v)
25637 +               goto out;
25638 +       p = tbl;
25639 +       while (p->pattern) {
25640 +               if (p->token
25641 +                   && (v & p->token) == p->token) {
25642 +                       *val &= ~p->token;
25643 +                       return p->pattern;
25644 +               }
25645 +               p++;
25646 +       }
25647 +
25648 +out:
25649 +       return NULL;
25650 +}
25651 +
25652 +/* ---------------------------------------------------------------------- */
25653 +
25654 +static match_table_t brperm = {
25655 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25656 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25657 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25658 +       {0, NULL}
25659 +};
25660 +
25661 +static match_table_t brattr = {
25662 +       /* general */
25663 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25664 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25665 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25666 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25667 +#ifdef CONFIG_AUFS_FHSM
25668 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25669 +#endif
25670 +#ifdef CONFIG_AUFS_XATTR
25671 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25672 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25673 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25674 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25675 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25676 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25677 +#endif
25678 +
25679 +       /* ro/rr branch */
25680 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25681 +
25682 +       /* rw branch */
25683 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25684 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25685 +
25686 +       {0, NULL}
25687 +};
25688 +
25689 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25690 +{
25691 +       int attr, v;
25692 +       char *p;
25693 +
25694 +       attr = 0;
25695 +       do {
25696 +               p = strchr(str, '+');
25697 +               if (p)
25698 +                       *p = 0;
25699 +               v = match_token(str, table, args);
25700 +               if (v) {
25701 +                       if (v & AuBrAttr_CMOO_Mask)
25702 +                               attr &= ~AuBrAttr_CMOO_Mask;
25703 +                       attr |= v;
25704 +               } else {
25705 +                       if (p)
25706 +                               *p = '+';
25707 +                       pr_warn("ignored branch attribute %s\n", str);
25708 +                       break;
25709 +               }
25710 +               if (p)
25711 +                       str = p + 1;
25712 +       } while (p);
25713 +
25714 +       return attr;
25715 +}
25716 +
25717 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25718 +{
25719 +       int sz;
25720 +       const char *p;
25721 +       char *q;
25722 +
25723 +       q = str->a;
25724 +       *q = 0;
25725 +       p = au_optstr(&perm, brattr);
25726 +       if (p) {
25727 +               sz = strlen(p);
25728 +               memcpy(q, p, sz + 1);
25729 +               q += sz;
25730 +       } else
25731 +               goto out;
25732 +
25733 +       do {
25734 +               p = au_optstr(&perm, brattr);
25735 +               if (p) {
25736 +                       *q++ = '+';
25737 +                       sz = strlen(p);
25738 +                       memcpy(q, p, sz + 1);
25739 +                       q += sz;
25740 +               }
25741 +       } while (p);
25742 +
25743 +out:
25744 +       return q - str->a;
25745 +}
25746 +
25747 +static int noinline_for_stack br_perm_val(char *perm)
25748 +{
25749 +       int val, bad, sz;
25750 +       char *p;
25751 +       substring_t args[MAX_OPT_ARGS];
25752 +       au_br_perm_str_t attr;
25753 +
25754 +       p = strchr(perm, '+');
25755 +       if (p)
25756 +               *p = 0;
25757 +       val = match_token(perm, brperm, args);
25758 +       if (!val) {
25759 +               if (p)
25760 +                       *p = '+';
25761 +               pr_warn("ignored branch permission %s\n", perm);
25762 +               val = AuBrPerm_RO;
25763 +               goto out;
25764 +       }
25765 +       if (!p)
25766 +               goto out;
25767 +
25768 +       val |= br_attr_val(p + 1, brattr, args);
25769 +
25770 +       bad = 0;
25771 +       switch (val & AuBrPerm_Mask) {
25772 +       case AuBrPerm_RO:
25773 +       case AuBrPerm_RR:
25774 +               bad = val & AuBrWAttr_Mask;
25775 +               val &= ~AuBrWAttr_Mask;
25776 +               break;
25777 +       case AuBrPerm_RW:
25778 +               bad = val & AuBrRAttr_Mask;
25779 +               val &= ~AuBrRAttr_Mask;
25780 +               break;
25781 +       }
25782 +
25783 +       /*
25784 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25785 +        * does not treat it as an error, just warning.
25786 +        * this is a tiny guard for the user operation.
25787 +        */
25788 +       if (val & AuBrAttr_UNPIN) {
25789 +               bad |= AuBrAttr_UNPIN;
25790 +               val &= ~AuBrAttr_UNPIN;
25791 +       }
25792 +
25793 +       if (unlikely(bad)) {
25794 +               sz = au_do_optstr_br_attr(&attr, bad);
25795 +               AuDebugOn(!sz);
25796 +               pr_warn("ignored branch attribute %s\n", attr.a);
25797 +       }
25798 +
25799 +out:
25800 +       return val;
25801 +}
25802 +
25803 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25804 +{
25805 +       au_br_perm_str_t attr;
25806 +       const char *p;
25807 +       char *q;
25808 +       int sz;
25809 +
25810 +       q = str->a;
25811 +       p = au_optstr(&perm, brperm);
25812 +       AuDebugOn(!p || !*p);
25813 +       sz = strlen(p);
25814 +       memcpy(q, p, sz + 1);
25815 +       q += sz;
25816 +
25817 +       sz = au_do_optstr_br_attr(&attr, perm);
25818 +       if (sz) {
25819 +               *q++ = '+';
25820 +               memcpy(q, attr.a, sz + 1);
25821 +       }
25822 +
25823 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
25824 +}
25825 +
25826 +/* ---------------------------------------------------------------------- */
25827 +
25828 +static match_table_t udbalevel = {
25829 +       {AuOpt_UDBA_REVAL, "reval"},
25830 +       {AuOpt_UDBA_NONE, "none"},
25831 +#ifdef CONFIG_AUFS_HNOTIFY
25832 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25833 +#ifdef CONFIG_AUFS_HFSNOTIFY
25834 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
25835 +#endif
25836 +#endif
25837 +       {-1, NULL}
25838 +};
25839 +
25840 +static int noinline_for_stack udba_val(char *str)
25841 +{
25842 +       substring_t args[MAX_OPT_ARGS];
25843 +
25844 +       return match_token(str, udbalevel, args);
25845 +}
25846 +
25847 +const char *au_optstr_udba(int udba)
25848 +{
25849 +       return au_parser_pattern(udba, udbalevel);
25850 +}
25851 +
25852 +/* ---------------------------------------------------------------------- */
25853 +
25854 +static match_table_t au_wbr_create_policy = {
25855 +       {AuWbrCreate_TDP, "tdp"},
25856 +       {AuWbrCreate_TDP, "top-down-parent"},
25857 +       {AuWbrCreate_RR, "rr"},
25858 +       {AuWbrCreate_RR, "round-robin"},
25859 +       {AuWbrCreate_MFS, "mfs"},
25860 +       {AuWbrCreate_MFS, "most-free-space"},
25861 +       {AuWbrCreate_MFSV, "mfs:%d"},
25862 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
25863 +
25864 +       /* top-down regardless the parent, and then mfs */
25865 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
25866 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25867 +
25868 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
25869 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25870 +       {AuWbrCreate_PMFS, "pmfs"},
25871 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
25872 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25873 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
25874 +
25875 +       {-1, NULL}
25876 +};
25877 +
25878 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25879 +                           struct au_opt_wbr_create *create)
25880 +{
25881 +       int err;
25882 +       unsigned long long ull;
25883 +
25884 +       err = 0;
25885 +       if (!match_u64(arg, &ull))
25886 +               create->mfsrr_watermark = ull;
25887 +       else {
25888 +               pr_err("bad integer in %s\n", str);
25889 +               err = -EINVAL;
25890 +       }
25891 +
25892 +       return err;
25893 +}
25894 +
25895 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
25896 +                         struct au_opt_wbr_create *create)
25897 +{
25898 +       int n, err;
25899 +
25900 +       err = 0;
25901 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
25902 +               create->mfs_second = n;
25903 +       else {
25904 +               pr_err("bad integer in %s\n", str);
25905 +               err = -EINVAL;
25906 +       }
25907 +
25908 +       return err;
25909 +}
25910 +
25911 +static int noinline_for_stack
25912 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
25913 +{
25914 +       int err, e;
25915 +       substring_t args[MAX_OPT_ARGS];
25916 +
25917 +       err = match_token(str, au_wbr_create_policy, args);
25918 +       create->wbr_create = err;
25919 +       switch (err) {
25920 +       case AuWbrCreate_MFSRRV:
25921 +       case AuWbrCreate_TDMFSV:
25922 +       case AuWbrCreate_PMFSRRV:
25923 +               e = au_wbr_mfs_wmark(&args[0], str, create);
25924 +               if (!e)
25925 +                       e = au_wbr_mfs_sec(&args[1], str, create);
25926 +               if (unlikely(e))
25927 +                       err = e;
25928 +               break;
25929 +       case AuWbrCreate_MFSRR:
25930 +       case AuWbrCreate_TDMFS:
25931 +       case AuWbrCreate_PMFSRR:
25932 +               e = au_wbr_mfs_wmark(&args[0], str, create);
25933 +               if (unlikely(e)) {
25934 +                       err = e;
25935 +                       break;
25936 +               }
25937 +               /*FALLTHROUGH*/
25938 +       case AuWbrCreate_MFS:
25939 +       case AuWbrCreate_PMFS:
25940 +               create->mfs_second = AUFS_MFS_DEF_SEC;
25941 +               break;
25942 +       case AuWbrCreate_MFSV:
25943 +       case AuWbrCreate_PMFSV:
25944 +               e = au_wbr_mfs_sec(&args[0], str, create);
25945 +               if (unlikely(e))
25946 +                       err = e;
25947 +               break;
25948 +       }
25949 +
25950 +       return err;
25951 +}
25952 +
25953 +const char *au_optstr_wbr_create(int wbr_create)
25954 +{
25955 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
25956 +}
25957 +
25958 +static match_table_t au_wbr_copyup_policy = {
25959 +       {AuWbrCopyup_TDP, "tdp"},
25960 +       {AuWbrCopyup_TDP, "top-down-parent"},
25961 +       {AuWbrCopyup_BUP, "bup"},
25962 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
25963 +       {AuWbrCopyup_BU, "bu"},
25964 +       {AuWbrCopyup_BU, "bottom-up"},
25965 +       {-1, NULL}
25966 +};
25967 +
25968 +static int noinline_for_stack au_wbr_copyup_val(char *str)
25969 +{
25970 +       substring_t args[MAX_OPT_ARGS];
25971 +
25972 +       return match_token(str, au_wbr_copyup_policy, args);
25973 +}
25974 +
25975 +const char *au_optstr_wbr_copyup(int wbr_copyup)
25976 +{
25977 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
25978 +}
25979 +
25980 +/* ---------------------------------------------------------------------- */
25981 +
25982 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
25983 +
25984 +static void dump_opts(struct au_opts *opts)
25985 +{
25986 +#ifdef CONFIG_AUFS_DEBUG
25987 +       /* reduce stack space */
25988 +       union {
25989 +               struct au_opt_add *add;
25990 +               struct au_opt_del *del;
25991 +               struct au_opt_mod *mod;
25992 +               struct au_opt_xino *xino;
25993 +               struct au_opt_xino_itrunc *xino_itrunc;
25994 +               struct au_opt_wbr_create *create;
25995 +       } u;
25996 +       struct au_opt *opt;
25997 +
25998 +       opt = opts->opt;
25999 +       while (opt->type != Opt_tail) {
26000 +               switch (opt->type) {
26001 +               case Opt_add:
26002 +                       u.add = &opt->add;
26003 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26004 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26005 +                                 u.add->path.dentry);
26006 +                       break;
26007 +               case Opt_del:
26008 +               case Opt_idel:
26009 +                       u.del = &opt->del;
26010 +                       AuDbg("del {%s, %p}\n",
26011 +                             u.del->pathname, u.del->h_path.dentry);
26012 +                       break;
26013 +               case Opt_mod:
26014 +               case Opt_imod:
26015 +                       u.mod = &opt->mod;
26016 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26017 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26018 +                       break;
26019 +               case Opt_append:
26020 +                       u.add = &opt->add;
26021 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26022 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26023 +                                 u.add->path.dentry);
26024 +                       break;
26025 +               case Opt_prepend:
26026 +                       u.add = &opt->add;
26027 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26028 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26029 +                                 u.add->path.dentry);
26030 +                       break;
26031 +               case Opt_dirwh:
26032 +                       AuDbg("dirwh %d\n", opt->dirwh);
26033 +                       break;
26034 +               case Opt_rdcache:
26035 +                       AuDbg("rdcache %d\n", opt->rdcache);
26036 +                       break;
26037 +               case Opt_rdblk:
26038 +                       AuDbg("rdblk %u\n", opt->rdblk);
26039 +                       break;
26040 +               case Opt_rdblk_def:
26041 +                       AuDbg("rdblk_def\n");
26042 +                       break;
26043 +               case Opt_rdhash:
26044 +                       AuDbg("rdhash %u\n", opt->rdhash);
26045 +                       break;
26046 +               case Opt_rdhash_def:
26047 +                       AuDbg("rdhash_def\n");
26048 +                       break;
26049 +               case Opt_xino:
26050 +                       u.xino = &opt->xino;
26051 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26052 +                       break;
26053 +               case Opt_trunc_xino:
26054 +                       AuLabel(trunc_xino);
26055 +                       break;
26056 +               case Opt_notrunc_xino:
26057 +                       AuLabel(notrunc_xino);
26058 +                       break;
26059 +               case Opt_trunc_xino_path:
26060 +               case Opt_itrunc_xino:
26061 +                       u.xino_itrunc = &opt->xino_itrunc;
26062 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26063 +                       break;
26064 +               case Opt_noxino:
26065 +                       AuLabel(noxino);
26066 +                       break;
26067 +               case Opt_trunc_xib:
26068 +                       AuLabel(trunc_xib);
26069 +                       break;
26070 +               case Opt_notrunc_xib:
26071 +                       AuLabel(notrunc_xib);
26072 +                       break;
26073 +               case Opt_shwh:
26074 +                       AuLabel(shwh);
26075 +                       break;
26076 +               case Opt_noshwh:
26077 +                       AuLabel(noshwh);
26078 +                       break;
26079 +               case Opt_dirperm1:
26080 +                       AuLabel(dirperm1);
26081 +                       break;
26082 +               case Opt_nodirperm1:
26083 +                       AuLabel(nodirperm1);
26084 +                       break;
26085 +               case Opt_plink:
26086 +                       AuLabel(plink);
26087 +                       break;
26088 +               case Opt_noplink:
26089 +                       AuLabel(noplink);
26090 +                       break;
26091 +               case Opt_list_plink:
26092 +                       AuLabel(list_plink);
26093 +                       break;
26094 +               case Opt_udba:
26095 +                       AuDbg("udba %d, %s\n",
26096 +                                 opt->udba, au_optstr_udba(opt->udba));
26097 +                       break;
26098 +               case Opt_dio:
26099 +                       AuLabel(dio);
26100 +                       break;
26101 +               case Opt_nodio:
26102 +                       AuLabel(nodio);
26103 +                       break;
26104 +               case Opt_diropq_a:
26105 +                       AuLabel(diropq_a);
26106 +                       break;
26107 +               case Opt_diropq_w:
26108 +                       AuLabel(diropq_w);
26109 +                       break;
26110 +               case Opt_warn_perm:
26111 +                       AuLabel(warn_perm);
26112 +                       break;
26113 +               case Opt_nowarn_perm:
26114 +                       AuLabel(nowarn_perm);
26115 +                       break;
26116 +               case Opt_verbose:
26117 +                       AuLabel(verbose);
26118 +                       break;
26119 +               case Opt_noverbose:
26120 +                       AuLabel(noverbose);
26121 +                       break;
26122 +               case Opt_sum:
26123 +                       AuLabel(sum);
26124 +                       break;
26125 +               case Opt_nosum:
26126 +                       AuLabel(nosum);
26127 +                       break;
26128 +               case Opt_wsum:
26129 +                       AuLabel(wsum);
26130 +                       break;
26131 +               case Opt_wbr_create:
26132 +                       u.create = &opt->wbr_create;
26133 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26134 +                                 au_optstr_wbr_create(u.create->wbr_create));
26135 +                       switch (u.create->wbr_create) {
26136 +                       case AuWbrCreate_MFSV:
26137 +                       case AuWbrCreate_PMFSV:
26138 +                               AuDbg("%d sec\n", u.create->mfs_second);
26139 +                               break;
26140 +                       case AuWbrCreate_MFSRR:
26141 +                       case AuWbrCreate_TDMFS:
26142 +                               AuDbg("%llu watermark\n",
26143 +                                         u.create->mfsrr_watermark);
26144 +                               break;
26145 +                       case AuWbrCreate_MFSRRV:
26146 +                       case AuWbrCreate_TDMFSV:
26147 +                       case AuWbrCreate_PMFSRRV:
26148 +                               AuDbg("%llu watermark, %d sec\n",
26149 +                                         u.create->mfsrr_watermark,
26150 +                                         u.create->mfs_second);
26151 +                               break;
26152 +                       }
26153 +                       break;
26154 +               case Opt_wbr_copyup:
26155 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26156 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26157 +                       break;
26158 +               case Opt_fhsm_sec:
26159 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26160 +                       break;
26161 +               case Opt_dirren:
26162 +                       AuLabel(dirren);
26163 +                       break;
26164 +               case Opt_nodirren:
26165 +                       AuLabel(nodirren);
26166 +                       break;
26167 +               case Opt_acl:
26168 +                       AuLabel(acl);
26169 +                       break;
26170 +               case Opt_noacl:
26171 +                       AuLabel(noacl);
26172 +                       break;
26173 +               default:
26174 +                       BUG();
26175 +               }
26176 +               opt++;
26177 +       }
26178 +#endif
26179 +}
26180 +
26181 +void au_opts_free(struct au_opts *opts)
26182 +{
26183 +       struct au_opt *opt;
26184 +
26185 +       opt = opts->opt;
26186 +       while (opt->type != Opt_tail) {
26187 +               switch (opt->type) {
26188 +               case Opt_add:
26189 +               case Opt_append:
26190 +               case Opt_prepend:
26191 +                       path_put(&opt->add.path);
26192 +                       break;
26193 +               case Opt_del:
26194 +               case Opt_idel:
26195 +                       path_put(&opt->del.h_path);
26196 +                       break;
26197 +               case Opt_mod:
26198 +               case Opt_imod:
26199 +                       dput(opt->mod.h_root);
26200 +                       break;
26201 +               case Opt_xino:
26202 +                       fput(opt->xino.file);
26203 +                       break;
26204 +               }
26205 +               opt++;
26206 +       }
26207 +}
26208 +
26209 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26210 +                  aufs_bindex_t bindex)
26211 +{
26212 +       int err;
26213 +       struct au_opt_add *add = &opt->add;
26214 +       char *p;
26215 +
26216 +       add->bindex = bindex;
26217 +       add->perm = AuBrPerm_RO;
26218 +       add->pathname = opt_str;
26219 +       p = strchr(opt_str, '=');
26220 +       if (p) {
26221 +               *p++ = 0;
26222 +               if (*p)
26223 +                       add->perm = br_perm_val(p);
26224 +       }
26225 +
26226 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26227 +       if (!err) {
26228 +               if (!p) {
26229 +                       add->perm = AuBrPerm_RO;
26230 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26231 +                               add->perm = AuBrPerm_RR;
26232 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26233 +                               add->perm = AuBrPerm_RW;
26234 +               }
26235 +               opt->type = Opt_add;
26236 +               goto out;
26237 +       }
26238 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26239 +       err = -EINVAL;
26240 +
26241 +out:
26242 +       return err;
26243 +}
26244 +
26245 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26246 +{
26247 +       int err;
26248 +
26249 +       del->pathname = args[0].from;
26250 +       AuDbg("del path %s\n", del->pathname);
26251 +
26252 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26253 +       if (unlikely(err))
26254 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26255 +
26256 +       return err;
26257 +}
26258 +
26259 +#if 0 /* reserved for future use */
26260 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26261 +                             struct au_opt_del *del, substring_t args[])
26262 +{
26263 +       int err;
26264 +       struct dentry *root;
26265 +
26266 +       err = -EINVAL;
26267 +       root = sb->s_root;
26268 +       aufs_read_lock(root, AuLock_FLUSH);
26269 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26270 +               pr_err("out of bounds, %d\n", bindex);
26271 +               goto out;
26272 +       }
26273 +
26274 +       err = 0;
26275 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26276 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26277 +
26278 +out:
26279 +       aufs_read_unlock(root, !AuLock_IR);
26280 +       return err;
26281 +}
26282 +#endif
26283 +
26284 +static int noinline_for_stack
26285 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26286 +{
26287 +       int err;
26288 +       struct path path;
26289 +       char *p;
26290 +
26291 +       err = -EINVAL;
26292 +       mod->path = args[0].from;
26293 +       p = strchr(mod->path, '=');
26294 +       if (unlikely(!p)) {
26295 +               pr_err("no permssion %s\n", args[0].from);
26296 +               goto out;
26297 +       }
26298 +
26299 +       *p++ = 0;
26300 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26301 +       if (unlikely(err)) {
26302 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26303 +               goto out;
26304 +       }
26305 +
26306 +       mod->perm = br_perm_val(p);
26307 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26308 +       mod->h_root = dget(path.dentry);
26309 +       path_put(&path);
26310 +
26311 +out:
26312 +       return err;
26313 +}
26314 +
26315 +#if 0 /* reserved for future use */
26316 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26317 +                             struct au_opt_mod *mod, substring_t args[])
26318 +{
26319 +       int err;
26320 +       struct dentry *root;
26321 +
26322 +       err = -EINVAL;
26323 +       root = sb->s_root;
26324 +       aufs_read_lock(root, AuLock_FLUSH);
26325 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26326 +               pr_err("out of bounds, %d\n", bindex);
26327 +               goto out;
26328 +       }
26329 +
26330 +       err = 0;
26331 +       mod->perm = br_perm_val(args[1].from);
26332 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26333 +             mod->path, mod->perm, args[1].from);
26334 +       mod->h_root = dget(au_h_dptr(root, bindex));
26335 +
26336 +out:
26337 +       aufs_read_unlock(root, !AuLock_IR);
26338 +       return err;
26339 +}
26340 +#endif
26341 +
26342 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26343 +                             substring_t args[])
26344 +{
26345 +       int err;
26346 +       struct file *file;
26347 +
26348 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
26349 +       err = PTR_ERR(file);
26350 +       if (IS_ERR(file))
26351 +               goto out;
26352 +
26353 +       err = -EINVAL;
26354 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26355 +               fput(file);
26356 +               pr_err("%s must be outside\n", args[0].from);
26357 +               goto out;
26358 +       }
26359 +
26360 +       err = 0;
26361 +       xino->file = file;
26362 +       xino->path = args[0].from;
26363 +
26364 +out:
26365 +       return err;
26366 +}
26367 +
26368 +static int noinline_for_stack
26369 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26370 +                              struct au_opt_xino_itrunc *xino_itrunc,
26371 +                              substring_t args[])
26372 +{
26373 +       int err;
26374 +       aufs_bindex_t bbot, bindex;
26375 +       struct path path;
26376 +       struct dentry *root;
26377 +
26378 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26379 +       if (unlikely(err)) {
26380 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26381 +               goto out;
26382 +       }
26383 +
26384 +       xino_itrunc->bindex = -1;
26385 +       root = sb->s_root;
26386 +       aufs_read_lock(root, AuLock_FLUSH);
26387 +       bbot = au_sbbot(sb);
26388 +       for (bindex = 0; bindex <= bbot; bindex++) {
26389 +               if (au_h_dptr(root, bindex) == path.dentry) {
26390 +                       xino_itrunc->bindex = bindex;
26391 +                       break;
26392 +               }
26393 +       }
26394 +       aufs_read_unlock(root, !AuLock_IR);
26395 +       path_put(&path);
26396 +
26397 +       if (unlikely(xino_itrunc->bindex < 0)) {
26398 +               pr_err("no such branch %s\n", args[0].from);
26399 +               err = -EINVAL;
26400 +       }
26401 +
26402 +out:
26403 +       return err;
26404 +}
26405 +
26406 +/* called without aufs lock */
26407 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26408 +{
26409 +       int err, n, token;
26410 +       aufs_bindex_t bindex;
26411 +       unsigned char skipped;
26412 +       struct dentry *root;
26413 +       struct au_opt *opt, *opt_tail;
26414 +       char *opt_str;
26415 +       /* reduce the stack space */
26416 +       union {
26417 +               struct au_opt_xino_itrunc *xino_itrunc;
26418 +               struct au_opt_wbr_create *create;
26419 +       } u;
26420 +       struct {
26421 +               substring_t args[MAX_OPT_ARGS];
26422 +       } *a;
26423 +
26424 +       err = -ENOMEM;
26425 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26426 +       if (unlikely(!a))
26427 +               goto out;
26428 +
26429 +       root = sb->s_root;
26430 +       err = 0;
26431 +       bindex = 0;
26432 +       opt = opts->opt;
26433 +       opt_tail = opt + opts->max_opt - 1;
26434 +       opt->type = Opt_tail;
26435 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26436 +               err = -EINVAL;
26437 +               skipped = 0;
26438 +               token = match_token(opt_str, options, a->args);
26439 +               switch (token) {
26440 +               case Opt_br:
26441 +                       err = 0;
26442 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26443 +                              && *opt_str) {
26444 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26445 +                                             bindex++);
26446 +                               if (unlikely(!err && ++opt > opt_tail)) {
26447 +                                       err = -E2BIG;
26448 +                                       break;
26449 +                               }
26450 +                               opt->type = Opt_tail;
26451 +                               skipped = 1;
26452 +                       }
26453 +                       break;
26454 +               case Opt_add:
26455 +                       if (unlikely(match_int(&a->args[0], &n))) {
26456 +                               pr_err("bad integer in %s\n", opt_str);
26457 +                               break;
26458 +                       }
26459 +                       bindex = n;
26460 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26461 +                                     bindex);
26462 +                       if (!err)
26463 +                               opt->type = token;
26464 +                       break;
26465 +               case Opt_append:
26466 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26467 +                                     /*dummy bindex*/1);
26468 +                       if (!err)
26469 +                               opt->type = token;
26470 +                       break;
26471 +               case Opt_prepend:
26472 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26473 +                                     /*bindex*/0);
26474 +                       if (!err)
26475 +                               opt->type = token;
26476 +                       break;
26477 +               case Opt_del:
26478 +                       err = au_opts_parse_del(&opt->del, a->args);
26479 +                       if (!err)
26480 +                               opt->type = token;
26481 +                       break;
26482 +#if 0 /* reserved for future use */
26483 +               case Opt_idel:
26484 +                       del->pathname = "(indexed)";
26485 +                       if (unlikely(match_int(&args[0], &n))) {
26486 +                               pr_err("bad integer in %s\n", opt_str);
26487 +                               break;
26488 +                       }
26489 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26490 +                       if (!err)
26491 +                               opt->type = token;
26492 +                       break;
26493 +#endif
26494 +               case Opt_mod:
26495 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26496 +                       if (!err)
26497 +                               opt->type = token;
26498 +                       break;
26499 +#ifdef IMOD /* reserved for future use */
26500 +               case Opt_imod:
26501 +                       u.mod->path = "(indexed)";
26502 +                       if (unlikely(match_int(&a->args[0], &n))) {
26503 +                               pr_err("bad integer in %s\n", opt_str);
26504 +                               break;
26505 +                       }
26506 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26507 +                       if (!err)
26508 +                               opt->type = token;
26509 +                       break;
26510 +#endif
26511 +               case Opt_xino:
26512 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26513 +                       if (!err)
26514 +                               opt->type = token;
26515 +                       break;
26516 +
26517 +               case Opt_trunc_xino_path:
26518 +                       err = au_opts_parse_xino_itrunc_path
26519 +                               (sb, &opt->xino_itrunc, a->args);
26520 +                       if (!err)
26521 +                               opt->type = token;
26522 +                       break;
26523 +
26524 +               case Opt_itrunc_xino:
26525 +                       u.xino_itrunc = &opt->xino_itrunc;
26526 +                       if (unlikely(match_int(&a->args[0], &n))) {
26527 +                               pr_err("bad integer in %s\n", opt_str);
26528 +                               break;
26529 +                       }
26530 +                       u.xino_itrunc->bindex = n;
26531 +                       aufs_read_lock(root, AuLock_FLUSH);
26532 +                       if (n < 0 || au_sbbot(sb) < n) {
26533 +                               pr_err("out of bounds, %d\n", n);
26534 +                               aufs_read_unlock(root, !AuLock_IR);
26535 +                               break;
26536 +                       }
26537 +                       aufs_read_unlock(root, !AuLock_IR);
26538 +                       err = 0;
26539 +                       opt->type = token;
26540 +                       break;
26541 +
26542 +               case Opt_dirwh:
26543 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26544 +                               break;
26545 +                       err = 0;
26546 +                       opt->type = token;
26547 +                       break;
26548 +
26549 +               case Opt_rdcache:
26550 +                       if (unlikely(match_int(&a->args[0], &n))) {
26551 +                               pr_err("bad integer in %s\n", opt_str);
26552 +                               break;
26553 +                       }
26554 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26555 +                               pr_err("rdcache must be smaller than %d\n",
26556 +                                      AUFS_RDCACHE_MAX);
26557 +                               break;
26558 +                       }
26559 +                       opt->rdcache = n;
26560 +                       err = 0;
26561 +                       opt->type = token;
26562 +                       break;
26563 +               case Opt_rdblk:
26564 +                       if (unlikely(match_int(&a->args[0], &n)
26565 +                                    || n < 0
26566 +                                    || n > KMALLOC_MAX_SIZE)) {
26567 +                               pr_err("bad integer in %s\n", opt_str);
26568 +                               break;
26569 +                       }
26570 +                       if (unlikely(n && n < NAME_MAX)) {
26571 +                               pr_err("rdblk must be larger than %d\n",
26572 +                                      NAME_MAX);
26573 +                               break;
26574 +                       }
26575 +                       opt->rdblk = n;
26576 +                       err = 0;
26577 +                       opt->type = token;
26578 +                       break;
26579 +               case Opt_rdhash:
26580 +                       if (unlikely(match_int(&a->args[0], &n)
26581 +                                    || n < 0
26582 +                                    || n * sizeof(struct hlist_head)
26583 +                                    > KMALLOC_MAX_SIZE)) {
26584 +                               pr_err("bad integer in %s\n", opt_str);
26585 +                               break;
26586 +                       }
26587 +                       opt->rdhash = n;
26588 +                       err = 0;
26589 +                       opt->type = token;
26590 +                       break;
26591 +
26592 +               case Opt_trunc_xino:
26593 +               case Opt_notrunc_xino:
26594 +               case Opt_noxino:
26595 +               case Opt_trunc_xib:
26596 +               case Opt_notrunc_xib:
26597 +               case Opt_shwh:
26598 +               case Opt_noshwh:
26599 +               case Opt_dirperm1:
26600 +               case Opt_nodirperm1:
26601 +               case Opt_plink:
26602 +               case Opt_noplink:
26603 +               case Opt_list_plink:
26604 +               case Opt_dio:
26605 +               case Opt_nodio:
26606 +               case Opt_diropq_a:
26607 +               case Opt_diropq_w:
26608 +               case Opt_warn_perm:
26609 +               case Opt_nowarn_perm:
26610 +               case Opt_verbose:
26611 +               case Opt_noverbose:
26612 +               case Opt_sum:
26613 +               case Opt_nosum:
26614 +               case Opt_wsum:
26615 +               case Opt_rdblk_def:
26616 +               case Opt_rdhash_def:
26617 +               case Opt_dirren:
26618 +               case Opt_nodirren:
26619 +               case Opt_acl:
26620 +               case Opt_noacl:
26621 +                       err = 0;
26622 +                       opt->type = token;
26623 +                       break;
26624 +
26625 +               case Opt_udba:
26626 +                       opt->udba = udba_val(a->args[0].from);
26627 +                       if (opt->udba >= 0) {
26628 +                               err = 0;
26629 +                               opt->type = token;
26630 +                       } else
26631 +                               pr_err("wrong value, %s\n", opt_str);
26632 +                       break;
26633 +
26634 +               case Opt_wbr_create:
26635 +                       u.create = &opt->wbr_create;
26636 +                       u.create->wbr_create
26637 +                               = au_wbr_create_val(a->args[0].from, u.create);
26638 +                       if (u.create->wbr_create >= 0) {
26639 +                               err = 0;
26640 +                               opt->type = token;
26641 +                       } else
26642 +                               pr_err("wrong value, %s\n", opt_str);
26643 +                       break;
26644 +               case Opt_wbr_copyup:
26645 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26646 +                       if (opt->wbr_copyup >= 0) {
26647 +                               err = 0;
26648 +                               opt->type = token;
26649 +                       } else
26650 +                               pr_err("wrong value, %s\n", opt_str);
26651 +                       break;
26652 +
26653 +               case Opt_fhsm_sec:
26654 +                       if (unlikely(match_int(&a->args[0], &n)
26655 +                                    || n < 0)) {
26656 +                               pr_err("bad integer in %s\n", opt_str);
26657 +                               break;
26658 +                       }
26659 +                       if (sysaufs_brs) {
26660 +                               opt->fhsm_second = n;
26661 +                               opt->type = token;
26662 +                       } else
26663 +                               pr_warn("ignored %s\n", opt_str);
26664 +                       err = 0;
26665 +                       break;
26666 +
26667 +               case Opt_ignore:
26668 +                       pr_warn("ignored %s\n", opt_str);
26669 +                       /*FALLTHROUGH*/
26670 +               case Opt_ignore_silent:
26671 +                       skipped = 1;
26672 +                       err = 0;
26673 +                       break;
26674 +               case Opt_err:
26675 +                       pr_err("unknown option %s\n", opt_str);
26676 +                       break;
26677 +               }
26678 +
26679 +               if (!err && !skipped) {
26680 +                       if (unlikely(++opt > opt_tail)) {
26681 +                               err = -E2BIG;
26682 +                               opt--;
26683 +                               opt->type = Opt_tail;
26684 +                               break;
26685 +                       }
26686 +                       opt->type = Opt_tail;
26687 +               }
26688 +       }
26689 +
26690 +       kfree(a);
26691 +       dump_opts(opts);
26692 +       if (unlikely(err))
26693 +               au_opts_free(opts);
26694 +
26695 +out:
26696 +       return err;
26697 +}
26698 +
26699 +static int au_opt_wbr_create(struct super_block *sb,
26700 +                            struct au_opt_wbr_create *create)
26701 +{
26702 +       int err;
26703 +       struct au_sbinfo *sbinfo;
26704 +
26705 +       SiMustWriteLock(sb);
26706 +
26707 +       err = 1; /* handled */
26708 +       sbinfo = au_sbi(sb);
26709 +       if (sbinfo->si_wbr_create_ops->fin) {
26710 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26711 +               if (!err)
26712 +                       err = 1;
26713 +       }
26714 +
26715 +       sbinfo->si_wbr_create = create->wbr_create;
26716 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26717 +       switch (create->wbr_create) {
26718 +       case AuWbrCreate_MFSRRV:
26719 +       case AuWbrCreate_MFSRR:
26720 +       case AuWbrCreate_TDMFS:
26721 +       case AuWbrCreate_TDMFSV:
26722 +       case AuWbrCreate_PMFSRR:
26723 +       case AuWbrCreate_PMFSRRV:
26724 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26725 +               /*FALLTHROUGH*/
26726 +       case AuWbrCreate_MFS:
26727 +       case AuWbrCreate_MFSV:
26728 +       case AuWbrCreate_PMFS:
26729 +       case AuWbrCreate_PMFSV:
26730 +               sbinfo->si_wbr_mfs.mfs_expire
26731 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26732 +               break;
26733 +       }
26734 +
26735 +       if (sbinfo->si_wbr_create_ops->init)
26736 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26737 +
26738 +       return err;
26739 +}
26740 +
26741 +/*
26742 + * returns,
26743 + * plus: processed without an error
26744 + * zero: unprocessed
26745 + */
26746 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26747 +                        struct au_opts *opts)
26748 +{
26749 +       int err;
26750 +       struct au_sbinfo *sbinfo;
26751 +
26752 +       SiMustWriteLock(sb);
26753 +
26754 +       err = 1; /* handled */
26755 +       sbinfo = au_sbi(sb);
26756 +       switch (opt->type) {
26757 +       case Opt_udba:
26758 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26759 +               sbinfo->si_mntflags |= opt->udba;
26760 +               opts->given_udba |= opt->udba;
26761 +               break;
26762 +
26763 +       case Opt_plink:
26764 +               au_opt_set(sbinfo->si_mntflags, PLINK);
26765 +               break;
26766 +       case Opt_noplink:
26767 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26768 +                       au_plink_put(sb, /*verbose*/1);
26769 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
26770 +               break;
26771 +       case Opt_list_plink:
26772 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26773 +                       au_plink_list(sb);
26774 +               break;
26775 +
26776 +       case Opt_dio:
26777 +               au_opt_set(sbinfo->si_mntflags, DIO);
26778 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26779 +               break;
26780 +       case Opt_nodio:
26781 +               au_opt_clr(sbinfo->si_mntflags, DIO);
26782 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26783 +               break;
26784 +
26785 +       case Opt_fhsm_sec:
26786 +               au_fhsm_set(sbinfo, opt->fhsm_second);
26787 +               break;
26788 +
26789 +       case Opt_diropq_a:
26790 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26791 +               break;
26792 +       case Opt_diropq_w:
26793 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26794 +               break;
26795 +
26796 +       case Opt_warn_perm:
26797 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26798 +               break;
26799 +       case Opt_nowarn_perm:
26800 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26801 +               break;
26802 +
26803 +       case Opt_verbose:
26804 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
26805 +               break;
26806 +       case Opt_noverbose:
26807 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26808 +               break;
26809 +
26810 +       case Opt_sum:
26811 +               au_opt_set(sbinfo->si_mntflags, SUM);
26812 +               break;
26813 +       case Opt_wsum:
26814 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26815 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
26816 +       case Opt_nosum:
26817 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26818 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
26819 +               break;
26820 +
26821 +       case Opt_wbr_create:
26822 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
26823 +               break;
26824 +       case Opt_wbr_copyup:
26825 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
26826 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26827 +               break;
26828 +
26829 +       case Opt_dirwh:
26830 +               sbinfo->si_dirwh = opt->dirwh;
26831 +               break;
26832 +
26833 +       case Opt_rdcache:
26834 +               sbinfo->si_rdcache
26835 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
26836 +               break;
26837 +       case Opt_rdblk:
26838 +               sbinfo->si_rdblk = opt->rdblk;
26839 +               break;
26840 +       case Opt_rdblk_def:
26841 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26842 +               break;
26843 +       case Opt_rdhash:
26844 +               sbinfo->si_rdhash = opt->rdhash;
26845 +               break;
26846 +       case Opt_rdhash_def:
26847 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26848 +               break;
26849 +
26850 +       case Opt_shwh:
26851 +               au_opt_set(sbinfo->si_mntflags, SHWH);
26852 +               break;
26853 +       case Opt_noshwh:
26854 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
26855 +               break;
26856 +
26857 +       case Opt_dirperm1:
26858 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26859 +               break;
26860 +       case Opt_nodirperm1:
26861 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26862 +               break;
26863 +
26864 +       case Opt_trunc_xino:
26865 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26866 +               break;
26867 +       case Opt_notrunc_xino:
26868 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26869 +               break;
26870 +
26871 +       case Opt_trunc_xino_path:
26872 +       case Opt_itrunc_xino:
26873 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
26874 +               if (!err)
26875 +                       err = 1;
26876 +               break;
26877 +
26878 +       case Opt_trunc_xib:
26879 +               au_fset_opts(opts->flags, TRUNC_XIB);
26880 +               break;
26881 +       case Opt_notrunc_xib:
26882 +               au_fclr_opts(opts->flags, TRUNC_XIB);
26883 +               break;
26884 +
26885 +       case Opt_dirren:
26886 +               err = 1;
26887 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26888 +                       err = au_dr_opt_set(sb);
26889 +                       if (!err)
26890 +                               err = 1;
26891 +               }
26892 +               if (err == 1)
26893 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
26894 +               break;
26895 +       case Opt_nodirren:
26896 +               err = 1;
26897 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26898 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
26899 +                                                             DR_FLUSHED));
26900 +                       if (!err)
26901 +                               err = 1;
26902 +               }
26903 +               if (err == 1)
26904 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
26905 +               break;
26906 +
26907 +       case Opt_acl:
26908 +               sb->s_flags |= SB_POSIXACL;
26909 +               break;
26910 +       case Opt_noacl:
26911 +               sb->s_flags &= ~SB_POSIXACL;
26912 +               break;
26913 +
26914 +       default:
26915 +               err = 0;
26916 +               break;
26917 +       }
26918 +
26919 +       return err;
26920 +}
26921 +
26922 +/*
26923 + * returns tri-state.
26924 + * plus: processed without an error
26925 + * zero: unprocessed
26926 + * minus: error
26927 + */
26928 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
26929 +                    struct au_opts *opts)
26930 +{
26931 +       int err, do_refresh;
26932 +
26933 +       err = 0;
26934 +       switch (opt->type) {
26935 +       case Opt_append:
26936 +               opt->add.bindex = au_sbbot(sb) + 1;
26937 +               if (opt->add.bindex < 0)
26938 +                       opt->add.bindex = 0;
26939 +               goto add;
26940 +       case Opt_prepend:
26941 +               opt->add.bindex = 0;
26942 +       add: /* indented label */
26943 +       case Opt_add:
26944 +               err = au_br_add(sb, &opt->add,
26945 +                               au_ftest_opts(opts->flags, REMOUNT));
26946 +               if (!err) {
26947 +                       err = 1;
26948 +                       au_fset_opts(opts->flags, REFRESH);
26949 +               }
26950 +               break;
26951 +
26952 +       case Opt_del:
26953 +       case Opt_idel:
26954 +               err = au_br_del(sb, &opt->del,
26955 +                               au_ftest_opts(opts->flags, REMOUNT));
26956 +               if (!err) {
26957 +                       err = 1;
26958 +                       au_fset_opts(opts->flags, TRUNC_XIB);
26959 +                       au_fset_opts(opts->flags, REFRESH);
26960 +               }
26961 +               break;
26962 +
26963 +       case Opt_mod:
26964 +       case Opt_imod:
26965 +               err = au_br_mod(sb, &opt->mod,
26966 +                               au_ftest_opts(opts->flags, REMOUNT),
26967 +                               &do_refresh);
26968 +               if (!err) {
26969 +                       err = 1;
26970 +                       if (do_refresh)
26971 +                               au_fset_opts(opts->flags, REFRESH);
26972 +               }
26973 +               break;
26974 +       }
26975 +       return err;
26976 +}
26977 +
26978 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
26979 +                      struct au_opt_xino **opt_xino,
26980 +                      struct au_opts *opts)
26981 +{
26982 +       int err;
26983 +       aufs_bindex_t bbot, bindex;
26984 +       struct dentry *root, *parent, *h_root;
26985 +
26986 +       err = 0;
26987 +       switch (opt->type) {
26988 +       case Opt_xino:
26989 +               au_xino_brid_set(sb, -1);
26990 +               /* safe d_parent access */
26991 +               parent = opt->xino.file->f_path.dentry->d_parent;
26992 +               root = sb->s_root;
26993 +               bbot = au_sbbot(sb);
26994 +               for (bindex = 0; bindex <= bbot; bindex++) {
26995 +                       h_root = au_h_dptr(root, bindex);
26996 +                       if (h_root == parent) {
26997 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
26998 +                               break;
26999 +                       }
27000 +               }
27001 +
27002 +               err = au_xino_set(sb, &opt->xino,
27003 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27004 +               if (unlikely(err))
27005 +                       break;
27006 +
27007 +               *opt_xino = &opt->xino;
27008 +               break;
27009 +
27010 +       case Opt_noxino:
27011 +               au_xino_clr(sb);
27012 +               *opt_xino = (void *)-1;
27013 +               break;
27014 +       }
27015 +
27016 +       return err;
27017 +}
27018 +
27019 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27020 +                  unsigned int pending)
27021 +{
27022 +       int err, fhsm;
27023 +       aufs_bindex_t bindex, bbot;
27024 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27025 +       struct au_branch *br;
27026 +       struct au_wbr *wbr;
27027 +       struct dentry *root, *dentry;
27028 +       struct inode *dir, *h_dir;
27029 +       struct au_sbinfo *sbinfo;
27030 +       struct au_hinode *hdir;
27031 +
27032 +       SiMustAnyLock(sb);
27033 +
27034 +       sbinfo = au_sbi(sb);
27035 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27036 +
27037 +       if (!(sb_flags & SB_RDONLY)) {
27038 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27039 +                       pr_warn("first branch should be rw\n");
27040 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27041 +                       pr_warn_once("shwh should be used with ro\n");
27042 +       }
27043 +
27044 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27045 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27046 +               pr_warn_once("udba=*notify requires xino\n");
27047 +
27048 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27049 +               pr_warn_once("dirperm1 breaks the protection"
27050 +                            " by the permission bits on the lower branch\n");
27051 +
27052 +       err = 0;
27053 +       fhsm = 0;
27054 +       root = sb->s_root;
27055 +       dir = d_inode(root);
27056 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27057 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27058 +                                     UDBA_NONE);
27059 +       bbot = au_sbbot(sb);
27060 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27061 +               skip = 0;
27062 +               h_dir = au_h_iptr(dir, bindex);
27063 +               br = au_sbr(sb, bindex);
27064 +
27065 +               if ((br->br_perm & AuBrAttr_ICEX)
27066 +                   && !h_dir->i_op->listxattr)
27067 +                       br->br_perm &= ~AuBrAttr_ICEX;
27068 +#if 0
27069 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27070 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27071 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27072 +#endif
27073 +
27074 +               do_free = 0;
27075 +               wbr = br->br_wbr;
27076 +               if (wbr)
27077 +                       wbr_wh_read_lock(wbr);
27078 +
27079 +               if (!au_br_writable(br->br_perm)) {
27080 +                       do_free = !!wbr;
27081 +                       skip = (!wbr
27082 +                               || (!wbr->wbr_whbase
27083 +                                   && !wbr->wbr_plink
27084 +                                   && !wbr->wbr_orph));
27085 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27086 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27087 +                       skip = (!wbr || !wbr->wbr_whbase);
27088 +                       if (skip && wbr) {
27089 +                               if (do_plink)
27090 +                                       skip = !!wbr->wbr_plink;
27091 +                               else
27092 +                                       skip = !wbr->wbr_plink;
27093 +                       }
27094 +               } else {
27095 +                       /* skip = (br->br_whbase && br->br_ohph); */
27096 +                       skip = (wbr && wbr->wbr_whbase);
27097 +                       if (skip) {
27098 +                               if (do_plink)
27099 +                                       skip = !!wbr->wbr_plink;
27100 +                               else
27101 +                                       skip = !wbr->wbr_plink;
27102 +                       }
27103 +               }
27104 +               if (wbr)
27105 +                       wbr_wh_read_unlock(wbr);
27106 +
27107 +               if (can_no_dreval) {
27108 +                       dentry = br->br_path.dentry;
27109 +                       spin_lock(&dentry->d_lock);
27110 +                       if (dentry->d_flags &
27111 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27112 +                               can_no_dreval = 0;
27113 +                       spin_unlock(&dentry->d_lock);
27114 +               }
27115 +
27116 +               if (au_br_fhsm(br->br_perm)) {
27117 +                       fhsm++;
27118 +                       AuDebugOn(!br->br_fhsm);
27119 +               }
27120 +
27121 +               if (skip)
27122 +                       continue;
27123 +
27124 +               hdir = au_hi(dir, bindex);
27125 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27126 +               if (wbr)
27127 +                       wbr_wh_write_lock(wbr);
27128 +               err = au_wh_init(br, sb);
27129 +               if (wbr)
27130 +                       wbr_wh_write_unlock(wbr);
27131 +               au_hn_inode_unlock(hdir);
27132 +
27133 +               if (!err && do_free) {
27134 +                       kfree(wbr);
27135 +                       br->br_wbr = NULL;
27136 +               }
27137 +       }
27138 +
27139 +       if (can_no_dreval)
27140 +               au_fset_si(sbinfo, NO_DREVAL);
27141 +       else
27142 +               au_fclr_si(sbinfo, NO_DREVAL);
27143 +
27144 +       if (fhsm >= 2) {
27145 +               au_fset_si(sbinfo, FHSM);
27146 +               for (bindex = bbot; bindex >= 0; bindex--) {
27147 +                       br = au_sbr(sb, bindex);
27148 +                       if (au_br_fhsm(br->br_perm)) {
27149 +                               au_fhsm_set_bottom(sb, bindex);
27150 +                               break;
27151 +                       }
27152 +               }
27153 +       } else {
27154 +               au_fclr_si(sbinfo, FHSM);
27155 +               au_fhsm_set_bottom(sb, -1);
27156 +       }
27157 +
27158 +       return err;
27159 +}
27160 +
27161 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27162 +{
27163 +       int err;
27164 +       unsigned int tmp;
27165 +       aufs_bindex_t bindex, bbot;
27166 +       struct au_opt *opt;
27167 +       struct au_opt_xino *opt_xino, xino;
27168 +       struct au_sbinfo *sbinfo;
27169 +       struct au_branch *br;
27170 +       struct inode *dir;
27171 +
27172 +       SiMustWriteLock(sb);
27173 +
27174 +       err = 0;
27175 +       opt_xino = NULL;
27176 +       opt = opts->opt;
27177 +       while (err >= 0 && opt->type != Opt_tail)
27178 +               err = au_opt_simple(sb, opt++, opts);
27179 +       if (err > 0)
27180 +               err = 0;
27181 +       else if (unlikely(err < 0))
27182 +               goto out;
27183 +
27184 +       /* disable xino and udba temporary */
27185 +       sbinfo = au_sbi(sb);
27186 +       tmp = sbinfo->si_mntflags;
27187 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27188 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27189 +
27190 +       opt = opts->opt;
27191 +       while (err >= 0 && opt->type != Opt_tail)
27192 +               err = au_opt_br(sb, opt++, opts);
27193 +       if (err > 0)
27194 +               err = 0;
27195 +       else if (unlikely(err < 0))
27196 +               goto out;
27197 +
27198 +       bbot = au_sbbot(sb);
27199 +       if (unlikely(bbot < 0)) {
27200 +               err = -EINVAL;
27201 +               pr_err("no branches\n");
27202 +               goto out;
27203 +       }
27204 +
27205 +       if (au_opt_test(tmp, XINO))
27206 +               au_opt_set(sbinfo->si_mntflags, XINO);
27207 +       opt = opts->opt;
27208 +       while (!err && opt->type != Opt_tail)
27209 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27210 +       if (unlikely(err))
27211 +               goto out;
27212 +
27213 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27214 +       if (unlikely(err))
27215 +               goto out;
27216 +
27217 +       /* restore xino */
27218 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27219 +               xino.file = au_xino_def(sb);
27220 +               err = PTR_ERR(xino.file);
27221 +               if (IS_ERR(xino.file))
27222 +                       goto out;
27223 +
27224 +               err = au_xino_set(sb, &xino, /*remount*/0);
27225 +               fput(xino.file);
27226 +               if (unlikely(err))
27227 +                       goto out;
27228 +       }
27229 +
27230 +       /* restore udba */
27231 +       tmp &= AuOptMask_UDBA;
27232 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27233 +       sbinfo->si_mntflags |= tmp;
27234 +       bbot = au_sbbot(sb);
27235 +       for (bindex = 0; bindex <= bbot; bindex++) {
27236 +               br = au_sbr(sb, bindex);
27237 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27238 +               if (unlikely(err))
27239 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27240 +                               bindex, err);
27241 +               /* go on even if err */
27242 +       }
27243 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27244 +               dir = d_inode(sb->s_root);
27245 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27246 +       }
27247 +
27248 +out:
27249 +       return err;
27250 +}
27251 +
27252 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27253 +{
27254 +       int err, rerr;
27255 +       unsigned char no_dreval;
27256 +       struct inode *dir;
27257 +       struct au_opt_xino *opt_xino;
27258 +       struct au_opt *opt;
27259 +       struct au_sbinfo *sbinfo;
27260 +
27261 +       SiMustWriteLock(sb);
27262 +
27263 +       err = au_dr_opt_flush(sb);
27264 +       if (unlikely(err))
27265 +               goto out;
27266 +       au_fset_opts(opts->flags, DR_FLUSHED);
27267 +
27268 +       dir = d_inode(sb->s_root);
27269 +       sbinfo = au_sbi(sb);
27270 +       opt_xino = NULL;
27271 +       opt = opts->opt;
27272 +       while (err >= 0 && opt->type != Opt_tail) {
27273 +               err = au_opt_simple(sb, opt, opts);
27274 +               if (!err)
27275 +                       err = au_opt_br(sb, opt, opts);
27276 +               if (!err)
27277 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27278 +               opt++;
27279 +       }
27280 +       if (err > 0)
27281 +               err = 0;
27282 +       AuTraceErr(err);
27283 +       /* go on even err */
27284 +
27285 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27286 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27287 +       if (unlikely(rerr && !err))
27288 +               err = rerr;
27289 +
27290 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27291 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27292 +
27293 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27294 +               rerr = au_xib_trunc(sb);
27295 +               if (unlikely(rerr && !err))
27296 +                       err = rerr;
27297 +       }
27298 +
27299 +       /* will be handled by the caller */
27300 +       if (!au_ftest_opts(opts->flags, REFRESH)
27301 +           && (opts->given_udba
27302 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27303 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27304 +                   ))
27305 +               au_fset_opts(opts->flags, REFRESH);
27306 +
27307 +       AuDbg("status 0x%x\n", opts->flags);
27308 +
27309 +out:
27310 +       return err;
27311 +}
27312 +
27313 +/* ---------------------------------------------------------------------- */
27314 +
27315 +unsigned int au_opt_udba(struct super_block *sb)
27316 +{
27317 +       return au_mntflags(sb) & AuOptMask_UDBA;
27318 +}
27319 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27320 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27321 +++ linux/fs/aufs/opts.h        2018-08-12 23:43:05.460124736 +0200
27322 @@ -0,0 +1,225 @@
27323 +/* SPDX-License-Identifier: GPL-2.0 */
27324 +/*
27325 + * Copyright (C) 2005-2018 Junjiro R. Okajima
27326 + *
27327 + * This program, aufs is free software; you can redistribute it and/or modify
27328 + * it under the terms of the GNU General Public License as published by
27329 + * the Free Software Foundation; either version 2 of the License, or
27330 + * (at your option) any later version.
27331 + *
27332 + * This program is distributed in the hope that it will be useful,
27333 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27334 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27335 + * GNU General Public License for more details.
27336 + *
27337 + * You should have received a copy of the GNU General Public License
27338 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27339 + */
27340 +
27341 +/*
27342 + * mount options/flags
27343 + */
27344 +
27345 +#ifndef __AUFS_OPTS_H__
27346 +#define __AUFS_OPTS_H__
27347 +
27348 +#ifdef __KERNEL__
27349 +
27350 +#include <linux/path.h>
27351 +
27352 +struct file;
27353 +
27354 +/* ---------------------------------------------------------------------- */
27355 +
27356 +/* mount flags */
27357 +#define AuOpt_XINO             1               /* external inode number bitmap
27358 +                                                  and translation table */
27359 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27360 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27361 +#define AuOpt_UDBA_REVAL       (1 << 3)
27362 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27363 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27364 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27365 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27366 +                                                  bits */
27367 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27368 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27369 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27370 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27371 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
27372 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27373 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27374 +
27375 +#ifndef CONFIG_AUFS_HNOTIFY
27376 +#undef AuOpt_UDBA_HNOTIFY
27377 +#define AuOpt_UDBA_HNOTIFY     0
27378 +#endif
27379 +#ifndef CONFIG_AUFS_DIRREN
27380 +#undef AuOpt_DIRREN
27381 +#define AuOpt_DIRREN           0
27382 +#endif
27383 +#ifndef CONFIG_AUFS_SHWH
27384 +#undef AuOpt_SHWH
27385 +#define AuOpt_SHWH             0
27386 +#endif
27387 +
27388 +#define AuOpt_Def      (AuOpt_XINO \
27389 +                        | AuOpt_UDBA_REVAL \
27390 +                        | AuOpt_PLINK \
27391 +                        /* | AuOpt_DIRPERM1 */ \
27392 +                        | AuOpt_WARN_PERM)
27393 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27394 +                        | AuOpt_UDBA_REVAL \
27395 +                        | AuOpt_UDBA_HNOTIFY)
27396 +
27397 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27398 +#define au_opt_set(flags, name) do { \
27399 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27400 +       ((flags) |= AuOpt_##name); \
27401 +} while (0)
27402 +#define au_opt_set_udba(flags, name) do { \
27403 +       (flags) &= ~AuOptMask_UDBA; \
27404 +       ((flags) |= AuOpt_##name); \
27405 +} while (0)
27406 +#define au_opt_clr(flags, name) do { \
27407 +       ((flags) &= ~AuOpt_##name); \
27408 +} while (0)
27409 +
27410 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27411 +{
27412 +#ifdef CONFIG_PROC_FS
27413 +       return mntflags;
27414 +#else
27415 +       return mntflags & ~AuOpt_PLINK;
27416 +#endif
27417 +}
27418 +
27419 +/* ---------------------------------------------------------------------- */
27420 +
27421 +/* policies to select one among multiple writable branches */
27422 +enum {
27423 +       AuWbrCreate_TDP,        /* top down parent */
27424 +       AuWbrCreate_RR,         /* round robin */
27425 +       AuWbrCreate_MFS,        /* most free space */
27426 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27427 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27428 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27429 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27430 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27431 +       AuWbrCreate_PMFS,       /* parent and mfs */
27432 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27433 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27434 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27435 +
27436 +       AuWbrCreate_Def = AuWbrCreate_TDP
27437 +};
27438 +
27439 +enum {
27440 +       AuWbrCopyup_TDP,        /* top down parent */
27441 +       AuWbrCopyup_BUP,        /* bottom up parent */
27442 +       AuWbrCopyup_BU,         /* bottom up */
27443 +
27444 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27445 +};
27446 +
27447 +/* ---------------------------------------------------------------------- */
27448 +
27449 +struct au_opt_add {
27450 +       aufs_bindex_t   bindex;
27451 +       char            *pathname;
27452 +       int             perm;
27453 +       struct path     path;
27454 +};
27455 +
27456 +struct au_opt_del {
27457 +       char            *pathname;
27458 +       struct path     h_path;
27459 +};
27460 +
27461 +struct au_opt_mod {
27462 +       char            *path;
27463 +       int             perm;
27464 +       struct dentry   *h_root;
27465 +};
27466 +
27467 +struct au_opt_xino {
27468 +       char            *path;
27469 +       struct file     *file;
27470 +};
27471 +
27472 +struct au_opt_xino_itrunc {
27473 +       aufs_bindex_t   bindex;
27474 +};
27475 +
27476 +struct au_opt_wbr_create {
27477 +       int                     wbr_create;
27478 +       int                     mfs_second;
27479 +       unsigned long long      mfsrr_watermark;
27480 +};
27481 +
27482 +struct au_opt {
27483 +       int type;
27484 +       union {
27485 +               struct au_opt_xino      xino;
27486 +               struct au_opt_xino_itrunc xino_itrunc;
27487 +               struct au_opt_add       add;
27488 +               struct au_opt_del       del;
27489 +               struct au_opt_mod       mod;
27490 +               int                     dirwh;
27491 +               int                     rdcache;
27492 +               unsigned int            rdblk;
27493 +               unsigned int            rdhash;
27494 +               int                     udba;
27495 +               struct au_opt_wbr_create wbr_create;
27496 +               int                     wbr_copyup;
27497 +               unsigned int            fhsm_second;
27498 +       };
27499 +};
27500 +
27501 +/* opts flags */
27502 +#define AuOpts_REMOUNT         1
27503 +#define AuOpts_REFRESH         (1 << 1)
27504 +#define AuOpts_TRUNC_XIB       (1 << 2)
27505 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27506 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27507 +#define AuOpts_DR_FLUSHED      (1 << 5)
27508 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27509 +#define au_fset_opts(flags, name) \
27510 +       do { (flags) |= AuOpts_##name; } while (0)
27511 +#define au_fclr_opts(flags, name) \
27512 +       do { (flags) &= ~AuOpts_##name; } while (0)
27513 +
27514 +#ifndef CONFIG_AUFS_DIRREN
27515 +#undef AuOpts_DR_FLUSHED
27516 +#define AuOpts_DR_FLUSHED      0
27517 +#endif
27518 +
27519 +struct au_opts {
27520 +       struct au_opt   *opt;
27521 +       int             max_opt;
27522 +
27523 +       unsigned int    given_udba;
27524 +       unsigned int    flags;
27525 +       unsigned long   sb_flags;
27526 +};
27527 +
27528 +/* ---------------------------------------------------------------------- */
27529 +
27530 +/* opts.c */
27531 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27532 +const char *au_optstr_udba(int udba);
27533 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27534 +const char *au_optstr_wbr_create(int wbr_create);
27535 +
27536 +void au_opts_free(struct au_opts *opts);
27537 +struct super_block;
27538 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27539 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27540 +                  unsigned int pending);
27541 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27542 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27543 +
27544 +unsigned int au_opt_udba(struct super_block *sb);
27545 +
27546 +#endif /* __KERNEL__ */
27547 +#endif /* __AUFS_OPTS_H__ */
27548 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27549 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27550 +++ linux/fs/aufs/plink.c       2018-08-12 23:43:05.460124736 +0200
27551 @@ -0,0 +1,516 @@
27552 +// SPDX-License-Identifier: GPL-2.0
27553 +/*
27554 + * Copyright (C) 2005-2018 Junjiro R. Okajima
27555 + *
27556 + * This program, aufs is free software; you can redistribute it and/or modify
27557 + * it under the terms of the GNU General Public License as published by
27558 + * the Free Software Foundation; either version 2 of the License, or
27559 + * (at your option) any later version.
27560 + *
27561 + * This program is distributed in the hope that it will be useful,
27562 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27563 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27564 + * GNU General Public License for more details.
27565 + *
27566 + * You should have received a copy of the GNU General Public License
27567 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27568 + */
27569 +
27570 +/*
27571 + * pseudo-link
27572 + */
27573 +
27574 +#include "aufs.h"
27575 +
27576 +/*
27577 + * the pseudo-link maintenance mode.
27578 + * during a user process maintains the pseudo-links,
27579 + * prohibit adding a new plink and branch manipulation.
27580 + *
27581 + * Flags
27582 + * NOPLM:
27583 + *     For entry functions which will handle plink, and i_mutex is already held
27584 + *     in VFS.
27585 + *     They cannot wait and should return an error at once.
27586 + *     Callers has to check the error.
27587 + * NOPLMW:
27588 + *     For entry functions which will handle plink, but i_mutex is not held
27589 + *     in VFS.
27590 + *     They can wait the plink maintenance mode to finish.
27591 + *
27592 + * They behave like F_SETLK and F_SETLKW.
27593 + * If the caller never handle plink, then both flags are unnecessary.
27594 + */
27595 +
27596 +int au_plink_maint(struct super_block *sb, int flags)
27597 +{
27598 +       int err;
27599 +       pid_t pid, ppid;
27600 +       struct task_struct *parent, *prev;
27601 +       struct au_sbinfo *sbi;
27602 +
27603 +       SiMustAnyLock(sb);
27604 +
27605 +       err = 0;
27606 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27607 +               goto out;
27608 +
27609 +       sbi = au_sbi(sb);
27610 +       pid = sbi->si_plink_maint_pid;
27611 +       if (!pid || pid == current->pid)
27612 +               goto out;
27613 +
27614 +       /* todo: it highly depends upon /sbin/mount.aufs */
27615 +       prev = NULL;
27616 +       parent = current;
27617 +       ppid = 0;
27618 +       rcu_read_lock();
27619 +       while (1) {
27620 +               parent = rcu_dereference(parent->real_parent);
27621 +               if (parent == prev)
27622 +                       break;
27623 +               ppid = task_pid_vnr(parent);
27624 +               if (pid == ppid) {
27625 +                       rcu_read_unlock();
27626 +                       goto out;
27627 +               }
27628 +               prev = parent;
27629 +       }
27630 +       rcu_read_unlock();
27631 +
27632 +       if (au_ftest_lock(flags, NOPLMW)) {
27633 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27634 +               /* AuDebugOn(!lockdep_depth(current)); */
27635 +               while (sbi->si_plink_maint_pid) {
27636 +                       si_read_unlock(sb);
27637 +                       /* gave up wake_up_bit() */
27638 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27639 +
27640 +                       if (au_ftest_lock(flags, FLUSH))
27641 +                               au_nwt_flush(&sbi->si_nowait);
27642 +                       si_noflush_read_lock(sb);
27643 +               }
27644 +       } else if (au_ftest_lock(flags, NOPLM)) {
27645 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27646 +               err = -EAGAIN;
27647 +       }
27648 +
27649 +out:
27650 +       return err;
27651 +}
27652 +
27653 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27654 +{
27655 +       spin_lock(&sbinfo->si_plink_maint_lock);
27656 +       sbinfo->si_plink_maint_pid = 0;
27657 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27658 +       wake_up_all(&sbinfo->si_plink_wq);
27659 +}
27660 +
27661 +int au_plink_maint_enter(struct super_block *sb)
27662 +{
27663 +       int err;
27664 +       struct au_sbinfo *sbinfo;
27665 +
27666 +       err = 0;
27667 +       sbinfo = au_sbi(sb);
27668 +       /* make sure i am the only one in this fs */
27669 +       si_write_lock(sb, AuLock_FLUSH);
27670 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27671 +               spin_lock(&sbinfo->si_plink_maint_lock);
27672 +               if (!sbinfo->si_plink_maint_pid)
27673 +                       sbinfo->si_plink_maint_pid = current->pid;
27674 +               else
27675 +                       err = -EBUSY;
27676 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27677 +       }
27678 +       si_write_unlock(sb);
27679 +
27680 +       return err;
27681 +}
27682 +
27683 +/* ---------------------------------------------------------------------- */
27684 +
27685 +#ifdef CONFIG_AUFS_DEBUG
27686 +void au_plink_list(struct super_block *sb)
27687 +{
27688 +       int i;
27689 +       struct au_sbinfo *sbinfo;
27690 +       struct hlist_bl_head *hbl;
27691 +       struct hlist_bl_node *pos;
27692 +       struct au_icntnr *icntnr;
27693 +
27694 +       SiMustAnyLock(sb);
27695 +
27696 +       sbinfo = au_sbi(sb);
27697 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27698 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27699 +
27700 +       for (i = 0; i < AuPlink_NHASH; i++) {
27701 +               hbl = sbinfo->si_plink + i;
27702 +               hlist_bl_lock(hbl);
27703 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27704 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27705 +               hlist_bl_unlock(hbl);
27706 +       }
27707 +}
27708 +#endif
27709 +
27710 +/* is the inode pseudo-linked? */
27711 +int au_plink_test(struct inode *inode)
27712 +{
27713 +       int found, i;
27714 +       struct au_sbinfo *sbinfo;
27715 +       struct hlist_bl_head *hbl;
27716 +       struct hlist_bl_node *pos;
27717 +       struct au_icntnr *icntnr;
27718 +
27719 +       sbinfo = au_sbi(inode->i_sb);
27720 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27721 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27722 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27723 +
27724 +       found = 0;
27725 +       i = au_plink_hash(inode->i_ino);
27726 +       hbl =  sbinfo->si_plink + i;
27727 +       hlist_bl_lock(hbl);
27728 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27729 +               if (&icntnr->vfs_inode == inode) {
27730 +                       found = 1;
27731 +                       break;
27732 +               }
27733 +       hlist_bl_unlock(hbl);
27734 +       return found;
27735 +}
27736 +
27737 +/* ---------------------------------------------------------------------- */
27738 +
27739 +/*
27740 + * generate a name for plink.
27741 + * the file will be stored under AUFS_WH_PLINKDIR.
27742 + */
27743 +/* 20 is max digits length of ulong 64 */
27744 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27745 +
27746 +static int plink_name(char *name, int len, struct inode *inode,
27747 +                     aufs_bindex_t bindex)
27748 +{
27749 +       int rlen;
27750 +       struct inode *h_inode;
27751 +
27752 +       h_inode = au_h_iptr(inode, bindex);
27753 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27754 +       return rlen;
27755 +}
27756 +
27757 +struct au_do_plink_lkup_args {
27758 +       struct dentry **errp;
27759 +       struct qstr *tgtname;
27760 +       struct dentry *h_parent;
27761 +       struct au_branch *br;
27762 +};
27763 +
27764 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27765 +                                      struct dentry *h_parent,
27766 +                                      struct au_branch *br)
27767 +{
27768 +       struct dentry *h_dentry;
27769 +       struct inode *h_inode;
27770 +
27771 +       h_inode = d_inode(h_parent);
27772 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27773 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
27774 +       inode_unlock_shared(h_inode);
27775 +       return h_dentry;
27776 +}
27777 +
27778 +static void au_call_do_plink_lkup(void *args)
27779 +{
27780 +       struct au_do_plink_lkup_args *a = args;
27781 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27782 +}
27783 +
27784 +/* lookup the plink-ed @inode under the branch at @bindex */
27785 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27786 +{
27787 +       struct dentry *h_dentry, *h_parent;
27788 +       struct au_branch *br;
27789 +       int wkq_err;
27790 +       char a[PLINK_NAME_LEN];
27791 +       struct qstr tgtname = QSTR_INIT(a, 0);
27792 +
27793 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27794 +
27795 +       br = au_sbr(inode->i_sb, bindex);
27796 +       h_parent = br->br_wbr->wbr_plink;
27797 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27798 +
27799 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27800 +               struct au_do_plink_lkup_args args = {
27801 +                       .errp           = &h_dentry,
27802 +                       .tgtname        = &tgtname,
27803 +                       .h_parent       = h_parent,
27804 +                       .br             = br
27805 +               };
27806 +
27807 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27808 +               if (unlikely(wkq_err))
27809 +                       h_dentry = ERR_PTR(wkq_err);
27810 +       } else
27811 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27812 +
27813 +       return h_dentry;
27814 +}
27815 +
27816 +/* create a pseudo-link */
27817 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27818 +                     struct dentry *h_dentry, struct au_branch *br)
27819 +{
27820 +       int err;
27821 +       struct path h_path = {
27822 +               .mnt = au_br_mnt(br)
27823 +       };
27824 +       struct inode *h_dir, *delegated;
27825 +
27826 +       h_dir = d_inode(h_parent);
27827 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27828 +again:
27829 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
27830 +       err = PTR_ERR(h_path.dentry);
27831 +       if (IS_ERR(h_path.dentry))
27832 +               goto out;
27833 +
27834 +       err = 0;
27835 +       /* wh.plink dir is not monitored */
27836 +       /* todo: is it really safe? */
27837 +       if (d_is_positive(h_path.dentry)
27838 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
27839 +               delegated = NULL;
27840 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27841 +               if (unlikely(err == -EWOULDBLOCK)) {
27842 +                       pr_warn("cannot retry for NFSv4 delegation"
27843 +                               " for an internal unlink\n");
27844 +                       iput(delegated);
27845 +               }
27846 +               dput(h_path.dentry);
27847 +               h_path.dentry = NULL;
27848 +               if (!err)
27849 +                       goto again;
27850 +       }
27851 +       if (!err && d_is_negative(h_path.dentry)) {
27852 +               delegated = NULL;
27853 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27854 +               if (unlikely(err == -EWOULDBLOCK)) {
27855 +                       pr_warn("cannot retry for NFSv4 delegation"
27856 +                               " for an internal link\n");
27857 +                       iput(delegated);
27858 +               }
27859 +       }
27860 +       dput(h_path.dentry);
27861 +
27862 +out:
27863 +       inode_unlock(h_dir);
27864 +       return err;
27865 +}
27866 +
27867 +struct do_whplink_args {
27868 +       int *errp;
27869 +       struct qstr *tgt;
27870 +       struct dentry *h_parent;
27871 +       struct dentry *h_dentry;
27872 +       struct au_branch *br;
27873 +};
27874 +
27875 +static void call_do_whplink(void *args)
27876 +{
27877 +       struct do_whplink_args *a = args;
27878 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27879 +}
27880 +
27881 +static int whplink(struct dentry *h_dentry, struct inode *inode,
27882 +                  aufs_bindex_t bindex, struct au_branch *br)
27883 +{
27884 +       int err, wkq_err;
27885 +       struct au_wbr *wbr;
27886 +       struct dentry *h_parent;
27887 +       char a[PLINK_NAME_LEN];
27888 +       struct qstr tgtname = QSTR_INIT(a, 0);
27889 +
27890 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27891 +       h_parent = wbr->wbr_plink;
27892 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27893 +
27894 +       /* always superio. */
27895 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27896 +               struct do_whplink_args args = {
27897 +                       .errp           = &err,
27898 +                       .tgt            = &tgtname,
27899 +                       .h_parent       = h_parent,
27900 +                       .h_dentry       = h_dentry,
27901 +                       .br             = br
27902 +               };
27903 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
27904 +               if (unlikely(wkq_err))
27905 +                       err = wkq_err;
27906 +       } else
27907 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
27908 +
27909 +       return err;
27910 +}
27911 +
27912 +/*
27913 + * create a new pseudo-link for @h_dentry on @bindex.
27914 + * the linked inode is held in aufs @inode.
27915 + */
27916 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
27917 +                    struct dentry *h_dentry)
27918 +{
27919 +       struct super_block *sb;
27920 +       struct au_sbinfo *sbinfo;
27921 +       struct hlist_bl_head *hbl;
27922 +       struct hlist_bl_node *pos;
27923 +       struct au_icntnr *icntnr;
27924 +       int found, err, cnt, i;
27925 +
27926 +       sb = inode->i_sb;
27927 +       sbinfo = au_sbi(sb);
27928 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27929 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27930 +
27931 +       found = au_plink_test(inode);
27932 +       if (found)
27933 +               return;
27934 +
27935 +       i = au_plink_hash(inode->i_ino);
27936 +       hbl = sbinfo->si_plink + i;
27937 +       au_igrab(inode);
27938 +
27939 +       hlist_bl_lock(hbl);
27940 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
27941 +               if (&icntnr->vfs_inode == inode) {
27942 +                       found = 1;
27943 +                       break;
27944 +               }
27945 +       }
27946 +       if (!found) {
27947 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
27948 +               hlist_bl_add_head(&icntnr->plink, hbl);
27949 +       }
27950 +       hlist_bl_unlock(hbl);
27951 +       if (!found) {
27952 +               cnt = au_hbl_count(hbl);
27953 +#define msg "unexpectedly unblanced or too many pseudo-links"
27954 +               if (cnt > AUFS_PLINK_WARN)
27955 +                       AuWarn1(msg ", %d\n", cnt);
27956 +#undef msg
27957 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
27958 +               if (unlikely(err)) {
27959 +                       pr_warn("err %d, damaged pseudo link.\n", err);
27960 +                       au_hbl_del(&icntnr->plink, hbl);
27961 +                       iput(&icntnr->vfs_inode);
27962 +               }
27963 +       } else
27964 +               iput(&icntnr->vfs_inode);
27965 +}
27966 +
27967 +/* free all plinks */
27968 +void au_plink_put(struct super_block *sb, int verbose)
27969 +{
27970 +       int i, warned;
27971 +       struct au_sbinfo *sbinfo;
27972 +       struct hlist_bl_head *hbl;
27973 +       struct hlist_bl_node *pos, *tmp;
27974 +       struct au_icntnr *icntnr;
27975 +
27976 +       SiMustWriteLock(sb);
27977 +
27978 +       sbinfo = au_sbi(sb);
27979 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27980 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27981 +
27982 +       /* no spin_lock since sbinfo is write-locked */
27983 +       warned = 0;
27984 +       for (i = 0; i < AuPlink_NHASH; i++) {
27985 +               hbl = sbinfo->si_plink + i;
27986 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
27987 +                       pr_warn("pseudo-link is not flushed");
27988 +                       warned = 1;
27989 +               }
27990 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
27991 +                       iput(&icntnr->vfs_inode);
27992 +               INIT_HLIST_BL_HEAD(hbl);
27993 +       }
27994 +}
27995 +
27996 +void au_plink_clean(struct super_block *sb, int verbose)
27997 +{
27998 +       struct dentry *root;
27999 +
28000 +       root = sb->s_root;
28001 +       aufs_write_lock(root);
28002 +       if (au_opt_test(au_mntflags(sb), PLINK))
28003 +               au_plink_put(sb, verbose);
28004 +       aufs_write_unlock(root);
28005 +}
28006 +
28007 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28008 +{
28009 +       int do_put;
28010 +       aufs_bindex_t btop, bbot, bindex;
28011 +
28012 +       do_put = 0;
28013 +       btop = au_ibtop(inode);
28014 +       bbot = au_ibbot(inode);
28015 +       if (btop >= 0) {
28016 +               for (bindex = btop; bindex <= bbot; bindex++) {
28017 +                       if (!au_h_iptr(inode, bindex)
28018 +                           || au_ii_br_id(inode, bindex) != br_id)
28019 +                               continue;
28020 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28021 +                       do_put = 1;
28022 +                       break;
28023 +               }
28024 +               if (do_put)
28025 +                       for (bindex = btop; bindex <= bbot; bindex++)
28026 +                               if (au_h_iptr(inode, bindex)) {
28027 +                                       do_put = 0;
28028 +                                       break;
28029 +                               }
28030 +       } else
28031 +               do_put = 1;
28032 +
28033 +       return do_put;
28034 +}
28035 +
28036 +/* free the plinks on a branch specified by @br_id */
28037 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28038 +{
28039 +       struct au_sbinfo *sbinfo;
28040 +       struct hlist_bl_head *hbl;
28041 +       struct hlist_bl_node *pos, *tmp;
28042 +       struct au_icntnr *icntnr;
28043 +       struct inode *inode;
28044 +       int i, do_put;
28045 +
28046 +       SiMustWriteLock(sb);
28047 +
28048 +       sbinfo = au_sbi(sb);
28049 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28050 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28051 +
28052 +       /* no bit_lock since sbinfo is write-locked */
28053 +       for (i = 0; i < AuPlink_NHASH; i++) {
28054 +               hbl = sbinfo->si_plink + i;
28055 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28056 +                       inode = au_igrab(&icntnr->vfs_inode);
28057 +                       ii_write_lock_child(inode);
28058 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28059 +                       if (do_put) {
28060 +                               hlist_bl_del(&icntnr->plink);
28061 +                               iput(inode);
28062 +                       }
28063 +                       ii_write_unlock(inode);
28064 +                       iput(inode);
28065 +               }
28066 +       }
28067 +}
28068 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28069 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28070 +++ linux/fs/aufs/poll.c        2018-08-12 23:43:05.460124736 +0200
28071 @@ -0,0 +1,51 @@
28072 +// SPDX-License-Identifier: GPL-2.0
28073 +/*
28074 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28075 + *
28076 + * This program, aufs is free software; you can redistribute it and/or modify
28077 + * it under the terms of the GNU General Public License as published by
28078 + * the Free Software Foundation; either version 2 of the License, or
28079 + * (at your option) any later version.
28080 + *
28081 + * This program is distributed in the hope that it will be useful,
28082 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28083 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28084 + * GNU General Public License for more details.
28085 + *
28086 + * You should have received a copy of the GNU General Public License
28087 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28088 + */
28089 +
28090 +/*
28091 + * poll operation
28092 + * There is only one filesystem which implements ->poll operation, currently.
28093 + */
28094 +
28095 +#include "aufs.h"
28096 +
28097 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28098 +{
28099 +       __poll_t mask;
28100 +       struct file *h_file;
28101 +       struct super_block *sb;
28102 +
28103 +       /* We should pretend an error happened. */
28104 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28105 +       sb = file->f_path.dentry->d_sb;
28106 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28107 +
28108 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28109 +       if (IS_ERR(h_file)) {
28110 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28111 +               goto out;
28112 +       }
28113 +
28114 +       mask = vfs_poll(h_file, pt);
28115 +       fput(h_file); /* instead of au_read_post() */
28116 +
28117 +out:
28118 +       si_read_unlock(sb);
28119 +       if (mask & EPOLLERR)
28120 +               AuDbg("mask 0x%x\n", mask);
28121 +       return mask;
28122 +}
28123 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28124 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28125 +++ linux/fs/aufs/posix_acl.c   2018-08-12 23:43:05.460124736 +0200
28126 @@ -0,0 +1,103 @@
28127 +// SPDX-License-Identifier: GPL-2.0
28128 +/*
28129 + * Copyright (C) 2014-2018 Junjiro R. Okajima
28130 + *
28131 + * This program, aufs is free software; you can redistribute it and/or modify
28132 + * it under the terms of the GNU General Public License as published by
28133 + * the Free Software Foundation; either version 2 of the License, or
28134 + * (at your option) any later version.
28135 + *
28136 + * This program is distributed in the hope that it will be useful,
28137 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28138 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28139 + * GNU General Public License for more details.
28140 + *
28141 + * You should have received a copy of the GNU General Public License
28142 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28143 + */
28144 +
28145 +/*
28146 + * posix acl operations
28147 + */
28148 +
28149 +#include <linux/fs.h>
28150 +#include "aufs.h"
28151 +
28152 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28153 +{
28154 +       struct posix_acl *acl;
28155 +       int err;
28156 +       aufs_bindex_t bindex;
28157 +       struct inode *h_inode;
28158 +       struct super_block *sb;
28159 +
28160 +       acl = NULL;
28161 +       sb = inode->i_sb;
28162 +       si_read_lock(sb, AuLock_FLUSH);
28163 +       ii_read_lock_child(inode);
28164 +       if (!(sb->s_flags & SB_POSIXACL))
28165 +               goto out;
28166 +
28167 +       bindex = au_ibtop(inode);
28168 +       h_inode = au_h_iptr(inode, bindex);
28169 +       if (unlikely(!h_inode
28170 +                    || ((h_inode->i_mode & S_IFMT)
28171 +                        != (inode->i_mode & S_IFMT)))) {
28172 +               err = au_busy_or_stale();
28173 +               acl = ERR_PTR(err);
28174 +               goto out;
28175 +       }
28176 +
28177 +       /* always topmost only */
28178 +       acl = get_acl(h_inode, type);
28179 +       if (!IS_ERR_OR_NULL(acl))
28180 +               set_cached_acl(inode, type, acl);
28181 +
28182 +out:
28183 +       ii_read_unlock(inode);
28184 +       si_read_unlock(sb);
28185 +
28186 +       AuTraceErrPtr(acl);
28187 +       return acl;
28188 +}
28189 +
28190 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28191 +{
28192 +       int err;
28193 +       ssize_t ssz;
28194 +       struct dentry *dentry;
28195 +       struct au_sxattr arg = {
28196 +               .type = AU_ACL_SET,
28197 +               .u.acl_set = {
28198 +                       .acl    = acl,
28199 +                       .type   = type
28200 +               },
28201 +       };
28202 +
28203 +       IMustLock(inode);
28204 +
28205 +       if (inode->i_ino == AUFS_ROOT_INO)
28206 +               dentry = dget(inode->i_sb->s_root);
28207 +       else {
28208 +               dentry = d_find_alias(inode);
28209 +               if (!dentry)
28210 +                       dentry = d_find_any_alias(inode);
28211 +               if (!dentry) {
28212 +                       pr_warn("cannot handle this inode, "
28213 +                               "please report to aufs-users ML\n");
28214 +                       err = -ENOENT;
28215 +                       goto out;
28216 +               }
28217 +       }
28218 +
28219 +       ssz = au_sxattr(dentry, inode, &arg);
28220 +       dput(dentry);
28221 +       err = ssz;
28222 +       if (ssz >= 0) {
28223 +               err = 0;
28224 +               set_cached_acl(inode, type, acl);
28225 +       }
28226 +
28227 +out:
28228 +       return err;
28229 +}
28230 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28231 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28232 +++ linux/fs/aufs/procfs.c      2018-08-12 23:43:05.460124736 +0200
28233 @@ -0,0 +1,171 @@
28234 +// SPDX-License-Identifier: GPL-2.0
28235 +/*
28236 + * Copyright (C) 2010-2018 Junjiro R. Okajima
28237 + *
28238 + * This program, aufs is free software; you can redistribute it and/or modify
28239 + * it under the terms of the GNU General Public License as published by
28240 + * the Free Software Foundation; either version 2 of the License, or
28241 + * (at your option) any later version.
28242 + *
28243 + * This program is distributed in the hope that it will be useful,
28244 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28245 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28246 + * GNU General Public License for more details.
28247 + *
28248 + * You should have received a copy of the GNU General Public License
28249 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28250 + */
28251 +
28252 +/*
28253 + * procfs interfaces
28254 + */
28255 +
28256 +#include <linux/proc_fs.h>
28257 +#include "aufs.h"
28258 +
28259 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28260 +{
28261 +       struct au_sbinfo *sbinfo;
28262 +
28263 +       sbinfo = file->private_data;
28264 +       if (sbinfo) {
28265 +               au_plink_maint_leave(sbinfo);
28266 +               kobject_put(&sbinfo->si_kobj);
28267 +       }
28268 +
28269 +       return 0;
28270 +}
28271 +
28272 +static void au_procfs_plm_write_clean(struct file *file)
28273 +{
28274 +       struct au_sbinfo *sbinfo;
28275 +
28276 +       sbinfo = file->private_data;
28277 +       if (sbinfo)
28278 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28279 +}
28280 +
28281 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28282 +{
28283 +       int err;
28284 +       struct super_block *sb;
28285 +       struct au_sbinfo *sbinfo;
28286 +       struct hlist_bl_node *pos;
28287 +
28288 +       err = -EBUSY;
28289 +       if (unlikely(file->private_data))
28290 +               goto out;
28291 +
28292 +       sb = NULL;
28293 +       /* don't use au_sbilist_lock() here */
28294 +       hlist_bl_lock(&au_sbilist);
28295 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28296 +               if (id == sysaufs_si_id(sbinfo)) {
28297 +                       kobject_get(&sbinfo->si_kobj);
28298 +                       sb = sbinfo->si_sb;
28299 +                       break;
28300 +               }
28301 +       hlist_bl_unlock(&au_sbilist);
28302 +
28303 +       err = -EINVAL;
28304 +       if (unlikely(!sb))
28305 +               goto out;
28306 +
28307 +       err = au_plink_maint_enter(sb);
28308 +       if (!err)
28309 +               /* keep kobject_get() */
28310 +               file->private_data = sbinfo;
28311 +       else
28312 +               kobject_put(&sbinfo->si_kobj);
28313 +out:
28314 +       return err;
28315 +}
28316 +
28317 +/*
28318 + * Accept a valid "si=xxxx" only.
28319 + * Once it is accepted successfully, accept "clean" too.
28320 + */
28321 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28322 +                                  size_t count, loff_t *ppos)
28323 +{
28324 +       ssize_t err;
28325 +       unsigned long id;
28326 +       /* last newline is allowed */
28327 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28328 +
28329 +       err = -EACCES;
28330 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28331 +               goto out;
28332 +
28333 +       err = -EINVAL;
28334 +       if (unlikely(count > sizeof(buf)))
28335 +               goto out;
28336 +
28337 +       err = copy_from_user(buf, ubuf, count);
28338 +       if (unlikely(err)) {
28339 +               err = -EFAULT;
28340 +               goto out;
28341 +       }
28342 +       buf[count] = 0;
28343 +
28344 +       err = -EINVAL;
28345 +       if (!strcmp("clean", buf)) {
28346 +               au_procfs_plm_write_clean(file);
28347 +               goto out_success;
28348 +       } else if (unlikely(strncmp("si=", buf, 3)))
28349 +               goto out;
28350 +
28351 +       err = kstrtoul(buf + 3, 16, &id);
28352 +       if (unlikely(err))
28353 +               goto out;
28354 +
28355 +       err = au_procfs_plm_write_si(file, id);
28356 +       if (unlikely(err))
28357 +               goto out;
28358 +
28359 +out_success:
28360 +       err = count; /* success */
28361 +out:
28362 +       return err;
28363 +}
28364 +
28365 +static const struct file_operations au_procfs_plm_fop = {
28366 +       .write          = au_procfs_plm_write,
28367 +       .release        = au_procfs_plm_release,
28368 +       .owner          = THIS_MODULE
28369 +};
28370 +
28371 +/* ---------------------------------------------------------------------- */
28372 +
28373 +static struct proc_dir_entry *au_procfs_dir;
28374 +
28375 +void au_procfs_fin(void)
28376 +{
28377 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28378 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28379 +}
28380 +
28381 +int __init au_procfs_init(void)
28382 +{
28383 +       int err;
28384 +       struct proc_dir_entry *entry;
28385 +
28386 +       err = -ENOMEM;
28387 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28388 +       if (unlikely(!au_procfs_dir))
28389 +               goto out;
28390 +
28391 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28392 +                           au_procfs_dir, &au_procfs_plm_fop);
28393 +       if (unlikely(!entry))
28394 +               goto out_dir;
28395 +
28396 +       err = 0;
28397 +       goto out; /* success */
28398 +
28399 +
28400 +out_dir:
28401 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28402 +out:
28403 +       return err;
28404 +}
28405 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28406 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28407 +++ linux/fs/aufs/rdu.c 2018-08-12 23:43:05.463458173 +0200
28408 @@ -0,0 +1,382 @@
28409 +// SPDX-License-Identifier: GPL-2.0
28410 +/*
28411 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28412 + *
28413 + * This program, aufs is free software; you can redistribute it and/or modify
28414 + * it under the terms of the GNU General Public License as published by
28415 + * the Free Software Foundation; either version 2 of the License, or
28416 + * (at your option) any later version.
28417 + *
28418 + * This program is distributed in the hope that it will be useful,
28419 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28420 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28421 + * GNU General Public License for more details.
28422 + *
28423 + * You should have received a copy of the GNU General Public License
28424 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28425 + */
28426 +
28427 +/*
28428 + * readdir in userspace.
28429 + */
28430 +
28431 +#include <linux/compat.h>
28432 +#include <linux/fs_stack.h>
28433 +#include <linux/security.h>
28434 +#include "aufs.h"
28435 +
28436 +/* bits for struct aufs_rdu.flags */
28437 +#define        AuRdu_CALLED    1
28438 +#define        AuRdu_CONT      (1 << 1)
28439 +#define        AuRdu_FULL      (1 << 2)
28440 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28441 +#define au_fset_rdu(flags, name) \
28442 +       do { (flags) |= AuRdu_##name; } while (0)
28443 +#define au_fclr_rdu(flags, name) \
28444 +       do { (flags) &= ~AuRdu_##name; } while (0)
28445 +
28446 +struct au_rdu_arg {
28447 +       struct dir_context              ctx;
28448 +       struct aufs_rdu                 *rdu;
28449 +       union au_rdu_ent_ul             ent;
28450 +       unsigned long                   end;
28451 +
28452 +       struct super_block              *sb;
28453 +       int                             err;
28454 +};
28455 +
28456 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28457 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28458 +{
28459 +       int err, len;
28460 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28461 +       struct aufs_rdu *rdu = arg->rdu;
28462 +       struct au_rdu_ent ent;
28463 +
28464 +       err = 0;
28465 +       arg->err = 0;
28466 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28467 +       len = au_rdu_len(nlen);
28468 +       if (arg->ent.ul + len  < arg->end) {
28469 +               ent.ino = h_ino;
28470 +               ent.bindex = rdu->cookie.bindex;
28471 +               ent.type = d_type;
28472 +               ent.nlen = nlen;
28473 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28474 +                       ent.type = DT_UNKNOWN;
28475 +
28476 +               /* unnecessary to support mmap_sem since this is a dir */
28477 +               err = -EFAULT;
28478 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28479 +                       goto out;
28480 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28481 +                       goto out;
28482 +               /* the terminating NULL */
28483 +               if (__put_user(0, arg->ent.e->name + nlen))
28484 +                       goto out;
28485 +               err = 0;
28486 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28487 +               arg->ent.ul += len;
28488 +               rdu->rent++;
28489 +       } else {
28490 +               err = -EFAULT;
28491 +               au_fset_rdu(rdu->cookie.flags, FULL);
28492 +               rdu->full = 1;
28493 +               rdu->tail = arg->ent;
28494 +       }
28495 +
28496 +out:
28497 +       /* AuTraceErr(err); */
28498 +       return err;
28499 +}
28500 +
28501 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28502 +{
28503 +       int err;
28504 +       loff_t offset;
28505 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28506 +
28507 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28508 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28509 +       err = offset;
28510 +       if (unlikely(offset != cookie->h_pos))
28511 +               goto out;
28512 +
28513 +       err = 0;
28514 +       do {
28515 +               arg->err = 0;
28516 +               au_fclr_rdu(cookie->flags, CALLED);
28517 +               /* smp_mb(); */
28518 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28519 +               if (err >= 0)
28520 +                       err = arg->err;
28521 +       } while (!err
28522 +                && au_ftest_rdu(cookie->flags, CALLED)
28523 +                && !au_ftest_rdu(cookie->flags, FULL));
28524 +       cookie->h_pos = h_file->f_pos;
28525 +
28526 +out:
28527 +       AuTraceErr(err);
28528 +       return err;
28529 +}
28530 +
28531 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28532 +{
28533 +       int err;
28534 +       aufs_bindex_t bbot;
28535 +       struct au_rdu_arg arg = {
28536 +               .ctx = {
28537 +                       .actor = au_rdu_fill
28538 +               }
28539 +       };
28540 +       struct dentry *dentry;
28541 +       struct inode *inode;
28542 +       struct file *h_file;
28543 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28544 +
28545 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28546 +       if (unlikely(err)) {
28547 +               err = -EFAULT;
28548 +               AuTraceErr(err);
28549 +               goto out;
28550 +       }
28551 +       rdu->rent = 0;
28552 +       rdu->tail = rdu->ent;
28553 +       rdu->full = 0;
28554 +       arg.rdu = rdu;
28555 +       arg.ent = rdu->ent;
28556 +       arg.end = arg.ent.ul;
28557 +       arg.end += rdu->sz;
28558 +
28559 +       err = -ENOTDIR;
28560 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28561 +               goto out;
28562 +
28563 +       err = security_file_permission(file, MAY_READ);
28564 +       AuTraceErr(err);
28565 +       if (unlikely(err))
28566 +               goto out;
28567 +
28568 +       dentry = file->f_path.dentry;
28569 +       inode = d_inode(dentry);
28570 +       inode_lock_shared(inode);
28571 +
28572 +       arg.sb = inode->i_sb;
28573 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28574 +       if (unlikely(err))
28575 +               goto out_mtx;
28576 +       err = au_alive_dir(dentry);
28577 +       if (unlikely(err))
28578 +               goto out_si;
28579 +       /* todo: reval? */
28580 +       fi_read_lock(file);
28581 +
28582 +       err = -EAGAIN;
28583 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28584 +                    && cookie->generation != au_figen(file)))
28585 +               goto out_unlock;
28586 +
28587 +       err = 0;
28588 +       if (!rdu->blk) {
28589 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28590 +               if (!rdu->blk)
28591 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28592 +       }
28593 +       bbot = au_fbtop(file);
28594 +       if (cookie->bindex < bbot)
28595 +               cookie->bindex = bbot;
28596 +       bbot = au_fbbot_dir(file);
28597 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28598 +       for (; !err && cookie->bindex <= bbot;
28599 +            cookie->bindex++, cookie->h_pos = 0) {
28600 +               h_file = au_hf_dir(file, cookie->bindex);
28601 +               if (!h_file)
28602 +                       continue;
28603 +
28604 +               au_fclr_rdu(cookie->flags, FULL);
28605 +               err = au_rdu_do(h_file, &arg);
28606 +               AuTraceErr(err);
28607 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28608 +                       break;
28609 +       }
28610 +       AuDbg("rent %llu\n", rdu->rent);
28611 +
28612 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28613 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28614 +               au_fset_rdu(cookie->flags, CONT);
28615 +               cookie->generation = au_figen(file);
28616 +       }
28617 +
28618 +       ii_read_lock_child(inode);
28619 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28620 +       ii_read_unlock(inode);
28621 +
28622 +out_unlock:
28623 +       fi_read_unlock(file);
28624 +out_si:
28625 +       si_read_unlock(arg.sb);
28626 +out_mtx:
28627 +       inode_unlock_shared(inode);
28628 +out:
28629 +       AuTraceErr(err);
28630 +       return err;
28631 +}
28632 +
28633 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28634 +{
28635 +       int err;
28636 +       ino_t ino;
28637 +       unsigned long long nent;
28638 +       union au_rdu_ent_ul *u;
28639 +       struct au_rdu_ent ent;
28640 +       struct super_block *sb;
28641 +
28642 +       err = 0;
28643 +       nent = rdu->nent;
28644 +       u = &rdu->ent;
28645 +       sb = file->f_path.dentry->d_sb;
28646 +       si_read_lock(sb, AuLock_FLUSH);
28647 +       while (nent-- > 0) {
28648 +               /* unnecessary to support mmap_sem since this is a dir */
28649 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28650 +               if (!err)
28651 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
28652 +               if (unlikely(err)) {
28653 +                       err = -EFAULT;
28654 +                       AuTraceErr(err);
28655 +                       break;
28656 +               }
28657 +
28658 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28659 +               if (!ent.wh)
28660 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28661 +               else
28662 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28663 +                                       &ino);
28664 +               if (unlikely(err)) {
28665 +                       AuTraceErr(err);
28666 +                       break;
28667 +               }
28668 +
28669 +               err = __put_user(ino, &u->e->ino);
28670 +               if (unlikely(err)) {
28671 +                       err = -EFAULT;
28672 +                       AuTraceErr(err);
28673 +                       break;
28674 +               }
28675 +               u->ul += au_rdu_len(ent.nlen);
28676 +       }
28677 +       si_read_unlock(sb);
28678 +
28679 +       return err;
28680 +}
28681 +
28682 +/* ---------------------------------------------------------------------- */
28683 +
28684 +static int au_rdu_verify(struct aufs_rdu *rdu)
28685 +{
28686 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28687 +             "%llu, b%d, 0x%x, g%u}\n",
28688 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28689 +             rdu->blk,
28690 +             rdu->rent, rdu->shwh, rdu->full,
28691 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28692 +             rdu->cookie.generation);
28693 +
28694 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28695 +               return 0;
28696 +
28697 +       AuDbg("%u:%u\n",
28698 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28699 +       return -EINVAL;
28700 +}
28701 +
28702 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28703 +{
28704 +       long err, e;
28705 +       struct aufs_rdu rdu;
28706 +       void __user *p = (void __user *)arg;
28707 +
28708 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28709 +       if (unlikely(err)) {
28710 +               err = -EFAULT;
28711 +               AuTraceErr(err);
28712 +               goto out;
28713 +       }
28714 +       err = au_rdu_verify(&rdu);
28715 +       if (unlikely(err))
28716 +               goto out;
28717 +
28718 +       switch (cmd) {
28719 +       case AUFS_CTL_RDU:
28720 +               err = au_rdu(file, &rdu);
28721 +               if (unlikely(err))
28722 +                       break;
28723 +
28724 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28725 +               if (unlikely(e)) {
28726 +                       err = -EFAULT;
28727 +                       AuTraceErr(err);
28728 +               }
28729 +               break;
28730 +       case AUFS_CTL_RDU_INO:
28731 +               err = au_rdu_ino(file, &rdu);
28732 +               break;
28733 +
28734 +       default:
28735 +               /* err = -ENOTTY; */
28736 +               err = -EINVAL;
28737 +       }
28738 +
28739 +out:
28740 +       AuTraceErr(err);
28741 +       return err;
28742 +}
28743 +
28744 +#ifdef CONFIG_COMPAT
28745 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28746 +{
28747 +       long err, e;
28748 +       struct aufs_rdu rdu;
28749 +       void __user *p = compat_ptr(arg);
28750 +
28751 +       /* todo: get_user()? */
28752 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28753 +       if (unlikely(err)) {
28754 +               err = -EFAULT;
28755 +               AuTraceErr(err);
28756 +               goto out;
28757 +       }
28758 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
28759 +       err = au_rdu_verify(&rdu);
28760 +       if (unlikely(err))
28761 +               goto out;
28762 +
28763 +       switch (cmd) {
28764 +       case AUFS_CTL_RDU:
28765 +               err = au_rdu(file, &rdu);
28766 +               if (unlikely(err))
28767 +                       break;
28768 +
28769 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28770 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28771 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28772 +               if (unlikely(e)) {
28773 +                       err = -EFAULT;
28774 +                       AuTraceErr(err);
28775 +               }
28776 +               break;
28777 +       case AUFS_CTL_RDU_INO:
28778 +               err = au_rdu_ino(file, &rdu);
28779 +               break;
28780 +
28781 +       default:
28782 +               /* err = -ENOTTY; */
28783 +               err = -EINVAL;
28784 +       }
28785 +
28786 +out:
28787 +       AuTraceErr(err);
28788 +       return err;
28789 +}
28790 +#endif
28791 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28792 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
28793 +++ linux/fs/aufs/rwsem.h       2018-08-12 23:43:05.463458173 +0200
28794 @@ -0,0 +1,73 @@
28795 +/* SPDX-License-Identifier: GPL-2.0 */
28796 +/*
28797 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28798 + *
28799 + * This program, aufs is free software; you can redistribute it and/or modify
28800 + * it under the terms of the GNU General Public License as published by
28801 + * the Free Software Foundation; either version 2 of the License, or
28802 + * (at your option) any later version.
28803 + *
28804 + * This program is distributed in the hope that it will be useful,
28805 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28806 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28807 + * GNU General Public License for more details.
28808 + *
28809 + * You should have received a copy of the GNU General Public License
28810 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28811 + */
28812 +
28813 +/*
28814 + * simple read-write semaphore wrappers
28815 + */
28816 +
28817 +#ifndef __AUFS_RWSEM_H__
28818 +#define __AUFS_RWSEM_H__
28819 +
28820 +#ifdef __KERNEL__
28821 +
28822 +#include "debug.h"
28823 +
28824 +/* in the futre, the name 'au_rwsem' will be totally gone */
28825 +#define au_rwsem       rw_semaphore
28826 +
28827 +/* to debug easier, do not make them inlined functions */
28828 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
28829 +/* rwsem_is_locked() is unusable */
28830 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
28831 +                                         && debug_locks \
28832 +                                         && !lockdep_is_held_type(rw, 1))
28833 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
28834 +                                         && debug_locks \
28835 +                                         && !lockdep_is_held_type(rw, 0))
28836 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
28837 +                                         && debug_locks \
28838 +                                         && !lockdep_is_held(rw))
28839 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
28840 +                                         && debug_locks \
28841 +                                         && lockdep_is_held(rw))
28842 +
28843 +#define au_rw_init(rw) init_rwsem(rw)
28844 +
28845 +#define au_rw_init_wlock(rw) do {              \
28846 +               au_rw_init(rw);                 \
28847 +               down_write(rw);                 \
28848 +       } while (0)
28849 +
28850 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
28851 +               au_rw_init(rw);                 \
28852 +               down_write_nested(rw, lsc);     \
28853 +       } while (0)
28854 +
28855 +#define au_rw_read_lock(rw)            down_read(rw)
28856 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
28857 +#define au_rw_read_unlock(rw)          up_read(rw)
28858 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
28859 +#define au_rw_write_lock(rw)           down_write(rw)
28860 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28861 +#define au_rw_write_unlock(rw)         up_write(rw)
28862 +/* why is not _nested version defined? */
28863 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
28864 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
28865 +
28866 +#endif /* __KERNEL__ */
28867 +#endif /* __AUFS_RWSEM_H__ */
28868 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28869 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
28870 +++ linux/fs/aufs/sbinfo.c      2018-08-12 23:43:05.463458173 +0200
28871 @@ -0,0 +1,312 @@
28872 +// SPDX-License-Identifier: GPL-2.0
28873 +/*
28874 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28875 + *
28876 + * This program, aufs is free software; you can redistribute it and/or modify
28877 + * it under the terms of the GNU General Public License as published by
28878 + * the Free Software Foundation; either version 2 of the License, or
28879 + * (at your option) any later version.
28880 + *
28881 + * This program is distributed in the hope that it will be useful,
28882 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28883 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28884 + * GNU General Public License for more details.
28885 + *
28886 + * You should have received a copy of the GNU General Public License
28887 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28888 + */
28889 +
28890 +/*
28891 + * superblock private data
28892 + */
28893 +
28894 +#include "aufs.h"
28895 +
28896 +/*
28897 + * they are necessary regardless sysfs is disabled.
28898 + */
28899 +void au_si_free(struct kobject *kobj)
28900 +{
28901 +       int i;
28902 +       struct au_sbinfo *sbinfo;
28903 +       char *locked __maybe_unused; /* debug only */
28904 +
28905 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28906 +       for (i = 0; i < AuPlink_NHASH; i++)
28907 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
28908 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
28909 +
28910 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
28911 +       percpu_counter_destroy(&sbinfo->si_ninodes);
28912 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
28913 +       percpu_counter_destroy(&sbinfo->si_nfiles);
28914 +
28915 +       dbgaufs_si_fin(sbinfo);
28916 +       au_rw_write_lock(&sbinfo->si_rwsem);
28917 +       au_br_free(sbinfo);
28918 +       au_rw_write_unlock(&sbinfo->si_rwsem);
28919 +
28920 +       kfree(sbinfo->si_branch);
28921 +       mutex_destroy(&sbinfo->si_xib_mtx);
28922 +       AuRwDestroy(&sbinfo->si_rwsem);
28923 +
28924 +       kfree(sbinfo);
28925 +}
28926 +
28927 +int au_si_alloc(struct super_block *sb)
28928 +{
28929 +       int err, i;
28930 +       struct au_sbinfo *sbinfo;
28931 +
28932 +       err = -ENOMEM;
28933 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
28934 +       if (unlikely(!sbinfo))
28935 +               goto out;
28936 +
28937 +       /* will be reallocated separately */
28938 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
28939 +       if (unlikely(!sbinfo->si_branch))
28940 +               goto out_sbinfo;
28941 +
28942 +       err = sysaufs_si_init(sbinfo);
28943 +       if (!err) {
28944 +               dbgaufs_si_null(sbinfo);
28945 +               err = dbgaufs_si_init(sbinfo);
28946 +               if (unlikely(err))
28947 +                       kobject_put(&sbinfo->si_kobj);
28948 +       }
28949 +       if (unlikely(err))
28950 +               goto out_br;
28951 +
28952 +       au_nwt_init(&sbinfo->si_nowait);
28953 +       au_rw_init_wlock(&sbinfo->si_rwsem);
28954 +
28955 +       percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
28956 +       percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
28957 +
28958 +       sbinfo->si_bbot = -1;
28959 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
28960 +
28961 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
28962 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
28963 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
28964 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
28965 +
28966 +       au_fhsm_init(sbinfo);
28967 +
28968 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
28969 +
28970 +       sbinfo->si_xino_jiffy = jiffies;
28971 +       sbinfo->si_xino_expire
28972 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
28973 +       mutex_init(&sbinfo->si_xib_mtx);
28974 +       sbinfo->si_xino_brid = -1;
28975 +       /* leave si_xib_last_pindex and si_xib_next_bit */
28976 +
28977 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
28978 +
28979 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
28980 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
28981 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
28982 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
28983 +
28984 +       for (i = 0; i < AuPlink_NHASH; i++)
28985 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
28986 +       init_waitqueue_head(&sbinfo->si_plink_wq);
28987 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
28988 +
28989 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
28990 +
28991 +       /* with getattr by default */
28992 +       sbinfo->si_iop_array = aufs_iop;
28993 +
28994 +       /* leave other members for sysaufs and si_mnt. */
28995 +       sbinfo->si_sb = sb;
28996 +       sb->s_fs_info = sbinfo;
28997 +       si_pid_set(sb);
28998 +       return 0; /* success */
28999 +
29000 +out_br:
29001 +       kfree(sbinfo->si_branch);
29002 +out_sbinfo:
29003 +       kfree(sbinfo);
29004 +out:
29005 +       return err;
29006 +}
29007 +
29008 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29009 +{
29010 +       int err, sz;
29011 +       struct au_branch **brp;
29012 +
29013 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29014 +
29015 +       err = -ENOMEM;
29016 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29017 +       if (unlikely(!sz))
29018 +               sz = sizeof(*brp);
29019 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29020 +                          may_shrink);
29021 +       if (brp) {
29022 +               sbinfo->si_branch = brp;
29023 +               err = 0;
29024 +       }
29025 +
29026 +       return err;
29027 +}
29028 +
29029 +/* ---------------------------------------------------------------------- */
29030 +
29031 +unsigned int au_sigen_inc(struct super_block *sb)
29032 +{
29033 +       unsigned int gen;
29034 +       struct inode *inode;
29035 +
29036 +       SiMustWriteLock(sb);
29037 +
29038 +       gen = ++au_sbi(sb)->si_generation;
29039 +       au_update_digen(sb->s_root);
29040 +       inode = d_inode(sb->s_root);
29041 +       au_update_iigen(inode, /*half*/0);
29042 +       inode_inc_iversion(inode);
29043 +       return gen;
29044 +}
29045 +
29046 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29047 +{
29048 +       aufs_bindex_t br_id;
29049 +       int i;
29050 +       struct au_sbinfo *sbinfo;
29051 +
29052 +       SiMustWriteLock(sb);
29053 +
29054 +       sbinfo = au_sbi(sb);
29055 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29056 +               br_id = ++sbinfo->si_last_br_id;
29057 +               AuDebugOn(br_id < 0);
29058 +               if (br_id && au_br_index(sb, br_id) < 0)
29059 +                       return br_id;
29060 +       }
29061 +
29062 +       return -1;
29063 +}
29064 +
29065 +/* ---------------------------------------------------------------------- */
29066 +
29067 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29068 +int si_read_lock(struct super_block *sb, int flags)
29069 +{
29070 +       int err;
29071 +
29072 +       err = 0;
29073 +       if (au_ftest_lock(flags, FLUSH))
29074 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29075 +
29076 +       si_noflush_read_lock(sb);
29077 +       err = au_plink_maint(sb, flags);
29078 +       if (unlikely(err))
29079 +               si_read_unlock(sb);
29080 +
29081 +       return err;
29082 +}
29083 +
29084 +int si_write_lock(struct super_block *sb, int flags)
29085 +{
29086 +       int err;
29087 +
29088 +       if (au_ftest_lock(flags, FLUSH))
29089 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29090 +
29091 +       si_noflush_write_lock(sb);
29092 +       err = au_plink_maint(sb, flags);
29093 +       if (unlikely(err))
29094 +               si_write_unlock(sb);
29095 +
29096 +       return err;
29097 +}
29098 +
29099 +/* dentry and super_block lock. call at entry point */
29100 +int aufs_read_lock(struct dentry *dentry, int flags)
29101 +{
29102 +       int err;
29103 +       struct super_block *sb;
29104 +
29105 +       sb = dentry->d_sb;
29106 +       err = si_read_lock(sb, flags);
29107 +       if (unlikely(err))
29108 +               goto out;
29109 +
29110 +       if (au_ftest_lock(flags, DW))
29111 +               di_write_lock_child(dentry);
29112 +       else
29113 +               di_read_lock_child(dentry, flags);
29114 +
29115 +       if (au_ftest_lock(flags, GEN)) {
29116 +               err = au_digen_test(dentry, au_sigen(sb));
29117 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29118 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29119 +               else if (!err)
29120 +                       err = au_dbrange_test(dentry);
29121 +               if (unlikely(err))
29122 +                       aufs_read_unlock(dentry, flags);
29123 +       }
29124 +
29125 +out:
29126 +       return err;
29127 +}
29128 +
29129 +void aufs_read_unlock(struct dentry *dentry, int flags)
29130 +{
29131 +       if (au_ftest_lock(flags, DW))
29132 +               di_write_unlock(dentry);
29133 +       else
29134 +               di_read_unlock(dentry, flags);
29135 +       si_read_unlock(dentry->d_sb);
29136 +}
29137 +
29138 +void aufs_write_lock(struct dentry *dentry)
29139 +{
29140 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29141 +       di_write_lock_child(dentry);
29142 +}
29143 +
29144 +void aufs_write_unlock(struct dentry *dentry)
29145 +{
29146 +       di_write_unlock(dentry);
29147 +       si_write_unlock(dentry->d_sb);
29148 +}
29149 +
29150 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29151 +{
29152 +       int err;
29153 +       unsigned int sigen;
29154 +       struct super_block *sb;
29155 +
29156 +       sb = d1->d_sb;
29157 +       err = si_read_lock(sb, flags);
29158 +       if (unlikely(err))
29159 +               goto out;
29160 +
29161 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29162 +
29163 +       if (au_ftest_lock(flags, GEN)) {
29164 +               sigen = au_sigen(sb);
29165 +               err = au_digen_test(d1, sigen);
29166 +               AuDebugOn(!err && au_dbrange_test(d1));
29167 +               if (!err) {
29168 +                       err = au_digen_test(d2, sigen);
29169 +                       AuDebugOn(!err && au_dbrange_test(d2));
29170 +               }
29171 +               if (unlikely(err))
29172 +                       aufs_read_and_write_unlock2(d1, d2);
29173 +       }
29174 +
29175 +out:
29176 +       return err;
29177 +}
29178 +
29179 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29180 +{
29181 +       di_write_unlock2(d1, d2);
29182 +       si_read_unlock(d1->d_sb);
29183 +}
29184 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29185 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29186 +++ linux/fs/aufs/super.c       2018-08-12 23:43:05.463458173 +0200
29187 @@ -0,0 +1,1043 @@
29188 +// SPDX-License-Identifier: GPL-2.0
29189 +/*
29190 + * Copyright (C) 2005-2018 Junjiro R. Okajima
29191 + *
29192 + * This program, aufs is free software; you can redistribute it and/or modify
29193 + * it under the terms of the GNU General Public License as published by
29194 + * the Free Software Foundation; either version 2 of the License, or
29195 + * (at your option) any later version.
29196 + *
29197 + * This program is distributed in the hope that it will be useful,
29198 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29199 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29200 + * GNU General Public License for more details.
29201 + *
29202 + * You should have received a copy of the GNU General Public License
29203 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29204 + */
29205 +
29206 +/*
29207 + * mount and super_block operations
29208 + */
29209 +
29210 +#include <linux/mm.h>
29211 +#include <linux/seq_file.h>
29212 +#include <linux/statfs.h>
29213 +#include <linux/vmalloc.h>
29214 +#include "aufs.h"
29215 +
29216 +/*
29217 + * super_operations
29218 + */
29219 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29220 +{
29221 +       struct au_icntnr *c;
29222 +
29223 +       c = au_cache_alloc_icntnr();
29224 +       if (c) {
29225 +               au_icntnr_init(c);
29226 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29227 +               c->iinfo.ii_hinode = NULL;
29228 +               return &c->vfs_inode;
29229 +       }
29230 +       return NULL;
29231 +}
29232 +
29233 +static void aufs_destroy_inode_cb(struct rcu_head *head)
29234 +{
29235 +       struct inode *inode = container_of(head, struct inode, i_rcu);
29236 +
29237 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29238 +}
29239 +
29240 +static void aufs_destroy_inode(struct inode *inode)
29241 +{
29242 +       if (!au_is_bad_inode(inode))
29243 +               au_iinfo_fin(inode);
29244 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
29245 +}
29246 +
29247 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29248 +{
29249 +       struct inode *inode;
29250 +       int err;
29251 +
29252 +       inode = iget_locked(sb, ino);
29253 +       if (unlikely(!inode)) {
29254 +               inode = ERR_PTR(-ENOMEM);
29255 +               goto out;
29256 +       }
29257 +       if (!(inode->i_state & I_NEW))
29258 +               goto out;
29259 +
29260 +       err = au_xigen_new(inode);
29261 +       if (!err)
29262 +               err = au_iinfo_init(inode);
29263 +       if (!err)
29264 +               inode_inc_iversion(inode);
29265 +       else {
29266 +               iget_failed(inode);
29267 +               inode = ERR_PTR(err);
29268 +       }
29269 +
29270 +out:
29271 +       /* never return NULL */
29272 +       AuDebugOn(!inode);
29273 +       AuTraceErrPtr(inode);
29274 +       return inode;
29275 +}
29276 +
29277 +/* lock free root dinfo */
29278 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29279 +{
29280 +       int err;
29281 +       aufs_bindex_t bindex, bbot;
29282 +       struct path path;
29283 +       struct au_hdentry *hdp;
29284 +       struct au_branch *br;
29285 +       au_br_perm_str_t perm;
29286 +
29287 +       err = 0;
29288 +       bbot = au_sbbot(sb);
29289 +       bindex = 0;
29290 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29291 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29292 +               br = au_sbr(sb, bindex);
29293 +               path.mnt = au_br_mnt(br);
29294 +               path.dentry = hdp->hd_dentry;
29295 +               err = au_seq_path(seq, &path);
29296 +               if (!err) {
29297 +                       au_optstr_br_perm(&perm, br->br_perm);
29298 +                       seq_printf(seq, "=%s", perm.a);
29299 +                       if (bindex != bbot)
29300 +                               seq_putc(seq, ':');
29301 +               }
29302 +       }
29303 +       if (unlikely(err || seq_has_overflowed(seq)))
29304 +               err = -E2BIG;
29305 +
29306 +       return err;
29307 +}
29308 +
29309 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29310 +                      const char *append)
29311 +{
29312 +       char *p;
29313 +
29314 +       p = fmt;
29315 +       while (*pat != ':')
29316 +               *p++ = *pat++;
29317 +       *p++ = *pat++;
29318 +       strcpy(p, append);
29319 +       AuDebugOn(strlen(fmt) >= len);
29320 +}
29321 +
29322 +static void au_show_wbr_create(struct seq_file *m, int v,
29323 +                              struct au_sbinfo *sbinfo)
29324 +{
29325 +       const char *pat;
29326 +       char fmt[32];
29327 +       struct au_wbr_mfs *mfs;
29328 +
29329 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29330 +
29331 +       seq_puts(m, ",create=");
29332 +       pat = au_optstr_wbr_create(v);
29333 +       mfs = &sbinfo->si_wbr_mfs;
29334 +       switch (v) {
29335 +       case AuWbrCreate_TDP:
29336 +       case AuWbrCreate_RR:
29337 +       case AuWbrCreate_MFS:
29338 +       case AuWbrCreate_PMFS:
29339 +               seq_puts(m, pat);
29340 +               break;
29341 +       case AuWbrCreate_MFSRR:
29342 +       case AuWbrCreate_TDMFS:
29343 +       case AuWbrCreate_PMFSRR:
29344 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29345 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29346 +               break;
29347 +       case AuWbrCreate_MFSV:
29348 +       case AuWbrCreate_PMFSV:
29349 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29350 +               seq_printf(m, fmt,
29351 +                          jiffies_to_msecs(mfs->mfs_expire)
29352 +                          / MSEC_PER_SEC);
29353 +               break;
29354 +       case AuWbrCreate_MFSRRV:
29355 +       case AuWbrCreate_TDMFSV:
29356 +       case AuWbrCreate_PMFSRRV:
29357 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29358 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29359 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29360 +               break;
29361 +       default:
29362 +               BUG();
29363 +       }
29364 +}
29365 +
29366 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29367 +{
29368 +#ifdef CONFIG_SYSFS
29369 +       return 0;
29370 +#else
29371 +       int err;
29372 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29373 +       aufs_bindex_t bindex, brid;
29374 +       struct qstr *name;
29375 +       struct file *f;
29376 +       struct dentry *d, *h_root;
29377 +
29378 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29379 +
29380 +       err = 0;
29381 +       f = au_sbi(sb)->si_xib;
29382 +       if (!f)
29383 +               goto out;
29384 +
29385 +       /* stop printing the default xino path on the first writable branch */
29386 +       h_root = NULL;
29387 +       brid = au_xino_brid(sb);
29388 +       if (brid >= 0) {
29389 +               bindex = au_br_index(sb, brid);
29390 +               h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
29391 +       }
29392 +       d = f->f_path.dentry;
29393 +       name = &d->d_name;
29394 +       /* safe ->d_parent because the file is unlinked */
29395 +       if (d->d_parent == h_root
29396 +           && name->len == len
29397 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29398 +               goto out;
29399 +
29400 +       seq_puts(seq, ",xino=");
29401 +       err = au_xino_path(seq, f);
29402 +
29403 +out:
29404 +       return err;
29405 +#endif
29406 +}
29407 +
29408 +/* seq_file will re-call me in case of too long string */
29409 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29410 +{
29411 +       int err;
29412 +       unsigned int mnt_flags, v;
29413 +       struct super_block *sb;
29414 +       struct au_sbinfo *sbinfo;
29415 +
29416 +#define AuBool(name, str) do { \
29417 +       v = au_opt_test(mnt_flags, name); \
29418 +       if (v != au_opt_test(AuOpt_Def, name)) \
29419 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29420 +} while (0)
29421 +
29422 +#define AuStr(name, str) do { \
29423 +       v = mnt_flags & AuOptMask_##name; \
29424 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29425 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29426 +} while (0)
29427 +
29428 +#define AuUInt(name, str, val) do { \
29429 +       if (val != AUFS_##name##_DEF) \
29430 +               seq_printf(m, "," #str "=%u", val); \
29431 +} while (0)
29432 +
29433 +       sb = dentry->d_sb;
29434 +       if (sb->s_flags & SB_POSIXACL)
29435 +               seq_puts(m, ",acl");
29436 +#if 0
29437 +       if (sb->s_flags & SB_I_VERSION)
29438 +               seq_puts(m, ",i_version");
29439 +#endif
29440 +
29441 +       /* lock free root dinfo */
29442 +       si_noflush_read_lock(sb);
29443 +       sbinfo = au_sbi(sb);
29444 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29445 +
29446 +       mnt_flags = au_mntflags(sb);
29447 +       if (au_opt_test(mnt_flags, XINO)) {
29448 +               err = au_show_xino(m, sb);
29449 +               if (unlikely(err))
29450 +                       goto out;
29451 +       } else
29452 +               seq_puts(m, ",noxino");
29453 +
29454 +       AuBool(TRUNC_XINO, trunc_xino);
29455 +       AuStr(UDBA, udba);
29456 +       AuBool(SHWH, shwh);
29457 +       AuBool(PLINK, plink);
29458 +       AuBool(DIO, dio);
29459 +       AuBool(DIRPERM1, dirperm1);
29460 +
29461 +       v = sbinfo->si_wbr_create;
29462 +       if (v != AuWbrCreate_Def)
29463 +               au_show_wbr_create(m, v, sbinfo);
29464 +
29465 +       v = sbinfo->si_wbr_copyup;
29466 +       if (v != AuWbrCopyup_Def)
29467 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29468 +
29469 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29470 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29471 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29472 +
29473 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29474 +
29475 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29476 +       AuUInt(RDCACHE, rdcache, v);
29477 +
29478 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29479 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29480 +
29481 +       au_fhsm_show(m, sbinfo);
29482 +
29483 +       AuBool(DIRREN, dirren);
29484 +       AuBool(SUM, sum);
29485 +       /* AuBool(SUM_W, wsum); */
29486 +       AuBool(WARN_PERM, warn_perm);
29487 +       AuBool(VERBOSE, verbose);
29488 +
29489 +out:
29490 +       /* be sure to print "br:" last */
29491 +       if (!sysaufs_brs) {
29492 +               seq_puts(m, ",br:");
29493 +               au_show_brs(m, sb);
29494 +       }
29495 +       si_read_unlock(sb);
29496 +       return 0;
29497 +
29498 +#undef AuBool
29499 +#undef AuStr
29500 +#undef AuUInt
29501 +}
29502 +
29503 +/* ---------------------------------------------------------------------- */
29504 +
29505 +/* sum mode which returns the summation for statfs(2) */
29506 +
29507 +static u64 au_add_till_max(u64 a, u64 b)
29508 +{
29509 +       u64 old;
29510 +
29511 +       old = a;
29512 +       a += b;
29513 +       if (old <= a)
29514 +               return a;
29515 +       return ULLONG_MAX;
29516 +}
29517 +
29518 +static u64 au_mul_till_max(u64 a, long mul)
29519 +{
29520 +       u64 old;
29521 +
29522 +       old = a;
29523 +       a *= mul;
29524 +       if (old <= a)
29525 +               return a;
29526 +       return ULLONG_MAX;
29527 +}
29528 +
29529 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29530 +{
29531 +       int err;
29532 +       long bsize, factor;
29533 +       u64 blocks, bfree, bavail, files, ffree;
29534 +       aufs_bindex_t bbot, bindex, i;
29535 +       unsigned char shared;
29536 +       struct path h_path;
29537 +       struct super_block *h_sb;
29538 +
29539 +       err = 0;
29540 +       bsize = LONG_MAX;
29541 +       files = 0;
29542 +       ffree = 0;
29543 +       blocks = 0;
29544 +       bfree = 0;
29545 +       bavail = 0;
29546 +       bbot = au_sbbot(sb);
29547 +       for (bindex = 0; bindex <= bbot; bindex++) {
29548 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29549 +               h_sb = h_path.mnt->mnt_sb;
29550 +               shared = 0;
29551 +               for (i = 0; !shared && i < bindex; i++)
29552 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29553 +               if (shared)
29554 +                       continue;
29555 +
29556 +               /* sb->s_root for NFS is unreliable */
29557 +               h_path.dentry = h_path.mnt->mnt_root;
29558 +               err = vfs_statfs(&h_path, buf);
29559 +               if (unlikely(err))
29560 +                       goto out;
29561 +
29562 +               if (bsize > buf->f_bsize) {
29563 +                       /*
29564 +                        * we will reduce bsize, so we have to expand blocks
29565 +                        * etc. to match them again
29566 +                        */
29567 +                       factor = (bsize / buf->f_bsize);
29568 +                       blocks = au_mul_till_max(blocks, factor);
29569 +                       bfree = au_mul_till_max(bfree, factor);
29570 +                       bavail = au_mul_till_max(bavail, factor);
29571 +                       bsize = buf->f_bsize;
29572 +               }
29573 +
29574 +               factor = (buf->f_bsize / bsize);
29575 +               blocks = au_add_till_max(blocks,
29576 +                               au_mul_till_max(buf->f_blocks, factor));
29577 +               bfree = au_add_till_max(bfree,
29578 +                               au_mul_till_max(buf->f_bfree, factor));
29579 +               bavail = au_add_till_max(bavail,
29580 +                               au_mul_till_max(buf->f_bavail, factor));
29581 +               files = au_add_till_max(files, buf->f_files);
29582 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29583 +       }
29584 +
29585 +       buf->f_bsize = bsize;
29586 +       buf->f_blocks = blocks;
29587 +       buf->f_bfree = bfree;
29588 +       buf->f_bavail = bavail;
29589 +       buf->f_files = files;
29590 +       buf->f_ffree = ffree;
29591 +       buf->f_frsize = 0;
29592 +
29593 +out:
29594 +       return err;
29595 +}
29596 +
29597 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29598 +{
29599 +       int err;
29600 +       struct path h_path;
29601 +       struct super_block *sb;
29602 +
29603 +       /* lock free root dinfo */
29604 +       sb = dentry->d_sb;
29605 +       si_noflush_read_lock(sb);
29606 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29607 +               /* sb->s_root for NFS is unreliable */
29608 +               h_path.mnt = au_sbr_mnt(sb, 0);
29609 +               h_path.dentry = h_path.mnt->mnt_root;
29610 +               err = vfs_statfs(&h_path, buf);
29611 +       } else
29612 +               err = au_statfs_sum(sb, buf);
29613 +       si_read_unlock(sb);
29614 +
29615 +       if (!err) {
29616 +               buf->f_type = AUFS_SUPER_MAGIC;
29617 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29618 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29619 +       }
29620 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29621 +
29622 +       return err;
29623 +}
29624 +
29625 +/* ---------------------------------------------------------------------- */
29626 +
29627 +static int aufs_sync_fs(struct super_block *sb, int wait)
29628 +{
29629 +       int err, e;
29630 +       aufs_bindex_t bbot, bindex;
29631 +       struct au_branch *br;
29632 +       struct super_block *h_sb;
29633 +
29634 +       err = 0;
29635 +       si_noflush_read_lock(sb);
29636 +       bbot = au_sbbot(sb);
29637 +       for (bindex = 0; bindex <= bbot; bindex++) {
29638 +               br = au_sbr(sb, bindex);
29639 +               if (!au_br_writable(br->br_perm))
29640 +                       continue;
29641 +
29642 +               h_sb = au_sbr_sb(sb, bindex);
29643 +               e = vfsub_sync_filesystem(h_sb, wait);
29644 +               if (unlikely(e && !err))
29645 +                       err = e;
29646 +               /* go on even if an error happens */
29647 +       }
29648 +       si_read_unlock(sb);
29649 +
29650 +       return err;
29651 +}
29652 +
29653 +/* ---------------------------------------------------------------------- */
29654 +
29655 +/* final actions when unmounting a file system */
29656 +static void aufs_put_super(struct super_block *sb)
29657 +{
29658 +       struct au_sbinfo *sbinfo;
29659 +
29660 +       sbinfo = au_sbi(sb);
29661 +       if (sbinfo)
29662 +               kobject_put(&sbinfo->si_kobj);
29663 +}
29664 +
29665 +/* ---------------------------------------------------------------------- */
29666 +
29667 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29668 +                    struct super_block *sb, void *arg)
29669 +{
29670 +       void *array;
29671 +       unsigned long long n, sz;
29672 +
29673 +       array = NULL;
29674 +       n = 0;
29675 +       if (!*hint)
29676 +               goto out;
29677 +
29678 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29679 +               array = ERR_PTR(-EMFILE);
29680 +               pr_err("hint %llu\n", *hint);
29681 +               goto out;
29682 +       }
29683 +
29684 +       sz = sizeof(array) * *hint;
29685 +       array = kzalloc(sz, GFP_NOFS);
29686 +       if (unlikely(!array))
29687 +               array = vzalloc(sz);
29688 +       if (unlikely(!array)) {
29689 +               array = ERR_PTR(-ENOMEM);
29690 +               goto out;
29691 +       }
29692 +
29693 +       n = cb(sb, array, *hint, arg);
29694 +       AuDebugOn(n > *hint);
29695 +
29696 +out:
29697 +       *hint = n;
29698 +       return array;
29699 +}
29700 +
29701 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29702 +                                      unsigned long long max __maybe_unused,
29703 +                                      void *arg)
29704 +{
29705 +       unsigned long long n;
29706 +       struct inode **p, *inode;
29707 +       struct list_head *head;
29708 +
29709 +       n = 0;
29710 +       p = a;
29711 +       head = arg;
29712 +       spin_lock(&sb->s_inode_list_lock);
29713 +       list_for_each_entry(inode, head, i_sb_list) {
29714 +               if (!au_is_bad_inode(inode)
29715 +                   && au_ii(inode)->ii_btop >= 0) {
29716 +                       spin_lock(&inode->i_lock);
29717 +                       if (atomic_read(&inode->i_count)) {
29718 +                               au_igrab(inode);
29719 +                               *p++ = inode;
29720 +                               n++;
29721 +                               AuDebugOn(n > max);
29722 +                       }
29723 +                       spin_unlock(&inode->i_lock);
29724 +               }
29725 +       }
29726 +       spin_unlock(&sb->s_inode_list_lock);
29727 +
29728 +       return n;
29729 +}
29730 +
29731 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29732 +{
29733 +       *max = au_ninodes(sb);
29734 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29735 +}
29736 +
29737 +void au_iarray_free(struct inode **a, unsigned long long max)
29738 +{
29739 +       unsigned long long ull;
29740 +
29741 +       for (ull = 0; ull < max; ull++)
29742 +               iput(a[ull]);
29743 +       kvfree(a);
29744 +}
29745 +
29746 +/* ---------------------------------------------------------------------- */
29747 +
29748 +/*
29749 + * refresh dentry and inode at remount time.
29750 + */
29751 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29752 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29753 +                     struct dentry *parent)
29754 +{
29755 +       int err;
29756 +
29757 +       di_write_lock_child(dentry);
29758 +       di_read_lock_parent(parent, AuLock_IR);
29759 +       err = au_refresh_dentry(dentry, parent);
29760 +       if (!err && dir_flags)
29761 +               au_hn_reset(d_inode(dentry), dir_flags);
29762 +       di_read_unlock(parent, AuLock_IR);
29763 +       di_write_unlock(dentry);
29764 +
29765 +       return err;
29766 +}
29767 +
29768 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29769 +                          struct au_sbinfo *sbinfo,
29770 +                          const unsigned int dir_flags, unsigned int do_idop)
29771 +{
29772 +       int err;
29773 +       struct dentry *parent;
29774 +
29775 +       err = 0;
29776 +       parent = dget_parent(dentry);
29777 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29778 +               if (d_really_is_positive(dentry)) {
29779 +                       if (!d_is_dir(dentry))
29780 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
29781 +                                                parent);
29782 +                       else {
29783 +                               err = au_do_refresh(dentry, dir_flags, parent);
29784 +                               if (unlikely(err))
29785 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29786 +                       }
29787 +               } else
29788 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29789 +               AuDbgDentry(dentry);
29790 +       }
29791 +       dput(parent);
29792 +
29793 +       if (!err) {
29794 +               if (do_idop)
29795 +                       au_refresh_dop(dentry, /*force_reval*/0);
29796 +       } else
29797 +               au_refresh_dop(dentry, /*force_reval*/1);
29798 +
29799 +       AuTraceErr(err);
29800 +       return err;
29801 +}
29802 +
29803 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29804 +{
29805 +       int err, i, j, ndentry, e;
29806 +       unsigned int sigen;
29807 +       struct au_dcsub_pages dpages;
29808 +       struct au_dpage *dpage;
29809 +       struct dentry **dentries, *d;
29810 +       struct au_sbinfo *sbinfo;
29811 +       struct dentry *root = sb->s_root;
29812 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
29813 +
29814 +       if (do_idop)
29815 +               au_refresh_dop(root, /*force_reval*/0);
29816 +
29817 +       err = au_dpages_init(&dpages, GFP_NOFS);
29818 +       if (unlikely(err))
29819 +               goto out;
29820 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
29821 +       if (unlikely(err))
29822 +               goto out_dpages;
29823 +
29824 +       sigen = au_sigen(sb);
29825 +       sbinfo = au_sbi(sb);
29826 +       for (i = 0; i < dpages.ndpage; i++) {
29827 +               dpage = dpages.dpages + i;
29828 +               dentries = dpage->dentries;
29829 +               ndentry = dpage->ndentry;
29830 +               for (j = 0; j < ndentry; j++) {
29831 +                       d = dentries[j];
29832 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
29833 +                                           do_idop);
29834 +                       if (unlikely(e && !err))
29835 +                               err = e;
29836 +                       /* go on even err */
29837 +               }
29838 +       }
29839 +
29840 +out_dpages:
29841 +       au_dpages_free(&dpages);
29842 +out:
29843 +       return err;
29844 +}
29845 +
29846 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
29847 +{
29848 +       int err, e;
29849 +       unsigned int sigen;
29850 +       unsigned long long max, ull;
29851 +       struct inode *inode, **array;
29852 +
29853 +       array = au_iarray_alloc(sb, &max);
29854 +       err = PTR_ERR(array);
29855 +       if (IS_ERR(array))
29856 +               goto out;
29857 +
29858 +       err = 0;
29859 +       sigen = au_sigen(sb);
29860 +       for (ull = 0; ull < max; ull++) {
29861 +               inode = array[ull];
29862 +               if (unlikely(!inode))
29863 +                       break;
29864 +
29865 +               e = 0;
29866 +               ii_write_lock_child(inode);
29867 +               if (au_iigen(inode, NULL) != sigen) {
29868 +                       e = au_refresh_hinode_self(inode);
29869 +                       if (unlikely(e)) {
29870 +                               au_refresh_iop(inode, /*force_getattr*/1);
29871 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
29872 +                               if (!err)
29873 +                                       err = e;
29874 +                               /* go on even if err */
29875 +                       }
29876 +               }
29877 +               if (!e && do_idop)
29878 +                       au_refresh_iop(inode, /*force_getattr*/0);
29879 +               ii_write_unlock(inode);
29880 +       }
29881 +
29882 +       au_iarray_free(array, max);
29883 +
29884 +out:
29885 +       return err;
29886 +}
29887 +
29888 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
29889 +{
29890 +       int err, e;
29891 +       unsigned int udba;
29892 +       aufs_bindex_t bindex, bbot;
29893 +       struct dentry *root;
29894 +       struct inode *inode;
29895 +       struct au_branch *br;
29896 +       struct au_sbinfo *sbi;
29897 +
29898 +       au_sigen_inc(sb);
29899 +       sbi = au_sbi(sb);
29900 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
29901 +
29902 +       root = sb->s_root;
29903 +       DiMustNoWaiters(root);
29904 +       inode = d_inode(root);
29905 +       IiMustNoWaiters(inode);
29906 +
29907 +       udba = au_opt_udba(sb);
29908 +       bbot = au_sbbot(sb);
29909 +       for (bindex = 0; bindex <= bbot; bindex++) {
29910 +               br = au_sbr(sb, bindex);
29911 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
29912 +               if (unlikely(err))
29913 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
29914 +                               bindex, err);
29915 +               /* go on even if err */
29916 +       }
29917 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
29918 +
29919 +       if (do_idop) {
29920 +               if (au_ftest_si(sbi, NO_DREVAL)) {
29921 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
29922 +                       sb->s_d_op = &aufs_dop_noreval;
29923 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
29924 +                       sbi->si_iop_array = aufs_iop_nogetattr;
29925 +               } else {
29926 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
29927 +                       sb->s_d_op = &aufs_dop;
29928 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
29929 +                       sbi->si_iop_array = aufs_iop;
29930 +               }
29931 +               pr_info("reset to %ps and %ps\n",
29932 +                       sb->s_d_op, sbi->si_iop_array);
29933 +       }
29934 +
29935 +       di_write_unlock(root);
29936 +       err = au_refresh_d(sb, do_idop);
29937 +       e = au_refresh_i(sb, do_idop);
29938 +       if (unlikely(e && !err))
29939 +               err = e;
29940 +       /* aufs_write_lock() calls ..._child() */
29941 +       di_write_lock_child(root);
29942 +
29943 +       au_cpup_attr_all(inode, /*force*/1);
29944 +
29945 +       if (unlikely(err))
29946 +               AuIOErr("refresh failed, ignored, %d\n", err);
29947 +}
29948 +
29949 +/* stop extra interpretation of errno in mount(8), and strange error messages */
29950 +static int cvt_err(int err)
29951 +{
29952 +       AuTraceErr(err);
29953 +
29954 +       switch (err) {
29955 +       case -ENOENT:
29956 +       case -ENOTDIR:
29957 +       case -EEXIST:
29958 +       case -EIO:
29959 +               err = -EINVAL;
29960 +       }
29961 +       return err;
29962 +}
29963 +
29964 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
29965 +{
29966 +       int err, do_dx;
29967 +       unsigned int mntflags;
29968 +       struct au_opts opts = {
29969 +               .opt = NULL
29970 +       };
29971 +       struct dentry *root;
29972 +       struct inode *inode;
29973 +       struct au_sbinfo *sbinfo;
29974 +
29975 +       err = 0;
29976 +       root = sb->s_root;
29977 +       if (!data || !*data) {
29978 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29979 +               if (!err) {
29980 +                       di_write_lock_child(root);
29981 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
29982 +                       aufs_write_unlock(root);
29983 +               }
29984 +               goto out;
29985 +       }
29986 +
29987 +       err = -ENOMEM;
29988 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
29989 +       if (unlikely(!opts.opt))
29990 +               goto out;
29991 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
29992 +       opts.flags = AuOpts_REMOUNT;
29993 +       opts.sb_flags = *flags;
29994 +
29995 +       /* parse it before aufs lock */
29996 +       err = au_opts_parse(sb, data, &opts);
29997 +       if (unlikely(err))
29998 +               goto out_opts;
29999 +
30000 +       sbinfo = au_sbi(sb);
30001 +       inode = d_inode(root);
30002 +       inode_lock(inode);
30003 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30004 +       if (unlikely(err))
30005 +               goto out_mtx;
30006 +       di_write_lock_child(root);
30007 +
30008 +       /* au_opts_remount() may return an error */
30009 +       err = au_opts_remount(sb, &opts);
30010 +       au_opts_free(&opts);
30011 +
30012 +       if (au_ftest_opts(opts.flags, REFRESH))
30013 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30014 +
30015 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30016 +               mntflags = au_mntflags(sb);
30017 +               do_dx = !!au_opt_test(mntflags, DIO);
30018 +               au_dy_arefresh(do_dx);
30019 +       }
30020 +
30021 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30022 +       aufs_write_unlock(root);
30023 +
30024 +out_mtx:
30025 +       inode_unlock(inode);
30026 +out_opts:
30027 +       free_page((unsigned long)opts.opt);
30028 +out:
30029 +       err = cvt_err(err);
30030 +       AuTraceErr(err);
30031 +       return err;
30032 +}
30033 +
30034 +static const struct super_operations aufs_sop = {
30035 +       .alloc_inode    = aufs_alloc_inode,
30036 +       .destroy_inode  = aufs_destroy_inode,
30037 +       /* always deleting, no clearing */
30038 +       .drop_inode     = generic_delete_inode,
30039 +       .show_options   = aufs_show_options,
30040 +       .statfs         = aufs_statfs,
30041 +       .put_super      = aufs_put_super,
30042 +       .sync_fs        = aufs_sync_fs,
30043 +       .remount_fs     = aufs_remount_fs
30044 +};
30045 +
30046 +/* ---------------------------------------------------------------------- */
30047 +
30048 +static int alloc_root(struct super_block *sb)
30049 +{
30050 +       int err;
30051 +       struct inode *inode;
30052 +       struct dentry *root;
30053 +
30054 +       err = -ENOMEM;
30055 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30056 +       err = PTR_ERR(inode);
30057 +       if (IS_ERR(inode))
30058 +               goto out;
30059 +
30060 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30061 +       inode->i_fop = &aufs_dir_fop;
30062 +       inode->i_mode = S_IFDIR;
30063 +       set_nlink(inode, 2);
30064 +       unlock_new_inode(inode);
30065 +
30066 +       root = d_make_root(inode);
30067 +       if (unlikely(!root))
30068 +               goto out;
30069 +       err = PTR_ERR(root);
30070 +       if (IS_ERR(root))
30071 +               goto out;
30072 +
30073 +       err = au_di_init(root);
30074 +       if (!err) {
30075 +               sb->s_root = root;
30076 +               return 0; /* success */
30077 +       }
30078 +       dput(root);
30079 +
30080 +out:
30081 +       return err;
30082 +}
30083 +
30084 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30085 +                          int silent __maybe_unused)
30086 +{
30087 +       int err;
30088 +       struct au_opts opts = {
30089 +               .opt = NULL
30090 +       };
30091 +       struct au_sbinfo *sbinfo;
30092 +       struct dentry *root;
30093 +       struct inode *inode;
30094 +       char *arg = raw_data;
30095 +
30096 +       if (unlikely(!arg || !*arg)) {
30097 +               err = -EINVAL;
30098 +               pr_err("no arg\n");
30099 +               goto out;
30100 +       }
30101 +
30102 +       err = -ENOMEM;
30103 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30104 +       if (unlikely(!opts.opt))
30105 +               goto out;
30106 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30107 +       opts.sb_flags = sb->s_flags;
30108 +
30109 +       err = au_si_alloc(sb);
30110 +       if (unlikely(err))
30111 +               goto out_opts;
30112 +       sbinfo = au_sbi(sb);
30113 +
30114 +       /* all timestamps always follow the ones on the branch */
30115 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30116 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30117 +       sb->s_op = &aufs_sop;
30118 +       sb->s_d_op = &aufs_dop;
30119 +       sb->s_magic = AUFS_SUPER_MAGIC;
30120 +       sb->s_maxbytes = 0;
30121 +       sb->s_stack_depth = 1;
30122 +       au_export_init(sb);
30123 +       au_xattr_init(sb);
30124 +
30125 +       err = alloc_root(sb);
30126 +       if (unlikely(err)) {
30127 +               si_write_unlock(sb);
30128 +               goto out_info;
30129 +       }
30130 +       root = sb->s_root;
30131 +       inode = d_inode(root);
30132 +
30133 +       /*
30134 +        * actually we can parse options regardless aufs lock here.
30135 +        * but at remount time, parsing must be done before aufs lock.
30136 +        * so we follow the same rule.
30137 +        */
30138 +       ii_write_lock_parent(inode);
30139 +       aufs_write_unlock(root);
30140 +       err = au_opts_parse(sb, arg, &opts);
30141 +       if (unlikely(err))
30142 +               goto out_root;
30143 +
30144 +       /* lock vfs_inode first, then aufs. */
30145 +       inode_lock(inode);
30146 +       aufs_write_lock(root);
30147 +       err = au_opts_mount(sb, &opts);
30148 +       au_opts_free(&opts);
30149 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30150 +               sb->s_d_op = &aufs_dop_noreval;
30151 +               pr_info("%ps\n", sb->s_d_op);
30152 +               au_refresh_dop(root, /*force_reval*/0);
30153 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30154 +               au_refresh_iop(inode, /*force_getattr*/0);
30155 +       }
30156 +       aufs_write_unlock(root);
30157 +       inode_unlock(inode);
30158 +       if (!err)
30159 +               goto out_opts; /* success */
30160 +
30161 +out_root:
30162 +       dput(root);
30163 +       sb->s_root = NULL;
30164 +out_info:
30165 +       kobject_put(&sbinfo->si_kobj);
30166 +       sb->s_fs_info = NULL;
30167 +out_opts:
30168 +       free_page((unsigned long)opts.opt);
30169 +out:
30170 +       AuTraceErr(err);
30171 +       err = cvt_err(err);
30172 +       AuTraceErr(err);
30173 +       return err;
30174 +}
30175 +
30176 +/* ---------------------------------------------------------------------- */
30177 +
30178 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30179 +                                const char *dev_name __maybe_unused,
30180 +                                void *raw_data)
30181 +{
30182 +       struct dentry *root;
30183 +
30184 +       /* all timestamps always follow the ones on the branch */
30185 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30186 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30187 +       if (IS_ERR(root))
30188 +               goto out;
30189 +
30190 +       au_sbilist_add(root->d_sb);
30191 +
30192 +out:
30193 +       return root;
30194 +}
30195 +
30196 +static void aufs_kill_sb(struct super_block *sb)
30197 +{
30198 +       struct au_sbinfo *sbinfo;
30199 +
30200 +       sbinfo = au_sbi(sb);
30201 +       if (sbinfo) {
30202 +               au_sbilist_del(sb);
30203 +               aufs_write_lock(sb->s_root);
30204 +               au_fhsm_fin(sb);
30205 +               if (sbinfo->si_wbr_create_ops->fin)
30206 +                       sbinfo->si_wbr_create_ops->fin(sb);
30207 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30208 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30209 +                       au_remount_refresh(sb, /*do_idop*/0);
30210 +               }
30211 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30212 +                       au_plink_put(sb, /*verbose*/1);
30213 +               au_xino_clr(sb);
30214 +               au_dr_opt_flush(sb);
30215 +               sbinfo->si_sb = NULL;
30216 +               aufs_write_unlock(sb->s_root);
30217 +               au_nwt_flush(&sbinfo->si_nowait);
30218 +       }
30219 +       kill_anon_super(sb);
30220 +}
30221 +
30222 +struct file_system_type aufs_fs_type = {
30223 +       .name           = AUFS_FSTYPE,
30224 +       /* a race between rename and others */
30225 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30226 +       .mount          = aufs_mount,
30227 +       .kill_sb        = aufs_kill_sb,
30228 +       /* no need to __module_get() and module_put(). */
30229 +       .owner          = THIS_MODULE,
30230 +};
30231 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30232 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30233 +++ linux/fs/aufs/super.h       2018-08-12 23:43:05.463458173 +0200
30234 @@ -0,0 +1,627 @@
30235 +/* SPDX-License-Identifier: GPL-2.0 */
30236 +/*
30237 + * Copyright (C) 2005-2018 Junjiro R. Okajima
30238 + *
30239 + * This program, aufs is free software; you can redistribute it and/or modify
30240 + * it under the terms of the GNU General Public License as published by
30241 + * the Free Software Foundation; either version 2 of the License, or
30242 + * (at your option) any later version.
30243 + *
30244 + * This program is distributed in the hope that it will be useful,
30245 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30246 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30247 + * GNU General Public License for more details.
30248 + *
30249 + * You should have received a copy of the GNU General Public License
30250 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30251 + */
30252 +
30253 +/*
30254 + * super_block operations
30255 + */
30256 +
30257 +#ifndef __AUFS_SUPER_H__
30258 +#define __AUFS_SUPER_H__
30259 +
30260 +#ifdef __KERNEL__
30261 +
30262 +#include <linux/fs.h>
30263 +#include <linux/kobject.h>
30264 +#include "hbl.h"
30265 +#include "rwsem.h"
30266 +#include "wkq.h"
30267 +
30268 +/* policies to select one among multiple writable branches */
30269 +struct au_wbr_copyup_operations {
30270 +       int (*copyup)(struct dentry *dentry);
30271 +};
30272 +
30273 +#define AuWbr_DIR      1               /* target is a dir */
30274 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30275 +
30276 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30277 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30278 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30279 +
30280 +struct au_wbr_create_operations {
30281 +       int (*create)(struct dentry *dentry, unsigned int flags);
30282 +       int (*init)(struct super_block *sb);
30283 +       int (*fin)(struct super_block *sb);
30284 +};
30285 +
30286 +struct au_wbr_mfs {
30287 +       struct mutex    mfs_lock; /* protect this structure */
30288 +       unsigned long   mfs_jiffy;
30289 +       unsigned long   mfs_expire;
30290 +       aufs_bindex_t   mfs_bindex;
30291 +
30292 +       unsigned long long      mfsrr_bytes;
30293 +       unsigned long long      mfsrr_watermark;
30294 +};
30295 +
30296 +#define AuPlink_NHASH 100
30297 +static inline int au_plink_hash(ino_t ino)
30298 +{
30299 +       return ino % AuPlink_NHASH;
30300 +}
30301 +
30302 +/* File-based Hierarchical Storage Management */
30303 +struct au_fhsm {
30304 +#ifdef CONFIG_AUFS_FHSM
30305 +       /* allow only one process who can receive the notification */
30306 +       spinlock_t              fhsm_spin;
30307 +       pid_t                   fhsm_pid;
30308 +       wait_queue_head_t       fhsm_wqh;
30309 +       atomic_t                fhsm_readable;
30310 +
30311 +       /* these are protected by si_rwsem */
30312 +       unsigned long           fhsm_expire;
30313 +       aufs_bindex_t           fhsm_bottom;
30314 +#endif
30315 +};
30316 +
30317 +struct au_branch;
30318 +struct au_sbinfo {
30319 +       /* nowait tasks in the system-wide workqueue */
30320 +       struct au_nowait_tasks  si_nowait;
30321 +
30322 +       /*
30323 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
30324 +        * rwsem for au_sbinfo is necessary.
30325 +        */
30326 +       struct au_rwsem         si_rwsem;
30327 +
30328 +       /*
30329 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30330 +        * remount.
30331 +        */
30332 +       struct percpu_counter   si_ninodes, si_nfiles;
30333 +
30334 +       /* branch management */
30335 +       unsigned int            si_generation;
30336 +
30337 +       /* see AuSi_ flags */
30338 +       unsigned char           au_si_status;
30339 +
30340 +       aufs_bindex_t           si_bbot;
30341 +
30342 +       /* dirty trick to keep br_id plus */
30343 +       unsigned int            si_last_br_id :
30344 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30345 +       struct au_branch        **si_branch;
30346 +
30347 +       /* policy to select a writable branch */
30348 +       unsigned char           si_wbr_copyup;
30349 +       unsigned char           si_wbr_create;
30350 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30351 +       struct au_wbr_create_operations *si_wbr_create_ops;
30352 +
30353 +       /* round robin */
30354 +       atomic_t                si_wbr_rr_next;
30355 +
30356 +       /* most free space */
30357 +       struct au_wbr_mfs       si_wbr_mfs;
30358 +
30359 +       /* File-based Hierarchical Storage Management */
30360 +       struct au_fhsm          si_fhsm;
30361 +
30362 +       /* mount flags */
30363 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30364 +       unsigned int            si_mntflags;
30365 +
30366 +       /* external inode number (bitmap and translation table) */
30367 +       vfs_readf_t             si_xread;
30368 +       vfs_writef_t            si_xwrite;
30369 +       struct file             *si_xib;
30370 +       struct mutex            si_xib_mtx; /* protect xib members */
30371 +       unsigned long           *si_xib_buf;
30372 +       unsigned long           si_xib_last_pindex;
30373 +       int                     si_xib_next_bit;
30374 +       aufs_bindex_t           si_xino_brid;
30375 +       unsigned long           si_xino_jiffy;
30376 +       unsigned long           si_xino_expire;
30377 +       /* reserved for future use */
30378 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30379 +
30380 +#ifdef CONFIG_AUFS_EXPORT
30381 +       /* i_generation */
30382 +       struct file             *si_xigen;
30383 +       atomic_t                si_xigen_next;
30384 +#endif
30385 +
30386 +       /* dirty trick to suppoer atomic_open */
30387 +       struct hlist_bl_head    si_aopen;
30388 +
30389 +       /* vdir parameters */
30390 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30391 +       unsigned int            si_rdblk;       /* deblk size */
30392 +       unsigned int            si_rdhash;      /* hash size */
30393 +
30394 +       /*
30395 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30396 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30397 +        * future fsck.aufs or kernel thread will remove them later.
30398 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30399 +        */
30400 +       unsigned int            si_dirwh;
30401 +
30402 +       /* pseudo_link list */
30403 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30404 +       wait_queue_head_t       si_plink_wq;
30405 +       spinlock_t              si_plink_maint_lock;
30406 +       pid_t                   si_plink_maint_pid;
30407 +
30408 +       /* file list */
30409 +       struct hlist_bl_head    si_files;
30410 +
30411 +       /* with/without getattr, brother of sb->s_d_op */
30412 +       struct inode_operations *si_iop_array;
30413 +
30414 +       /*
30415 +        * sysfs and lifetime management.
30416 +        * this is not a small structure and it may be a waste of memory in case
30417 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
30418 +        * but using sysfs is majority.
30419 +        */
30420 +       struct kobject          si_kobj;
30421 +#ifdef CONFIG_DEBUG_FS
30422 +       struct dentry            *si_dbgaufs;
30423 +       struct dentry            *si_dbgaufs_plink;
30424 +       struct dentry            *si_dbgaufs_xib;
30425 +#ifdef CONFIG_AUFS_EXPORT
30426 +       struct dentry            *si_dbgaufs_xigen;
30427 +#endif
30428 +#endif
30429 +
30430 +#ifdef CONFIG_AUFS_SBILIST
30431 +       struct hlist_bl_node    si_list;
30432 +#endif
30433 +
30434 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30435 +       struct super_block      *si_sb;
30436 +};
30437 +
30438 +/* sbinfo status flags */
30439 +/*
30440 + * set true when refresh_dirs() failed at remount time.
30441 + * then try refreshing dirs at access time again.
30442 + * if it is false, refreshing dirs at access time is unnecessary
30443 + */
30444 +#define AuSi_FAILED_REFRESH_DIR        1
30445 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30446 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30447 +
30448 +#ifndef CONFIG_AUFS_FHSM
30449 +#undef AuSi_FHSM
30450 +#define AuSi_FHSM              0
30451 +#endif
30452 +
30453 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30454 +                                          unsigned int flag)
30455 +{
30456 +       AuRwMustAnyLock(&sbi->si_rwsem);
30457 +       return sbi->au_si_status & flag;
30458 +}
30459 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30460 +#define au_fset_si(sbinfo, name) do { \
30461 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30462 +       (sbinfo)->au_si_status |= AuSi_##name; \
30463 +} while (0)
30464 +#define au_fclr_si(sbinfo, name) do { \
30465 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30466 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30467 +} while (0)
30468 +
30469 +/* ---------------------------------------------------------------------- */
30470 +
30471 +/* policy to select one among writable branches */
30472 +#define AuWbrCopyup(sbinfo, ...) \
30473 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30474 +#define AuWbrCreate(sbinfo, ...) \
30475 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30476 +
30477 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30478 +#define AuLock_DW              1               /* write-lock dentry */
30479 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30480 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30481 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30482 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30483 +                                               /* except RENAME_EXCHANGE */
30484 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30485 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30486 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30487 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30488 +#define au_fset_lock(flags, name) \
30489 +       do { (flags) |= AuLock_##name; } while (0)
30490 +#define au_fclr_lock(flags, name) \
30491 +       do { (flags) &= ~AuLock_##name; } while (0)
30492 +
30493 +/* ---------------------------------------------------------------------- */
30494 +
30495 +/* super.c */
30496 +extern struct file_system_type aufs_fs_type;
30497 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30498 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30499 +                                          unsigned long long max, void *arg);
30500 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30501 +                    struct super_block *sb, void *arg);
30502 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30503 +void au_iarray_free(struct inode **a, unsigned long long max);
30504 +
30505 +/* sbinfo.c */
30506 +void au_si_free(struct kobject *kobj);
30507 +int au_si_alloc(struct super_block *sb);
30508 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30509 +
30510 +unsigned int au_sigen_inc(struct super_block *sb);
30511 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30512 +
30513 +int si_read_lock(struct super_block *sb, int flags);
30514 +int si_write_lock(struct super_block *sb, int flags);
30515 +int aufs_read_lock(struct dentry *dentry, int flags);
30516 +void aufs_read_unlock(struct dentry *dentry, int flags);
30517 +void aufs_write_lock(struct dentry *dentry);
30518 +void aufs_write_unlock(struct dentry *dentry);
30519 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30520 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30521 +
30522 +/* wbr_policy.c */
30523 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30524 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30525 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30526 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30527 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30528 +
30529 +/* mvdown.c */
30530 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30531 +
30532 +#ifdef CONFIG_AUFS_FHSM
30533 +/* fhsm.c */
30534 +
30535 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30536 +{
30537 +       pid_t pid;
30538 +
30539 +       spin_lock(&fhsm->fhsm_spin);
30540 +       pid = fhsm->fhsm_pid;
30541 +       spin_unlock(&fhsm->fhsm_spin);
30542 +
30543 +       return pid;
30544 +}
30545 +
30546 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30547 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30548 +int au_fhsm_fd(struct super_block *sb, int oflags);
30549 +int au_fhsm_br_alloc(struct au_branch *br);
30550 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30551 +void au_fhsm_fin(struct super_block *sb);
30552 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30553 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30554 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30555 +#else
30556 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30557 +          int force)
30558 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30559 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30560 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30561 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30562 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30563 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30564 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30565 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30566 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30567 +#endif
30568 +
30569 +/* ---------------------------------------------------------------------- */
30570 +
30571 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30572 +{
30573 +       return sb->s_fs_info;
30574 +}
30575 +
30576 +/* ---------------------------------------------------------------------- */
30577 +
30578 +#ifdef CONFIG_AUFS_EXPORT
30579 +int au_test_nfsd(void);
30580 +void au_export_init(struct super_block *sb);
30581 +void au_xigen_inc(struct inode *inode);
30582 +int au_xigen_new(struct inode *inode);
30583 +int au_xigen_set(struct super_block *sb, struct path *path);
30584 +void au_xigen_clr(struct super_block *sb);
30585 +
30586 +static inline int au_busy_or_stale(void)
30587 +{
30588 +       if (!au_test_nfsd())
30589 +               return -EBUSY;
30590 +       return -ESTALE;
30591 +}
30592 +#else
30593 +AuStubInt0(au_test_nfsd, void)
30594 +AuStubVoid(au_export_init, struct super_block *sb)
30595 +AuStubVoid(au_xigen_inc, struct inode *inode)
30596 +AuStubInt0(au_xigen_new, struct inode *inode)
30597 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30598 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30599 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30600 +#endif /* CONFIG_AUFS_EXPORT */
30601 +
30602 +/* ---------------------------------------------------------------------- */
30603 +
30604 +#ifdef CONFIG_AUFS_SBILIST
30605 +/* module.c */
30606 +extern struct hlist_bl_head au_sbilist;
30607 +
30608 +static inline void au_sbilist_init(void)
30609 +{
30610 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30611 +}
30612 +
30613 +static inline void au_sbilist_add(struct super_block *sb)
30614 +{
30615 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30616 +}
30617 +
30618 +static inline void au_sbilist_del(struct super_block *sb)
30619 +{
30620 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30621 +}
30622 +
30623 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30624 +static inline void au_sbilist_lock(void)
30625 +{
30626 +       hlist_bl_lock(&au_sbilist);
30627 +}
30628 +
30629 +static inline void au_sbilist_unlock(void)
30630 +{
30631 +       hlist_bl_unlock(&au_sbilist);
30632 +}
30633 +#define AuGFP_SBILIST  GFP_ATOMIC
30634 +#else
30635 +AuStubVoid(au_sbilist_lock, void)
30636 +AuStubVoid(au_sbilist_unlock, void)
30637 +#define AuGFP_SBILIST  GFP_NOFS
30638 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30639 +#else
30640 +AuStubVoid(au_sbilist_init, void)
30641 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30642 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30643 +AuStubVoid(au_sbilist_lock, void)
30644 +AuStubVoid(au_sbilist_unlock, void)
30645 +#define AuGFP_SBILIST  GFP_NOFS
30646 +#endif
30647 +
30648 +/* ---------------------------------------------------------------------- */
30649 +
30650 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30651 +{
30652 +       /*
30653 +        * This function is a dynamic '__init' function actually,
30654 +        * so the tiny check for si_rwsem is unnecessary.
30655 +        */
30656 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30657 +#ifdef CONFIG_DEBUG_FS
30658 +       sbinfo->si_dbgaufs = NULL;
30659 +       sbinfo->si_dbgaufs_plink = NULL;
30660 +       sbinfo->si_dbgaufs_xib = NULL;
30661 +#ifdef CONFIG_AUFS_EXPORT
30662 +       sbinfo->si_dbgaufs_xigen = NULL;
30663 +#endif
30664 +#endif
30665 +}
30666 +
30667 +/* ---------------------------------------------------------------------- */
30668 +
30669 +/* current->atomic_flags */
30670 +/* this value should never corrupt the ones defined in linux/sched.h */
30671 +#define PFA_AUFS       7
30672 +
30673 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30674 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30675 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30676 +
30677 +static inline int si_pid_test(struct super_block *sb)
30678 +{
30679 +       return !!task_test_aufs(current);
30680 +}
30681 +
30682 +static inline void si_pid_clr(struct super_block *sb)
30683 +{
30684 +       AuDebugOn(!task_test_aufs(current));
30685 +       task_clear_aufs(current);
30686 +}
30687 +
30688 +static inline void si_pid_set(struct super_block *sb)
30689 +{
30690 +       AuDebugOn(task_test_aufs(current));
30691 +       task_set_aufs(current);
30692 +}
30693 +
30694 +/* ---------------------------------------------------------------------- */
30695 +
30696 +/* lock superblock. mainly for entry point functions */
30697 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30698 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30699 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30700 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30701 +/*
30702 +#define __si_read_trylock_nested(sb) \
30703 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30704 +#define __si_write_trylock_nested(sb) \
30705 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30706 +*/
30707 +
30708 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30709 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30710 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30711 +
30712 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30713 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30714 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30715 +
30716 +static inline void si_noflush_read_lock(struct super_block *sb)
30717 +{
30718 +       __si_read_lock(sb);
30719 +       si_pid_set(sb);
30720 +}
30721 +
30722 +static inline int si_noflush_read_trylock(struct super_block *sb)
30723 +{
30724 +       int locked;
30725 +
30726 +       locked = __si_read_trylock(sb);
30727 +       if (locked)
30728 +               si_pid_set(sb);
30729 +       return locked;
30730 +}
30731 +
30732 +static inline void si_noflush_write_lock(struct super_block *sb)
30733 +{
30734 +       __si_write_lock(sb);
30735 +       si_pid_set(sb);
30736 +}
30737 +
30738 +static inline int si_noflush_write_trylock(struct super_block *sb)
30739 +{
30740 +       int locked;
30741 +
30742 +       locked = __si_write_trylock(sb);
30743 +       if (locked)
30744 +               si_pid_set(sb);
30745 +       return locked;
30746 +}
30747 +
30748 +#if 0 /* reserved */
30749 +static inline int si_read_trylock(struct super_block *sb, int flags)
30750 +{
30751 +       if (au_ftest_lock(flags, FLUSH))
30752 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30753 +       return si_noflush_read_trylock(sb);
30754 +}
30755 +#endif
30756 +
30757 +static inline void si_read_unlock(struct super_block *sb)
30758 +{
30759 +       si_pid_clr(sb);
30760 +       __si_read_unlock(sb);
30761 +}
30762 +
30763 +#if 0 /* reserved */
30764 +static inline int si_write_trylock(struct super_block *sb, int flags)
30765 +{
30766 +       if (au_ftest_lock(flags, FLUSH))
30767 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30768 +       return si_noflush_write_trylock(sb);
30769 +}
30770 +#endif
30771 +
30772 +static inline void si_write_unlock(struct super_block *sb)
30773 +{
30774 +       si_pid_clr(sb);
30775 +       __si_write_unlock(sb);
30776 +}
30777 +
30778 +#if 0 /* reserved */
30779 +static inline void si_downgrade_lock(struct super_block *sb)
30780 +{
30781 +       __si_downgrade_lock(sb);
30782 +}
30783 +#endif
30784 +
30785 +/* ---------------------------------------------------------------------- */
30786 +
30787 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30788 +{
30789 +       SiMustAnyLock(sb);
30790 +       return au_sbi(sb)->si_bbot;
30791 +}
30792 +
30793 +static inline unsigned int au_mntflags(struct super_block *sb)
30794 +{
30795 +       SiMustAnyLock(sb);
30796 +       return au_sbi(sb)->si_mntflags;
30797 +}
30798 +
30799 +static inline unsigned int au_sigen(struct super_block *sb)
30800 +{
30801 +       SiMustAnyLock(sb);
30802 +       return au_sbi(sb)->si_generation;
30803 +}
30804 +
30805 +static inline unsigned long long au_ninodes(struct super_block *sb)
30806 +{
30807 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
30808 +
30809 +       BUG_ON(n < 0);
30810 +       return n;
30811 +}
30812 +
30813 +static inline void au_ninodes_inc(struct super_block *sb)
30814 +{
30815 +       percpu_counter_inc(&au_sbi(sb)->si_ninodes);
30816 +}
30817 +
30818 +static inline void au_ninodes_dec(struct super_block *sb)
30819 +{
30820 +       percpu_counter_dec(&au_sbi(sb)->si_ninodes);
30821 +}
30822 +
30823 +static inline unsigned long long au_nfiles(struct super_block *sb)
30824 +{
30825 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
30826 +
30827 +       BUG_ON(n < 0);
30828 +       return n;
30829 +}
30830 +
30831 +static inline void au_nfiles_inc(struct super_block *sb)
30832 +{
30833 +       percpu_counter_inc(&au_sbi(sb)->si_nfiles);
30834 +}
30835 +
30836 +static inline void au_nfiles_dec(struct super_block *sb)
30837 +{
30838 +       percpu_counter_dec(&au_sbi(sb)->si_nfiles);
30839 +}
30840 +
30841 +static inline struct au_branch *au_sbr(struct super_block *sb,
30842 +                                      aufs_bindex_t bindex)
30843 +{
30844 +       SiMustAnyLock(sb);
30845 +       return au_sbi(sb)->si_branch[0 + bindex];
30846 +}
30847 +
30848 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
30849 +{
30850 +       SiMustWriteLock(sb);
30851 +       au_sbi(sb)->si_xino_brid = brid;
30852 +}
30853 +
30854 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
30855 +{
30856 +       SiMustAnyLock(sb);
30857 +       return au_sbi(sb)->si_xino_brid;
30858 +}
30859 +
30860 +#endif /* __KERNEL__ */
30861 +#endif /* __AUFS_SUPER_H__ */
30862 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30863 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
30864 +++ linux/fs/aufs/sysaufs.c     2018-08-12 23:43:05.463458173 +0200
30865 @@ -0,0 +1,93 @@
30866 +// SPDX-License-Identifier: GPL-2.0
30867 +/*
30868 + * Copyright (C) 2005-2018 Junjiro R. Okajima
30869 + *
30870 + * This program, aufs is free software; you can redistribute it and/or modify
30871 + * it under the terms of the GNU General Public License as published by
30872 + * the Free Software Foundation; either version 2 of the License, or
30873 + * (at your option) any later version.
30874 + *
30875 + * This program is distributed in the hope that it will be useful,
30876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30878 + * GNU General Public License for more details.
30879 + *
30880 + * You should have received a copy of the GNU General Public License
30881 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30882 + */
30883 +
30884 +/*
30885 + * sysfs interface and lifetime management
30886 + * they are necessary regardless sysfs is disabled.
30887 + */
30888 +
30889 +#include <linux/random.h>
30890 +#include "aufs.h"
30891 +
30892 +unsigned long sysaufs_si_mask;
30893 +struct kset *sysaufs_kset;
30894 +
30895 +#define AuSiAttr(_name) { \
30896 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
30897 +       .show   = sysaufs_si_##_name,                           \
30898 +}
30899 +
30900 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30901 +struct attribute *sysaufs_si_attrs[] = {
30902 +       &sysaufs_si_attr_xi_path.attr,
30903 +       NULL,
30904 +};
30905 +
30906 +static const struct sysfs_ops au_sbi_ops = {
30907 +       .show   = sysaufs_si_show
30908 +};
30909 +
30910 +static struct kobj_type au_sbi_ktype = {
30911 +       .release        = au_si_free,
30912 +       .sysfs_ops      = &au_sbi_ops,
30913 +       .default_attrs  = sysaufs_si_attrs
30914 +};
30915 +
30916 +/* ---------------------------------------------------------------------- */
30917 +
30918 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
30919 +{
30920 +       int err;
30921 +
30922 +       sbinfo->si_kobj.kset = sysaufs_kset;
30923 +       /* cf. sysaufs_name() */
30924 +       err = kobject_init_and_add
30925 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
30926 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30927 +
30928 +       return err;
30929 +}
30930 +
30931 +void sysaufs_fin(void)
30932 +{
30933 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30934 +       kset_unregister(sysaufs_kset);
30935 +}
30936 +
30937 +int __init sysaufs_init(void)
30938 +{
30939 +       int err;
30940 +
30941 +       do {
30942 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30943 +       } while (!sysaufs_si_mask);
30944 +
30945 +       err = -EINVAL;
30946 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30947 +       if (unlikely(!sysaufs_kset))
30948 +               goto out;
30949 +       err = PTR_ERR(sysaufs_kset);
30950 +       if (IS_ERR(sysaufs_kset))
30951 +               goto out;
30952 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30953 +       if (unlikely(err))
30954 +               kset_unregister(sysaufs_kset);
30955 +
30956 +out:
30957 +       return err;
30958 +}
30959 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
30960 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
30961 +++ linux/fs/aufs/sysaufs.h     2018-08-12 23:43:05.463458173 +0200
30962 @@ -0,0 +1,102 @@
30963 +/* SPDX-License-Identifier: GPL-2.0 */
30964 +/*
30965 + * Copyright (C) 2005-2018 Junjiro R. Okajima
30966 + *
30967 + * This program, aufs is free software; you can redistribute it and/or modify
30968 + * it under the terms of the GNU General Public License as published by
30969 + * the Free Software Foundation; either version 2 of the License, or
30970 + * (at your option) any later version.
30971 + *
30972 + * This program is distributed in the hope that it will be useful,
30973 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30974 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30975 + * GNU General Public License for more details.
30976 + *
30977 + * You should have received a copy of the GNU General Public License
30978 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30979 + */
30980 +
30981 +/*
30982 + * sysfs interface and mount lifetime management
30983 + */
30984 +
30985 +#ifndef __SYSAUFS_H__
30986 +#define __SYSAUFS_H__
30987 +
30988 +#ifdef __KERNEL__
30989 +
30990 +#include <linux/sysfs.h>
30991 +#include "module.h"
30992 +
30993 +struct super_block;
30994 +struct au_sbinfo;
30995 +
30996 +struct sysaufs_si_attr {
30997 +       struct attribute attr;
30998 +       int (*show)(struct seq_file *seq, struct super_block *sb);
30999 +};
31000 +
31001 +/* ---------------------------------------------------------------------- */
31002 +
31003 +/* sysaufs.c */
31004 +extern unsigned long sysaufs_si_mask;
31005 +extern struct kset *sysaufs_kset;
31006 +extern struct attribute *sysaufs_si_attrs[];
31007 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31008 +int __init sysaufs_init(void);
31009 +void sysaufs_fin(void);
31010 +
31011 +/* ---------------------------------------------------------------------- */
31012 +
31013 +/* some people doesn't like to show a pointer in kernel */
31014 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31015 +{
31016 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31017 +}
31018 +
31019 +#define SysaufsSiNamePrefix    "si_"
31020 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31021 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31022 +{
31023 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31024 +                sysaufs_si_id(sbinfo));
31025 +}
31026 +
31027 +struct au_branch;
31028 +#ifdef CONFIG_SYSFS
31029 +/* sysfs.c */
31030 +extern struct attribute_group *sysaufs_attr_group;
31031 +
31032 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31033 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31034 +                        char *buf);
31035 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31036 +#ifdef CONFIG_COMPAT
31037 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31038 +#endif
31039 +
31040 +void sysaufs_br_init(struct au_branch *br);
31041 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31042 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31043 +
31044 +#define sysaufs_brs_init()     do {} while (0)
31045 +
31046 +#else
31047 +#define sysaufs_attr_group     NULL
31048 +
31049 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31050 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31051 +       struct attribute *attr, char *buf)
31052 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31053 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31054 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31055 +
31056 +static inline void sysaufs_brs_init(void)
31057 +{
31058 +       sysaufs_brs = 0;
31059 +}
31060 +
31061 +#endif /* CONFIG_SYSFS */
31062 +
31063 +#endif /* __KERNEL__ */
31064 +#endif /* __SYSAUFS_H__ */
31065 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31066 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31067 +++ linux/fs/aufs/sysfs.c       2018-08-12 23:43:05.463458173 +0200
31068 @@ -0,0 +1,373 @@
31069 +// SPDX-License-Identifier: GPL-2.0
31070 +/*
31071 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31072 + *
31073 + * This program, aufs is free software; you can redistribute it and/or modify
31074 + * it under the terms of the GNU General Public License as published by
31075 + * the Free Software Foundation; either version 2 of the License, or
31076 + * (at your option) any later version.
31077 + *
31078 + * This program is distributed in the hope that it will be useful,
31079 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31080 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31081 + * GNU General Public License for more details.
31082 + *
31083 + * You should have received a copy of the GNU General Public License
31084 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31085 + */
31086 +
31087 +/*
31088 + * sysfs interface
31089 + */
31090 +
31091 +#include <linux/compat.h>
31092 +#include <linux/seq_file.h>
31093 +#include "aufs.h"
31094 +
31095 +#ifdef CONFIG_AUFS_FS_MODULE
31096 +/* this entry violates the "one line per file" policy of sysfs */
31097 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31098 +                          char *buf)
31099 +{
31100 +       ssize_t err;
31101 +       static char *conf =
31102 +/* this file is generated at compiling */
31103 +#include "conf.str"
31104 +               ;
31105 +
31106 +       err = snprintf(buf, PAGE_SIZE, conf);
31107 +       if (unlikely(err >= PAGE_SIZE))
31108 +               err = -EFBIG;
31109 +       return err;
31110 +}
31111 +
31112 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31113 +#endif
31114 +
31115 +static struct attribute *au_attr[] = {
31116 +#ifdef CONFIG_AUFS_FS_MODULE
31117 +       &au_config_attr.attr,
31118 +#endif
31119 +       NULL,   /* need to NULL terminate the list of attributes */
31120 +};
31121 +
31122 +static struct attribute_group sysaufs_attr_group_body = {
31123 +       .attrs = au_attr
31124 +};
31125 +
31126 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31127 +
31128 +/* ---------------------------------------------------------------------- */
31129 +
31130 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31131 +{
31132 +       int err;
31133 +
31134 +       SiMustAnyLock(sb);
31135 +
31136 +       err = 0;
31137 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31138 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31139 +               seq_putc(seq, '\n');
31140 +       }
31141 +       return err;
31142 +}
31143 +
31144 +/*
31145 + * the lifetime of branch is independent from the entry under sysfs.
31146 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31147 + * unlinked.
31148 + */
31149 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31150 +                        aufs_bindex_t bindex, int idx)
31151 +{
31152 +       int err;
31153 +       struct path path;
31154 +       struct dentry *root;
31155 +       struct au_branch *br;
31156 +       au_br_perm_str_t perm;
31157 +
31158 +       AuDbg("b%d\n", bindex);
31159 +
31160 +       err = 0;
31161 +       root = sb->s_root;
31162 +       di_read_lock_parent(root, !AuLock_IR);
31163 +       br = au_sbr(sb, bindex);
31164 +
31165 +       switch (idx) {
31166 +       case AuBrSysfs_BR:
31167 +               path.mnt = au_br_mnt(br);
31168 +               path.dentry = au_h_dptr(root, bindex);
31169 +               err = au_seq_path(seq, &path);
31170 +               if (!err) {
31171 +                       au_optstr_br_perm(&perm, br->br_perm);
31172 +                       seq_printf(seq, "=%s\n", perm.a);
31173 +               }
31174 +               break;
31175 +       case AuBrSysfs_BRID:
31176 +               seq_printf(seq, "%d\n", br->br_id);
31177 +               break;
31178 +       }
31179 +       di_read_unlock(root, !AuLock_IR);
31180 +       if (unlikely(err || seq_has_overflowed(seq)))
31181 +               err = -E2BIG;
31182 +
31183 +       return err;
31184 +}
31185 +
31186 +/* ---------------------------------------------------------------------- */
31187 +
31188 +static struct seq_file *au_seq(char *p, ssize_t len)
31189 +{
31190 +       struct seq_file *seq;
31191 +
31192 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31193 +       if (seq) {
31194 +               /* mutex_init(&seq.lock); */
31195 +               seq->buf = p;
31196 +               seq->size = len;
31197 +               return seq; /* success */
31198 +       }
31199 +
31200 +       seq = ERR_PTR(-ENOMEM);
31201 +       return seq;
31202 +}
31203 +
31204 +#define SysaufsBr_PREFIX       "br"
31205 +#define SysaufsBrid_PREFIX     "brid"
31206 +
31207 +/* todo: file size may exceed PAGE_SIZE */
31208 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31209 +                       char *buf)
31210 +{
31211 +       ssize_t err;
31212 +       int idx;
31213 +       long l;
31214 +       aufs_bindex_t bbot;
31215 +       struct au_sbinfo *sbinfo;
31216 +       struct super_block *sb;
31217 +       struct seq_file *seq;
31218 +       char *name;
31219 +       struct attribute **cattr;
31220 +
31221 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31222 +       sb = sbinfo->si_sb;
31223 +
31224 +       /*
31225 +        * prevent a race condition between sysfs and aufs.
31226 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31227 +        * prohibits maintaining the sysfs entries.
31228 +        * hew we acquire read lock after sysfs_get_active_two().
31229 +        * on the other hand, the remount process may maintain the sysfs/aufs
31230 +        * entries after acquiring write lock.
31231 +        * it can cause a deadlock.
31232 +        * simply we gave up processing read here.
31233 +        */
31234 +       err = -EBUSY;
31235 +       if (unlikely(!si_noflush_read_trylock(sb)))
31236 +               goto out;
31237 +
31238 +       seq = au_seq(buf, PAGE_SIZE);
31239 +       err = PTR_ERR(seq);
31240 +       if (IS_ERR(seq))
31241 +               goto out_unlock;
31242 +
31243 +       name = (void *)attr->name;
31244 +       cattr = sysaufs_si_attrs;
31245 +       while (*cattr) {
31246 +               if (!strcmp(name, (*cattr)->name)) {
31247 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31248 +                               ->show(seq, sb);
31249 +                       goto out_seq;
31250 +               }
31251 +               cattr++;
31252 +       }
31253 +
31254 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31255 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31256 +               idx = AuBrSysfs_BRID;
31257 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31258 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31259 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31260 +               idx = AuBrSysfs_BR;
31261 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31262 +       } else
31263 +                 BUG();
31264 +
31265 +       err = kstrtol(name, 10, &l);
31266 +       if (!err) {
31267 +               bbot = au_sbbot(sb);
31268 +               if (l <= bbot)
31269 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31270 +               else
31271 +                       err = -ENOENT;
31272 +       }
31273 +
31274 +out_seq:
31275 +       if (!err) {
31276 +               err = seq->count;
31277 +               /* sysfs limit */
31278 +               if (unlikely(err == PAGE_SIZE))
31279 +                       err = -EFBIG;
31280 +       }
31281 +       kfree(seq);
31282 +out_unlock:
31283 +       si_read_unlock(sb);
31284 +out:
31285 +       return err;
31286 +}
31287 +
31288 +/* ---------------------------------------------------------------------- */
31289 +
31290 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31291 +{
31292 +       int err;
31293 +       int16_t brid;
31294 +       aufs_bindex_t bindex, bbot;
31295 +       size_t sz;
31296 +       char *buf;
31297 +       struct seq_file *seq;
31298 +       struct au_branch *br;
31299 +
31300 +       si_read_lock(sb, AuLock_FLUSH);
31301 +       bbot = au_sbbot(sb);
31302 +       err = bbot + 1;
31303 +       if (!arg)
31304 +               goto out;
31305 +
31306 +       err = -ENOMEM;
31307 +       buf = (void *)__get_free_page(GFP_NOFS);
31308 +       if (unlikely(!buf))
31309 +               goto out;
31310 +
31311 +       seq = au_seq(buf, PAGE_SIZE);
31312 +       err = PTR_ERR(seq);
31313 +       if (IS_ERR(seq))
31314 +               goto out_buf;
31315 +
31316 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31317 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31318 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31319 +               if (unlikely(err))
31320 +                       break;
31321 +
31322 +               br = au_sbr(sb, bindex);
31323 +               brid = br->br_id;
31324 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31325 +               err = __put_user(brid, &arg->id);
31326 +               if (unlikely(err))
31327 +                       break;
31328 +
31329 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31330 +               err = __put_user(br->br_perm, &arg->perm);
31331 +               if (unlikely(err))
31332 +                       break;
31333 +
31334 +               err = au_seq_path(seq, &br->br_path);
31335 +               if (unlikely(err))
31336 +                       break;
31337 +               seq_putc(seq, '\0');
31338 +               if (!seq_has_overflowed(seq)) {
31339 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31340 +                       seq->count = 0;
31341 +                       if (unlikely(err))
31342 +                               break;
31343 +               } else {
31344 +                       err = -E2BIG;
31345 +                       goto out_seq;
31346 +               }
31347 +       }
31348 +       if (unlikely(err))
31349 +               err = -EFAULT;
31350 +
31351 +out_seq:
31352 +       kfree(seq);
31353 +out_buf:
31354 +       free_page((unsigned long)buf);
31355 +out:
31356 +       si_read_unlock(sb);
31357 +       return err;
31358 +}
31359 +
31360 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31361 +{
31362 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31363 +}
31364 +
31365 +#ifdef CONFIG_COMPAT
31366 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31367 +{
31368 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31369 +}
31370 +#endif
31371 +
31372 +/* ---------------------------------------------------------------------- */
31373 +
31374 +void sysaufs_br_init(struct au_branch *br)
31375 +{
31376 +       int i;
31377 +       struct au_brsysfs *br_sysfs;
31378 +       struct attribute *attr;
31379 +
31380 +       br_sysfs = br->br_sysfs;
31381 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31382 +               attr = &br_sysfs->attr;
31383 +               sysfs_attr_init(attr);
31384 +               attr->name = br_sysfs->name;
31385 +               attr->mode = 0444;
31386 +               br_sysfs++;
31387 +       }
31388 +}
31389 +
31390 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31391 +{
31392 +       struct au_branch *br;
31393 +       struct kobject *kobj;
31394 +       struct au_brsysfs *br_sysfs;
31395 +       int i;
31396 +       aufs_bindex_t bbot;
31397 +
31398 +       if (!sysaufs_brs)
31399 +               return;
31400 +
31401 +       kobj = &au_sbi(sb)->si_kobj;
31402 +       bbot = au_sbbot(sb);
31403 +       for (; bindex <= bbot; bindex++) {
31404 +               br = au_sbr(sb, bindex);
31405 +               br_sysfs = br->br_sysfs;
31406 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31407 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31408 +                       br_sysfs++;
31409 +               }
31410 +       }
31411 +}
31412 +
31413 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31414 +{
31415 +       int err, i;
31416 +       aufs_bindex_t bbot;
31417 +       struct kobject *kobj;
31418 +       struct au_branch *br;
31419 +       struct au_brsysfs *br_sysfs;
31420 +
31421 +       if (!sysaufs_brs)
31422 +               return;
31423 +
31424 +       kobj = &au_sbi(sb)->si_kobj;
31425 +       bbot = au_sbbot(sb);
31426 +       for (; bindex <= bbot; bindex++) {
31427 +               br = au_sbr(sb, bindex);
31428 +               br_sysfs = br->br_sysfs;
31429 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31430 +                        SysaufsBr_PREFIX "%d", bindex);
31431 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31432 +                        SysaufsBrid_PREFIX "%d", bindex);
31433 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31434 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31435 +                       if (unlikely(err))
31436 +                               pr_warn("failed %s under sysfs(%d)\n",
31437 +                                       br_sysfs->name, err);
31438 +                       br_sysfs++;
31439 +               }
31440 +       }
31441 +}
31442 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31443 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31444 +++ linux/fs/aufs/sysrq.c       2018-08-12 23:43:05.463458173 +0200
31445 @@ -0,0 +1,160 @@
31446 +// SPDX-License-Identifier: GPL-2.0
31447 +/*
31448 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31449 + *
31450 + * This program, aufs is free software; you can redistribute it and/or modify
31451 + * it under the terms of the GNU General Public License as published by
31452 + * the Free Software Foundation; either version 2 of the License, or
31453 + * (at your option) any later version.
31454 + *
31455 + * This program is distributed in the hope that it will be useful,
31456 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31457 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31458 + * GNU General Public License for more details.
31459 + *
31460 + * You should have received a copy of the GNU General Public License
31461 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31462 + */
31463 +
31464 +/*
31465 + * magic sysrq hanlder
31466 + */
31467 +
31468 +/* #include <linux/sysrq.h> */
31469 +#include <linux/writeback.h>
31470 +#include "aufs.h"
31471 +
31472 +/* ---------------------------------------------------------------------- */
31473 +
31474 +static void sysrq_sb(struct super_block *sb)
31475 +{
31476 +       char *plevel;
31477 +       struct au_sbinfo *sbinfo;
31478 +       struct file *file;
31479 +       struct hlist_bl_head *files;
31480 +       struct hlist_bl_node *pos;
31481 +       struct au_finfo *finfo;
31482 +
31483 +       plevel = au_plevel;
31484 +       au_plevel = KERN_WARNING;
31485 +
31486 +       /* since we define pr_fmt, call printk directly */
31487 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31488 +
31489 +       sbinfo = au_sbi(sb);
31490 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31491 +       pr("superblock\n");
31492 +       au_dpri_sb(sb);
31493 +
31494 +#if 0
31495 +       pr("root dentry\n");
31496 +       au_dpri_dentry(sb->s_root);
31497 +       pr("root inode\n");
31498 +       au_dpri_inode(d_inode(sb->s_root));
31499 +#endif
31500 +
31501 +#if 0
31502 +       do {
31503 +               int err, i, j, ndentry;
31504 +               struct au_dcsub_pages dpages;
31505 +               struct au_dpage *dpage;
31506 +
31507 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31508 +               if (unlikely(err))
31509 +                       break;
31510 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31511 +               if (!err)
31512 +                       for (i = 0; i < dpages.ndpage; i++) {
31513 +                               dpage = dpages.dpages + i;
31514 +                               ndentry = dpage->ndentry;
31515 +                               for (j = 0; j < ndentry; j++)
31516 +                                       au_dpri_dentry(dpage->dentries[j]);
31517 +                       }
31518 +               au_dpages_free(&dpages);
31519 +       } while (0);
31520 +#endif
31521 +
31522 +#if 1
31523 +       {
31524 +               struct inode *i;
31525 +
31526 +               pr("isolated inode\n");
31527 +               spin_lock(&sb->s_inode_list_lock);
31528 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31529 +                       spin_lock(&i->i_lock);
31530 +                       if (1 || hlist_empty(&i->i_dentry))
31531 +                               au_dpri_inode(i);
31532 +                       spin_unlock(&i->i_lock);
31533 +               }
31534 +               spin_unlock(&sb->s_inode_list_lock);
31535 +       }
31536 +#endif
31537 +       pr("files\n");
31538 +       files = &au_sbi(sb)->si_files;
31539 +       hlist_bl_lock(files);
31540 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31541 +               umode_t mode;
31542 +
31543 +               file = finfo->fi_file;
31544 +               mode = file_inode(file)->i_mode;
31545 +               if (!special_file(mode))
31546 +                       au_dpri_file(file);
31547 +       }
31548 +       hlist_bl_unlock(files);
31549 +       pr("done\n");
31550 +
31551 +#undef pr
31552 +       au_plevel = plevel;
31553 +}
31554 +
31555 +/* ---------------------------------------------------------------------- */
31556 +
31557 +/* module parameter */
31558 +static char *aufs_sysrq_key = "a";
31559 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31560 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31561 +
31562 +static void au_sysrq(int key __maybe_unused)
31563 +{
31564 +       struct au_sbinfo *sbinfo;
31565 +       struct hlist_bl_node *pos;
31566 +
31567 +       lockdep_off();
31568 +       au_sbilist_lock();
31569 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31570 +               sysrq_sb(sbinfo->si_sb);
31571 +       au_sbilist_unlock();
31572 +       lockdep_on();
31573 +}
31574 +
31575 +static struct sysrq_key_op au_sysrq_op = {
31576 +       .handler        = au_sysrq,
31577 +       .help_msg       = "Aufs",
31578 +       .action_msg     = "Aufs",
31579 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31580 +};
31581 +
31582 +/* ---------------------------------------------------------------------- */
31583 +
31584 +int __init au_sysrq_init(void)
31585 +{
31586 +       int err;
31587 +       char key;
31588 +
31589 +       err = -1;
31590 +       key = *aufs_sysrq_key;
31591 +       if ('a' <= key && key <= 'z')
31592 +               err = register_sysrq_key(key, &au_sysrq_op);
31593 +       if (unlikely(err))
31594 +               pr_err("err %d, sysrq=%c\n", err, key);
31595 +       return err;
31596 +}
31597 +
31598 +void au_sysrq_fin(void)
31599 +{
31600 +       int err;
31601 +
31602 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31603 +       if (unlikely(err))
31604 +               pr_err("err %d (ignored)\n", err);
31605 +}
31606 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31607 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31608 +++ linux/fs/aufs/vdir.c        2018-08-12 23:43:05.463458173 +0200
31609 @@ -0,0 +1,894 @@
31610 +// SPDX-License-Identifier: GPL-2.0
31611 +/*
31612 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31613 + *
31614 + * This program, aufs is free software; you can redistribute it and/or modify
31615 + * it under the terms of the GNU General Public License as published by
31616 + * the Free Software Foundation; either version 2 of the License, or
31617 + * (at your option) any later version.
31618 + *
31619 + * This program is distributed in the hope that it will be useful,
31620 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31621 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31622 + * GNU General Public License for more details.
31623 + *
31624 + * You should have received a copy of the GNU General Public License
31625 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31626 + */
31627 +
31628 +/*
31629 + * virtual or vertical directory
31630 + */
31631 +
31632 +#include "aufs.h"
31633 +
31634 +static unsigned int calc_size(int nlen)
31635 +{
31636 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31637 +}
31638 +
31639 +static int set_deblk_end(union au_vdir_deblk_p *p,
31640 +                        union au_vdir_deblk_p *deblk_end)
31641 +{
31642 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31643 +               p->de->de_str.len = 0;
31644 +               /* smp_mb(); */
31645 +               return 0;
31646 +       }
31647 +       return -1; /* error */
31648 +}
31649 +
31650 +/* returns true or false */
31651 +static int is_deblk_end(union au_vdir_deblk_p *p,
31652 +                       union au_vdir_deblk_p *deblk_end)
31653 +{
31654 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31655 +               return !p->de->de_str.len;
31656 +       return 1;
31657 +}
31658 +
31659 +static unsigned char *last_deblk(struct au_vdir *vdir)
31660 +{
31661 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31662 +}
31663 +
31664 +/* ---------------------------------------------------------------------- */
31665 +
31666 +/* estimate the appropriate size for name hash table */
31667 +unsigned int au_rdhash_est(loff_t sz)
31668 +{
31669 +       unsigned int n;
31670 +
31671 +       n = UINT_MAX;
31672 +       sz >>= 10;
31673 +       if (sz < n)
31674 +               n = sz;
31675 +       if (sz < AUFS_RDHASH_DEF)
31676 +               n = AUFS_RDHASH_DEF;
31677 +       /* pr_info("n %u\n", n); */
31678 +       return n;
31679 +}
31680 +
31681 +/*
31682 + * the allocated memory has to be freed by
31683 + * au_nhash_wh_free() or au_nhash_de_free().
31684 + */
31685 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31686 +{
31687 +       struct hlist_head *head;
31688 +       unsigned int u;
31689 +       size_t sz;
31690 +
31691 +       sz = sizeof(*nhash->nh_head) * num_hash;
31692 +       head = kmalloc(sz, gfp);
31693 +       if (head) {
31694 +               nhash->nh_num = num_hash;
31695 +               nhash->nh_head = head;
31696 +               for (u = 0; u < num_hash; u++)
31697 +                       INIT_HLIST_HEAD(head++);
31698 +               return 0; /* success */
31699 +       }
31700 +
31701 +       return -ENOMEM;
31702 +}
31703 +
31704 +static void nhash_count(struct hlist_head *head)
31705 +{
31706 +#if 0
31707 +       unsigned long n;
31708 +       struct hlist_node *pos;
31709 +
31710 +       n = 0;
31711 +       hlist_for_each(pos, head)
31712 +               n++;
31713 +       pr_info("%lu\n", n);
31714 +#endif
31715 +}
31716 +
31717 +static void au_nhash_wh_do_free(struct hlist_head *head)
31718 +{
31719 +       struct au_vdir_wh *pos;
31720 +       struct hlist_node *node;
31721 +
31722 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31723 +               kfree(pos);
31724 +}
31725 +
31726 +static void au_nhash_de_do_free(struct hlist_head *head)
31727 +{
31728 +       struct au_vdir_dehstr *pos;
31729 +       struct hlist_node *node;
31730 +
31731 +       hlist_for_each_entry_safe(pos, node, head, hash)
31732 +               au_cache_free_vdir_dehstr(pos);
31733 +}
31734 +
31735 +static void au_nhash_do_free(struct au_nhash *nhash,
31736 +                            void (*free)(struct hlist_head *head))
31737 +{
31738 +       unsigned int n;
31739 +       struct hlist_head *head;
31740 +
31741 +       n = nhash->nh_num;
31742 +       if (!n)
31743 +               return;
31744 +
31745 +       head = nhash->nh_head;
31746 +       while (n-- > 0) {
31747 +               nhash_count(head);
31748 +               free(head++);
31749 +       }
31750 +       kfree(nhash->nh_head);
31751 +}
31752 +
31753 +void au_nhash_wh_free(struct au_nhash *whlist)
31754 +{
31755 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31756 +}
31757 +
31758 +static void au_nhash_de_free(struct au_nhash *delist)
31759 +{
31760 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31761 +}
31762 +
31763 +/* ---------------------------------------------------------------------- */
31764 +
31765 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31766 +                           int limit)
31767 +{
31768 +       int num;
31769 +       unsigned int u, n;
31770 +       struct hlist_head *head;
31771 +       struct au_vdir_wh *pos;
31772 +
31773 +       num = 0;
31774 +       n = whlist->nh_num;
31775 +       head = whlist->nh_head;
31776 +       for (u = 0; u < n; u++, head++)
31777 +               hlist_for_each_entry(pos, head, wh_hash)
31778 +                       if (pos->wh_bindex == btgt && ++num > limit)
31779 +                               return 1;
31780 +       return 0;
31781 +}
31782 +
31783 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31784 +                                      unsigned char *name,
31785 +                                      unsigned int len)
31786 +{
31787 +       unsigned int v;
31788 +       /* const unsigned int magic_bit = 12; */
31789 +
31790 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31791 +
31792 +       v = 0;
31793 +       if (len > 8)
31794 +               len = 8;
31795 +       while (len--)
31796 +               v += *name++;
31797 +       /* v = hash_long(v, magic_bit); */
31798 +       v %= nhash->nh_num;
31799 +       return nhash->nh_head + v;
31800 +}
31801 +
31802 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31803 +                             int nlen)
31804 +{
31805 +       return str->len == nlen && !memcmp(str->name, name, nlen);
31806 +}
31807 +
31808 +/* returns found or not */
31809 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31810 +{
31811 +       struct hlist_head *head;
31812 +       struct au_vdir_wh *pos;
31813 +       struct au_vdir_destr *str;
31814 +
31815 +       head = au_name_hash(whlist, name, nlen);
31816 +       hlist_for_each_entry(pos, head, wh_hash) {
31817 +               str = &pos->wh_str;
31818 +               AuDbg("%.*s\n", str->len, str->name);
31819 +               if (au_nhash_test_name(str, name, nlen))
31820 +                       return 1;
31821 +       }
31822 +       return 0;
31823 +}
31824 +
31825 +/* returns found(true) or not */
31826 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31827 +{
31828 +       struct hlist_head *head;
31829 +       struct au_vdir_dehstr *pos;
31830 +       struct au_vdir_destr *str;
31831 +
31832 +       head = au_name_hash(delist, name, nlen);
31833 +       hlist_for_each_entry(pos, head, hash) {
31834 +               str = pos->str;
31835 +               AuDbg("%.*s\n", str->len, str->name);
31836 +               if (au_nhash_test_name(str, name, nlen))
31837 +                       return 1;
31838 +       }
31839 +       return 0;
31840 +}
31841 +
31842 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31843 +                           unsigned char d_type)
31844 +{
31845 +#ifdef CONFIG_AUFS_SHWH
31846 +       wh->wh_ino = ino;
31847 +       wh->wh_type = d_type;
31848 +#endif
31849 +}
31850 +
31851 +/* ---------------------------------------------------------------------- */
31852 +
31853 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31854 +                      unsigned int d_type, aufs_bindex_t bindex,
31855 +                      unsigned char shwh)
31856 +{
31857 +       int err;
31858 +       struct au_vdir_destr *str;
31859 +       struct au_vdir_wh *wh;
31860 +
31861 +       AuDbg("%.*s\n", nlen, name);
31862 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31863 +
31864 +       err = -ENOMEM;
31865 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
31866 +       if (unlikely(!wh))
31867 +               goto out;
31868 +
31869 +       err = 0;
31870 +       wh->wh_bindex = bindex;
31871 +       if (shwh)
31872 +               au_shwh_init_wh(wh, ino, d_type);
31873 +       str = &wh->wh_str;
31874 +       str->len = nlen;
31875 +       memcpy(str->name, name, nlen);
31876 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
31877 +       /* smp_mb(); */
31878 +
31879 +out:
31880 +       return err;
31881 +}
31882 +
31883 +static int append_deblk(struct au_vdir *vdir)
31884 +{
31885 +       int err;
31886 +       unsigned long ul;
31887 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31888 +       union au_vdir_deblk_p p, deblk_end;
31889 +       unsigned char **o;
31890 +
31891 +       err = -ENOMEM;
31892 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31893 +                       GFP_NOFS, /*may_shrink*/0);
31894 +       if (unlikely(!o))
31895 +               goto out;
31896 +
31897 +       vdir->vd_deblk = o;
31898 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31899 +       if (p.deblk) {
31900 +               ul = vdir->vd_nblk++;
31901 +               vdir->vd_deblk[ul] = p.deblk;
31902 +               vdir->vd_last.ul = ul;
31903 +               vdir->vd_last.p.deblk = p.deblk;
31904 +               deblk_end.deblk = p.deblk + deblk_sz;
31905 +               err = set_deblk_end(&p, &deblk_end);
31906 +       }
31907 +
31908 +out:
31909 +       return err;
31910 +}
31911 +
31912 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31913 +                    unsigned int d_type, struct au_nhash *delist)
31914 +{
31915 +       int err;
31916 +       unsigned int sz;
31917 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31918 +       union au_vdir_deblk_p p, *room, deblk_end;
31919 +       struct au_vdir_dehstr *dehstr;
31920 +
31921 +       p.deblk = last_deblk(vdir);
31922 +       deblk_end.deblk = p.deblk + deblk_sz;
31923 +       room = &vdir->vd_last.p;
31924 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31925 +                 || !is_deblk_end(room, &deblk_end));
31926 +
31927 +       sz = calc_size(nlen);
31928 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31929 +               err = append_deblk(vdir);
31930 +               if (unlikely(err))
31931 +                       goto out;
31932 +
31933 +               p.deblk = last_deblk(vdir);
31934 +               deblk_end.deblk = p.deblk + deblk_sz;
31935 +               /* smp_mb(); */
31936 +               AuDebugOn(room->deblk != p.deblk);
31937 +       }
31938 +
31939 +       err = -ENOMEM;
31940 +       dehstr = au_cache_alloc_vdir_dehstr();
31941 +       if (unlikely(!dehstr))
31942 +               goto out;
31943 +
31944 +       dehstr->str = &room->de->de_str;
31945 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31946 +       room->de->de_ino = ino;
31947 +       room->de->de_type = d_type;
31948 +       room->de->de_str.len = nlen;
31949 +       memcpy(room->de->de_str.name, name, nlen);
31950 +
31951 +       err = 0;
31952 +       room->deblk += sz;
31953 +       if (unlikely(set_deblk_end(room, &deblk_end)))
31954 +               err = append_deblk(vdir);
31955 +       /* smp_mb(); */
31956 +
31957 +out:
31958 +       return err;
31959 +}
31960 +
31961 +/* ---------------------------------------------------------------------- */
31962 +
31963 +void au_vdir_free(struct au_vdir *vdir)
31964 +{
31965 +       unsigned char **deblk;
31966 +
31967 +       deblk = vdir->vd_deblk;
31968 +       while (vdir->vd_nblk--)
31969 +               kfree(*deblk++);
31970 +       kfree(vdir->vd_deblk);
31971 +       au_cache_free_vdir(vdir);
31972 +}
31973 +
31974 +static struct au_vdir *alloc_vdir(struct file *file)
31975 +{
31976 +       struct au_vdir *vdir;
31977 +       struct super_block *sb;
31978 +       int err;
31979 +
31980 +       sb = file->f_path.dentry->d_sb;
31981 +       SiMustAnyLock(sb);
31982 +
31983 +       err = -ENOMEM;
31984 +       vdir = au_cache_alloc_vdir();
31985 +       if (unlikely(!vdir))
31986 +               goto out;
31987 +
31988 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31989 +       if (unlikely(!vdir->vd_deblk))
31990 +               goto out_free;
31991 +
31992 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
31993 +       if (!vdir->vd_deblk_sz) {
31994 +               /* estimate the appropriate size for deblk */
31995 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
31996 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
31997 +       }
31998 +       vdir->vd_nblk = 0;
31999 +       vdir->vd_version = 0;
32000 +       vdir->vd_jiffy = 0;
32001 +       err = append_deblk(vdir);
32002 +       if (!err)
32003 +               return vdir; /* success */
32004 +
32005 +       kfree(vdir->vd_deblk);
32006 +
32007 +out_free:
32008 +       au_cache_free_vdir(vdir);
32009 +out:
32010 +       vdir = ERR_PTR(err);
32011 +       return vdir;
32012 +}
32013 +
32014 +static int reinit_vdir(struct au_vdir *vdir)
32015 +{
32016 +       int err;
32017 +       union au_vdir_deblk_p p, deblk_end;
32018 +
32019 +       while (vdir->vd_nblk > 1) {
32020 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
32021 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32022 +               vdir->vd_nblk--;
32023 +       }
32024 +       p.deblk = vdir->vd_deblk[0];
32025 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32026 +       err = set_deblk_end(&p, &deblk_end);
32027 +       /* keep vd_dblk_sz */
32028 +       vdir->vd_last.ul = 0;
32029 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32030 +       vdir->vd_version = 0;
32031 +       vdir->vd_jiffy = 0;
32032 +       /* smp_mb(); */
32033 +       return err;
32034 +}
32035 +
32036 +/* ---------------------------------------------------------------------- */
32037 +
32038 +#define AuFillVdir_CALLED      1
32039 +#define AuFillVdir_WHABLE      (1 << 1)
32040 +#define AuFillVdir_SHWH                (1 << 2)
32041 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32042 +#define au_fset_fillvdir(flags, name) \
32043 +       do { (flags) |= AuFillVdir_##name; } while (0)
32044 +#define au_fclr_fillvdir(flags, name) \
32045 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32046 +
32047 +#ifndef CONFIG_AUFS_SHWH
32048 +#undef AuFillVdir_SHWH
32049 +#define AuFillVdir_SHWH                0
32050 +#endif
32051 +
32052 +struct fillvdir_arg {
32053 +       struct dir_context      ctx;
32054 +       struct file             *file;
32055 +       struct au_vdir          *vdir;
32056 +       struct au_nhash         delist;
32057 +       struct au_nhash         whlist;
32058 +       aufs_bindex_t           bindex;
32059 +       unsigned int            flags;
32060 +       int                     err;
32061 +};
32062 +
32063 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32064 +                   loff_t offset __maybe_unused, u64 h_ino,
32065 +                   unsigned int d_type)
32066 +{
32067 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32068 +       char *name = (void *)__name;
32069 +       struct super_block *sb;
32070 +       ino_t ino;
32071 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32072 +
32073 +       arg->err = 0;
32074 +       sb = arg->file->f_path.dentry->d_sb;
32075 +       au_fset_fillvdir(arg->flags, CALLED);
32076 +       /* smp_mb(); */
32077 +       if (nlen <= AUFS_WH_PFX_LEN
32078 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32079 +               if (test_known(&arg->delist, name, nlen)
32080 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32081 +                       goto out; /* already exists or whiteouted */
32082 +
32083 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32084 +               if (!arg->err) {
32085 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32086 +                               d_type = DT_UNKNOWN;
32087 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32088 +                                            d_type, &arg->delist);
32089 +               }
32090 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32091 +               name += AUFS_WH_PFX_LEN;
32092 +               nlen -= AUFS_WH_PFX_LEN;
32093 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32094 +                       goto out; /* already whiteouted */
32095 +
32096 +               if (shwh)
32097 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32098 +                                            &ino);
32099 +               if (!arg->err) {
32100 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32101 +                               d_type = DT_UNKNOWN;
32102 +                       arg->err = au_nhash_append_wh
32103 +                               (&arg->whlist, name, nlen, ino, d_type,
32104 +                                arg->bindex, shwh);
32105 +               }
32106 +       }
32107 +
32108 +out:
32109 +       if (!arg->err)
32110 +               arg->vdir->vd_jiffy = jiffies;
32111 +       /* smp_mb(); */
32112 +       AuTraceErr(arg->err);
32113 +       return arg->err;
32114 +}
32115 +
32116 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32117 +                         struct au_nhash *whlist, struct au_nhash *delist)
32118 +{
32119 +#ifdef CONFIG_AUFS_SHWH
32120 +       int err;
32121 +       unsigned int nh, u;
32122 +       struct hlist_head *head;
32123 +       struct au_vdir_wh *pos;
32124 +       struct hlist_node *n;
32125 +       char *p, *o;
32126 +       struct au_vdir_destr *destr;
32127 +
32128 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32129 +
32130 +       err = -ENOMEM;
32131 +       o = p = (void *)__get_free_page(GFP_NOFS);
32132 +       if (unlikely(!p))
32133 +               goto out;
32134 +
32135 +       err = 0;
32136 +       nh = whlist->nh_num;
32137 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32138 +       p += AUFS_WH_PFX_LEN;
32139 +       for (u = 0; u < nh; u++) {
32140 +               head = whlist->nh_head + u;
32141 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32142 +                       destr = &pos->wh_str;
32143 +                       memcpy(p, destr->name, destr->len);
32144 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32145 +                                       pos->wh_ino, pos->wh_type, delist);
32146 +                       if (unlikely(err))
32147 +                               break;
32148 +               }
32149 +       }
32150 +
32151 +       free_page((unsigned long)o);
32152 +
32153 +out:
32154 +       AuTraceErr(err);
32155 +       return err;
32156 +#else
32157 +       return 0;
32158 +#endif
32159 +}
32160 +
32161 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32162 +{
32163 +       int err;
32164 +       unsigned int rdhash;
32165 +       loff_t offset;
32166 +       aufs_bindex_t bbot, bindex, btop;
32167 +       unsigned char shwh;
32168 +       struct file *hf, *file;
32169 +       struct super_block *sb;
32170 +
32171 +       file = arg->file;
32172 +       sb = file->f_path.dentry->d_sb;
32173 +       SiMustAnyLock(sb);
32174 +
32175 +       rdhash = au_sbi(sb)->si_rdhash;
32176 +       if (!rdhash)
32177 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32178 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32179 +       if (unlikely(err))
32180 +               goto out;
32181 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32182 +       if (unlikely(err))
32183 +               goto out_delist;
32184 +
32185 +       err = 0;
32186 +       arg->flags = 0;
32187 +       shwh = 0;
32188 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32189 +               shwh = 1;
32190 +               au_fset_fillvdir(arg->flags, SHWH);
32191 +       }
32192 +       btop = au_fbtop(file);
32193 +       bbot = au_fbbot_dir(file);
32194 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32195 +               hf = au_hf_dir(file, bindex);
32196 +               if (!hf)
32197 +                       continue;
32198 +
32199 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32200 +               err = offset;
32201 +               if (unlikely(offset))
32202 +                       break;
32203 +
32204 +               arg->bindex = bindex;
32205 +               au_fclr_fillvdir(arg->flags, WHABLE);
32206 +               if (shwh
32207 +                   || (bindex != bbot
32208 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32209 +                       au_fset_fillvdir(arg->flags, WHABLE);
32210 +               do {
32211 +                       arg->err = 0;
32212 +                       au_fclr_fillvdir(arg->flags, CALLED);
32213 +                       /* smp_mb(); */
32214 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32215 +                       if (err >= 0)
32216 +                               err = arg->err;
32217 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32218 +
32219 +               /*
32220 +                * dir_relax() may be good for concurrency, but aufs should not
32221 +                * use it since it will cause a lockdep problem.
32222 +                */
32223 +       }
32224 +
32225 +       if (!err && shwh)
32226 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32227 +
32228 +       au_nhash_wh_free(&arg->whlist);
32229 +
32230 +out_delist:
32231 +       au_nhash_de_free(&arg->delist);
32232 +out:
32233 +       return err;
32234 +}
32235 +
32236 +static int read_vdir(struct file *file, int may_read)
32237 +{
32238 +       int err;
32239 +       unsigned long expire;
32240 +       unsigned char do_read;
32241 +       struct fillvdir_arg arg = {
32242 +               .ctx = {
32243 +                       .actor = fillvdir
32244 +               }
32245 +       };
32246 +       struct inode *inode;
32247 +       struct au_vdir *vdir, *allocated;
32248 +
32249 +       err = 0;
32250 +       inode = file_inode(file);
32251 +       IMustLock(inode);
32252 +       IiMustWriteLock(inode);
32253 +       SiMustAnyLock(inode->i_sb);
32254 +
32255 +       allocated = NULL;
32256 +       do_read = 0;
32257 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32258 +       vdir = au_ivdir(inode);
32259 +       if (!vdir) {
32260 +               do_read = 1;
32261 +               vdir = alloc_vdir(file);
32262 +               err = PTR_ERR(vdir);
32263 +               if (IS_ERR(vdir))
32264 +                       goto out;
32265 +               err = 0;
32266 +               allocated = vdir;
32267 +       } else if (may_read
32268 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32269 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32270 +               do_read = 1;
32271 +               err = reinit_vdir(vdir);
32272 +               if (unlikely(err))
32273 +                       goto out;
32274 +       }
32275 +
32276 +       if (!do_read)
32277 +               return 0; /* success */
32278 +
32279 +       arg.file = file;
32280 +       arg.vdir = vdir;
32281 +       err = au_do_read_vdir(&arg);
32282 +       if (!err) {
32283 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32284 +               vdir->vd_version = inode_query_iversion(inode);
32285 +               vdir->vd_last.ul = 0;
32286 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32287 +               if (allocated)
32288 +                       au_set_ivdir(inode, allocated);
32289 +       } else if (allocated)
32290 +               au_vdir_free(allocated);
32291 +
32292 +out:
32293 +       return err;
32294 +}
32295 +
32296 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32297 +{
32298 +       int err, rerr;
32299 +       unsigned long ul, n;
32300 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32301 +
32302 +       AuDebugOn(tgt->vd_nblk != 1);
32303 +
32304 +       err = -ENOMEM;
32305 +       if (tgt->vd_nblk < src->vd_nblk) {
32306 +               unsigned char **p;
32307 +
32308 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32309 +                               GFP_NOFS, /*may_shrink*/0);
32310 +               if (unlikely(!p))
32311 +                       goto out;
32312 +               tgt->vd_deblk = p;
32313 +       }
32314 +
32315 +       if (tgt->vd_deblk_sz != deblk_sz) {
32316 +               unsigned char *p;
32317 +
32318 +               tgt->vd_deblk_sz = deblk_sz;
32319 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32320 +                               /*may_shrink*/1);
32321 +               if (unlikely(!p))
32322 +                       goto out;
32323 +               tgt->vd_deblk[0] = p;
32324 +       }
32325 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32326 +       tgt->vd_version = src->vd_version;
32327 +       tgt->vd_jiffy = src->vd_jiffy;
32328 +
32329 +       n = src->vd_nblk;
32330 +       for (ul = 1; ul < n; ul++) {
32331 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32332 +                                           GFP_NOFS);
32333 +               if (unlikely(!tgt->vd_deblk[ul]))
32334 +                       goto out;
32335 +               tgt->vd_nblk++;
32336 +       }
32337 +       tgt->vd_nblk = n;
32338 +       tgt->vd_last.ul = tgt->vd_last.ul;
32339 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32340 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32341 +               - src->vd_deblk[src->vd_last.ul];
32342 +       /* smp_mb(); */
32343 +       return 0; /* success */
32344 +
32345 +out:
32346 +       rerr = reinit_vdir(tgt);
32347 +       BUG_ON(rerr);
32348 +       return err;
32349 +}
32350 +
32351 +int au_vdir_init(struct file *file)
32352 +{
32353 +       int err;
32354 +       struct inode *inode;
32355 +       struct au_vdir *vdir_cache, *allocated;
32356 +
32357 +       /* test file->f_pos here instead of ctx->pos */
32358 +       err = read_vdir(file, !file->f_pos);
32359 +       if (unlikely(err))
32360 +               goto out;
32361 +
32362 +       allocated = NULL;
32363 +       vdir_cache = au_fvdir_cache(file);
32364 +       if (!vdir_cache) {
32365 +               vdir_cache = alloc_vdir(file);
32366 +               err = PTR_ERR(vdir_cache);
32367 +               if (IS_ERR(vdir_cache))
32368 +                       goto out;
32369 +               allocated = vdir_cache;
32370 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32371 +               /* test file->f_pos here instead of ctx->pos */
32372 +               err = reinit_vdir(vdir_cache);
32373 +               if (unlikely(err))
32374 +                       goto out;
32375 +       } else
32376 +               return 0; /* success */
32377 +
32378 +       inode = file_inode(file);
32379 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32380 +       if (!err) {
32381 +               file->f_version = inode_query_iversion(inode);
32382 +               if (allocated)
32383 +                       au_set_fvdir_cache(file, allocated);
32384 +       } else if (allocated)
32385 +               au_vdir_free(allocated);
32386 +
32387 +out:
32388 +       return err;
32389 +}
32390 +
32391 +static loff_t calc_offset(struct au_vdir *vdir)
32392 +{
32393 +       loff_t offset;
32394 +       union au_vdir_deblk_p p;
32395 +
32396 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32397 +       offset = vdir->vd_last.p.deblk - p.deblk;
32398 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32399 +       return offset;
32400 +}
32401 +
32402 +/* returns true or false */
32403 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32404 +{
32405 +       int valid;
32406 +       unsigned int deblk_sz;
32407 +       unsigned long ul, n;
32408 +       loff_t offset;
32409 +       union au_vdir_deblk_p p, deblk_end;
32410 +       struct au_vdir *vdir_cache;
32411 +
32412 +       valid = 1;
32413 +       vdir_cache = au_fvdir_cache(file);
32414 +       offset = calc_offset(vdir_cache);
32415 +       AuDbg("offset %lld\n", offset);
32416 +       if (ctx->pos == offset)
32417 +               goto out;
32418 +
32419 +       vdir_cache->vd_last.ul = 0;
32420 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32421 +       if (!ctx->pos)
32422 +               goto out;
32423 +
32424 +       valid = 0;
32425 +       deblk_sz = vdir_cache->vd_deblk_sz;
32426 +       ul = div64_u64(ctx->pos, deblk_sz);
32427 +       AuDbg("ul %lu\n", ul);
32428 +       if (ul >= vdir_cache->vd_nblk)
32429 +               goto out;
32430 +
32431 +       n = vdir_cache->vd_nblk;
32432 +       for (; ul < n; ul++) {
32433 +               p.deblk = vdir_cache->vd_deblk[ul];
32434 +               deblk_end.deblk = p.deblk + deblk_sz;
32435 +               offset = ul;
32436 +               offset *= deblk_sz;
32437 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32438 +                       unsigned int l;
32439 +
32440 +                       l = calc_size(p.de->de_str.len);
32441 +                       offset += l;
32442 +                       p.deblk += l;
32443 +               }
32444 +               if (!is_deblk_end(&p, &deblk_end)) {
32445 +                       valid = 1;
32446 +                       vdir_cache->vd_last.ul = ul;
32447 +                       vdir_cache->vd_last.p = p;
32448 +                       break;
32449 +               }
32450 +       }
32451 +
32452 +out:
32453 +       /* smp_mb(); */
32454 +       if (!valid)
32455 +               AuDbg("valid %d\n", !valid);
32456 +       return valid;
32457 +}
32458 +
32459 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32460 +{
32461 +       unsigned int l, deblk_sz;
32462 +       union au_vdir_deblk_p deblk_end;
32463 +       struct au_vdir *vdir_cache;
32464 +       struct au_vdir_de *de;
32465 +
32466 +       vdir_cache = au_fvdir_cache(file);
32467 +       if (!seek_vdir(file, ctx))
32468 +               return 0;
32469 +
32470 +       deblk_sz = vdir_cache->vd_deblk_sz;
32471 +       while (1) {
32472 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32473 +               deblk_end.deblk += deblk_sz;
32474 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32475 +                       de = vdir_cache->vd_last.p.de;
32476 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32477 +                             de->de_str.len, de->de_str.name, ctx->pos,
32478 +                             (unsigned long)de->de_ino, de->de_type);
32479 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32480 +                                              de->de_str.len, de->de_ino,
32481 +                                              de->de_type))) {
32482 +                               /* todo: ignore the error caused by udba? */
32483 +                               /* return err; */
32484 +                               return 0;
32485 +                       }
32486 +
32487 +                       l = calc_size(de->de_str.len);
32488 +                       vdir_cache->vd_last.p.deblk += l;
32489 +                       ctx->pos += l;
32490 +               }
32491 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32492 +                       vdir_cache->vd_last.ul++;
32493 +                       vdir_cache->vd_last.p.deblk
32494 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32495 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32496 +                       continue;
32497 +               }
32498 +               break;
32499 +       }
32500 +
32501 +       /* smp_mb(); */
32502 +       return 0;
32503 +}
32504 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32505 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32506 +++ linux/fs/aufs/vfsub.c       2018-08-12 23:43:05.463458173 +0200
32507 @@ -0,0 +1,895 @@
32508 +// SPDX-License-Identifier: GPL-2.0
32509 +/*
32510 + * Copyright (C) 2005-2018 Junjiro R. Okajima
32511 + *
32512 + * This program, aufs is free software; you can redistribute it and/or modify
32513 + * it under the terms of the GNU General Public License as published by
32514 + * the Free Software Foundation; either version 2 of the License, or
32515 + * (at your option) any later version.
32516 + *
32517 + * This program is distributed in the hope that it will be useful,
32518 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32519 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32520 + * GNU General Public License for more details.
32521 + *
32522 + * You should have received a copy of the GNU General Public License
32523 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32524 + */
32525 +
32526 +/*
32527 + * sub-routines for VFS
32528 + */
32529 +
32530 +#include <linux/mnt_namespace.h>
32531 +#include <linux/namei.h>
32532 +#include <linux/nsproxy.h>
32533 +#include <linux/security.h>
32534 +#include <linux/splice.h>
32535 +#include "aufs.h"
32536 +
32537 +#ifdef CONFIG_AUFS_BR_FUSE
32538 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32539 +{
32540 +       if (!au_test_fuse(h_sb) || !au_userns)
32541 +               return 0;
32542 +
32543 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32544 +}
32545 +#endif
32546 +
32547 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32548 +{
32549 +       int err;
32550 +
32551 +       lockdep_off();
32552 +       down_read(&h_sb->s_umount);
32553 +       err = __sync_filesystem(h_sb, wait);
32554 +       up_read(&h_sb->s_umount);
32555 +       lockdep_on();
32556 +
32557 +       return err;
32558 +}
32559 +
32560 +/* ---------------------------------------------------------------------- */
32561 +
32562 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32563 +{
32564 +       int err;
32565 +       struct kstat st;
32566 +       struct super_block *h_sb;
32567 +
32568 +       /* for remote fs, leave work for its getattr or d_revalidate */
32569 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32570 +       /* still some fs may acquire i_mutex. we need to skip them */
32571 +       err = 0;
32572 +       if (!did)
32573 +               did = &err;
32574 +       h_sb = h_path->dentry->d_sb;
32575 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32576 +       if (*did)
32577 +               err = vfsub_getattr(h_path, &st);
32578 +
32579 +       return err;
32580 +}
32581 +
32582 +/* ---------------------------------------------------------------------- */
32583 +
32584 +struct file *vfsub_dentry_open(struct path *path, int flags)
32585 +{
32586 +       struct file *file;
32587 +
32588 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32589 +                          current_cred());
32590 +       if (!IS_ERR_OR_NULL(file)
32591 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
32592 +               i_readcount_inc(d_inode(path->dentry));
32593 +
32594 +       return file;
32595 +}
32596 +
32597 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32598 +{
32599 +       struct file *file;
32600 +
32601 +       lockdep_off();
32602 +       file = filp_open(path,
32603 +                        oflags /* | __FMODE_NONOTIFY */,
32604 +                        mode);
32605 +       lockdep_on();
32606 +       if (IS_ERR(file))
32607 +               goto out;
32608 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32609 +
32610 +out:
32611 +       return file;
32612 +}
32613 +
32614 +/*
32615 + * Ideally this function should call VFS:do_last() in order to keep all its
32616 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32617 + * structure such as nameidata. This is a second (or third) best approach.
32618 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32619 + */
32620 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32621 +                     struct vfsub_aopen_args *args, struct au_branch *br)
32622 +{
32623 +       int err;
32624 +       struct file *file = args->file;
32625 +       /* copied from linux/fs/namei.c:atomic_open() */
32626 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32627 +
32628 +       IMustLock(dir);
32629 +       AuDebugOn(!dir->i_op->atomic_open);
32630 +
32631 +       err = au_br_test_oflag(args->open_flag, br);
32632 +       if (unlikely(err))
32633 +               goto out;
32634 +
32635 +       args->file->f_path.dentry = DENTRY_NOT_SET;
32636 +       args->file->f_path.mnt = au_br_mnt(br);
32637 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32638 +                                    args->create_mode, args->opened);
32639 +       if (err >= 0) {
32640 +               /* some filesystems don't set FILE_CREATED while succeeded? */
32641 +               if (*args->opened & FILE_CREATED)
32642 +                       fsnotify_create(dir, dentry);
32643 +       } else
32644 +               goto out;
32645 +
32646 +
32647 +       if (!err) {
32648 +               /* todo: call VFS:may_open() here */
32649 +               err = open_check_o_direct(file);
32650 +               /* todo: ima_file_check() too? */
32651 +               if (!err && (args->open_flag & __FMODE_EXEC))
32652 +                       err = deny_write_access(file);
32653 +               if (unlikely(err))
32654 +                       /* note that the file is created and still opened */
32655 +                       goto out;
32656 +       }
32657 +
32658 +       au_br_get(br);
32659 +       fsnotify_open(file);
32660 +
32661 +out:
32662 +       return err;
32663 +}
32664 +
32665 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32666 +{
32667 +       int err;
32668 +
32669 +       err = kern_path(name, flags, path);
32670 +       if (!err && d_is_positive(path->dentry))
32671 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32672 +       return err;
32673 +}
32674 +
32675 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32676 +                                            struct dentry *parent, int len)
32677 +{
32678 +       struct path path = {
32679 +               .mnt = NULL
32680 +       };
32681 +
32682 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32683 +       if (IS_ERR(path.dentry))
32684 +               goto out;
32685 +       if (d_is_positive(path.dentry))
32686 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32687 +
32688 +out:
32689 +       AuTraceErrPtr(path.dentry);
32690 +       return path.dentry;
32691 +}
32692 +
32693 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32694 +                                   int len)
32695 +{
32696 +       struct path path = {
32697 +               .mnt = NULL
32698 +       };
32699 +
32700 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32701 +       IMustLock(d_inode(parent));
32702 +
32703 +       path.dentry = lookup_one_len(name, parent, len);
32704 +       if (IS_ERR(path.dentry))
32705 +               goto out;
32706 +       if (d_is_positive(path.dentry))
32707 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32708 +
32709 +out:
32710 +       AuTraceErrPtr(path.dentry);
32711 +       return path.dentry;
32712 +}
32713 +
32714 +void vfsub_call_lkup_one(void *args)
32715 +{
32716 +       struct vfsub_lkup_one_args *a = args;
32717 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32718 +}
32719 +
32720 +/* ---------------------------------------------------------------------- */
32721 +
32722 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32723 +                                struct dentry *d2, struct au_hinode *hdir2)
32724 +{
32725 +       struct dentry *d;
32726 +
32727 +       lockdep_off();
32728 +       d = lock_rename(d1, d2);
32729 +       lockdep_on();
32730 +       au_hn_suspend(hdir1);
32731 +       if (hdir1 != hdir2)
32732 +               au_hn_suspend(hdir2);
32733 +
32734 +       return d;
32735 +}
32736 +
32737 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32738 +                        struct dentry *d2, struct au_hinode *hdir2)
32739 +{
32740 +       au_hn_resume(hdir1);
32741 +       if (hdir1 != hdir2)
32742 +               au_hn_resume(hdir2);
32743 +       lockdep_off();
32744 +       unlock_rename(d1, d2);
32745 +       lockdep_on();
32746 +}
32747 +
32748 +/* ---------------------------------------------------------------------- */
32749 +
32750 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32751 +{
32752 +       int err;
32753 +       struct dentry *d;
32754 +
32755 +       IMustLock(dir);
32756 +
32757 +       d = path->dentry;
32758 +       path->dentry = d->d_parent;
32759 +       err = security_path_mknod(path, d, mode, 0);
32760 +       path->dentry = d;
32761 +       if (unlikely(err))
32762 +               goto out;
32763 +
32764 +       lockdep_off();
32765 +       err = vfs_create(dir, path->dentry, mode, want_excl);
32766 +       lockdep_on();
32767 +       if (!err) {
32768 +               struct path tmp = *path;
32769 +               int did;
32770 +
32771 +               vfsub_update_h_iattr(&tmp, &did);
32772 +               if (did) {
32773 +                       tmp.dentry = path->dentry->d_parent;
32774 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32775 +               }
32776 +               /*ignore*/
32777 +       }
32778 +
32779 +out:
32780 +       return err;
32781 +}
32782 +
32783 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32784 +{
32785 +       int err;
32786 +       struct dentry *d;
32787 +
32788 +       IMustLock(dir);
32789 +
32790 +       d = path->dentry;
32791 +       path->dentry = d->d_parent;
32792 +       err = security_path_symlink(path, d, symname);
32793 +       path->dentry = d;
32794 +       if (unlikely(err))
32795 +               goto out;
32796 +
32797 +       lockdep_off();
32798 +       err = vfs_symlink(dir, path->dentry, symname);
32799 +       lockdep_on();
32800 +       if (!err) {
32801 +               struct path tmp = *path;
32802 +               int did;
32803 +
32804 +               vfsub_update_h_iattr(&tmp, &did);
32805 +               if (did) {
32806 +                       tmp.dentry = path->dentry->d_parent;
32807 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32808 +               }
32809 +               /*ignore*/
32810 +       }
32811 +
32812 +out:
32813 +       return err;
32814 +}
32815 +
32816 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32817 +{
32818 +       int err;
32819 +       struct dentry *d;
32820 +
32821 +       IMustLock(dir);
32822 +
32823 +       d = path->dentry;
32824 +       path->dentry = d->d_parent;
32825 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32826 +       path->dentry = d;
32827 +       if (unlikely(err))
32828 +               goto out;
32829 +
32830 +       lockdep_off();
32831 +       err = vfs_mknod(dir, path->dentry, mode, dev);
32832 +       lockdep_on();
32833 +       if (!err) {
32834 +               struct path tmp = *path;
32835 +               int did;
32836 +
32837 +               vfsub_update_h_iattr(&tmp, &did);
32838 +               if (did) {
32839 +                       tmp.dentry = path->dentry->d_parent;
32840 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32841 +               }
32842 +               /*ignore*/
32843 +       }
32844 +
32845 +out:
32846 +       return err;
32847 +}
32848 +
32849 +static int au_test_nlink(struct inode *inode)
32850 +{
32851 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32852 +
32853 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
32854 +           || inode->i_nlink < link_max)
32855 +               return 0;
32856 +       return -EMLINK;
32857 +}
32858 +
32859 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32860 +              struct inode **delegated_inode)
32861 +{
32862 +       int err;
32863 +       struct dentry *d;
32864 +
32865 +       IMustLock(dir);
32866 +
32867 +       err = au_test_nlink(d_inode(src_dentry));
32868 +       if (unlikely(err))
32869 +               return err;
32870 +
32871 +       /* we don't call may_linkat() */
32872 +       d = path->dentry;
32873 +       path->dentry = d->d_parent;
32874 +       err = security_path_link(src_dentry, path, d);
32875 +       path->dentry = d;
32876 +       if (unlikely(err))
32877 +               goto out;
32878 +
32879 +       lockdep_off();
32880 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
32881 +       lockdep_on();
32882 +       if (!err) {
32883 +               struct path tmp = *path;
32884 +               int did;
32885 +
32886 +               /* fuse has different memory inode for the same inumber */
32887 +               vfsub_update_h_iattr(&tmp, &did);
32888 +               if (did) {
32889 +                       tmp.dentry = path->dentry->d_parent;
32890 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32891 +                       tmp.dentry = src_dentry;
32892 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32893 +               }
32894 +               /*ignore*/
32895 +       }
32896 +
32897 +out:
32898 +       return err;
32899 +}
32900 +
32901 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
32902 +                struct inode *dir, struct path *path,
32903 +                struct inode **delegated_inode, unsigned int flags)
32904 +{
32905 +       int err;
32906 +       struct path tmp = {
32907 +               .mnt    = path->mnt
32908 +       };
32909 +       struct dentry *d;
32910 +
32911 +       IMustLock(dir);
32912 +       IMustLock(src_dir);
32913 +
32914 +       d = path->dentry;
32915 +       path->dentry = d->d_parent;
32916 +       tmp.dentry = src_dentry->d_parent;
32917 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
32918 +       path->dentry = d;
32919 +       if (unlikely(err))
32920 +               goto out;
32921 +
32922 +       lockdep_off();
32923 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
32924 +                        delegated_inode, flags);
32925 +       lockdep_on();
32926 +       if (!err) {
32927 +               int did;
32928 +
32929 +               tmp.dentry = d->d_parent;
32930 +               vfsub_update_h_iattr(&tmp, &did);
32931 +               if (did) {
32932 +                       tmp.dentry = src_dentry;
32933 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32934 +                       tmp.dentry = src_dentry->d_parent;
32935 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32936 +               }
32937 +               /*ignore*/
32938 +       }
32939 +
32940 +out:
32941 +       return err;
32942 +}
32943 +
32944 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32945 +{
32946 +       int err;
32947 +       struct dentry *d;
32948 +
32949 +       IMustLock(dir);
32950 +
32951 +       d = path->dentry;
32952 +       path->dentry = d->d_parent;
32953 +       err = security_path_mkdir(path, d, mode);
32954 +       path->dentry = d;
32955 +       if (unlikely(err))
32956 +               goto out;
32957 +
32958 +       lockdep_off();
32959 +       err = vfs_mkdir(dir, path->dentry, mode);
32960 +       lockdep_on();
32961 +       if (!err) {
32962 +               struct path tmp = *path;
32963 +               int did;
32964 +
32965 +               vfsub_update_h_iattr(&tmp, &did);
32966 +               if (did) {
32967 +                       tmp.dentry = path->dentry->d_parent;
32968 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32969 +               }
32970 +               /*ignore*/
32971 +       }
32972 +
32973 +out:
32974 +       return err;
32975 +}
32976 +
32977 +int vfsub_rmdir(struct inode *dir, struct path *path)
32978 +{
32979 +       int err;
32980 +       struct dentry *d;
32981 +
32982 +       IMustLock(dir);
32983 +
32984 +       d = path->dentry;
32985 +       path->dentry = d->d_parent;
32986 +       err = security_path_rmdir(path, d);
32987 +       path->dentry = d;
32988 +       if (unlikely(err))
32989 +               goto out;
32990 +
32991 +       lockdep_off();
32992 +       err = vfs_rmdir(dir, path->dentry);
32993 +       lockdep_on();
32994 +       if (!err) {
32995 +               struct path tmp = {
32996 +                       .dentry = path->dentry->d_parent,
32997 +                       .mnt    = path->mnt
32998 +               };
32999 +
33000 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33001 +       }
33002 +
33003 +out:
33004 +       return err;
33005 +}
33006 +
33007 +/* ---------------------------------------------------------------------- */
33008 +
33009 +/* todo: support mmap_sem? */
33010 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33011 +                    loff_t *ppos)
33012 +{
33013 +       ssize_t err;
33014 +
33015 +       lockdep_off();
33016 +       err = vfs_read(file, ubuf, count, ppos);
33017 +       lockdep_on();
33018 +       if (err >= 0)
33019 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33020 +       return err;
33021 +}
33022 +
33023 +/* todo: kernel_read()? */
33024 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33025 +                    loff_t *ppos)
33026 +{
33027 +       ssize_t err;
33028 +       mm_segment_t oldfs;
33029 +       union {
33030 +               void *k;
33031 +               char __user *u;
33032 +       } buf;
33033 +
33034 +       buf.k = kbuf;
33035 +       oldfs = get_fs();
33036 +       set_fs(KERNEL_DS);
33037 +       err = vfsub_read_u(file, buf.u, count, ppos);
33038 +       set_fs(oldfs);
33039 +       return err;
33040 +}
33041 +
33042 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33043 +                     loff_t *ppos)
33044 +{
33045 +       ssize_t err;
33046 +
33047 +       lockdep_off();
33048 +       err = vfs_write(file, ubuf, count, ppos);
33049 +       lockdep_on();
33050 +       if (err >= 0)
33051 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33052 +       return err;
33053 +}
33054 +
33055 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33056 +{
33057 +       ssize_t err;
33058 +       mm_segment_t oldfs;
33059 +       union {
33060 +               void *k;
33061 +               const char __user *u;
33062 +       } buf;
33063 +
33064 +       buf.k = kbuf;
33065 +       oldfs = get_fs();
33066 +       set_fs(KERNEL_DS);
33067 +       err = vfsub_write_u(file, buf.u, count, ppos);
33068 +       set_fs(oldfs);
33069 +       return err;
33070 +}
33071 +
33072 +int vfsub_flush(struct file *file, fl_owner_t id)
33073 +{
33074 +       int err;
33075 +
33076 +       err = 0;
33077 +       if (file->f_op->flush) {
33078 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33079 +                       err = file->f_op->flush(file, id);
33080 +               else {
33081 +                       lockdep_off();
33082 +                       err = file->f_op->flush(file, id);
33083 +                       lockdep_on();
33084 +               }
33085 +               if (!err)
33086 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33087 +               /*ignore*/
33088 +       }
33089 +       return err;
33090 +}
33091 +
33092 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33093 +{
33094 +       int err;
33095 +
33096 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33097 +
33098 +       lockdep_off();
33099 +       err = iterate_dir(file, ctx);
33100 +       lockdep_on();
33101 +       if (err >= 0)
33102 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33103 +
33104 +       return err;
33105 +}
33106 +
33107 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33108 +                    struct pipe_inode_info *pipe, size_t len,
33109 +                    unsigned int flags)
33110 +{
33111 +       long err;
33112 +
33113 +       lockdep_off();
33114 +       err = do_splice_to(in, ppos, pipe, len, flags);
33115 +       lockdep_on();
33116 +       file_accessed(in);
33117 +       if (err >= 0)
33118 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33119 +       return err;
33120 +}
33121 +
33122 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33123 +                      loff_t *ppos, size_t len, unsigned int flags)
33124 +{
33125 +       long err;
33126 +
33127 +       lockdep_off();
33128 +       err = do_splice_from(pipe, out, ppos, len, flags);
33129 +       lockdep_on();
33130 +       if (err >= 0)
33131 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33132 +       return err;
33133 +}
33134 +
33135 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33136 +{
33137 +       int err;
33138 +
33139 +       /* file can be NULL */
33140 +       lockdep_off();
33141 +       err = vfs_fsync(file, datasync);
33142 +       lockdep_on();
33143 +       if (!err) {
33144 +               if (!path) {
33145 +                       AuDebugOn(!file);
33146 +                       path = &file->f_path;
33147 +               }
33148 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33149 +       }
33150 +       return err;
33151 +}
33152 +
33153 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33154 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33155 +               struct file *h_file)
33156 +{
33157 +       int err;
33158 +       struct inode *h_inode;
33159 +       struct super_block *h_sb;
33160 +
33161 +       if (!h_file) {
33162 +               err = vfsub_truncate(h_path, length);
33163 +               goto out;
33164 +       }
33165 +
33166 +       h_inode = d_inode(h_path->dentry);
33167 +       h_sb = h_inode->i_sb;
33168 +       lockdep_off();
33169 +       sb_start_write(h_sb);
33170 +       lockdep_on();
33171 +       err = locks_verify_truncate(h_inode, h_file, length);
33172 +       if (!err)
33173 +               err = security_path_truncate(h_path);
33174 +       if (!err) {
33175 +               lockdep_off();
33176 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33177 +               lockdep_on();
33178 +       }
33179 +       lockdep_off();
33180 +       sb_end_write(h_sb);
33181 +       lockdep_on();
33182 +
33183 +out:
33184 +       return err;
33185 +}
33186 +
33187 +/* ---------------------------------------------------------------------- */
33188 +
33189 +struct au_vfsub_mkdir_args {
33190 +       int *errp;
33191 +       struct inode *dir;
33192 +       struct path *path;
33193 +       int mode;
33194 +};
33195 +
33196 +static void au_call_vfsub_mkdir(void *args)
33197 +{
33198 +       struct au_vfsub_mkdir_args *a = args;
33199 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33200 +}
33201 +
33202 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33203 +{
33204 +       int err, do_sio, wkq_err;
33205 +
33206 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33207 +       if (!do_sio) {
33208 +               lockdep_off();
33209 +               err = vfsub_mkdir(dir, path, mode);
33210 +               lockdep_on();
33211 +       } else {
33212 +               struct au_vfsub_mkdir_args args = {
33213 +                       .errp   = &err,
33214 +                       .dir    = dir,
33215 +                       .path   = path,
33216 +                       .mode   = mode
33217 +               };
33218 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33219 +               if (unlikely(wkq_err))
33220 +                       err = wkq_err;
33221 +       }
33222 +
33223 +       return err;
33224 +}
33225 +
33226 +struct au_vfsub_rmdir_args {
33227 +       int *errp;
33228 +       struct inode *dir;
33229 +       struct path *path;
33230 +};
33231 +
33232 +static void au_call_vfsub_rmdir(void *args)
33233 +{
33234 +       struct au_vfsub_rmdir_args *a = args;
33235 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33236 +}
33237 +
33238 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33239 +{
33240 +       int err, do_sio, wkq_err;
33241 +
33242 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33243 +       if (!do_sio) {
33244 +               lockdep_off();
33245 +               err = vfsub_rmdir(dir, path);
33246 +               lockdep_on();
33247 +       } else {
33248 +               struct au_vfsub_rmdir_args args = {
33249 +                       .errp   = &err,
33250 +                       .dir    = dir,
33251 +                       .path   = path
33252 +               };
33253 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33254 +               if (unlikely(wkq_err))
33255 +                       err = wkq_err;
33256 +       }
33257 +
33258 +       return err;
33259 +}
33260 +
33261 +/* ---------------------------------------------------------------------- */
33262 +
33263 +struct notify_change_args {
33264 +       int *errp;
33265 +       struct path *path;
33266 +       struct iattr *ia;
33267 +       struct inode **delegated_inode;
33268 +};
33269 +
33270 +static void call_notify_change(void *args)
33271 +{
33272 +       struct notify_change_args *a = args;
33273 +       struct inode *h_inode;
33274 +
33275 +       h_inode = d_inode(a->path->dentry);
33276 +       IMustLock(h_inode);
33277 +
33278 +       *a->errp = -EPERM;
33279 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33280 +               lockdep_off();
33281 +               *a->errp = notify_change(a->path->dentry, a->ia,
33282 +                                        a->delegated_inode);
33283 +               lockdep_on();
33284 +               if (!*a->errp)
33285 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33286 +       }
33287 +       AuTraceErr(*a->errp);
33288 +}
33289 +
33290 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33291 +                       struct inode **delegated_inode)
33292 +{
33293 +       int err;
33294 +       struct notify_change_args args = {
33295 +               .errp                   = &err,
33296 +               .path                   = path,
33297 +               .ia                     = ia,
33298 +               .delegated_inode        = delegated_inode
33299 +       };
33300 +
33301 +       call_notify_change(&args);
33302 +
33303 +       return err;
33304 +}
33305 +
33306 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33307 +                           struct inode **delegated_inode)
33308 +{
33309 +       int err, wkq_err;
33310 +       struct notify_change_args args = {
33311 +               .errp                   = &err,
33312 +               .path                   = path,
33313 +               .ia                     = ia,
33314 +               .delegated_inode        = delegated_inode
33315 +       };
33316 +
33317 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33318 +       if (unlikely(wkq_err))
33319 +               err = wkq_err;
33320 +
33321 +       return err;
33322 +}
33323 +
33324 +/* ---------------------------------------------------------------------- */
33325 +
33326 +struct unlink_args {
33327 +       int *errp;
33328 +       struct inode *dir;
33329 +       struct path *path;
33330 +       struct inode **delegated_inode;
33331 +};
33332 +
33333 +static void call_unlink(void *args)
33334 +{
33335 +       struct unlink_args *a = args;
33336 +       struct dentry *d = a->path->dentry;
33337 +       struct inode *h_inode;
33338 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33339 +                                     && au_dcount(d) == 1);
33340 +
33341 +       IMustLock(a->dir);
33342 +
33343 +       a->path->dentry = d->d_parent;
33344 +       *a->errp = security_path_unlink(a->path, d);
33345 +       a->path->dentry = d;
33346 +       if (unlikely(*a->errp))
33347 +               return;
33348 +
33349 +       if (!stop_sillyrename)
33350 +               dget(d);
33351 +       h_inode = NULL;
33352 +       if (d_is_positive(d)) {
33353 +               h_inode = d_inode(d);
33354 +               ihold(h_inode);
33355 +       }
33356 +
33357 +       lockdep_off();
33358 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33359 +       lockdep_on();
33360 +       if (!*a->errp) {
33361 +               struct path tmp = {
33362 +                       .dentry = d->d_parent,
33363 +                       .mnt    = a->path->mnt
33364 +               };
33365 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33366 +       }
33367 +
33368 +       if (!stop_sillyrename)
33369 +               dput(d);
33370 +       if (h_inode)
33371 +               iput(h_inode);
33372 +
33373 +       AuTraceErr(*a->errp);
33374 +}
33375 +
33376 +/*
33377 + * @dir: must be locked.
33378 + * @dentry: target dentry.
33379 + */
33380 +int vfsub_unlink(struct inode *dir, struct path *path,
33381 +                struct inode **delegated_inode, int force)
33382 +{
33383 +       int err;
33384 +       struct unlink_args args = {
33385 +               .errp                   = &err,
33386 +               .dir                    = dir,
33387 +               .path                   = path,
33388 +               .delegated_inode        = delegated_inode
33389 +       };
33390 +
33391 +       if (!force)
33392 +               call_unlink(&args);
33393 +       else {
33394 +               int wkq_err;
33395 +
33396 +               wkq_err = au_wkq_wait(call_unlink, &args);
33397 +               if (unlikely(wkq_err))
33398 +                       err = wkq_err;
33399 +       }
33400 +
33401 +       return err;
33402 +}
33403 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33404 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33405 +++ linux/fs/aufs/vfsub.h       2018-08-12 23:43:05.463458173 +0200
33406 @@ -0,0 +1,355 @@
33407 +/* SPDX-License-Identifier: GPL-2.0 */
33408 +/*
33409 + * Copyright (C) 2005-2018 Junjiro R. Okajima
33410 + *
33411 + * This program, aufs is free software; you can redistribute it and/or modify
33412 + * it under the terms of the GNU General Public License as published by
33413 + * the Free Software Foundation; either version 2 of the License, or
33414 + * (at your option) any later version.
33415 + *
33416 + * This program is distributed in the hope that it will be useful,
33417 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33418 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33419 + * GNU General Public License for more details.
33420 + *
33421 + * You should have received a copy of the GNU General Public License
33422 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33423 + */
33424 +
33425 +/*
33426 + * sub-routines for VFS
33427 + */
33428 +
33429 +#ifndef __AUFS_VFSUB_H__
33430 +#define __AUFS_VFSUB_H__
33431 +
33432 +#ifdef __KERNEL__
33433 +
33434 +#include <linux/fs.h>
33435 +#include <linux/iversion.h>
33436 +#include <linux/mount.h>
33437 +#include <linux/posix_acl.h>
33438 +#include <linux/xattr.h>
33439 +#include "debug.h"
33440 +
33441 +/* copied from linux/fs/internal.h */
33442 +/* todo: BAD approach!! */
33443 +extern void __mnt_drop_write(struct vfsmount *);
33444 +extern int open_check_o_direct(struct file *f);
33445 +
33446 +/* ---------------------------------------------------------------------- */
33447 +
33448 +/* lock subclass for lower inode */
33449 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33450 +/* reduce? gave up. */
33451 +enum {
33452 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33453 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33454 +       AuLsc_I_PARENT2,        /* copyup dirs */
33455 +       AuLsc_I_PARENT3,        /* copyup wh */
33456 +       AuLsc_I_CHILD,
33457 +       AuLsc_I_CHILD2,
33458 +       AuLsc_I_End
33459 +};
33460 +
33461 +/* to debug easier, do not make them inlined functions */
33462 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33463 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33464 +
33465 +/* ---------------------------------------------------------------------- */
33466 +
33467 +static inline void vfsub_drop_nlink(struct inode *inode)
33468 +{
33469 +       AuDebugOn(!inode->i_nlink);
33470 +       drop_nlink(inode);
33471 +}
33472 +
33473 +static inline void vfsub_dead_dir(struct inode *inode)
33474 +{
33475 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33476 +       inode->i_flags |= S_DEAD;
33477 +       clear_nlink(inode);
33478 +}
33479 +
33480 +static inline int vfsub_native_ro(struct inode *inode)
33481 +{
33482 +       return sb_rdonly(inode->i_sb)
33483 +               || IS_RDONLY(inode)
33484 +               /* || IS_APPEND(inode) */
33485 +               || IS_IMMUTABLE(inode);
33486 +}
33487 +
33488 +#ifdef CONFIG_AUFS_BR_FUSE
33489 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33490 +#else
33491 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33492 +#endif
33493 +
33494 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33495 +
33496 +/* ---------------------------------------------------------------------- */
33497 +
33498 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33499 +struct file *vfsub_dentry_open(struct path *path, int flags);
33500 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33501 +struct vfsub_aopen_args {
33502 +       struct file     *file;
33503 +       unsigned int    open_flag;
33504 +       umode_t         create_mode;
33505 +       int             *opened;
33506 +};
33507 +struct au_branch;
33508 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33509 +                     struct vfsub_aopen_args *args, struct au_branch *br);
33510 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33511 +
33512 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33513 +                                            struct dentry *parent, int len);
33514 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33515 +                                   int len);
33516 +
33517 +struct vfsub_lkup_one_args {
33518 +       struct dentry **errp;
33519 +       struct qstr *name;
33520 +       struct dentry *parent;
33521 +};
33522 +
33523 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33524 +                                           struct dentry *parent)
33525 +{
33526 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33527 +}
33528 +
33529 +void vfsub_call_lkup_one(void *args);
33530 +
33531 +/* ---------------------------------------------------------------------- */
33532 +
33533 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33534 +{
33535 +       int err;
33536 +
33537 +       lockdep_off();
33538 +       err = mnt_want_write(mnt);
33539 +       lockdep_on();
33540 +       return err;
33541 +}
33542 +
33543 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33544 +{
33545 +       lockdep_off();
33546 +       mnt_drop_write(mnt);
33547 +       lockdep_on();
33548 +}
33549 +
33550 +#if 0 /* reserved */
33551 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33552 +{
33553 +       lockdep_off();
33554 +       mnt_drop_write_file(file);
33555 +       lockdep_on();
33556 +}
33557 +#endif
33558 +
33559 +/* ---------------------------------------------------------------------- */
33560 +
33561 +struct au_hinode;
33562 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33563 +                                struct dentry *d2, struct au_hinode *hdir2);
33564 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33565 +                        struct dentry *d2, struct au_hinode *hdir2);
33566 +
33567 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33568 +                bool want_excl);
33569 +int vfsub_symlink(struct inode *dir, struct path *path,
33570 +                 const char *symname);
33571 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33572 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33573 +              struct path *path, struct inode **delegated_inode);
33574 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33575 +                struct inode *hdir, struct path *path,
33576 +                struct inode **delegated_inode, unsigned int flags);
33577 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33578 +int vfsub_rmdir(struct inode *dir, struct path *path);
33579 +
33580 +/* ---------------------------------------------------------------------- */
33581 +
33582 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33583 +                    loff_t *ppos);
33584 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33585 +                       loff_t *ppos);
33586 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33587 +                     loff_t *ppos);
33588 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33589 +                     loff_t *ppos);
33590 +int vfsub_flush(struct file *file, fl_owner_t id);
33591 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33592 +
33593 +static inline loff_t vfsub_f_size_read(struct file *file)
33594 +{
33595 +       return i_size_read(file_inode(file));
33596 +}
33597 +
33598 +static inline unsigned int vfsub_file_flags(struct file *file)
33599 +{
33600 +       unsigned int flags;
33601 +
33602 +       spin_lock(&file->f_lock);
33603 +       flags = file->f_flags;
33604 +       spin_unlock(&file->f_lock);
33605 +
33606 +       return flags;
33607 +}
33608 +
33609 +static inline int vfsub_file_execed(struct file *file)
33610 +{
33611 +       /* todo: direct access f_flags */
33612 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33613 +}
33614 +
33615 +#if 0 /* reserved */
33616 +static inline void vfsub_file_accessed(struct file *h_file)
33617 +{
33618 +       file_accessed(h_file);
33619 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33620 +}
33621 +#endif
33622 +
33623 +#if 0 /* reserved */
33624 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33625 +                                    struct dentry *h_dentry)
33626 +{
33627 +       struct path h_path = {
33628 +               .dentry = h_dentry,
33629 +               .mnt    = h_mnt
33630 +       };
33631 +       touch_atime(&h_path);
33632 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33633 +}
33634 +#endif
33635 +
33636 +static inline int vfsub_update_time(struct inode *h_inode,
33637 +                                   struct timespec64 *ts, int flags)
33638 +{
33639 +       return update_time(h_inode, ts, flags);
33640 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33641 +}
33642 +
33643 +#ifdef CONFIG_FS_POSIX_ACL
33644 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33645 +{
33646 +       int err;
33647 +
33648 +       err = posix_acl_chmod(h_inode, h_mode);
33649 +       if (err == -EOPNOTSUPP)
33650 +               err = 0;
33651 +       return err;
33652 +}
33653 +#else
33654 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33655 +#endif
33656 +
33657 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33658 +                    struct pipe_inode_info *pipe, size_t len,
33659 +                    unsigned int flags);
33660 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33661 +                      loff_t *ppos, size_t len, unsigned int flags);
33662 +
33663 +static inline long vfsub_truncate(struct path *path, loff_t length)
33664 +{
33665 +       long err;
33666 +
33667 +       lockdep_off();
33668 +       err = vfs_truncate(path, length);
33669 +       lockdep_on();
33670 +       return err;
33671 +}
33672 +
33673 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33674 +               struct file *h_file);
33675 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33676 +
33677 +/*
33678 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33679 + * ioctl.
33680 + */
33681 +static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33682 +                                        u64 len)
33683 +{
33684 +       int err;
33685 +
33686 +       lockdep_off();
33687 +       err = vfs_clone_file_range(src, 0, dst, 0, len);
33688 +       lockdep_on();
33689 +
33690 +       return err;
33691 +}
33692 +
33693 +/* copy_file_range(2) is a systemcall */
33694 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33695 +                                           struct file *dst, loff_t dst_pos,
33696 +                                           size_t len, unsigned int flags)
33697 +{
33698 +       ssize_t ssz;
33699 +
33700 +       lockdep_off();
33701 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33702 +       lockdep_on();
33703 +
33704 +       return ssz;
33705 +}
33706 +
33707 +/* ---------------------------------------------------------------------- */
33708 +
33709 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33710 +{
33711 +       loff_t err;
33712 +
33713 +       lockdep_off();
33714 +       err = vfs_llseek(file, offset, origin);
33715 +       lockdep_on();
33716 +       return err;
33717 +}
33718 +
33719 +/* ---------------------------------------------------------------------- */
33720 +
33721 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33722 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33723 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33724 +                           struct inode **delegated_inode);
33725 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33726 +                       struct inode **delegated_inode);
33727 +int vfsub_unlink(struct inode *dir, struct path *path,
33728 +                struct inode **delegated_inode, int force);
33729 +
33730 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33731 +{
33732 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33733 +}
33734 +
33735 +/* ---------------------------------------------------------------------- */
33736 +
33737 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33738 +                                const void *value, size_t size, int flags)
33739 +{
33740 +       int err;
33741 +
33742 +       lockdep_off();
33743 +       err = vfs_setxattr(dentry, name, value, size, flags);
33744 +       lockdep_on();
33745 +
33746 +       return err;
33747 +}
33748 +
33749 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33750 +{
33751 +       int err;
33752 +
33753 +       lockdep_off();
33754 +       err = vfs_removexattr(dentry, name);
33755 +       lockdep_on();
33756 +
33757 +       return err;
33758 +}
33759 +
33760 +#endif /* __KERNEL__ */
33761 +#endif /* __AUFS_VFSUB_H__ */
33762 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33763 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33764 +++ linux/fs/aufs/wbr_policy.c  2018-08-12 23:43:05.463458173 +0200
33765 @@ -0,0 +1,830 @@
33766 +// SPDX-License-Identifier: GPL-2.0
33767 +/*
33768 + * Copyright (C) 2005-2018 Junjiro R. Okajima
33769 + *
33770 + * This program, aufs is free software; you can redistribute it and/or modify
33771 + * it under the terms of the GNU General Public License as published by
33772 + * the Free Software Foundation; either version 2 of the License, or
33773 + * (at your option) any later version.
33774 + *
33775 + * This program is distributed in the hope that it will be useful,
33776 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33777 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33778 + * GNU General Public License for more details.
33779 + *
33780 + * You should have received a copy of the GNU General Public License
33781 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33782 + */
33783 +
33784 +/*
33785 + * policies for selecting one among multiple writable branches
33786 + */
33787 +
33788 +#include <linux/statfs.h>
33789 +#include "aufs.h"
33790 +
33791 +/* subset of cpup_attr() */
33792 +static noinline_for_stack
33793 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33794 +{
33795 +       int err, sbits;
33796 +       struct iattr ia;
33797 +       struct inode *h_isrc;
33798 +
33799 +       h_isrc = d_inode(h_src);
33800 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33801 +       ia.ia_mode = h_isrc->i_mode;
33802 +       ia.ia_uid = h_isrc->i_uid;
33803 +       ia.ia_gid = h_isrc->i_gid;
33804 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33805 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33806 +       /* no delegation since it is just created */
33807 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33808 +
33809 +       /* is this nfs only? */
33810 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33811 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33812 +               ia.ia_mode = h_isrc->i_mode;
33813 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33814 +       }
33815 +
33816 +       return err;
33817 +}
33818 +
33819 +#define AuCpdown_PARENT_OPQ    1
33820 +#define AuCpdown_WHED          (1 << 1)
33821 +#define AuCpdown_MADE_DIR      (1 << 2)
33822 +#define AuCpdown_DIROPQ                (1 << 3)
33823 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
33824 +#define au_fset_cpdown(flags, name) \
33825 +       do { (flags) |= AuCpdown_##name; } while (0)
33826 +#define au_fclr_cpdown(flags, name) \
33827 +       do { (flags) &= ~AuCpdown_##name; } while (0)
33828 +
33829 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33830 +                            unsigned int *flags)
33831 +{
33832 +       int err;
33833 +       struct dentry *opq_dentry;
33834 +
33835 +       opq_dentry = au_diropq_create(dentry, bdst);
33836 +       err = PTR_ERR(opq_dentry);
33837 +       if (IS_ERR(opq_dentry))
33838 +               goto out;
33839 +       dput(opq_dentry);
33840 +       au_fset_cpdown(*flags, DIROPQ);
33841 +
33842 +out:
33843 +       return err;
33844 +}
33845 +
33846 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33847 +                           struct inode *dir, aufs_bindex_t bdst)
33848 +{
33849 +       int err;
33850 +       struct path h_path;
33851 +       struct au_branch *br;
33852 +
33853 +       br = au_sbr(dentry->d_sb, bdst);
33854 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33855 +       err = PTR_ERR(h_path.dentry);
33856 +       if (IS_ERR(h_path.dentry))
33857 +               goto out;
33858 +
33859 +       err = 0;
33860 +       if (d_is_positive(h_path.dentry)) {
33861 +               h_path.mnt = au_br_mnt(br);
33862 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33863 +                                         dentry);
33864 +       }
33865 +       dput(h_path.dentry);
33866 +
33867 +out:
33868 +       return err;
33869 +}
33870 +
33871 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
33872 +                        struct au_pin *pin,
33873 +                        struct dentry *h_parent, void *arg)
33874 +{
33875 +       int err, rerr;
33876 +       aufs_bindex_t bopq, btop;
33877 +       struct path h_path;
33878 +       struct dentry *parent;
33879 +       struct inode *h_dir, *h_inode, *inode, *dir;
33880 +       unsigned int *flags = arg;
33881 +
33882 +       btop = au_dbtop(dentry);
33883 +       /* dentry is di-locked */
33884 +       parent = dget_parent(dentry);
33885 +       dir = d_inode(parent);
33886 +       h_dir = d_inode(h_parent);
33887 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33888 +       IMustLock(h_dir);
33889 +
33890 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
33891 +       if (unlikely(err < 0))
33892 +               goto out;
33893 +       h_path.dentry = au_h_dptr(dentry, bdst);
33894 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33895 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
33896 +       if (unlikely(err))
33897 +               goto out_put;
33898 +       au_fset_cpdown(*flags, MADE_DIR);
33899 +
33900 +       bopq = au_dbdiropq(dentry);
33901 +       au_fclr_cpdown(*flags, WHED);
33902 +       au_fclr_cpdown(*flags, DIROPQ);
33903 +       if (au_dbwh(dentry) == bdst)
33904 +               au_fset_cpdown(*flags, WHED);
33905 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33906 +               au_fset_cpdown(*flags, PARENT_OPQ);
33907 +       h_inode = d_inode(h_path.dentry);
33908 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
33909 +       if (au_ftest_cpdown(*flags, WHED)) {
33910 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
33911 +               if (unlikely(err)) {
33912 +                       inode_unlock(h_inode);
33913 +                       goto out_dir;
33914 +               }
33915 +       }
33916 +
33917 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
33918 +       inode_unlock(h_inode);
33919 +       if (unlikely(err))
33920 +               goto out_opq;
33921 +
33922 +       if (au_ftest_cpdown(*flags, WHED)) {
33923 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33924 +               if (unlikely(err))
33925 +                       goto out_opq;
33926 +       }
33927 +
33928 +       inode = d_inode(dentry);
33929 +       if (au_ibbot(inode) < bdst)
33930 +               au_set_ibbot(inode, bdst);
33931 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33932 +                     au_hi_flags(inode, /*isdir*/1));
33933 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
33934 +       goto out; /* success */
33935 +
33936 +       /* revert */
33937 +out_opq:
33938 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
33939 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
33940 +               rerr = au_diropq_remove(dentry, bdst);
33941 +               inode_unlock(h_inode);
33942 +               if (unlikely(rerr)) {
33943 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
33944 +                               dentry, bdst, rerr);
33945 +                       err = -EIO;
33946 +                       goto out;
33947 +               }
33948 +       }
33949 +out_dir:
33950 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
33951 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
33952 +               if (unlikely(rerr)) {
33953 +                       AuIOErr("failed removing %pd b%d (%d)\n",
33954 +                               dentry, bdst, rerr);
33955 +                       err = -EIO;
33956 +               }
33957 +       }
33958 +out_put:
33959 +       au_set_h_dptr(dentry, bdst, NULL);
33960 +       if (au_dbbot(dentry) == bdst)
33961 +               au_update_dbbot(dentry);
33962 +out:
33963 +       dput(parent);
33964 +       return err;
33965 +}
33966 +
33967 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
33968 +{
33969 +       int err;
33970 +       unsigned int flags;
33971 +
33972 +       flags = 0;
33973 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
33974 +
33975 +       return err;
33976 +}
33977 +
33978 +/* ---------------------------------------------------------------------- */
33979 +
33980 +/* policies for create */
33981 +
33982 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
33983 +{
33984 +       int err, i, j, ndentry;
33985 +       aufs_bindex_t bopq;
33986 +       struct au_dcsub_pages dpages;
33987 +       struct au_dpage *dpage;
33988 +       struct dentry **dentries, *parent, *d;
33989 +
33990 +       err = au_dpages_init(&dpages, GFP_NOFS);
33991 +       if (unlikely(err))
33992 +               goto out;
33993 +       parent = dget_parent(dentry);
33994 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
33995 +       if (unlikely(err))
33996 +               goto out_free;
33997 +
33998 +       err = bindex;
33999 +       for (i = 0; i < dpages.ndpage; i++) {
34000 +               dpage = dpages.dpages + i;
34001 +               dentries = dpage->dentries;
34002 +               ndentry = dpage->ndentry;
34003 +               for (j = 0; j < ndentry; j++) {
34004 +                       d = dentries[j];
34005 +                       di_read_lock_parent2(d, !AuLock_IR);
34006 +                       bopq = au_dbdiropq(d);
34007 +                       di_read_unlock(d, !AuLock_IR);
34008 +                       if (bopq >= 0 && bopq < err)
34009 +                               err = bopq;
34010 +               }
34011 +       }
34012 +
34013 +out_free:
34014 +       dput(parent);
34015 +       au_dpages_free(&dpages);
34016 +out:
34017 +       return err;
34018 +}
34019 +
34020 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34021 +{
34022 +       for (; bindex >= 0; bindex--)
34023 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34024 +                       return bindex;
34025 +       return -EROFS;
34026 +}
34027 +
34028 +/* top down parent */
34029 +static int au_wbr_create_tdp(struct dentry *dentry,
34030 +                            unsigned int flags __maybe_unused)
34031 +{
34032 +       int err;
34033 +       aufs_bindex_t btop, bindex;
34034 +       struct super_block *sb;
34035 +       struct dentry *parent, *h_parent;
34036 +
34037 +       sb = dentry->d_sb;
34038 +       btop = au_dbtop(dentry);
34039 +       err = btop;
34040 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34041 +               goto out;
34042 +
34043 +       err = -EROFS;
34044 +       parent = dget_parent(dentry);
34045 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34046 +               h_parent = au_h_dptr(parent, bindex);
34047 +               if (!h_parent || d_is_negative(h_parent))
34048 +                       continue;
34049 +
34050 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34051 +                       err = bindex;
34052 +                       break;
34053 +               }
34054 +       }
34055 +       dput(parent);
34056 +
34057 +       /* bottom up here */
34058 +       if (unlikely(err < 0)) {
34059 +               err = au_wbr_bu(sb, btop - 1);
34060 +               if (err >= 0)
34061 +                       err = au_wbr_nonopq(dentry, err);
34062 +       }
34063 +
34064 +out:
34065 +       AuDbg("b%d\n", err);
34066 +       return err;
34067 +}
34068 +
34069 +/* ---------------------------------------------------------------------- */
34070 +
34071 +/* an exception for the policy other than tdp */
34072 +static int au_wbr_create_exp(struct dentry *dentry)
34073 +{
34074 +       int err;
34075 +       aufs_bindex_t bwh, bdiropq;
34076 +       struct dentry *parent;
34077 +
34078 +       err = -1;
34079 +       bwh = au_dbwh(dentry);
34080 +       parent = dget_parent(dentry);
34081 +       bdiropq = au_dbdiropq(parent);
34082 +       if (bwh >= 0) {
34083 +               if (bdiropq >= 0)
34084 +                       err = min(bdiropq, bwh);
34085 +               else
34086 +                       err = bwh;
34087 +               AuDbg("%d\n", err);
34088 +       } else if (bdiropq >= 0) {
34089 +               err = bdiropq;
34090 +               AuDbg("%d\n", err);
34091 +       }
34092 +       dput(parent);
34093 +
34094 +       if (err >= 0)
34095 +               err = au_wbr_nonopq(dentry, err);
34096 +
34097 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34098 +               err = -1;
34099 +
34100 +       AuDbg("%d\n", err);
34101 +       return err;
34102 +}
34103 +
34104 +/* ---------------------------------------------------------------------- */
34105 +
34106 +/* round robin */
34107 +static int au_wbr_create_init_rr(struct super_block *sb)
34108 +{
34109 +       int err;
34110 +
34111 +       err = au_wbr_bu(sb, au_sbbot(sb));
34112 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34113 +       /* smp_mb(); */
34114 +
34115 +       AuDbg("b%d\n", err);
34116 +       return err;
34117 +}
34118 +
34119 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34120 +{
34121 +       int err, nbr;
34122 +       unsigned int u;
34123 +       aufs_bindex_t bindex, bbot;
34124 +       struct super_block *sb;
34125 +       atomic_t *next;
34126 +
34127 +       err = au_wbr_create_exp(dentry);
34128 +       if (err >= 0)
34129 +               goto out;
34130 +
34131 +       sb = dentry->d_sb;
34132 +       next = &au_sbi(sb)->si_wbr_rr_next;
34133 +       bbot = au_sbbot(sb);
34134 +       nbr = bbot + 1;
34135 +       for (bindex = 0; bindex <= bbot; bindex++) {
34136 +               if (!au_ftest_wbr(flags, DIR)) {
34137 +                       err = atomic_dec_return(next) + 1;
34138 +                       /* modulo for 0 is meaningless */
34139 +                       if (unlikely(!err))
34140 +                               err = atomic_dec_return(next) + 1;
34141 +               } else
34142 +                       err = atomic_read(next);
34143 +               AuDbg("%d\n", err);
34144 +               u = err;
34145 +               err = u % nbr;
34146 +               AuDbg("%d\n", err);
34147 +               if (!au_br_rdonly(au_sbr(sb, err)))
34148 +                       break;
34149 +               err = -EROFS;
34150 +       }
34151 +
34152 +       if (err >= 0)
34153 +               err = au_wbr_nonopq(dentry, err);
34154 +
34155 +out:
34156 +       AuDbg("%d\n", err);
34157 +       return err;
34158 +}
34159 +
34160 +/* ---------------------------------------------------------------------- */
34161 +
34162 +/* most free space */
34163 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34164 +{
34165 +       struct super_block *sb;
34166 +       struct au_branch *br;
34167 +       struct au_wbr_mfs *mfs;
34168 +       struct dentry *h_parent;
34169 +       aufs_bindex_t bindex, bbot;
34170 +       int err;
34171 +       unsigned long long b, bavail;
34172 +       struct path h_path;
34173 +       /* reduce the stack usage */
34174 +       struct kstatfs *st;
34175 +
34176 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34177 +       if (unlikely(!st)) {
34178 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34179 +               return;
34180 +       }
34181 +
34182 +       bavail = 0;
34183 +       sb = dentry->d_sb;
34184 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34185 +       MtxMustLock(&mfs->mfs_lock);
34186 +       mfs->mfs_bindex = -EROFS;
34187 +       mfs->mfsrr_bytes = 0;
34188 +       if (!parent) {
34189 +               bindex = 0;
34190 +               bbot = au_sbbot(sb);
34191 +       } else {
34192 +               bindex = au_dbtop(parent);
34193 +               bbot = au_dbtaildir(parent);
34194 +       }
34195 +
34196 +       for (; bindex <= bbot; bindex++) {
34197 +               if (parent) {
34198 +                       h_parent = au_h_dptr(parent, bindex);
34199 +                       if (!h_parent || d_is_negative(h_parent))
34200 +                               continue;
34201 +               }
34202 +               br = au_sbr(sb, bindex);
34203 +               if (au_br_rdonly(br))
34204 +                       continue;
34205 +
34206 +               /* sb->s_root for NFS is unreliable */
34207 +               h_path.mnt = au_br_mnt(br);
34208 +               h_path.dentry = h_path.mnt->mnt_root;
34209 +               err = vfs_statfs(&h_path, st);
34210 +               if (unlikely(err)) {
34211 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34212 +                       continue;
34213 +               }
34214 +
34215 +               /* when the available size is equal, select the lower one */
34216 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34217 +                            || sizeof(b) < sizeof(st->f_bsize));
34218 +               b = st->f_bavail * st->f_bsize;
34219 +               br->br_wbr->wbr_bytes = b;
34220 +               if (b >= bavail) {
34221 +                       bavail = b;
34222 +                       mfs->mfs_bindex = bindex;
34223 +                       mfs->mfs_jiffy = jiffies;
34224 +               }
34225 +       }
34226 +
34227 +       mfs->mfsrr_bytes = bavail;
34228 +       AuDbg("b%d\n", mfs->mfs_bindex);
34229 +       kfree(st);
34230 +}
34231 +
34232 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34233 +{
34234 +       int err;
34235 +       struct dentry *parent;
34236 +       struct super_block *sb;
34237 +       struct au_wbr_mfs *mfs;
34238 +
34239 +       err = au_wbr_create_exp(dentry);
34240 +       if (err >= 0)
34241 +               goto out;
34242 +
34243 +       sb = dentry->d_sb;
34244 +       parent = NULL;
34245 +       if (au_ftest_wbr(flags, PARENT))
34246 +               parent = dget_parent(dentry);
34247 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34248 +       mutex_lock(&mfs->mfs_lock);
34249 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34250 +           || mfs->mfs_bindex < 0
34251 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34252 +               au_mfs(dentry, parent);
34253 +       mutex_unlock(&mfs->mfs_lock);
34254 +       err = mfs->mfs_bindex;
34255 +       dput(parent);
34256 +
34257 +       if (err >= 0)
34258 +               err = au_wbr_nonopq(dentry, err);
34259 +
34260 +out:
34261 +       AuDbg("b%d\n", err);
34262 +       return err;
34263 +}
34264 +
34265 +static int au_wbr_create_init_mfs(struct super_block *sb)
34266 +{
34267 +       struct au_wbr_mfs *mfs;
34268 +
34269 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34270 +       mutex_init(&mfs->mfs_lock);
34271 +       mfs->mfs_jiffy = 0;
34272 +       mfs->mfs_bindex = -EROFS;
34273 +
34274 +       return 0;
34275 +}
34276 +
34277 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34278 +{
34279 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34280 +       return 0;
34281 +}
34282 +
34283 +/* ---------------------------------------------------------------------- */
34284 +
34285 +/* top down regardless parent, and then mfs */
34286 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34287 +                              unsigned int flags __maybe_unused)
34288 +{
34289 +       int err;
34290 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34291 +       unsigned long long watermark;
34292 +       struct super_block *sb;
34293 +       struct au_wbr_mfs *mfs;
34294 +       struct au_branch *br;
34295 +       struct dentry *parent;
34296 +
34297 +       sb = dentry->d_sb;
34298 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34299 +       mutex_lock(&mfs->mfs_lock);
34300 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34301 +           || mfs->mfs_bindex < 0)
34302 +               au_mfs(dentry, /*parent*/NULL);
34303 +       watermark = mfs->mfsrr_watermark;
34304 +       bmfs = mfs->mfs_bindex;
34305 +       mutex_unlock(&mfs->mfs_lock);
34306 +
34307 +       /* another style of au_wbr_create_exp() */
34308 +       bwh = au_dbwh(dentry);
34309 +       parent = dget_parent(dentry);
34310 +       btail = au_dbtaildir(parent);
34311 +       if (bwh >= 0 && bwh < btail)
34312 +               btail = bwh;
34313 +
34314 +       err = au_wbr_nonopq(dentry, btail);
34315 +       if (unlikely(err < 0))
34316 +               goto out;
34317 +       btail = err;
34318 +       bfound = -1;
34319 +       for (bindex = 0; bindex <= btail; bindex++) {
34320 +               br = au_sbr(sb, bindex);
34321 +               if (au_br_rdonly(br))
34322 +                       continue;
34323 +               if (br->br_wbr->wbr_bytes > watermark) {
34324 +                       bfound = bindex;
34325 +                       break;
34326 +               }
34327 +       }
34328 +       err = bfound;
34329 +       if (err < 0)
34330 +               err = bmfs;
34331 +
34332 +out:
34333 +       dput(parent);
34334 +       AuDbg("b%d\n", err);
34335 +       return err;
34336 +}
34337 +
34338 +/* ---------------------------------------------------------------------- */
34339 +
34340 +/* most free space and then round robin */
34341 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34342 +{
34343 +       int err;
34344 +       struct au_wbr_mfs *mfs;
34345 +
34346 +       err = au_wbr_create_mfs(dentry, flags);
34347 +       if (err >= 0) {
34348 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34349 +               mutex_lock(&mfs->mfs_lock);
34350 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34351 +                       err = au_wbr_create_rr(dentry, flags);
34352 +               mutex_unlock(&mfs->mfs_lock);
34353 +       }
34354 +
34355 +       AuDbg("b%d\n", err);
34356 +       return err;
34357 +}
34358 +
34359 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34360 +{
34361 +       int err;
34362 +
34363 +       au_wbr_create_init_mfs(sb); /* ignore */
34364 +       err = au_wbr_create_init_rr(sb);
34365 +
34366 +       return err;
34367 +}
34368 +
34369 +/* ---------------------------------------------------------------------- */
34370 +
34371 +/* top down parent and most free space */
34372 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34373 +{
34374 +       int err, e2;
34375 +       unsigned long long b;
34376 +       aufs_bindex_t bindex, btop, bbot;
34377 +       struct super_block *sb;
34378 +       struct dentry *parent, *h_parent;
34379 +       struct au_branch *br;
34380 +
34381 +       err = au_wbr_create_tdp(dentry, flags);
34382 +       if (unlikely(err < 0))
34383 +               goto out;
34384 +       parent = dget_parent(dentry);
34385 +       btop = au_dbtop(parent);
34386 +       bbot = au_dbtaildir(parent);
34387 +       if (btop == bbot)
34388 +               goto out_parent; /* success */
34389 +
34390 +       e2 = au_wbr_create_mfs(dentry, flags);
34391 +       if (e2 < 0)
34392 +               goto out_parent; /* success */
34393 +
34394 +       /* when the available size is equal, select upper one */
34395 +       sb = dentry->d_sb;
34396 +       br = au_sbr(sb, err);
34397 +       b = br->br_wbr->wbr_bytes;
34398 +       AuDbg("b%d, %llu\n", err, b);
34399 +
34400 +       for (bindex = btop; bindex <= bbot; bindex++) {
34401 +               h_parent = au_h_dptr(parent, bindex);
34402 +               if (!h_parent || d_is_negative(h_parent))
34403 +                       continue;
34404 +
34405 +               br = au_sbr(sb, bindex);
34406 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34407 +                       b = br->br_wbr->wbr_bytes;
34408 +                       err = bindex;
34409 +                       AuDbg("b%d, %llu\n", err, b);
34410 +               }
34411 +       }
34412 +
34413 +       if (err >= 0)
34414 +               err = au_wbr_nonopq(dentry, err);
34415 +
34416 +out_parent:
34417 +       dput(parent);
34418 +out:
34419 +       AuDbg("b%d\n", err);
34420 +       return err;
34421 +}
34422 +
34423 +/* ---------------------------------------------------------------------- */
34424 +
34425 +/*
34426 + * - top down parent
34427 + * - most free space with parent
34428 + * - most free space round-robin regardless parent
34429 + */
34430 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34431 +{
34432 +       int err;
34433 +       unsigned long long watermark;
34434 +       struct super_block *sb;
34435 +       struct au_branch *br;
34436 +       struct au_wbr_mfs *mfs;
34437 +
34438 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34439 +       if (unlikely(err < 0))
34440 +               goto out;
34441 +
34442 +       sb = dentry->d_sb;
34443 +       br = au_sbr(sb, err);
34444 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34445 +       mutex_lock(&mfs->mfs_lock);
34446 +       watermark = mfs->mfsrr_watermark;
34447 +       mutex_unlock(&mfs->mfs_lock);
34448 +       if (br->br_wbr->wbr_bytes < watermark)
34449 +               /* regardless the parent dir */
34450 +               err = au_wbr_create_mfsrr(dentry, flags);
34451 +
34452 +out:
34453 +       AuDbg("b%d\n", err);
34454 +       return err;
34455 +}
34456 +
34457 +/* ---------------------------------------------------------------------- */
34458 +
34459 +/* policies for copyup */
34460 +
34461 +/* top down parent */
34462 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34463 +{
34464 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34465 +}
34466 +
34467 +/* bottom up parent */
34468 +static int au_wbr_copyup_bup(struct dentry *dentry)
34469 +{
34470 +       int err;
34471 +       aufs_bindex_t bindex, btop;
34472 +       struct dentry *parent, *h_parent;
34473 +       struct super_block *sb;
34474 +
34475 +       err = -EROFS;
34476 +       sb = dentry->d_sb;
34477 +       parent = dget_parent(dentry);
34478 +       btop = au_dbtop(parent);
34479 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34480 +               h_parent = au_h_dptr(parent, bindex);
34481 +               if (!h_parent || d_is_negative(h_parent))
34482 +                       continue;
34483 +
34484 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34485 +                       err = bindex;
34486 +                       break;
34487 +               }
34488 +       }
34489 +       dput(parent);
34490 +
34491 +       /* bottom up here */
34492 +       if (unlikely(err < 0))
34493 +               err = au_wbr_bu(sb, btop - 1);
34494 +
34495 +       AuDbg("b%d\n", err);
34496 +       return err;
34497 +}
34498 +
34499 +/* bottom up */
34500 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34501 +{
34502 +       int err;
34503 +
34504 +       err = au_wbr_bu(dentry->d_sb, btop);
34505 +       AuDbg("b%d\n", err);
34506 +       if (err > btop)
34507 +               err = au_wbr_nonopq(dentry, err);
34508 +
34509 +       AuDbg("b%d\n", err);
34510 +       return err;
34511 +}
34512 +
34513 +static int au_wbr_copyup_bu(struct dentry *dentry)
34514 +{
34515 +       int err;
34516 +       aufs_bindex_t btop;
34517 +
34518 +       btop = au_dbtop(dentry);
34519 +       err = au_wbr_do_copyup_bu(dentry, btop);
34520 +       return err;
34521 +}
34522 +
34523 +/* ---------------------------------------------------------------------- */
34524 +
34525 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34526 +       [AuWbrCopyup_TDP] = {
34527 +               .copyup = au_wbr_copyup_tdp
34528 +       },
34529 +       [AuWbrCopyup_BUP] = {
34530 +               .copyup = au_wbr_copyup_bup
34531 +       },
34532 +       [AuWbrCopyup_BU] = {
34533 +               .copyup = au_wbr_copyup_bu
34534 +       }
34535 +};
34536 +
34537 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34538 +       [AuWbrCreate_TDP] = {
34539 +               .create = au_wbr_create_tdp
34540 +       },
34541 +       [AuWbrCreate_RR] = {
34542 +               .create = au_wbr_create_rr,
34543 +               .init   = au_wbr_create_init_rr
34544 +       },
34545 +       [AuWbrCreate_MFS] = {
34546 +               .create = au_wbr_create_mfs,
34547 +               .init   = au_wbr_create_init_mfs,
34548 +               .fin    = au_wbr_create_fin_mfs
34549 +       },
34550 +       [AuWbrCreate_MFSV] = {
34551 +               .create = au_wbr_create_mfs,
34552 +               .init   = au_wbr_create_init_mfs,
34553 +               .fin    = au_wbr_create_fin_mfs
34554 +       },
34555 +       [AuWbrCreate_MFSRR] = {
34556 +               .create = au_wbr_create_mfsrr,
34557 +               .init   = au_wbr_create_init_mfsrr,
34558 +               .fin    = au_wbr_create_fin_mfs
34559 +       },
34560 +       [AuWbrCreate_MFSRRV] = {
34561 +               .create = au_wbr_create_mfsrr,
34562 +               .init   = au_wbr_create_init_mfsrr,
34563 +               .fin    = au_wbr_create_fin_mfs
34564 +       },
34565 +       [AuWbrCreate_TDMFS] = {
34566 +               .create = au_wbr_create_tdmfs,
34567 +               .init   = au_wbr_create_init_mfs,
34568 +               .fin    = au_wbr_create_fin_mfs
34569 +       },
34570 +       [AuWbrCreate_TDMFSV] = {
34571 +               .create = au_wbr_create_tdmfs,
34572 +               .init   = au_wbr_create_init_mfs,
34573 +               .fin    = au_wbr_create_fin_mfs
34574 +       },
34575 +       [AuWbrCreate_PMFS] = {
34576 +               .create = au_wbr_create_pmfs,
34577 +               .init   = au_wbr_create_init_mfs,
34578 +               .fin    = au_wbr_create_fin_mfs
34579 +       },
34580 +       [AuWbrCreate_PMFSV] = {
34581 +               .create = au_wbr_create_pmfs,
34582 +               .init   = au_wbr_create_init_mfs,
34583 +               .fin    = au_wbr_create_fin_mfs
34584 +       },
34585 +       [AuWbrCreate_PMFSRR] = {
34586 +               .create = au_wbr_create_pmfsrr,
34587 +               .init   = au_wbr_create_init_mfsrr,
34588 +               .fin    = au_wbr_create_fin_mfs
34589 +       },
34590 +       [AuWbrCreate_PMFSRRV] = {
34591 +               .create = au_wbr_create_pmfsrr,
34592 +               .init   = au_wbr_create_init_mfsrr,
34593 +               .fin    = au_wbr_create_fin_mfs
34594 +       }
34595 +};
34596 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34597 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34598 +++ linux/fs/aufs/whout.c       2018-08-12 23:43:05.463458173 +0200
34599 @@ -0,0 +1,1062 @@
34600 +// SPDX-License-Identifier: GPL-2.0
34601 +/*
34602 + * Copyright (C) 2005-2018 Junjiro R. Okajima
34603 + *
34604 + * This program, aufs is free software; you can redistribute it and/or modify
34605 + * it under the terms of the GNU General Public License as published by
34606 + * the Free Software Foundation; either version 2 of the License, or
34607 + * (at your option) any later version.
34608 + *
34609 + * This program is distributed in the hope that it will be useful,
34610 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34611 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34612 + * GNU General Public License for more details.
34613 + *
34614 + * You should have received a copy of the GNU General Public License
34615 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34616 + */
34617 +
34618 +/*
34619 + * whiteout for logical deletion and opaque directory
34620 + */
34621 +
34622 +#include "aufs.h"
34623 +
34624 +#define WH_MASK                        0444
34625 +
34626 +/*
34627 + * If a directory contains this file, then it is opaque.  We start with the
34628 + * .wh. flag so that it is blocked by lookup.
34629 + */
34630 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34631 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34632 +
34633 +/*
34634 + * generate whiteout name, which is NOT terminated by NULL.
34635 + * @name: original d_name.name
34636 + * @len: original d_name.len
34637 + * @wh: whiteout qstr
34638 + * returns zero when succeeds, otherwise error.
34639 + * succeeded value as wh->name should be freed by kfree().
34640 + */
34641 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34642 +{
34643 +       char *p;
34644 +
34645 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34646 +               return -ENAMETOOLONG;
34647 +
34648 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34649 +       p = kmalloc(wh->len, GFP_NOFS);
34650 +       wh->name = p;
34651 +       if (p) {
34652 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34653 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34654 +               /* smp_mb(); */
34655 +               return 0;
34656 +       }
34657 +       return -ENOMEM;
34658 +}
34659 +
34660 +/* ---------------------------------------------------------------------- */
34661 +
34662 +/*
34663 + * test if the @wh_name exists under @h_parent.
34664 + * @try_sio specifies the necessary of super-io.
34665 + */
34666 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34667 +{
34668 +       int err;
34669 +       struct dentry *wh_dentry;
34670 +
34671 +       if (!try_sio)
34672 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34673 +       else
34674 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34675 +       err = PTR_ERR(wh_dentry);
34676 +       if (IS_ERR(wh_dentry)) {
34677 +               if (err == -ENAMETOOLONG)
34678 +                       err = 0;
34679 +               goto out;
34680 +       }
34681 +
34682 +       err = 0;
34683 +       if (d_is_negative(wh_dentry))
34684 +               goto out_wh; /* success */
34685 +
34686 +       err = 1;
34687 +       if (d_is_reg(wh_dentry))
34688 +               goto out_wh; /* success */
34689 +
34690 +       err = -EIO;
34691 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34692 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34693 +
34694 +out_wh:
34695 +       dput(wh_dentry);
34696 +out:
34697 +       return err;
34698 +}
34699 +
34700 +/*
34701 + * test if the @h_dentry sets opaque or not.
34702 + */
34703 +int au_diropq_test(struct dentry *h_dentry)
34704 +{
34705 +       int err;
34706 +       struct inode *h_dir;
34707 +
34708 +       h_dir = d_inode(h_dentry);
34709 +       err = au_wh_test(h_dentry, &diropq_name,
34710 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34711 +       return err;
34712 +}
34713 +
34714 +/*
34715 + * returns a negative dentry whose name is unique and temporary.
34716 + */
34717 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34718 +                            struct qstr *prefix)
34719 +{
34720 +       struct dentry *dentry;
34721 +       int i;
34722 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34723 +               *name, *p;
34724 +       /* strict atomic_t is unnecessary here */
34725 +       static unsigned short cnt;
34726 +       struct qstr qs;
34727 +
34728 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34729 +
34730 +       name = defname;
34731 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34732 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34733 +               dentry = ERR_PTR(-ENAMETOOLONG);
34734 +               if (unlikely(qs.len > NAME_MAX))
34735 +                       goto out;
34736 +               dentry = ERR_PTR(-ENOMEM);
34737 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34738 +               if (unlikely(!name))
34739 +                       goto out;
34740 +       }
34741 +
34742 +       /* doubly whiteout-ed */
34743 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34744 +       p = name + AUFS_WH_PFX_LEN * 2;
34745 +       memcpy(p, prefix->name, prefix->len);
34746 +       p += prefix->len;
34747 +       *p++ = '.';
34748 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34749 +
34750 +       qs.name = name;
34751 +       for (i = 0; i < 3; i++) {
34752 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34753 +               dentry = au_sio_lkup_one(&qs, h_parent);
34754 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34755 +                       goto out_name;
34756 +               dput(dentry);
34757 +       }
34758 +       /* pr_warn("could not get random name\n"); */
34759 +       dentry = ERR_PTR(-EEXIST);
34760 +       AuDbg("%.*s\n", AuLNPair(&qs));
34761 +       BUG();
34762 +
34763 +out_name:
34764 +       if (name != defname)
34765 +               kfree(name);
34766 +out:
34767 +       AuTraceErrPtr(dentry);
34768 +       return dentry;
34769 +}
34770 +
34771 +/*
34772 + * rename the @h_dentry on @br to the whiteouted temporary name.
34773 + */
34774 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34775 +{
34776 +       int err;
34777 +       struct path h_path = {
34778 +               .mnt = au_br_mnt(br)
34779 +       };
34780 +       struct inode *h_dir, *delegated;
34781 +       struct dentry *h_parent;
34782 +
34783 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34784 +       h_dir = d_inode(h_parent);
34785 +       IMustLock(h_dir);
34786 +
34787 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34788 +       err = PTR_ERR(h_path.dentry);
34789 +       if (IS_ERR(h_path.dentry))
34790 +               goto out;
34791 +
34792 +       /* under the same dir, no need to lock_rename() */
34793 +       delegated = NULL;
34794 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34795 +                          /*flags*/0);
34796 +       AuTraceErr(err);
34797 +       if (unlikely(err == -EWOULDBLOCK)) {
34798 +               pr_warn("cannot retry for NFSv4 delegation"
34799 +                       " for an internal rename\n");
34800 +               iput(delegated);
34801 +       }
34802 +       dput(h_path.dentry);
34803 +
34804 +out:
34805 +       AuTraceErr(err);
34806 +       return err;
34807 +}
34808 +
34809 +/* ---------------------------------------------------------------------- */
34810 +/*
34811 + * functions for removing a whiteout
34812 + */
34813 +
34814 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34815 +{
34816 +       int err, force;
34817 +       struct inode *delegated;
34818 +
34819 +       /*
34820 +        * forces superio when the dir has a sticky bit.
34821 +        * this may be a violation of unix fs semantics.
34822 +        */
34823 +       force = (h_dir->i_mode & S_ISVTX)
34824 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34825 +       delegated = NULL;
34826 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
34827 +       if (unlikely(err == -EWOULDBLOCK)) {
34828 +               pr_warn("cannot retry for NFSv4 delegation"
34829 +                       " for an internal unlink\n");
34830 +               iput(delegated);
34831 +       }
34832 +       return err;
34833 +}
34834 +
34835 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34836 +                       struct dentry *dentry)
34837 +{
34838 +       int err;
34839 +
34840 +       err = do_unlink_wh(h_dir, h_path);
34841 +       if (!err && dentry)
34842 +               au_set_dbwh(dentry, -1);
34843 +
34844 +       return err;
34845 +}
34846 +
34847 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34848 +                         struct au_branch *br)
34849 +{
34850 +       int err;
34851 +       struct path h_path = {
34852 +               .mnt = au_br_mnt(br)
34853 +       };
34854 +
34855 +       err = 0;
34856 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
34857 +       if (IS_ERR(h_path.dentry))
34858 +               err = PTR_ERR(h_path.dentry);
34859 +       else {
34860 +               if (d_is_reg(h_path.dentry))
34861 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
34862 +               dput(h_path.dentry);
34863 +       }
34864 +
34865 +       return err;
34866 +}
34867 +
34868 +/* ---------------------------------------------------------------------- */
34869 +/*
34870 + * initialize/clean whiteout for a branch
34871 + */
34872 +
34873 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34874 +                       const int isdir)
34875 +{
34876 +       int err;
34877 +       struct inode *delegated;
34878 +
34879 +       if (d_is_negative(whpath->dentry))
34880 +               return;
34881 +
34882 +       if (isdir)
34883 +               err = vfsub_rmdir(h_dir, whpath);
34884 +       else {
34885 +               delegated = NULL;
34886 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34887 +               if (unlikely(err == -EWOULDBLOCK)) {
34888 +                       pr_warn("cannot retry for NFSv4 delegation"
34889 +                               " for an internal unlink\n");
34890 +                       iput(delegated);
34891 +               }
34892 +       }
34893 +       if (unlikely(err))
34894 +               pr_warn("failed removing %pd (%d), ignored.\n",
34895 +                       whpath->dentry, err);
34896 +}
34897 +
34898 +static int test_linkable(struct dentry *h_root)
34899 +{
34900 +       struct inode *h_dir = d_inode(h_root);
34901 +
34902 +       if (h_dir->i_op->link)
34903 +               return 0;
34904 +
34905 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34906 +              h_root, au_sbtype(h_root->d_sb));
34907 +       return -ENOSYS;
34908 +}
34909 +
34910 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34911 +static int au_whdir(struct inode *h_dir, struct path *path)
34912 +{
34913 +       int err;
34914 +
34915 +       err = -EEXIST;
34916 +       if (d_is_negative(path->dentry)) {
34917 +               int mode = 0700;
34918 +
34919 +               if (au_test_nfs(path->dentry->d_sb))
34920 +                       mode |= 0111;
34921 +               err = vfsub_mkdir(h_dir, path, mode);
34922 +       } else if (d_is_dir(path->dentry))
34923 +               err = 0;
34924 +       else
34925 +               pr_err("unknown %pd exists\n", path->dentry);
34926 +
34927 +       return err;
34928 +}
34929 +
34930 +struct au_wh_base {
34931 +       const struct qstr *name;
34932 +       struct dentry *dentry;
34933 +};
34934 +
34935 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
34936 +                         struct path *h_path)
34937 +{
34938 +       h_path->dentry = base[AuBrWh_BASE].dentry;
34939 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
34940 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
34941 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
34942 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
34943 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
34944 +}
34945 +
34946 +/*
34947 + * returns tri-state,
34948 + * minus: error, caller should print the message
34949 + * zero: succuess
34950 + * plus: error, caller should NOT print the message
34951 + */
34952 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
34953 +                               int do_plink, struct au_wh_base base[],
34954 +                               struct path *h_path)
34955 +{
34956 +       int err;
34957 +       struct inode *h_dir;
34958 +
34959 +       h_dir = d_inode(h_root);
34960 +       h_path->dentry = base[AuBrWh_BASE].dentry;
34961 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
34962 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
34963 +       if (do_plink) {
34964 +               err = test_linkable(h_root);
34965 +               if (unlikely(err)) {
34966 +                       err = 1;
34967 +                       goto out;
34968 +               }
34969 +
34970 +               err = au_whdir(h_dir, h_path);
34971 +               if (unlikely(err))
34972 +                       goto out;
34973 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34974 +       } else
34975 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
34976 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
34977 +       err = au_whdir(h_dir, h_path);
34978 +       if (unlikely(err))
34979 +               goto out;
34980 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34981 +
34982 +out:
34983 +       return err;
34984 +}
34985 +
34986 +/*
34987 + * for the moment, aufs supports the branch filesystem which does not support
34988 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
34989 + * copyup failed. finally, such filesystem will not be used as the writable
34990 + * branch.
34991 + *
34992 + * returns tri-state, see above.
34993 + */
34994 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
34995 +                        int do_plink, struct au_wh_base base[],
34996 +                        struct path *h_path)
34997 +{
34998 +       int err;
34999 +       struct inode *h_dir;
35000 +
35001 +       WbrWhMustWriteLock(wbr);
35002 +
35003 +       err = test_linkable(h_root);
35004 +       if (unlikely(err)) {
35005 +               err = 1;
35006 +               goto out;
35007 +       }
35008 +
35009 +       /*
35010 +        * todo: should this create be done in /sbin/mount.aufs helper?
35011 +        */
35012 +       err = -EEXIST;
35013 +       h_dir = d_inode(h_root);
35014 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35015 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35016 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35017 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35018 +               err = 0;
35019 +       else
35020 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35021 +       if (unlikely(err))
35022 +               goto out;
35023 +
35024 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35025 +       if (do_plink) {
35026 +               err = au_whdir(h_dir, h_path);
35027 +               if (unlikely(err))
35028 +                       goto out;
35029 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35030 +       } else
35031 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35032 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35033 +
35034 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35035 +       err = au_whdir(h_dir, h_path);
35036 +       if (unlikely(err))
35037 +               goto out;
35038 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35039 +
35040 +out:
35041 +       return err;
35042 +}
35043 +
35044 +/*
35045 + * initialize the whiteout base file/dir for @br.
35046 + */
35047 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35048 +{
35049 +       int err, i;
35050 +       const unsigned char do_plink
35051 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35052 +       struct inode *h_dir;
35053 +       struct path path = br->br_path;
35054 +       struct dentry *h_root = path.dentry;
35055 +       struct au_wbr *wbr = br->br_wbr;
35056 +       static const struct qstr base_name[] = {
35057 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35058 +                                         sizeof(AUFS_BASE_NAME) - 1),
35059 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35060 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35061 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35062 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35063 +       };
35064 +       struct au_wh_base base[] = {
35065 +               [AuBrWh_BASE] = {
35066 +                       .name   = base_name + AuBrWh_BASE,
35067 +                       .dentry = NULL
35068 +               },
35069 +               [AuBrWh_PLINK] = {
35070 +                       .name   = base_name + AuBrWh_PLINK,
35071 +                       .dentry = NULL
35072 +               },
35073 +               [AuBrWh_ORPH] = {
35074 +                       .name   = base_name + AuBrWh_ORPH,
35075 +                       .dentry = NULL
35076 +               }
35077 +       };
35078 +
35079 +       if (wbr)
35080 +               WbrWhMustWriteLock(wbr);
35081 +
35082 +       for (i = 0; i < AuBrWh_Last; i++) {
35083 +               /* doubly whiteouted */
35084 +               struct dentry *d;
35085 +
35086 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35087 +               err = PTR_ERR(d);
35088 +               if (IS_ERR(d))
35089 +                       goto out;
35090 +
35091 +               base[i].dentry = d;
35092 +               AuDebugOn(wbr
35093 +                         && wbr->wbr_wh[i]
35094 +                         && wbr->wbr_wh[i] != base[i].dentry);
35095 +       }
35096 +
35097 +       if (wbr)
35098 +               for (i = 0; i < AuBrWh_Last; i++) {
35099 +                       dput(wbr->wbr_wh[i]);
35100 +                       wbr->wbr_wh[i] = NULL;
35101 +               }
35102 +
35103 +       err = 0;
35104 +       if (!au_br_writable(br->br_perm)) {
35105 +               h_dir = d_inode(h_root);
35106 +               au_wh_init_ro(h_dir, base, &path);
35107 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35108 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35109 +               if (err > 0)
35110 +                       goto out;
35111 +               else if (err)
35112 +                       goto out_err;
35113 +       } else {
35114 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35115 +               if (err > 0)
35116 +                       goto out;
35117 +               else if (err)
35118 +                       goto out_err;
35119 +       }
35120 +       goto out; /* success */
35121 +
35122 +out_err:
35123 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35124 +              err, h_root, au_sbtype(h_root->d_sb));
35125 +out:
35126 +       for (i = 0; i < AuBrWh_Last; i++)
35127 +               dput(base[i].dentry);
35128 +       return err;
35129 +}
35130 +
35131 +/* ---------------------------------------------------------------------- */
35132 +/*
35133 + * whiteouts are all hard-linked usually.
35134 + * when its link count reaches a ceiling, we create a new whiteout base
35135 + * asynchronously.
35136 + */
35137 +
35138 +struct reinit_br_wh {
35139 +       struct super_block *sb;
35140 +       struct au_branch *br;
35141 +};
35142 +
35143 +static void reinit_br_wh(void *arg)
35144 +{
35145 +       int err;
35146 +       aufs_bindex_t bindex;
35147 +       struct path h_path;
35148 +       struct reinit_br_wh *a = arg;
35149 +       struct au_wbr *wbr;
35150 +       struct inode *dir, *delegated;
35151 +       struct dentry *h_root;
35152 +       struct au_hinode *hdir;
35153 +
35154 +       err = 0;
35155 +       wbr = a->br->br_wbr;
35156 +       /* big aufs lock */
35157 +       si_noflush_write_lock(a->sb);
35158 +       if (!au_br_writable(a->br->br_perm))
35159 +               goto out;
35160 +       bindex = au_br_index(a->sb, a->br->br_id);
35161 +       if (unlikely(bindex < 0))
35162 +               goto out;
35163 +
35164 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35165 +       dir = d_inode(a->sb->s_root);
35166 +       hdir = au_hi(dir, bindex);
35167 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35168 +       AuDebugOn(h_root != au_br_dentry(a->br));
35169 +
35170 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35171 +       wbr_wh_write_lock(wbr);
35172 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35173 +                         h_root, a->br);
35174 +       if (!err) {
35175 +               h_path.dentry = wbr->wbr_whbase;
35176 +               h_path.mnt = au_br_mnt(a->br);
35177 +               delegated = NULL;
35178 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35179 +                                  /*force*/0);
35180 +               if (unlikely(err == -EWOULDBLOCK)) {
35181 +                       pr_warn("cannot retry for NFSv4 delegation"
35182 +                               " for an internal unlink\n");
35183 +                       iput(delegated);
35184 +               }
35185 +       } else {
35186 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35187 +               err = 0;
35188 +       }
35189 +       dput(wbr->wbr_whbase);
35190 +       wbr->wbr_whbase = NULL;
35191 +       if (!err)
35192 +               err = au_wh_init(a->br, a->sb);
35193 +       wbr_wh_write_unlock(wbr);
35194 +       au_hn_inode_unlock(hdir);
35195 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35196 +       if (!err)
35197 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35198 +
35199 +out:
35200 +       if (wbr)
35201 +               atomic_dec(&wbr->wbr_wh_running);
35202 +       au_br_put(a->br);
35203 +       si_write_unlock(a->sb);
35204 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35205 +       kfree(arg);
35206 +       if (unlikely(err))
35207 +               AuIOErr("err %d\n", err);
35208 +}
35209 +
35210 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35211 +{
35212 +       int do_dec, wkq_err;
35213 +       struct reinit_br_wh *arg;
35214 +
35215 +       do_dec = 1;
35216 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35217 +               goto out;
35218 +
35219 +       /* ignore ENOMEM */
35220 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35221 +       if (arg) {
35222 +               /*
35223 +                * dec(wh_running), kfree(arg) and dec(br_count)
35224 +                * in reinit function
35225 +                */
35226 +               arg->sb = sb;
35227 +               arg->br = br;
35228 +               au_br_get(br);
35229 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35230 +               if (unlikely(wkq_err)) {
35231 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35232 +                       au_br_put(br);
35233 +                       kfree(arg);
35234 +               }
35235 +               do_dec = 0;
35236 +       }
35237 +
35238 +out:
35239 +       if (do_dec)
35240 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35241 +}
35242 +
35243 +/* ---------------------------------------------------------------------- */
35244 +
35245 +/*
35246 + * create the whiteout @wh.
35247 + */
35248 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35249 +                            struct dentry *wh)
35250 +{
35251 +       int err;
35252 +       struct path h_path = {
35253 +               .dentry = wh
35254 +       };
35255 +       struct au_branch *br;
35256 +       struct au_wbr *wbr;
35257 +       struct dentry *h_parent;
35258 +       struct inode *h_dir, *delegated;
35259 +
35260 +       h_parent = wh->d_parent; /* dir inode is locked */
35261 +       h_dir = d_inode(h_parent);
35262 +       IMustLock(h_dir);
35263 +
35264 +       br = au_sbr(sb, bindex);
35265 +       h_path.mnt = au_br_mnt(br);
35266 +       wbr = br->br_wbr;
35267 +       wbr_wh_read_lock(wbr);
35268 +       if (wbr->wbr_whbase) {
35269 +               delegated = NULL;
35270 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35271 +               if (unlikely(err == -EWOULDBLOCK)) {
35272 +                       pr_warn("cannot retry for NFSv4 delegation"
35273 +                               " for an internal link\n");
35274 +                       iput(delegated);
35275 +               }
35276 +               if (!err || err != -EMLINK)
35277 +                       goto out;
35278 +
35279 +               /* link count full. re-initialize br_whbase. */
35280 +               kick_reinit_br_wh(sb, br);
35281 +       }
35282 +
35283 +       /* return this error in this context */
35284 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35285 +       if (!err)
35286 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35287 +
35288 +out:
35289 +       wbr_wh_read_unlock(wbr);
35290 +       return err;
35291 +}
35292 +
35293 +/* ---------------------------------------------------------------------- */
35294 +
35295 +/*
35296 + * create or remove the diropq.
35297 + */
35298 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35299 +                               unsigned int flags)
35300 +{
35301 +       struct dentry *opq_dentry, *h_dentry;
35302 +       struct super_block *sb;
35303 +       struct au_branch *br;
35304 +       int err;
35305 +
35306 +       sb = dentry->d_sb;
35307 +       br = au_sbr(sb, bindex);
35308 +       h_dentry = au_h_dptr(dentry, bindex);
35309 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35310 +       if (IS_ERR(opq_dentry))
35311 +               goto out;
35312 +
35313 +       if (au_ftest_diropq(flags, CREATE)) {
35314 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35315 +               if (!err) {
35316 +                       au_set_dbdiropq(dentry, bindex);
35317 +                       goto out; /* success */
35318 +               }
35319 +       } else {
35320 +               struct path tmp = {
35321 +                       .dentry = opq_dentry,
35322 +                       .mnt    = au_br_mnt(br)
35323 +               };
35324 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35325 +               if (!err)
35326 +                       au_set_dbdiropq(dentry, -1);
35327 +       }
35328 +       dput(opq_dentry);
35329 +       opq_dentry = ERR_PTR(err);
35330 +
35331 +out:
35332 +       return opq_dentry;
35333 +}
35334 +
35335 +struct do_diropq_args {
35336 +       struct dentry **errp;
35337 +       struct dentry *dentry;
35338 +       aufs_bindex_t bindex;
35339 +       unsigned int flags;
35340 +};
35341 +
35342 +static void call_do_diropq(void *args)
35343 +{
35344 +       struct do_diropq_args *a = args;
35345 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35346 +}
35347 +
35348 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35349 +                            unsigned int flags)
35350 +{
35351 +       struct dentry *diropq, *h_dentry;
35352 +
35353 +       h_dentry = au_h_dptr(dentry, bindex);
35354 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35355 +               diropq = do_diropq(dentry, bindex, flags);
35356 +       else {
35357 +               int wkq_err;
35358 +               struct do_diropq_args args = {
35359 +                       .errp           = &diropq,
35360 +                       .dentry         = dentry,
35361 +                       .bindex         = bindex,
35362 +                       .flags          = flags
35363 +               };
35364 +
35365 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35366 +               if (unlikely(wkq_err))
35367 +                       diropq = ERR_PTR(wkq_err);
35368 +       }
35369 +
35370 +       return diropq;
35371 +}
35372 +
35373 +/* ---------------------------------------------------------------------- */
35374 +
35375 +/*
35376 + * lookup whiteout dentry.
35377 + * @h_parent: lower parent dentry which must exist and be locked
35378 + * @base_name: name of dentry which will be whiteouted
35379 + * returns dentry for whiteout.
35380 + */
35381 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35382 +                         struct au_branch *br)
35383 +{
35384 +       int err;
35385 +       struct qstr wh_name;
35386 +       struct dentry *wh_dentry;
35387 +
35388 +       err = au_wh_name_alloc(&wh_name, base_name);
35389 +       wh_dentry = ERR_PTR(err);
35390 +       if (!err) {
35391 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35392 +               kfree(wh_name.name);
35393 +       }
35394 +       return wh_dentry;
35395 +}
35396 +
35397 +/*
35398 + * link/create a whiteout for @dentry on @bindex.
35399 + */
35400 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35401 +                           struct dentry *h_parent)
35402 +{
35403 +       struct dentry *wh_dentry;
35404 +       struct super_block *sb;
35405 +       int err;
35406 +
35407 +       sb = dentry->d_sb;
35408 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35409 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35410 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35411 +               if (!err) {
35412 +                       au_set_dbwh(dentry, bindex);
35413 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35414 +               } else {
35415 +                       dput(wh_dentry);
35416 +                       wh_dentry = ERR_PTR(err);
35417 +               }
35418 +       }
35419 +
35420 +       return wh_dentry;
35421 +}
35422 +
35423 +/* ---------------------------------------------------------------------- */
35424 +
35425 +/* Delete all whiteouts in this directory on branch bindex. */
35426 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35427 +                          aufs_bindex_t bindex, struct au_branch *br)
35428 +{
35429 +       int err;
35430 +       unsigned long ul, n;
35431 +       struct qstr wh_name;
35432 +       char *p;
35433 +       struct hlist_head *head;
35434 +       struct au_vdir_wh *pos;
35435 +       struct au_vdir_destr *str;
35436 +
35437 +       err = -ENOMEM;
35438 +       p = (void *)__get_free_page(GFP_NOFS);
35439 +       wh_name.name = p;
35440 +       if (unlikely(!wh_name.name))
35441 +               goto out;
35442 +
35443 +       err = 0;
35444 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35445 +       p += AUFS_WH_PFX_LEN;
35446 +       n = whlist->nh_num;
35447 +       head = whlist->nh_head;
35448 +       for (ul = 0; !err && ul < n; ul++, head++) {
35449 +               hlist_for_each_entry(pos, head, wh_hash) {
35450 +                       if (pos->wh_bindex != bindex)
35451 +                               continue;
35452 +
35453 +                       str = &pos->wh_str;
35454 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35455 +                               memcpy(p, str->name, str->len);
35456 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35457 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35458 +                               if (!err)
35459 +                                       continue;
35460 +                               break;
35461 +                       }
35462 +                       AuIOErr("whiteout name too long %.*s\n",
35463 +                               str->len, str->name);
35464 +                       err = -EIO;
35465 +                       break;
35466 +               }
35467 +       }
35468 +       free_page((unsigned long)wh_name.name);
35469 +
35470 +out:
35471 +       return err;
35472 +}
35473 +
35474 +struct del_wh_children_args {
35475 +       int *errp;
35476 +       struct dentry *h_dentry;
35477 +       struct au_nhash *whlist;
35478 +       aufs_bindex_t bindex;
35479 +       struct au_branch *br;
35480 +};
35481 +
35482 +static void call_del_wh_children(void *args)
35483 +{
35484 +       struct del_wh_children_args *a = args;
35485 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35486 +}
35487 +
35488 +/* ---------------------------------------------------------------------- */
35489 +
35490 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35491 +{
35492 +       struct au_whtmp_rmdir *whtmp;
35493 +       int err;
35494 +       unsigned int rdhash;
35495 +
35496 +       SiMustAnyLock(sb);
35497 +
35498 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35499 +       if (unlikely(!whtmp)) {
35500 +               whtmp = ERR_PTR(-ENOMEM);
35501 +               goto out;
35502 +       }
35503 +
35504 +       /* no estimation for dir size */
35505 +       rdhash = au_sbi(sb)->si_rdhash;
35506 +       if (!rdhash)
35507 +               rdhash = AUFS_RDHASH_DEF;
35508 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35509 +       if (unlikely(err)) {
35510 +               kfree(whtmp);
35511 +               whtmp = ERR_PTR(err);
35512 +       }
35513 +
35514 +out:
35515 +       return whtmp;
35516 +}
35517 +
35518 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35519 +{
35520 +       if (whtmp->br)
35521 +               au_br_put(whtmp->br);
35522 +       dput(whtmp->wh_dentry);
35523 +       iput(whtmp->dir);
35524 +       au_nhash_wh_free(&whtmp->whlist);
35525 +       kfree(whtmp);
35526 +}
35527 +
35528 +/*
35529 + * rmdir the whiteouted temporary named dir @h_dentry.
35530 + * @whlist: whiteouted children.
35531 + */
35532 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35533 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35534 +{
35535 +       int err;
35536 +       unsigned int h_nlink;
35537 +       struct path h_tmp;
35538 +       struct inode *wh_inode, *h_dir;
35539 +       struct au_branch *br;
35540 +
35541 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35542 +       IMustLock(h_dir);
35543 +
35544 +       br = au_sbr(dir->i_sb, bindex);
35545 +       wh_inode = d_inode(wh_dentry);
35546 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35547 +
35548 +       /*
35549 +        * someone else might change some whiteouts while we were sleeping.
35550 +        * it means this whlist may have an obsoleted entry.
35551 +        */
35552 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35553 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35554 +       else {
35555 +               int wkq_err;
35556 +               struct del_wh_children_args args = {
35557 +                       .errp           = &err,
35558 +                       .h_dentry       = wh_dentry,
35559 +                       .whlist         = whlist,
35560 +                       .bindex         = bindex,
35561 +                       .br             = br
35562 +               };
35563 +
35564 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35565 +               if (unlikely(wkq_err))
35566 +                       err = wkq_err;
35567 +       }
35568 +       inode_unlock(wh_inode);
35569 +
35570 +       if (!err) {
35571 +               h_tmp.dentry = wh_dentry;
35572 +               h_tmp.mnt = au_br_mnt(br);
35573 +               h_nlink = h_dir->i_nlink;
35574 +               err = vfsub_rmdir(h_dir, &h_tmp);
35575 +               /* some fs doesn't change the parent nlink in some cases */
35576 +               h_nlink -= h_dir->i_nlink;
35577 +       }
35578 +
35579 +       if (!err) {
35580 +               if (au_ibtop(dir) == bindex) {
35581 +                       /* todo: dir->i_mutex is necessary */
35582 +                       au_cpup_attr_timesizes(dir);
35583 +                       if (h_nlink)
35584 +                               vfsub_drop_nlink(dir);
35585 +               }
35586 +               return 0; /* success */
35587 +       }
35588 +
35589 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35590 +       return err;
35591 +}
35592 +
35593 +static void call_rmdir_whtmp(void *args)
35594 +{
35595 +       int err;
35596 +       aufs_bindex_t bindex;
35597 +       struct au_whtmp_rmdir *a = args;
35598 +       struct super_block *sb;
35599 +       struct dentry *h_parent;
35600 +       struct inode *h_dir;
35601 +       struct au_hinode *hdir;
35602 +
35603 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35604 +       /* inode_lock(a->dir); */
35605 +       err = -EROFS;
35606 +       sb = a->dir->i_sb;
35607 +       si_read_lock(sb, !AuLock_FLUSH);
35608 +       if (!au_br_writable(a->br->br_perm))
35609 +               goto out;
35610 +       bindex = au_br_index(sb, a->br->br_id);
35611 +       if (unlikely(bindex < 0))
35612 +               goto out;
35613 +
35614 +       err = -EIO;
35615 +       ii_write_lock_parent(a->dir);
35616 +       h_parent = dget_parent(a->wh_dentry);
35617 +       h_dir = d_inode(h_parent);
35618 +       hdir = au_hi(a->dir, bindex);
35619 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35620 +       if (unlikely(err))
35621 +               goto out_mnt;
35622 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35623 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35624 +                         a->br);
35625 +       if (!err)
35626 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35627 +       au_hn_inode_unlock(hdir);
35628 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35629 +
35630 +out_mnt:
35631 +       dput(h_parent);
35632 +       ii_write_unlock(a->dir);
35633 +out:
35634 +       /* inode_unlock(a->dir); */
35635 +       au_whtmp_rmdir_free(a);
35636 +       si_read_unlock(sb);
35637 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35638 +       if (unlikely(err))
35639 +               AuIOErr("err %d\n", err);
35640 +}
35641 +
35642 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35643 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35644 +{
35645 +       int wkq_err;
35646 +       struct super_block *sb;
35647 +
35648 +       IMustLock(dir);
35649 +
35650 +       /* all post-process will be done in do_rmdir_whtmp(). */
35651 +       sb = dir->i_sb;
35652 +       args->dir = au_igrab(dir);
35653 +       args->br = au_sbr(sb, bindex);
35654 +       au_br_get(args->br);
35655 +       args->wh_dentry = dget(wh_dentry);
35656 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35657 +       if (unlikely(wkq_err)) {
35658 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35659 +               au_whtmp_rmdir_free(args);
35660 +       }
35661 +}
35662 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35663 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35664 +++ linux/fs/aufs/whout.h       2018-08-12 23:43:05.463458173 +0200
35665 @@ -0,0 +1,86 @@
35666 +/* SPDX-License-Identifier: GPL-2.0 */
35667 +/*
35668 + * Copyright (C) 2005-2018 Junjiro R. Okajima
35669 + *
35670 + * This program, aufs is free software; you can redistribute it and/or modify
35671 + * it under the terms of the GNU General Public License as published by
35672 + * the Free Software Foundation; either version 2 of the License, or
35673 + * (at your option) any later version.
35674 + *
35675 + * This program is distributed in the hope that it will be useful,
35676 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35677 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35678 + * GNU General Public License for more details.
35679 + *
35680 + * You should have received a copy of the GNU General Public License
35681 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35682 + */
35683 +
35684 +/*
35685 + * whiteout for logical deletion and opaque directory
35686 + */
35687 +
35688 +#ifndef __AUFS_WHOUT_H__
35689 +#define __AUFS_WHOUT_H__
35690 +
35691 +#ifdef __KERNEL__
35692 +
35693 +#include "dir.h"
35694 +
35695 +/* whout.c */
35696 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35697 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35698 +int au_diropq_test(struct dentry *h_dentry);
35699 +struct au_branch;
35700 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35701 +                            struct qstr *prefix);
35702 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35703 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35704 +                       struct dentry *dentry);
35705 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35706 +
35707 +/* diropq flags */
35708 +#define AuDiropq_CREATE        1
35709 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35710 +#define au_fset_diropq(flags, name) \
35711 +       do { (flags) |= AuDiropq_##name; } while (0)
35712 +#define au_fclr_diropq(flags, name) \
35713 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35714 +
35715 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35716 +                            unsigned int flags);
35717 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35718 +                         struct au_branch *br);
35719 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35720 +                           struct dentry *h_parent);
35721 +
35722 +/* real rmdir for the whiteout-ed dir */
35723 +struct au_whtmp_rmdir {
35724 +       struct inode *dir;
35725 +       struct au_branch *br;
35726 +       struct dentry *wh_dentry;
35727 +       struct au_nhash whlist;
35728 +};
35729 +
35730 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35731 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35732 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35733 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35734 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35735 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35736 +
35737 +/* ---------------------------------------------------------------------- */
35738 +
35739 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35740 +                                             aufs_bindex_t bindex)
35741 +{
35742 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35743 +}
35744 +
35745 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35746 +{
35747 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35748 +}
35749 +
35750 +#endif /* __KERNEL__ */
35751 +#endif /* __AUFS_WHOUT_H__ */
35752 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35753 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35754 +++ linux/fs/aufs/wkq.c 2018-08-12 23:43:05.463458173 +0200
35755 @@ -0,0 +1,391 @@
35756 +// SPDX-License-Identifier: GPL-2.0
35757 +/*
35758 + * Copyright (C) 2005-2018 Junjiro R. Okajima
35759 + *
35760 + * This program, aufs is free software; you can redistribute it and/or modify
35761 + * it under the terms of the GNU General Public License as published by
35762 + * the Free Software Foundation; either version 2 of the License, or
35763 + * (at your option) any later version.
35764 + *
35765 + * This program is distributed in the hope that it will be useful,
35766 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35767 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35768 + * GNU General Public License for more details.
35769 + *
35770 + * You should have received a copy of the GNU General Public License
35771 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35772 + */
35773 +
35774 +/*
35775 + * workqueue for asynchronous/super-io operations
35776 + * todo: try new dredential scheme
35777 + */
35778 +
35779 +#include <linux/module.h>
35780 +#include "aufs.h"
35781 +
35782 +/* internal workqueue named AUFS_WKQ_NAME */
35783 +
35784 +static struct workqueue_struct *au_wkq;
35785 +
35786 +struct au_wkinfo {
35787 +       struct work_struct wk;
35788 +       struct kobject *kobj;
35789 +
35790 +       unsigned int flags; /* see wkq.h */
35791 +
35792 +       au_wkq_func_t func;
35793 +       void *args;
35794 +
35795 +#ifdef CONFIG_LOCKDEP
35796 +       int dont_check;
35797 +       struct held_lock **hlock;
35798 +#endif
35799 +
35800 +       struct completion *comp;
35801 +};
35802 +
35803 +/* ---------------------------------------------------------------------- */
35804 +/*
35805 + * Aufs passes some operations to the workqueue such as the internal copyup.
35806 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35807 + * job run by workqueue depends upon the locks acquired in the other task.
35808 + * Delegating a small operation to the workqueue, aufs passes its lockdep
35809 + * information too. And the job in the workqueue restores the info in order to
35810 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
35811 + * correctly and expectedly.
35812 + */
35813 +
35814 +#ifndef CONFIG_LOCKDEP
35815 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35816 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35817 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35818 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35819 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35820 +#else
35821 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35822 +{
35823 +       wkinfo->hlock = NULL;
35824 +       wkinfo->dont_check = 0;
35825 +}
35826 +
35827 +/*
35828 + * 1: matched
35829 + * 0: unmatched
35830 + */
35831 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35832 +{
35833 +       static DEFINE_SPINLOCK(spin);
35834 +       static struct {
35835 +               char *name;
35836 +               struct lock_class_key *key;
35837 +       } a[] = {
35838 +               { .name = "&sbinfo->si_rwsem" },
35839 +               { .name = "&finfo->fi_rwsem" },
35840 +               { .name = "&dinfo->di_rwsem" },
35841 +               { .name = "&iinfo->ii_rwsem" }
35842 +       };
35843 +       static int set;
35844 +       int i;
35845 +
35846 +       /* lockless read from 'set.' see below */
35847 +       if (set == ARRAY_SIZE(a)) {
35848 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35849 +                       if (a[i].key == key)
35850 +                               goto match;
35851 +               goto unmatch;
35852 +       }
35853 +
35854 +       spin_lock(&spin);
35855 +       if (set)
35856 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35857 +                       if (a[i].key == key) {
35858 +                               spin_unlock(&spin);
35859 +                               goto match;
35860 +                       }
35861 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
35862 +               if (a[i].key) {
35863 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
35864 +                               spin_unlock(&spin);
35865 +                               goto match;
35866 +                       } else
35867 +                               continue;
35868 +               }
35869 +               if (strstr(a[i].name, name)) {
35870 +                       /*
35871 +                        * the order of these three lines is important for the
35872 +                        * lockless read above.
35873 +                        */
35874 +                       a[i].key = key;
35875 +                       spin_unlock(&spin);
35876 +                       set++;
35877 +                       /* AuDbg("%d, %s\n", set, name); */
35878 +                       goto match;
35879 +               }
35880 +       }
35881 +       spin_unlock(&spin);
35882 +       goto unmatch;
35883 +
35884 +match:
35885 +       return 1;
35886 +unmatch:
35887 +       return 0;
35888 +}
35889 +
35890 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35891 +{
35892 +       int err, n;
35893 +       struct task_struct *curr;
35894 +       struct held_lock **hl, *held_locks, *p;
35895 +
35896 +       err = 0;
35897 +       curr = current;
35898 +       wkinfo->dont_check = lockdep_recursing(curr);
35899 +       if (wkinfo->dont_check)
35900 +               goto out;
35901 +       n = curr->lockdep_depth;
35902 +       if (!n)
35903 +               goto out;
35904 +
35905 +       err = -ENOMEM;
35906 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35907 +       if (unlikely(!wkinfo->hlock))
35908 +               goto out;
35909 +
35910 +       err = 0;
35911 +#if 0
35912 +       if (0 && au_debug_test()) /* left for debugging */
35913 +               lockdep_print_held_locks(curr);
35914 +#endif
35915 +       held_locks = curr->held_locks;
35916 +       hl = wkinfo->hlock;
35917 +       while (n--) {
35918 +               p = held_locks++;
35919 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35920 +                       *hl++ = p;
35921 +       }
35922 +       *hl = NULL;
35923 +
35924 +out:
35925 +       return err;
35926 +}
35927 +
35928 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35929 +{
35930 +       kfree(wkinfo->hlock);
35931 +}
35932 +
35933 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
35934 +{
35935 +       struct held_lock *p, **hl = wkinfo->hlock;
35936 +       int subclass;
35937 +
35938 +       if (wkinfo->dont_check)
35939 +               lockdep_off();
35940 +       if (!hl)
35941 +               return;
35942 +       while ((p = *hl++)) { /* assignment */
35943 +               subclass = lockdep_hlock_class(p)->subclass;
35944 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
35945 +               if (p->read)
35946 +                       rwsem_acquire_read(p->instance, subclass, 0,
35947 +                                          /*p->acquire_ip*/_RET_IP_);
35948 +               else
35949 +                       rwsem_acquire(p->instance, subclass, 0,
35950 +                                     /*p->acquire_ip*/_RET_IP_);
35951 +       }
35952 +}
35953 +
35954 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
35955 +{
35956 +       struct held_lock *p, **hl = wkinfo->hlock;
35957 +
35958 +       if (wkinfo->dont_check)
35959 +               lockdep_on();
35960 +       if (!hl)
35961 +               return;
35962 +       while ((p = *hl++)) /* assignment */
35963 +               rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
35964 +}
35965 +#endif
35966 +
35967 +static void wkq_func(struct work_struct *wk)
35968 +{
35969 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
35970 +
35971 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
35972 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
35973 +
35974 +       au_wkq_lockdep_pre(wkinfo);
35975 +       wkinfo->func(wkinfo->args);
35976 +       au_wkq_lockdep_post(wkinfo);
35977 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
35978 +               complete(wkinfo->comp);
35979 +       else {
35980 +               kobject_put(wkinfo->kobj);
35981 +               module_put(THIS_MODULE); /* todo: ?? */
35982 +               kfree(wkinfo);
35983 +       }
35984 +}
35985 +
35986 +/*
35987 + * Since struct completion is large, try allocating it dynamically.
35988 + */
35989 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
35990 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
35991 +
35992 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
35993 +{
35994 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
35995 +       if (*comp) {
35996 +               init_completion(*comp);
35997 +               wkinfo->comp = *comp;
35998 +               return 0;
35999 +       }
36000 +       return -ENOMEM;
36001 +}
36002 +
36003 +static void au_wkq_comp_free(struct completion *comp)
36004 +{
36005 +       kfree(comp);
36006 +}
36007 +
36008 +#else
36009 +
36010 +/* no braces */
36011 +#define AuWkqCompDeclare(name) \
36012 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
36013 +       struct completion *comp = &_ ## name
36014 +
36015 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36016 +{
36017 +       wkinfo->comp = *comp;
36018 +       return 0;
36019 +}
36020 +
36021 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36022 +{
36023 +       /* empty */
36024 +}
36025 +#endif /* 4KSTACKS */
36026 +
36027 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36028 +{
36029 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36030 +               if (au_wkq_test()) {
36031 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36032 +                               " due to a dead dir by UDBA?\n");
36033 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36034 +               }
36035 +       } else
36036 +               au_dbg_verify_kthread();
36037 +
36038 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36039 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36040 +               queue_work(au_wkq, &wkinfo->wk);
36041 +       } else {
36042 +               INIT_WORK(&wkinfo->wk, wkq_func);
36043 +               schedule_work(&wkinfo->wk);
36044 +       }
36045 +}
36046 +
36047 +/*
36048 + * Be careful. It is easy to make deadlock happen.
36049 + * processA: lock, wkq and wait
36050 + * processB: wkq and wait, lock in wkq
36051 + * --> deadlock
36052 + */
36053 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36054 +{
36055 +       int err;
36056 +       AuWkqCompDeclare(comp);
36057 +       struct au_wkinfo wkinfo = {
36058 +               .flags  = flags,
36059 +               .func   = func,
36060 +               .args   = args
36061 +       };
36062 +
36063 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36064 +       if (unlikely(err))
36065 +               goto out;
36066 +       err = au_wkq_lockdep_alloc(&wkinfo);
36067 +       if (unlikely(err))
36068 +               goto out_comp;
36069 +       if (!err) {
36070 +               au_wkq_run(&wkinfo);
36071 +               /* no timeout, no interrupt */
36072 +               wait_for_completion(wkinfo.comp);
36073 +       }
36074 +       au_wkq_lockdep_free(&wkinfo);
36075 +
36076 +out_comp:
36077 +       au_wkq_comp_free(comp);
36078 +out:
36079 +       destroy_work_on_stack(&wkinfo.wk);
36080 +       return err;
36081 +}
36082 +
36083 +/*
36084 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36085 + * problem in a concurrent umounting.
36086 + */
36087 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36088 +                 unsigned int flags)
36089 +{
36090 +       int err;
36091 +       struct au_wkinfo *wkinfo;
36092 +
36093 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36094 +
36095 +       /*
36096 +        * wkq_func() must free this wkinfo.
36097 +        * it highly depends upon the implementation of workqueue.
36098 +        */
36099 +       err = 0;
36100 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36101 +       if (wkinfo) {
36102 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36103 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36104 +               wkinfo->func = func;
36105 +               wkinfo->args = args;
36106 +               wkinfo->comp = NULL;
36107 +               au_wkq_lockdep_init(wkinfo);
36108 +               kobject_get(wkinfo->kobj);
36109 +               __module_get(THIS_MODULE); /* todo: ?? */
36110 +
36111 +               au_wkq_run(wkinfo);
36112 +       } else {
36113 +               err = -ENOMEM;
36114 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36115 +       }
36116 +
36117 +       return err;
36118 +}
36119 +
36120 +/* ---------------------------------------------------------------------- */
36121 +
36122 +void au_nwt_init(struct au_nowait_tasks *nwt)
36123 +{
36124 +       atomic_set(&nwt->nw_len, 0);
36125 +       /* smp_mb(); */ /* atomic_set */
36126 +       init_waitqueue_head(&nwt->nw_wq);
36127 +}
36128 +
36129 +void au_wkq_fin(void)
36130 +{
36131 +       destroy_workqueue(au_wkq);
36132 +}
36133 +
36134 +int __init au_wkq_init(void)
36135 +{
36136 +       int err;
36137 +
36138 +       err = 0;
36139 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36140 +       if (IS_ERR(au_wkq))
36141 +               err = PTR_ERR(au_wkq);
36142 +       else if (!au_wkq)
36143 +               err = -ENOMEM;
36144 +
36145 +       return err;
36146 +}
36147 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36148 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36149 +++ linux/fs/aufs/wkq.h 2018-08-12 23:43:05.463458173 +0200
36150 @@ -0,0 +1,94 @@
36151 +/* SPDX-License-Identifier: GPL-2.0 */
36152 +/*
36153 + * Copyright (C) 2005-2018 Junjiro R. Okajima
36154 + *
36155 + * This program, aufs is free software; you can redistribute it and/or modify
36156 + * it under the terms of the GNU General Public License as published by
36157 + * the Free Software Foundation; either version 2 of the License, or
36158 + * (at your option) any later version.
36159 + *
36160 + * This program is distributed in the hope that it will be useful,
36161 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36162 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36163 + * GNU General Public License for more details.
36164 + *
36165 + * You should have received a copy of the GNU General Public License
36166 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36167 + */
36168 +
36169 +/*
36170 + * workqueue for asynchronous/super-io operations
36171 + * todo: try new credentials management scheme
36172 + */
36173 +
36174 +#ifndef __AUFS_WKQ_H__
36175 +#define __AUFS_WKQ_H__
36176 +
36177 +#ifdef __KERNEL__
36178 +
36179 +#include <linux/wait.h>
36180 +
36181 +struct super_block;
36182 +
36183 +/* ---------------------------------------------------------------------- */
36184 +
36185 +/*
36186 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36187 + */
36188 +struct au_nowait_tasks {
36189 +       atomic_t                nw_len;
36190 +       wait_queue_head_t       nw_wq;
36191 +};
36192 +
36193 +/* ---------------------------------------------------------------------- */
36194 +
36195 +typedef void (*au_wkq_func_t)(void *args);
36196 +
36197 +/* wkq flags */
36198 +#define AuWkq_WAIT     1
36199 +#define AuWkq_NEST     (1 << 1)
36200 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36201 +#define au_fset_wkq(flags, name) \
36202 +       do { (flags) |= AuWkq_##name; } while (0)
36203 +#define au_fclr_wkq(flags, name) \
36204 +       do { (flags) &= ~AuWkq_##name; } while (0)
36205 +
36206 +#ifndef CONFIG_AUFS_HNOTIFY
36207 +#undef AuWkq_NEST
36208 +#define AuWkq_NEST     0
36209 +#endif
36210 +
36211 +/* wkq.c */
36212 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36213 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36214 +                 unsigned int flags);
36215 +void au_nwt_init(struct au_nowait_tasks *nwt);
36216 +int __init au_wkq_init(void);
36217 +void au_wkq_fin(void);
36218 +
36219 +/* ---------------------------------------------------------------------- */
36220 +
36221 +static inline int au_wkq_test(void)
36222 +{
36223 +       return current->flags & PF_WQ_WORKER;
36224 +}
36225 +
36226 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36227 +{
36228 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36229 +}
36230 +
36231 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36232 +{
36233 +       if (atomic_dec_and_test(&nwt->nw_len))
36234 +               wake_up_all(&nwt->nw_wq);
36235 +}
36236 +
36237 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36238 +{
36239 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36240 +       return 0;
36241 +}
36242 +
36243 +#endif /* __KERNEL__ */
36244 +#endif /* __AUFS_WKQ_H__ */
36245 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36246 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36247 +++ linux/fs/aufs/xattr.c       2018-08-12 23:43:05.466791610 +0200
36248 @@ -0,0 +1,356 @@
36249 +// SPDX-License-Identifier: GPL-2.0
36250 +/*
36251 + * Copyright (C) 2014-2018 Junjiro R. Okajima
36252 + *
36253 + * This program, aufs is free software; you can redistribute it and/or modify
36254 + * it under the terms of the GNU General Public License as published by
36255 + * the Free Software Foundation; either version 2 of the License, or
36256 + * (at your option) any later version.
36257 + *
36258 + * This program is distributed in the hope that it will be useful,
36259 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36260 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36261 + * GNU General Public License for more details.
36262 + *
36263 + * You should have received a copy of the GNU General Public License
36264 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36265 + */
36266 +
36267 +/*
36268 + * handling xattr functions
36269 + */
36270 +
36271 +#include <linux/fs.h>
36272 +#include <linux/posix_acl_xattr.h>
36273 +#include <linux/xattr.h>
36274 +#include "aufs.h"
36275 +
36276 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36277 +{
36278 +       if (!ignore_flags)
36279 +               goto out;
36280 +       switch (err) {
36281 +       case -ENOMEM:
36282 +       case -EDQUOT:
36283 +               goto out;
36284 +       }
36285 +
36286 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36287 +               err = 0;
36288 +               goto out;
36289 +       }
36290 +
36291 +#define cmp(brattr, prefix) do {                                       \
36292 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36293 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36294 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36295 +                               err = 0;                                \
36296 +                       goto out;                                       \
36297 +               }                                                       \
36298 +       } while (0)
36299 +
36300 +       cmp(SEC, SECURITY);
36301 +       cmp(SYS, SYSTEM);
36302 +       cmp(TR, TRUSTED);
36303 +       cmp(USR, USER);
36304 +#undef cmp
36305 +
36306 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36307 +               err = 0;
36308 +
36309 +out:
36310 +       return err;
36311 +}
36312 +
36313 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36314 +
36315 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36316 +                           char *name, char **buf, unsigned int ignore_flags,
36317 +                           unsigned int verbose)
36318 +{
36319 +       int err;
36320 +       ssize_t ssz;
36321 +       struct inode *h_idst;
36322 +
36323 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36324 +       err = ssz;
36325 +       if (unlikely(err <= 0)) {
36326 +               if (err == -ENODATA
36327 +                   || (err == -EOPNOTSUPP
36328 +                       && ((ignore_flags & au_xattr_out_of_list)
36329 +                           || (au_test_nfs_noacl(d_inode(h_src))
36330 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36331 +                                   || !strcmp(name,
36332 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36333 +                           ))
36334 +                       err = 0;
36335 +               if (err && (verbose || au_debug_test()))
36336 +                       pr_err("%s, err %d\n", name, err);
36337 +               goto out;
36338 +       }
36339 +
36340 +       /* unlock it temporary */
36341 +       h_idst = d_inode(h_dst);
36342 +       inode_unlock(h_idst);
36343 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36344 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36345 +       if (unlikely(err)) {
36346 +               if (verbose || au_debug_test())
36347 +                       pr_err("%s, err %d\n", name, err);
36348 +               err = au_xattr_ignore(err, name, ignore_flags);
36349 +       }
36350 +
36351 +out:
36352 +       return err;
36353 +}
36354 +
36355 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36356 +                 unsigned int verbose)
36357 +{
36358 +       int err, unlocked, acl_access, acl_default;
36359 +       ssize_t ssz;
36360 +       struct inode *h_isrc, *h_idst;
36361 +       char *value, *p, *o, *e;
36362 +
36363 +       /* try stopping to update the source inode while we are referencing */
36364 +       /* there should not be the parent-child relationship between them */
36365 +       h_isrc = d_inode(h_src);
36366 +       h_idst = d_inode(h_dst);
36367 +       inode_unlock(h_idst);
36368 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36369 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36370 +       unlocked = 0;
36371 +
36372 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36373 +       ssz = vfs_listxattr(h_src, NULL, 0);
36374 +       err = ssz;
36375 +       if (unlikely(err < 0)) {
36376 +               AuTraceErr(err);
36377 +               if (err == -ENODATA
36378 +                   || err == -EOPNOTSUPP)
36379 +                       err = 0;        /* ignore */
36380 +               goto out;
36381 +       }
36382 +
36383 +       err = 0;
36384 +       p = NULL;
36385 +       o = NULL;
36386 +       if (ssz) {
36387 +               err = -ENOMEM;
36388 +               p = kmalloc(ssz, GFP_NOFS);
36389 +               o = p;
36390 +               if (unlikely(!p))
36391 +                       goto out;
36392 +               err = vfs_listxattr(h_src, p, ssz);
36393 +       }
36394 +       inode_unlock_shared(h_isrc);
36395 +       unlocked = 1;
36396 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36397 +       if (unlikely(err < 0))
36398 +               goto out_free;
36399 +
36400 +       err = 0;
36401 +       e = p + ssz;
36402 +       value = NULL;
36403 +       acl_access = 0;
36404 +       acl_default = 0;
36405 +       while (!err && p < e) {
36406 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36407 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36408 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36409 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36410 +                                       - 1);
36411 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36412 +                                      verbose);
36413 +               p += strlen(p) + 1;
36414 +       }
36415 +       AuTraceErr(err);
36416 +       ignore_flags |= au_xattr_out_of_list;
36417 +       if (!err && !acl_access) {
36418 +               err = au_do_cpup_xattr(h_dst, h_src,
36419 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36420 +                                      ignore_flags, verbose);
36421 +               AuTraceErr(err);
36422 +       }
36423 +       if (!err && !acl_default) {
36424 +               err = au_do_cpup_xattr(h_dst, h_src,
36425 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36426 +                                      ignore_flags, verbose);
36427 +               AuTraceErr(err);
36428 +       }
36429 +
36430 +       kfree(value);
36431 +
36432 +out_free:
36433 +       kfree(o);
36434 +out:
36435 +       if (!unlocked)
36436 +               inode_unlock_shared(h_isrc);
36437 +       AuTraceErr(err);
36438 +       return err;
36439 +}
36440 +
36441 +/* ---------------------------------------------------------------------- */
36442 +
36443 +static int au_smack_reentering(struct super_block *sb)
36444 +{
36445 +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36446 +       /*
36447 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36448 +        * i_op->getxattr(). ouch.
36449 +        */
36450 +       return si_pid_test(sb);
36451 +#else
36452 +       return 0;
36453 +#endif
36454 +}
36455 +
36456 +enum {
36457 +       AU_XATTR_LIST,
36458 +       AU_XATTR_GET
36459 +};
36460 +
36461 +struct au_lgxattr {
36462 +       int type;
36463 +       union {
36464 +               struct {
36465 +                       char    *list;
36466 +                       size_t  size;
36467 +               } list;
36468 +               struct {
36469 +                       const char      *name;
36470 +                       void            *value;
36471 +                       size_t          size;
36472 +               } get;
36473 +       } u;
36474 +};
36475 +
36476 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36477 +{
36478 +       ssize_t err;
36479 +       int reenter;
36480 +       struct path h_path;
36481 +       struct super_block *sb;
36482 +
36483 +       sb = dentry->d_sb;
36484 +       reenter = au_smack_reentering(sb);
36485 +       if (!reenter) {
36486 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36487 +               if (unlikely(err))
36488 +                       goto out;
36489 +       }
36490 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
36491 +       if (unlikely(err))
36492 +               goto out_si;
36493 +       if (unlikely(!h_path.dentry))
36494 +               /* illegally overlapped or something */
36495 +               goto out_di; /* pretending success */
36496 +
36497 +       /* always topmost entry only */
36498 +       switch (arg->type) {
36499 +       case AU_XATTR_LIST:
36500 +               err = vfs_listxattr(h_path.dentry,
36501 +                                   arg->u.list.list, arg->u.list.size);
36502 +               break;
36503 +       case AU_XATTR_GET:
36504 +               AuDebugOn(d_is_negative(h_path.dentry));
36505 +               err = vfs_getxattr(h_path.dentry,
36506 +                                  arg->u.get.name, arg->u.get.value,
36507 +                                  arg->u.get.size);
36508 +               break;
36509 +       }
36510 +
36511 +out_di:
36512 +       if (!reenter)
36513 +               di_read_unlock(dentry, AuLock_IR);
36514 +out_si:
36515 +       if (!reenter)
36516 +               si_read_unlock(sb);
36517 +out:
36518 +       AuTraceErr(err);
36519 +       return err;
36520 +}
36521 +
36522 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36523 +{
36524 +       struct au_lgxattr arg = {
36525 +               .type = AU_XATTR_LIST,
36526 +               .u.list = {
36527 +                       .list   = list,
36528 +                       .size   = size
36529 +               },
36530 +       };
36531 +
36532 +       return au_lgxattr(dentry, &arg);
36533 +}
36534 +
36535 +static ssize_t au_getxattr(struct dentry *dentry,
36536 +                          struct inode *inode __maybe_unused,
36537 +                          const char *name, void *value, size_t size)
36538 +{
36539 +       struct au_lgxattr arg = {
36540 +               .type = AU_XATTR_GET,
36541 +               .u.get = {
36542 +                       .name   = name,
36543 +                       .value  = value,
36544 +                       .size   = size
36545 +               },
36546 +       };
36547 +
36548 +       return au_lgxattr(dentry, &arg);
36549 +}
36550 +
36551 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36552 +                      const char *name, const void *value, size_t size,
36553 +                      int flags)
36554 +{
36555 +       struct au_sxattr arg = {
36556 +               .type = AU_XATTR_SET,
36557 +               .u.set = {
36558 +                       .name   = name,
36559 +                       .value  = value,
36560 +                       .size   = size,
36561 +                       .flags  = flags
36562 +               },
36563 +       };
36564 +
36565 +       return au_sxattr(dentry, inode, &arg);
36566 +}
36567 +
36568 +/* ---------------------------------------------------------------------- */
36569 +
36570 +static int au_xattr_get(const struct xattr_handler *handler,
36571 +                       struct dentry *dentry, struct inode *inode,
36572 +                       const char *name, void *buffer, size_t size)
36573 +{
36574 +       return au_getxattr(dentry, inode, name, buffer, size);
36575 +}
36576 +
36577 +static int au_xattr_set(const struct xattr_handler *handler,
36578 +                       struct dentry *dentry, struct inode *inode,
36579 +                       const char *name, const void *value, size_t size,
36580 +                       int flags)
36581 +{
36582 +       return au_setxattr(dentry, inode, name, value, size, flags);
36583 +}
36584 +
36585 +static const struct xattr_handler au_xattr_handler = {
36586 +       .name   = "",
36587 +       .prefix = "",
36588 +       .get    = au_xattr_get,
36589 +       .set    = au_xattr_set
36590 +};
36591 +
36592 +static const struct xattr_handler *au_xattr_handlers[] = {
36593 +#ifdef CONFIG_FS_POSIX_ACL
36594 +       &posix_acl_access_xattr_handler,
36595 +       &posix_acl_default_xattr_handler,
36596 +#endif
36597 +       &au_xattr_handler, /* must be last */
36598 +       NULL
36599 +};
36600 +
36601 +void au_xattr_init(struct super_block *sb)
36602 +{
36603 +       sb->s_xattr = au_xattr_handlers;
36604 +}
36605 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36606 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36607 +++ linux/fs/aufs/xino.c        2018-08-12 23:43:05.466791610 +0200
36608 @@ -0,0 +1,1593 @@
36609 +// SPDX-License-Identifier: GPL-2.0
36610 +/*
36611 + * Copyright (C) 2005-2018 Junjiro R. Okajima
36612 + *
36613 + * This program, aufs is free software; you can redistribute it and/or modify
36614 + * it under the terms of the GNU General Public License as published by
36615 + * the Free Software Foundation; either version 2 of the License, or
36616 + * (at your option) any later version.
36617 + *
36618 + * This program is distributed in the hope that it will be useful,
36619 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36620 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36621 + * GNU General Public License for more details.
36622 + *
36623 + * You should have received a copy of the GNU General Public License
36624 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36625 + */
36626 +
36627 +/*
36628 + * external inode number translation table and bitmap
36629 + *
36630 + * things to consider
36631 + * - the lifetime
36632 + *   + au_xino object
36633 + *   + XINO files (xino, xib, xigen)
36634 + *   + dynamic debugfs entries (xiN)
36635 + *   + static debugfs entries (xib, xigen)
36636 + *   + static sysfs entry (xi_path)
36637 + * - several entry points to handle them.
36638 + *   + mount(2) without xino option (default)
36639 + *   + mount(2) with xino option
36640 + *   + mount(2) with noxino option
36641 + *   + umount(2)
36642 + *   + remount with add/del branches
36643 + *   + remount with xino/noxino options
36644 + */
36645 +
36646 +#include <linux/seq_file.h>
36647 +#include <linux/statfs.h>
36648 +#include "aufs.h"
36649 +
36650 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36651 +                                    aufs_bindex_t bbot,
36652 +                                    struct super_block *h_sb)
36653 +{
36654 +       /* todo: try binary-search if the branches are many */
36655 +       for (; btop <= bbot; btop++)
36656 +               if (h_sb == au_sbr_sb(sb, btop))
36657 +                       return btop;
36658 +       return -1;
36659 +}
36660 +
36661 +/*
36662 + * find another branch who is on the same filesystem of the specified
36663 + * branch{@btgt}. search until @bbot.
36664 + */
36665 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36666 +                                 aufs_bindex_t bbot)
36667 +{
36668 +       aufs_bindex_t bindex;
36669 +       struct super_block *tgt_sb;
36670 +
36671 +       tgt_sb = au_sbr_sb(sb, btgt);
36672 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36673 +       if (bindex < 0)
36674 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36675 +
36676 +       return bindex;
36677 +}
36678 +
36679 +/* ---------------------------------------------------------------------- */
36680 +
36681 +/*
36682 + * stop unnecessary notify events at creating xino files
36683 + */
36684 +struct au_xino_lock_dir {
36685 +       struct au_hinode *hdir;
36686 +       struct dentry *parent;
36687 +       struct inode *dir;
36688 +};
36689 +
36690 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36691 +                                         unsigned int lsc)
36692 +{
36693 +       struct dentry *parent;
36694 +       struct inode *dir;
36695 +
36696 +       parent = dget_parent(dentry);
36697 +       dir = d_inode(parent);
36698 +       inode_lock_nested(dir, lsc);
36699 +#if 0 /* it should not happen */
36700 +       spin_lock(&dentry->d_lock);
36701 +       if (unlikely(dentry->d_parent != parent)) {
36702 +               spin_unlock(&dentry->d_lock);
36703 +               inode_unlock(dir);
36704 +               dput(parent);
36705 +               parent = NULL;
36706 +               goto out;
36707 +       }
36708 +       spin_unlock(&dentry->d_lock);
36709 +
36710 +out:
36711 +#endif
36712 +       return parent;
36713 +}
36714 +
36715 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36716 +                            struct au_xino_lock_dir *ldir)
36717 +{
36718 +       aufs_bindex_t brid, bindex;
36719 +
36720 +       ldir->hdir = NULL;
36721 +       bindex = -1;
36722 +       brid = au_xino_brid(sb);
36723 +       if (brid >= 0)
36724 +               bindex = au_br_index(sb, brid);
36725 +       if (bindex >= 0) {
36726 +               /* rw branch root */
36727 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36728 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36729 +       } else {
36730 +               /* other */
36731 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36732 +                                                  AuLsc_I_PARENT);
36733 +               ldir->dir = d_inode(ldir->parent);
36734 +       }
36735 +}
36736 +
36737 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36738 +{
36739 +       if (ldir->hdir)
36740 +               au_hn_inode_unlock(ldir->hdir);
36741 +       else {
36742 +               inode_unlock(ldir->dir);
36743 +               dput(ldir->parent);
36744 +       }
36745 +}
36746 +
36747 +/* ---------------------------------------------------------------------- */
36748 +
36749 +/*
36750 + * create and set a new xino file
36751 + */
36752 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
36753 +{
36754 +       struct file *file;
36755 +       struct dentry *h_parent, *d;
36756 +       struct inode *h_dir, *inode;
36757 +       int err;
36758 +
36759 +       /*
36760 +        * at mount-time, and the xino file is the default path,
36761 +        * hnotify is disabled so we have no notify events to ignore.
36762 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36763 +        */
36764 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36765 +                              /* | __FMODE_NONOTIFY */,
36766 +                              0666);
36767 +       if (IS_ERR(file)) {
36768 +               if (!silent)
36769 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36770 +               return file;
36771 +       }
36772 +
36773 +       /* keep file count */
36774 +       err = 0;
36775 +       d = file->f_path.dentry;
36776 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36777 +       /* mnt_want_write() is unnecessary here */
36778 +       h_dir = d_inode(h_parent);
36779 +       inode = file_inode(file);
36780 +       /* no delegation since it is just created */
36781 +       if (inode->i_nlink)
36782 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36783 +                                  /*force*/0);
36784 +       inode_unlock(h_dir);
36785 +       dput(h_parent);
36786 +       if (unlikely(err)) {
36787 +               if (!silent)
36788 +                       pr_err("unlink %s(%d)\n", fpath, err);
36789 +               goto out;
36790 +       }
36791 +
36792 +       err = -EINVAL;
36793 +       if (unlikely(sb == d->d_sb)) {
36794 +               if (!silent)
36795 +                       pr_err("%s must be outside\n", fpath);
36796 +               goto out;
36797 +       }
36798 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36799 +               if (!silent)
36800 +                       pr_err("xino doesn't support %s(%s)\n",
36801 +                              fpath, au_sbtype(d->d_sb));
36802 +               goto out;
36803 +       }
36804 +       return file; /* success */
36805 +
36806 +out:
36807 +       fput(file);
36808 +       file = ERR_PTR(err);
36809 +       return file;
36810 +}
36811 +
36812 +/*
36813 + * create a new xinofile at the same place/path as @base.
36814 + */
36815 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
36816 +                            struct file *copy_src)
36817 +{
36818 +       struct file *file;
36819 +       struct dentry *dentry, *parent;
36820 +       struct inode *dir, *delegated;
36821 +       struct qstr *name;
36822 +       struct path path;
36823 +       int err, do_unlock;
36824 +       struct au_xino_lock_dir ldir;
36825 +
36826 +       do_unlock = 1;
36827 +       au_xino_lock_dir(sb, base, &ldir);
36828 +       dentry = base->dentry;
36829 +       parent = dentry->d_parent; /* dir inode is locked */
36830 +       dir = d_inode(parent);
36831 +       IMustLock(dir);
36832 +
36833 +       name = &dentry->d_name;
36834 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36835 +       if (IS_ERR(path.dentry)) {
36836 +               file = (void *)path.dentry;
36837 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36838 +               goto out;
36839 +       }
36840 +
36841 +       /* no need to mnt_want_write() since we call dentry_open() later */
36842 +       err = vfs_create(dir, path.dentry, 0666, NULL);
36843 +       if (unlikely(err)) {
36844 +               file = ERR_PTR(err);
36845 +               pr_err("%pd create err %d\n", dentry, err);
36846 +               goto out_dput;
36847 +       }
36848 +
36849 +       path.mnt = base->mnt;
36850 +       file = vfsub_dentry_open(&path,
36851 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36852 +                                /* | __FMODE_NONOTIFY */);
36853 +       if (IS_ERR(file)) {
36854 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36855 +               goto out_dput;
36856 +       }
36857 +
36858 +       delegated = NULL;
36859 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36860 +       au_xino_unlock_dir(&ldir);
36861 +       do_unlock = 0;
36862 +       if (unlikely(err == -EWOULDBLOCK)) {
36863 +               pr_warn("cannot retry for NFSv4 delegation"
36864 +                       " for an internal unlink\n");
36865 +               iput(delegated);
36866 +       }
36867 +       if (unlikely(err)) {
36868 +               pr_err("%pd unlink err %d\n", dentry, err);
36869 +               goto out_fput;
36870 +       }
36871 +
36872 +       if (copy_src) {
36873 +               /* no one can touch copy_src xino */
36874 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36875 +               if (unlikely(err)) {
36876 +                       pr_err("%pd copy err %d\n", dentry, err);
36877 +                       goto out_fput;
36878 +               }
36879 +       }
36880 +       goto out_dput; /* success */
36881 +
36882 +out_fput:
36883 +       fput(file);
36884 +       file = ERR_PTR(err);
36885 +out_dput:
36886 +       dput(path.dentry);
36887 +out:
36888 +       if (do_unlock)
36889 +               au_xino_unlock_dir(&ldir);
36890 +       return file;
36891 +}
36892 +
36893 +/* ---------------------------------------------------------------------- */
36894 +
36895 +/*
36896 + * truncate xino files
36897 + */
36898 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
36899 +{
36900 +       int err;
36901 +       unsigned long jiffy;
36902 +       blkcnt_t blocks;
36903 +       aufs_bindex_t bbot;
36904 +       struct kstatfs *st;
36905 +       struct au_branch *br;
36906 +       struct file *new_xino, *file;
36907 +       struct path *path;
36908 +
36909 +       err = -ENOMEM;
36910 +       st = kmalloc(sizeof(*st), GFP_NOFS);
36911 +       if (unlikely(!st))
36912 +               goto out;
36913 +
36914 +       err = -EINVAL;
36915 +       bbot = au_sbbot(sb);
36916 +       if (unlikely(bindex < 0 || bbot < bindex))
36917 +               goto out_st;
36918 +       br = au_sbr(sb, bindex);
36919 +       file = au_xino_file(br);
36920 +       if (!file)
36921 +               goto out_st;
36922 +
36923 +       path = &file->f_path;
36924 +       err = vfs_statfs(path, st);
36925 +       if (unlikely(err))
36926 +               AuErr1("statfs err %d, ignored\n", err);
36927 +       jiffy = jiffies;
36928 +       blocks = file_inode(file)->i_blocks;
36929 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36930 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
36931 +
36932 +       new_xino = au_xino_create2(sb, path, file);
36933 +       err = PTR_ERR(new_xino);
36934 +       if (IS_ERR(new_xino)) {
36935 +               pr_err("err %d, ignored\n", err);
36936 +               goto out_st;
36937 +       }
36938 +       err = 0;
36939 +       au_xino_file_set(br, new_xino);
36940 +
36941 +       err = vfs_statfs(&new_xino->f_path, st);
36942 +       if (!err) {
36943 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36944 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
36945 +                       st->f_bfree, st->f_blocks);
36946 +               if (file_inode(new_xino)->i_blocks < blocks)
36947 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
36948 +       } else
36949 +               AuErr1("statfs err %d, ignored\n", err);
36950 +       fput(new_xino);
36951 +
36952 +out_st:
36953 +       kfree(st);
36954 +out:
36955 +       return err;
36956 +}
36957 +
36958 +struct xino_do_trunc_args {
36959 +       struct super_block *sb;
36960 +       struct au_branch *br;
36961 +};
36962 +
36963 +static void xino_do_trunc(void *_args)
36964 +{
36965 +       struct xino_do_trunc_args *args = _args;
36966 +       struct super_block *sb;
36967 +       struct au_branch *br;
36968 +       struct inode *dir;
36969 +       int err;
36970 +       aufs_bindex_t bindex;
36971 +
36972 +       err = 0;
36973 +       sb = args->sb;
36974 +       dir = d_inode(sb->s_root);
36975 +       br = args->br;
36976 +
36977 +       si_noflush_write_lock(sb);
36978 +       ii_read_lock_parent(dir);
36979 +       bindex = au_br_index(sb, br->br_id);
36980 +       err = au_xino_trunc(sb, bindex);
36981 +       ii_read_unlock(dir);
36982 +       if (unlikely(err))
36983 +               pr_warn("err b%d, (%d)\n", bindex, err);
36984 +       atomic_dec(&br->br_xino->xi_truncating);
36985 +       au_br_put(br);
36986 +       si_write_unlock(sb);
36987 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36988 +       kfree(args);
36989 +}
36990 +
36991 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
36992 +{
36993 +       int err;
36994 +       struct kstatfs st;
36995 +       struct au_sbinfo *sbinfo;
36996 +       struct file *file;
36997 +
36998 +       /* todo: si_xino_expire and the ratio should be customizable */
36999 +       sbinfo = au_sbi(sb);
37000 +       if (time_before(jiffies,
37001 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37002 +               return 0;
37003 +
37004 +       /* truncation border */
37005 +       file = au_xino_file(br);
37006 +       AuDebugOn(!file);
37007 +       err = vfs_statfs(&file->f_path, &st);
37008 +       if (unlikely(err)) {
37009 +               AuErr1("statfs err %d, ignored\n", err);
37010 +               return 0;
37011 +       }
37012 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
37013 +               return 0;
37014 +
37015 +       return 1;
37016 +}
37017 +
37018 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37019 +{
37020 +       struct xino_do_trunc_args *args;
37021 +       int wkq_err;
37022 +
37023 +       if (!xino_trunc_test(sb, br))
37024 +               return;
37025 +
37026 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37027 +               goto out;
37028 +
37029 +       /* lock and kfree() will be called in trunc_xino() */
37030 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37031 +       if (unlikely(!args)) {
37032 +               AuErr1("no memory\n");
37033 +               goto out;
37034 +       }
37035 +
37036 +       au_br_get(br);
37037 +       args->sb = sb;
37038 +       args->br = br;
37039 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37040 +       if (!wkq_err)
37041 +               return; /* success */
37042 +
37043 +       pr_err("wkq %d\n", wkq_err);
37044 +       au_br_put(br);
37045 +       kfree(args);
37046 +
37047 +out:
37048 +       atomic_dec(&br->br_xino->xi_truncating);
37049 +}
37050 +
37051 +/* ---------------------------------------------------------------------- */
37052 +
37053 +/*
37054 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37055 + * at the position of @h_ino.
37056 + */
37057 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37058 +                ino_t *ino)
37059 +{
37060 +       int err;
37061 +       ssize_t sz;
37062 +       loff_t pos;
37063 +       struct au_branch *br;
37064 +       struct file *file;
37065 +       struct au_sbinfo *sbinfo;
37066 +
37067 +       *ino = 0;
37068 +       if (!au_opt_test(au_mntflags(sb), XINO))
37069 +               return 0; /* no xino */
37070 +
37071 +       err = 0;
37072 +       sbinfo = au_sbi(sb);
37073 +       pos = h_ino;
37074 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
37075 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37076 +               return -EFBIG;
37077 +       }
37078 +       pos *= sizeof(*ino);
37079 +
37080 +       br = au_sbr(sb, bindex);
37081 +       file = au_xino_file(br);
37082 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
37083 +               return 0; /* no ino */
37084 +
37085 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
37086 +       if (sz == sizeof(*ino))
37087 +               return 0; /* success */
37088 +
37089 +       err = sz;
37090 +       if (unlikely(sz >= 0)) {
37091 +               err = -EIO;
37092 +               AuIOErr("xino read error (%zd)\n", sz);
37093 +       }
37094 +
37095 +       return err;
37096 +}
37097 +
37098 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37099 +                           ino_t h_ino, ino_t ino)
37100 +{
37101 +       loff_t pos;
37102 +       ssize_t sz;
37103 +
37104 +       pos = h_ino;
37105 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
37106 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37107 +               return -EFBIG;
37108 +       }
37109 +       pos *= sizeof(ino);
37110 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
37111 +       if (sz == sizeof(ino))
37112 +               return 0; /* success */
37113 +
37114 +       AuIOErr("write failed (%zd)\n", sz);
37115 +       return -EIO;
37116 +}
37117 +
37118 +/*
37119 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37120 + * at the position of @h_ino.
37121 + * even if @ino is zero, it is written to the xinofile and means no entry.
37122 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37123 + * try truncating it.
37124 + */
37125 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37126 +                 ino_t ino)
37127 +{
37128 +       int err;
37129 +       unsigned int mnt_flags;
37130 +       struct au_branch *br;
37131 +
37132 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
37133 +                    || ((loff_t)-1) > 0);
37134 +       SiMustAnyLock(sb);
37135 +
37136 +       mnt_flags = au_mntflags(sb);
37137 +       if (!au_opt_test(mnt_flags, XINO))
37138 +               return 0;
37139 +
37140 +       br = au_sbr(sb, bindex);
37141 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, au_xino_file(br), h_ino,
37142 +                              ino);
37143 +       if (!err) {
37144 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37145 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37146 +                       xino_try_trunc(sb, br);
37147 +               return 0; /* success */
37148 +       }
37149 +
37150 +       AuIOErr("write failed (%d)\n", err);
37151 +       return -EIO;
37152 +}
37153 +
37154 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37155 +                             size_t size, loff_t *pos);
37156 +
37157 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37158 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37159 +                  loff_t *pos)
37160 +{
37161 +       ssize_t err;
37162 +       mm_segment_t oldfs;
37163 +       union {
37164 +               void *k;
37165 +               char __user *u;
37166 +       } buf;
37167 +       int i;
37168 +       const int prevent_endless = 10;
37169 +
37170 +       i = 0;
37171 +       buf.k = kbuf;
37172 +       oldfs = get_fs();
37173 +       set_fs(KERNEL_DS);
37174 +       do {
37175 +               err = func(file, buf.u, size, pos);
37176 +               if (err == -EINTR
37177 +                   && !au_wkq_test()
37178 +                   && fatal_signal_pending(current)) {
37179 +                       set_fs(oldfs);
37180 +                       err = xino_fread_wkq(func, file, kbuf, size, pos);
37181 +                       BUG_ON(err == -EINTR);
37182 +                       oldfs = get_fs();
37183 +                       set_fs(KERNEL_DS);
37184 +               }
37185 +       } while (i++ < prevent_endless
37186 +                && (err == -EAGAIN || err == -EINTR));
37187 +       set_fs(oldfs);
37188 +
37189 +#if 0 /* reserved for future use */
37190 +       if (err > 0)
37191 +               fsnotify_access(file->f_path.dentry);
37192 +#endif
37193 +
37194 +       return err;
37195 +}
37196 +
37197 +struct xino_fread_args {
37198 +       ssize_t *errp;
37199 +       vfs_readf_t func;
37200 +       struct file *file;
37201 +       void *buf;
37202 +       size_t size;
37203 +       loff_t *pos;
37204 +};
37205 +
37206 +static void call_xino_fread(void *args)
37207 +{
37208 +       struct xino_fread_args *a = args;
37209 +       *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37210 +}
37211 +
37212 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37213 +                             size_t size, loff_t *pos)
37214 +{
37215 +       ssize_t err;
37216 +       int wkq_err;
37217 +       struct xino_fread_args args = {
37218 +               .errp   = &err,
37219 +               .func   = func,
37220 +               .file   = file,
37221 +               .buf    = buf,
37222 +               .size   = size,
37223 +               .pos    = pos
37224 +       };
37225 +
37226 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37227 +       if (unlikely(wkq_err))
37228 +               err = wkq_err;
37229 +
37230 +       return err;
37231 +}
37232 +
37233 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37234 +                              size_t size, loff_t *pos);
37235 +
37236 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37237 +                             size_t size, loff_t *pos)
37238 +{
37239 +       ssize_t err;
37240 +       mm_segment_t oldfs;
37241 +       union {
37242 +               void *k;
37243 +               const char __user *u;
37244 +       } buf;
37245 +       int i;
37246 +       const int prevent_endless = 10;
37247 +
37248 +       i = 0;
37249 +       buf.k = kbuf;
37250 +       oldfs = get_fs();
37251 +       set_fs(KERNEL_DS);
37252 +       do {
37253 +               err = func(file, buf.u, size, pos);
37254 +               if (err == -EINTR
37255 +                   && !au_wkq_test()
37256 +                   && fatal_signal_pending(current)) {
37257 +                       set_fs(oldfs);
37258 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37259 +                       BUG_ON(err == -EINTR);
37260 +                       oldfs = get_fs();
37261 +                       set_fs(KERNEL_DS);
37262 +               }
37263 +       } while (i++ < prevent_endless
37264 +                && (err == -EAGAIN || err == -EINTR));
37265 +       set_fs(oldfs);
37266 +
37267 +#if 0 /* reserved for future use */
37268 +       if (err > 0)
37269 +               fsnotify_modify(file->f_path.dentry);
37270 +#endif
37271 +
37272 +       return err;
37273 +}
37274 +
37275 +struct do_xino_fwrite_args {
37276 +       ssize_t *errp;
37277 +       vfs_writef_t func;
37278 +       struct file *file;
37279 +       void *buf;
37280 +       size_t size;
37281 +       loff_t *pos;
37282 +};
37283 +
37284 +static void call_do_xino_fwrite(void *args)
37285 +{
37286 +       struct do_xino_fwrite_args *a = args;
37287 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37288 +}
37289 +
37290 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37291 +                              size_t size, loff_t *pos)
37292 +{
37293 +       ssize_t err;
37294 +       int wkq_err;
37295 +       struct do_xino_fwrite_args args = {
37296 +               .errp   = &err,
37297 +               .func   = func,
37298 +               .file   = file,
37299 +               .buf    = buf,
37300 +               .size   = size,
37301 +               .pos    = pos
37302 +       };
37303 +
37304 +       /*
37305 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37306 +        * users should care about quota and real 'filesystem full.'
37307 +        */
37308 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37309 +       if (unlikely(wkq_err))
37310 +               err = wkq_err;
37311 +
37312 +       return err;
37313 +}
37314 +
37315 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37316 +                   size_t size, loff_t *pos)
37317 +{
37318 +       ssize_t err;
37319 +
37320 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37321 +               lockdep_off();
37322 +               err = do_xino_fwrite(func, file, buf, size, pos);
37323 +               lockdep_on();
37324 +       } else {
37325 +               lockdep_off();
37326 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
37327 +               lockdep_on();
37328 +       }
37329 +
37330 +       return err;
37331 +}
37332 +
37333 +/* ---------------------------------------------------------------------- */
37334 +
37335 +/*
37336 + * inode number bitmap
37337 + */
37338 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37339 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37340 +{
37341 +       ino_t ino;
37342 +
37343 +       AuDebugOn(bit < 0 || page_bits <= bit);
37344 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37345 +       return ino;
37346 +}
37347 +
37348 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37349 +{
37350 +       AuDebugOn(ino < AUFS_FIRST_INO);
37351 +       ino -= AUFS_FIRST_INO;
37352 +       *pindex = ino / page_bits;
37353 +       *bit = ino % page_bits;
37354 +}
37355 +
37356 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37357 +{
37358 +       int err;
37359 +       loff_t pos;
37360 +       ssize_t sz;
37361 +       struct au_sbinfo *sbinfo;
37362 +       struct file *xib;
37363 +       unsigned long *p;
37364 +
37365 +       sbinfo = au_sbi(sb);
37366 +       MtxMustLock(&sbinfo->si_xib_mtx);
37367 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37368 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37369 +
37370 +       if (pindex == sbinfo->si_xib_last_pindex)
37371 +               return 0;
37372 +
37373 +       xib = sbinfo->si_xib;
37374 +       p = sbinfo->si_xib_buf;
37375 +       pos = sbinfo->si_xib_last_pindex;
37376 +       pos *= PAGE_SIZE;
37377 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37378 +       if (unlikely(sz != PAGE_SIZE))
37379 +               goto out;
37380 +
37381 +       pos = pindex;
37382 +       pos *= PAGE_SIZE;
37383 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37384 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37385 +       else {
37386 +               memset(p, 0, PAGE_SIZE);
37387 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37388 +       }
37389 +       if (sz == PAGE_SIZE) {
37390 +               sbinfo->si_xib_last_pindex = pindex;
37391 +               return 0; /* success */
37392 +       }
37393 +
37394 +out:
37395 +       AuIOErr1("write failed (%zd)\n", sz);
37396 +       err = sz;
37397 +       if (sz >= 0)
37398 +               err = -EIO;
37399 +       return err;
37400 +}
37401 +
37402 +static void au_xib_clear_bit(struct inode *inode)
37403 +{
37404 +       int err, bit;
37405 +       unsigned long pindex;
37406 +       struct super_block *sb;
37407 +       struct au_sbinfo *sbinfo;
37408 +
37409 +       AuDebugOn(inode->i_nlink);
37410 +
37411 +       sb = inode->i_sb;
37412 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37413 +       AuDebugOn(page_bits <= bit);
37414 +       sbinfo = au_sbi(sb);
37415 +       mutex_lock(&sbinfo->si_xib_mtx);
37416 +       err = xib_pindex(sb, pindex);
37417 +       if (!err) {
37418 +               clear_bit(bit, sbinfo->si_xib_buf);
37419 +               sbinfo->si_xib_next_bit = bit;
37420 +       }
37421 +       mutex_unlock(&sbinfo->si_xib_mtx);
37422 +}
37423 +
37424 +/* ---------------------------------------------------------------------- */
37425 +
37426 +/*
37427 + * truncate a xino bitmap file
37428 + */
37429 +
37430 +/* todo: slow */
37431 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37432 +{
37433 +       int err, bit;
37434 +       ssize_t sz;
37435 +       unsigned long pindex;
37436 +       loff_t pos, pend;
37437 +       struct au_sbinfo *sbinfo;
37438 +       vfs_readf_t func;
37439 +       ino_t *ino;
37440 +       unsigned long *p;
37441 +
37442 +       err = 0;
37443 +       sbinfo = au_sbi(sb);
37444 +       MtxMustLock(&sbinfo->si_xib_mtx);
37445 +       p = sbinfo->si_xib_buf;
37446 +       func = sbinfo->si_xread;
37447 +       pend = vfsub_f_size_read(file);
37448 +       pos = 0;
37449 +       while (pos < pend) {
37450 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37451 +               err = sz;
37452 +               if (unlikely(sz <= 0))
37453 +                       goto out;
37454 +
37455 +               err = 0;
37456 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37457 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37458 +                               continue;
37459 +
37460 +                       xib_calc_bit(*ino, &pindex, &bit);
37461 +                       AuDebugOn(page_bits <= bit);
37462 +                       err = xib_pindex(sb, pindex);
37463 +                       if (!err)
37464 +                               set_bit(bit, p);
37465 +                       else
37466 +                               goto out;
37467 +               }
37468 +       }
37469 +
37470 +out:
37471 +       return err;
37472 +}
37473 +
37474 +static int xib_restore(struct super_block *sb)
37475 +{
37476 +       int err;
37477 +       aufs_bindex_t bindex, bbot;
37478 +       void *page;
37479 +       struct au_branch *br;
37480 +
37481 +       err = -ENOMEM;
37482 +       page = (void *)__get_free_page(GFP_NOFS);
37483 +       if (unlikely(!page))
37484 +               goto out;
37485 +
37486 +       err = 0;
37487 +       bbot = au_sbbot(sb);
37488 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
37489 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37490 +                       br = au_sbr(sb, bindex);
37491 +                       err = do_xib_restore(sb, au_xino_file(br), page);
37492 +               } else
37493 +                       AuDbg("skip shared b%d\n", bindex);
37494 +       free_page((unsigned long)page);
37495 +
37496 +out:
37497 +       return err;
37498 +}
37499 +
37500 +int au_xib_trunc(struct super_block *sb)
37501 +{
37502 +       int err;
37503 +       ssize_t sz;
37504 +       loff_t pos;
37505 +       struct au_sbinfo *sbinfo;
37506 +       unsigned long *p;
37507 +       struct file *file;
37508 +
37509 +       SiMustWriteLock(sb);
37510 +
37511 +       err = 0;
37512 +       sbinfo = au_sbi(sb);
37513 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
37514 +               goto out;
37515 +
37516 +       file = sbinfo->si_xib;
37517 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
37518 +               goto out;
37519 +
37520 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37521 +       err = PTR_ERR(file);
37522 +       if (IS_ERR(file))
37523 +               goto out;
37524 +       fput(sbinfo->si_xib);
37525 +       sbinfo->si_xib = file;
37526 +
37527 +       p = sbinfo->si_xib_buf;
37528 +       memset(p, 0, PAGE_SIZE);
37529 +       pos = 0;
37530 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
37531 +       if (unlikely(sz != PAGE_SIZE)) {
37532 +               err = sz;
37533 +               AuIOErr("err %d\n", err);
37534 +               if (sz >= 0)
37535 +                       err = -EIO;
37536 +               goto out;
37537 +       }
37538 +
37539 +       mutex_lock(&sbinfo->si_xib_mtx);
37540 +       /* mnt_want_write() is unnecessary here */
37541 +       err = xib_restore(sb);
37542 +       mutex_unlock(&sbinfo->si_xib_mtx);
37543 +
37544 +out:
37545 +       return err;
37546 +}
37547 +
37548 +/* ---------------------------------------------------------------------- */
37549 +
37550 +struct au_xino *au_xino_alloc(void)
37551 +{
37552 +       struct au_xino *xi;
37553 +
37554 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
37555 +       if (unlikely(!xi))
37556 +               goto out;
37557 +
37558 +       xi->xi_nondir.total = 8; /* initial size */
37559 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37560 +                                     GFP_NOFS);
37561 +       if (unlikely(!xi->xi_nondir.array))
37562 +               goto out_free;
37563 +
37564 +       spin_lock_init(&xi->xi_nondir.spin);
37565 +       init_waitqueue_head(&xi->xi_nondir.wqh);
37566 +       atomic_set(&xi->xi_truncating, 0);
37567 +       kref_init(&xi->xi_kref);
37568 +       goto out; /* success */
37569 +
37570 +out_free:
37571 +       kfree(xi);
37572 +       xi = NULL;
37573 +out:
37574 +       return xi;
37575 +}
37576 +
37577 +static int au_xino_init(struct au_branch *br, struct file *file)
37578 +{
37579 +       int err;
37580 +       struct au_xino *xi;
37581 +
37582 +       err = 0;
37583 +       xi = au_xino_alloc();
37584 +       if (unlikely(!xi)) {
37585 +               err = -ENOMEM;
37586 +               goto out;
37587 +       }
37588 +
37589 +       get_file(file);
37590 +       xi->xi_file = file;
37591 +       AuDebugOn(br->br_xino);
37592 +       br->br_xino = xi;
37593 +
37594 +out:
37595 +       return err;
37596 +}
37597 +
37598 +static void au_xino_release(struct kref *kref)
37599 +{
37600 +       struct au_xino *xi;
37601 +       int i;
37602 +
37603 +       xi = container_of(kref, struct au_xino, xi_kref);
37604 +       if (xi->xi_file)
37605 +               fput(xi->xi_file);
37606 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
37607 +               AuDebugOn(xi->xi_nondir.array[i]);
37608 +       kfree(xi->xi_nondir.array);
37609 +       kfree(xi);
37610 +}
37611 +
37612 +int au_xino_put(struct au_branch *br)
37613 +{
37614 +       int ret;
37615 +       struct au_xino *xi;
37616 +
37617 +       ret = 0;
37618 +       xi = br->br_xino;
37619 +       if (xi) {
37620 +               br->br_xino = NULL;
37621 +               ret = kref_put(&xi->xi_kref, au_xino_release);
37622 +       }
37623 +
37624 +       return ret;
37625 +}
37626 +
37627 +void au_xino_file_set(struct au_branch *br, struct file *file)
37628 +{
37629 +       struct au_xino *xi;
37630 +       struct file *f;
37631 +
37632 +       if (file)
37633 +               get_file(file);
37634 +       xi = br->br_xino;
37635 +       AuDebugOn(!xi);
37636 +       f = xi->xi_file;
37637 +       if (f)
37638 +               fput(f);
37639 +       xi->xi_file = file;
37640 +}
37641 +
37642 +/* ---------------------------------------------------------------------- */
37643 +
37644 +/*
37645 + * xino mount option handlers
37646 + */
37647 +
37648 +/* xino bitmap */
37649 +static void xino_clear_xib(struct super_block *sb)
37650 +{
37651 +       struct au_sbinfo *sbinfo;
37652 +
37653 +       SiMustWriteLock(sb);
37654 +
37655 +       sbinfo = au_sbi(sb);
37656 +       /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
37657 +       if (sbinfo->si_xib)
37658 +               fput(sbinfo->si_xib);
37659 +       sbinfo->si_xib = NULL;
37660 +       if (sbinfo->si_xib_buf)
37661 +               free_page((unsigned long)sbinfo->si_xib_buf);
37662 +       sbinfo->si_xib_buf = NULL;
37663 +}
37664 +
37665 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
37666 +{
37667 +       int err;
37668 +       loff_t pos;
37669 +       struct au_sbinfo *sbinfo;
37670 +       struct file *file;
37671 +
37672 +       SiMustWriteLock(sb);
37673 +
37674 +       sbinfo = au_sbi(sb);
37675 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
37676 +       err = PTR_ERR(file);
37677 +       if (IS_ERR(file))
37678 +               goto out;
37679 +       if (sbinfo->si_xib)
37680 +               fput(sbinfo->si_xib);
37681 +       sbinfo->si_xib = file;
37682 +       sbinfo->si_xread = vfs_readf(file);
37683 +       sbinfo->si_xwrite = vfs_writef(file);
37684 +
37685 +       err = -ENOMEM;
37686 +       if (!sbinfo->si_xib_buf)
37687 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
37688 +       if (unlikely(!sbinfo->si_xib_buf))
37689 +               goto out_unset;
37690 +
37691 +       sbinfo->si_xib_last_pindex = 0;
37692 +       sbinfo->si_xib_next_bit = 0;
37693 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
37694 +               pos = 0;
37695 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
37696 +                                 PAGE_SIZE, &pos);
37697 +               if (unlikely(err != PAGE_SIZE))
37698 +                       goto out_free;
37699 +       }
37700 +       err = 0;
37701 +       goto out; /* success */
37702 +
37703 +out_free:
37704 +       if (sbinfo->si_xib_buf)
37705 +               free_page((unsigned long)sbinfo->si_xib_buf);
37706 +       sbinfo->si_xib_buf = NULL;
37707 +       if (err >= 0)
37708 +               err = -EIO;
37709 +out_unset:
37710 +       fput(sbinfo->si_xib);
37711 +       sbinfo->si_xib = NULL;
37712 +out:
37713 +       AuTraceErr(err);
37714 +       return err;
37715 +}
37716 +
37717 +/* xino for each branch */
37718 +static void xino_clear_br(struct super_block *sb)
37719 +{
37720 +       aufs_bindex_t bindex, bbot;
37721 +       struct au_branch *br;
37722 +
37723 +       bbot = au_sbbot(sb);
37724 +       for (bindex = 0; bindex <= bbot; bindex++) {
37725 +               br = au_sbr(sb, bindex);
37726 +               AuDebugOn(!br);
37727 +               au_xino_put(br);
37728 +       }
37729 +}
37730 +
37731 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
37732 +                                 aufs_bindex_t bshared)
37733 +{
37734 +       struct au_branch *brshared;
37735 +
37736 +       brshared = au_sbr(sb, bshared);
37737 +       AuDebugOn(!brshared->br_xino);
37738 +       AuDebugOn(!brshared->br_xino->xi_file);
37739 +       if (br->br_xino != brshared->br_xino) {
37740 +               au_xino_get(brshared);
37741 +               au_xino_put(br);
37742 +               br->br_xino = brshared->br_xino;
37743 +       }
37744 +}
37745 +
37746 +struct au_xino_do_set_br {
37747 +       vfs_writef_t writef;
37748 +       struct au_branch *br;
37749 +       ino_t h_ino;
37750 +       aufs_bindex_t bshared;
37751 +};
37752 +
37753 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
37754 +                            struct au_xino_do_set_br *args)
37755 +{
37756 +       int err;
37757 +       struct file *file;
37758 +
37759 +       if (args->bshared >= 0) {
37760 +               /* shared xino */
37761 +               au_xino_set_br_shared(sb, args->br, args->bshared);
37762 +               file = au_xino_file(args->br);
37763 +               goto out_ino; /* success */
37764 +       }
37765 +
37766 +       /* new xino */
37767 +       file = au_xino_create2(sb, path, au_xino_file(args->br));
37768 +       err = PTR_ERR(file);
37769 +       if (IS_ERR(file))
37770 +               goto out;
37771 +       if (!args->br->br_xino) {
37772 +               err = au_xino_init(args->br, file);
37773 +               fput(file);
37774 +               if (unlikely(err))
37775 +                       goto out;
37776 +       } else {
37777 +               au_xino_file_set(args->br, file);
37778 +               fput(file);
37779 +       }
37780 +
37781 +out_ino:
37782 +       err = au_xino_do_write(args->writef, file, args->h_ino, AUFS_ROOT_INO);
37783 +out:
37784 +       AuTraceErr(err);
37785 +       return err;
37786 +}
37787 +
37788 +static int au_xino_set_br(struct super_block *sb, struct path *path)
37789 +{
37790 +       int err;
37791 +       aufs_bindex_t bindex, bbot;
37792 +       struct au_xino_do_set_br args;
37793 +       struct inode *inode;
37794 +
37795 +       SiMustWriteLock(sb);
37796 +
37797 +       bbot = au_sbbot(sb);
37798 +       inode = d_inode(sb->s_root);
37799 +       args.writef = au_sbi(sb)->si_xwrite;
37800 +       for (bindex = 0; bindex <= bbot; bindex++) {
37801 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
37802 +               args.br = au_sbr(sb, bindex);
37803 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
37804 +               err = au_xino_do_set_br(sb, path, &args);
37805 +               if (unlikely(err))
37806 +                       break;
37807 +       }
37808 +
37809 +       AuTraceErr(err);
37810 +       return err;
37811 +}
37812 +
37813 +void au_xino_clr(struct super_block *sb)
37814 +{
37815 +       struct au_sbinfo *sbinfo;
37816 +
37817 +       au_xigen_clr(sb);
37818 +       xino_clear_xib(sb);
37819 +       xino_clear_br(sb);
37820 +       dbgaufs_brs_del(sb, 0);
37821 +       au_xino_brid_set(sb, -1);
37822 +       sbinfo = au_sbi(sb);
37823 +       /* lvalue, do not call au_mntflags() */
37824 +       au_opt_clr(sbinfo->si_mntflags, XINO);
37825 +}
37826 +
37827 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
37828 +{
37829 +       int err, skip;
37830 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
37831 +       struct qstr *dname, *cur_name;
37832 +       struct file *cur_xino;
37833 +       struct au_sbinfo *sbinfo;
37834 +       struct path *path, *cur_path;
37835 +
37836 +       SiMustWriteLock(sb);
37837 +
37838 +       err = 0;
37839 +       sbinfo = au_sbi(sb);
37840 +       path = &xiopt->file->f_path;
37841 +       dentry = path->dentry;
37842 +       parent = dget_parent(dentry);
37843 +       if (remount) {
37844 +               skip = 0;
37845 +               cur_xino = sbinfo->si_xib;
37846 +               if (cur_xino) {
37847 +                       cur_path = &cur_xino->f_path;
37848 +                       cur_dentry = cur_path->dentry;
37849 +                       cur_parent = dget_parent(cur_dentry);
37850 +                       cur_name = &cur_dentry->d_name;
37851 +                       dname = &dentry->d_name;
37852 +                       skip = (cur_parent == parent
37853 +                               && au_qstreq(dname, cur_name));
37854 +                       dput(cur_parent);
37855 +               }
37856 +               if (skip)
37857 +                       goto out;
37858 +       }
37859 +
37860 +       au_opt_set(sbinfo->si_mntflags, XINO);
37861 +       err = au_xino_set_xib(sb, path);
37862 +       /* si_x{read,write} are set */
37863 +       if (!err)
37864 +               err = au_xigen_set(sb, path);
37865 +       if (!err)
37866 +               err = au_xino_set_br(sb, path);
37867 +       if (!err) {
37868 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
37869 +               goto out; /* success */
37870 +       }
37871 +
37872 +       /* reset all */
37873 +       AuIOErr("failed setting xino(%d).\n", err);
37874 +       au_xino_clr(sb);
37875 +
37876 +out:
37877 +       dput(parent);
37878 +       return err;
37879 +}
37880 +
37881 +/*
37882 + * create a xinofile at the default place/path.
37883 + */
37884 +struct file *au_xino_def(struct super_block *sb)
37885 +{
37886 +       struct file *file;
37887 +       char *page, *p;
37888 +       struct au_branch *br;
37889 +       struct super_block *h_sb;
37890 +       struct path path;
37891 +       aufs_bindex_t bbot, bindex, bwr;
37892 +
37893 +       br = NULL;
37894 +       bbot = au_sbbot(sb);
37895 +       bwr = -1;
37896 +       for (bindex = 0; bindex <= bbot; bindex++) {
37897 +               br = au_sbr(sb, bindex);
37898 +               if (au_br_writable(br->br_perm)
37899 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
37900 +                       bwr = bindex;
37901 +                       break;
37902 +               }
37903 +       }
37904 +
37905 +       if (bwr >= 0) {
37906 +               file = ERR_PTR(-ENOMEM);
37907 +               page = (void *)__get_free_page(GFP_NOFS);
37908 +               if (unlikely(!page))
37909 +                       goto out;
37910 +               path.mnt = au_br_mnt(br);
37911 +               path.dentry = au_h_dptr(sb->s_root, bwr);
37912 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
37913 +               file = (void *)p;
37914 +               if (!IS_ERR(p)) {
37915 +                       strcat(p, "/" AUFS_XINO_FNAME);
37916 +                       AuDbg("%s\n", p);
37917 +                       file = au_xino_create(sb, p, /*silent*/0);
37918 +                       if (!IS_ERR(file))
37919 +                               au_xino_brid_set(sb, br->br_id);
37920 +               }
37921 +               free_page((unsigned long)page);
37922 +       } else {
37923 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
37924 +               if (IS_ERR(file))
37925 +                       goto out;
37926 +               h_sb = file->f_path.dentry->d_sb;
37927 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
37928 +                       pr_err("xino doesn't support %s(%s)\n",
37929 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
37930 +                       fput(file);
37931 +                       file = ERR_PTR(-EINVAL);
37932 +               }
37933 +               if (!IS_ERR(file))
37934 +                       au_xino_brid_set(sb, -1);
37935 +       }
37936 +
37937 +out:
37938 +       return file;
37939 +}
37940 +
37941 +/* ---------------------------------------------------------------------- */
37942 +
37943 +/*
37944 + * initialize the xinofile for the specified branch @br
37945 + * at the place/path where @base_file indicates.
37946 + * test whether another branch is on the same filesystem or not,
37947 + * if found then share the xinofile with another branch.
37948 + */
37949 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
37950 +                   struct path *base)
37951 +{
37952 +       int err;
37953 +       struct au_xino_do_set_br args = {
37954 +               .h_ino  = h_ino,
37955 +               .br     = br
37956 +       };
37957 +
37958 +       args.writef = au_sbi(sb)->si_xwrite;
37959 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
37960 +                                      au_br_sb(br));
37961 +       err = au_xino_do_set_br(sb, base, &args);
37962 +       if (unlikely(err))
37963 +               au_xino_put(br);
37964 +
37965 +       return err;
37966 +}
37967 +
37968 +/* ---------------------------------------------------------------------- */
37969 +
37970 +/*
37971 + * get an unused inode number from bitmap
37972 + */
37973 +ino_t au_xino_new_ino(struct super_block *sb)
37974 +{
37975 +       ino_t ino;
37976 +       unsigned long *p, pindex, ul, pend;
37977 +       struct au_sbinfo *sbinfo;
37978 +       struct file *file;
37979 +       int free_bit, err;
37980 +
37981 +       if (!au_opt_test(au_mntflags(sb), XINO))
37982 +               return iunique(sb, AUFS_FIRST_INO);
37983 +
37984 +       sbinfo = au_sbi(sb);
37985 +       mutex_lock(&sbinfo->si_xib_mtx);
37986 +       p = sbinfo->si_xib_buf;
37987 +       free_bit = sbinfo->si_xib_next_bit;
37988 +       if (free_bit < page_bits && !test_bit(free_bit, p))
37989 +               goto out; /* success */
37990 +       free_bit = find_first_zero_bit(p, page_bits);
37991 +       if (free_bit < page_bits)
37992 +               goto out; /* success */
37993 +
37994 +       pindex = sbinfo->si_xib_last_pindex;
37995 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
37996 +               err = xib_pindex(sb, ul);
37997 +               if (unlikely(err))
37998 +                       goto out_err;
37999 +               free_bit = find_first_zero_bit(p, page_bits);
38000 +               if (free_bit < page_bits)
38001 +                       goto out; /* success */
38002 +       }
38003 +
38004 +       file = sbinfo->si_xib;
38005 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38006 +       for (ul = pindex + 1; ul <= pend; ul++) {
38007 +               err = xib_pindex(sb, ul);
38008 +               if (unlikely(err))
38009 +                       goto out_err;
38010 +               free_bit = find_first_zero_bit(p, page_bits);
38011 +               if (free_bit < page_bits)
38012 +                       goto out; /* success */
38013 +       }
38014 +       BUG();
38015 +
38016 +out:
38017 +       set_bit(free_bit, p);
38018 +       sbinfo->si_xib_next_bit = free_bit + 1;
38019 +       pindex = sbinfo->si_xib_last_pindex;
38020 +       mutex_unlock(&sbinfo->si_xib_mtx);
38021 +       ino = xib_calc_ino(pindex, free_bit);
38022 +       AuDbg("i%lu\n", (unsigned long)ino);
38023 +       return ino;
38024 +out_err:
38025 +       mutex_unlock(&sbinfo->si_xib_mtx);
38026 +       AuDbg("i0\n");
38027 +       return 0;
38028 +}
38029 +
38030 +/* for s_op->delete_inode() */
38031 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38032 +{
38033 +       int err;
38034 +       unsigned int mnt_flags;
38035 +       aufs_bindex_t bindex, bbot, bi;
38036 +       unsigned char try_trunc;
38037 +       struct au_iinfo *iinfo;
38038 +       struct super_block *sb;
38039 +       struct au_hinode *hi;
38040 +       struct inode *h_inode;
38041 +       struct au_branch *br;
38042 +       vfs_writef_t xwrite;
38043 +
38044 +       AuDebugOn(au_is_bad_inode(inode));
38045 +
38046 +       sb = inode->i_sb;
38047 +       mnt_flags = au_mntflags(sb);
38048 +       if (!au_opt_test(mnt_flags, XINO)
38049 +           || inode->i_ino == AUFS_ROOT_INO)
38050 +               return;
38051 +
38052 +       if (unlinked) {
38053 +               au_xigen_inc(inode);
38054 +               au_xib_clear_bit(inode);
38055 +       }
38056 +
38057 +       iinfo = au_ii(inode);
38058 +       bindex = iinfo->ii_btop;
38059 +       if (bindex < 0)
38060 +               return;
38061 +
38062 +       xwrite = au_sbi(sb)->si_xwrite;
38063 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38064 +       hi = au_hinode(iinfo, bindex);
38065 +       bbot = iinfo->ii_bbot;
38066 +       for (; bindex <= bbot; bindex++, hi++) {
38067 +               h_inode = hi->hi_inode;
38068 +               if (!h_inode
38069 +                   || (!unlinked && h_inode->i_nlink))
38070 +                       continue;
38071 +
38072 +               /* inode may not be revalidated */
38073 +               bi = au_br_index(sb, hi->hi_id);
38074 +               if (bi < 0)
38075 +                       continue;
38076 +
38077 +               br = au_sbr(sb, bi);
38078 +               err = au_xino_do_write(xwrite, au_xino_file(br),
38079 +                                      h_inode->i_ino, /*ino*/0);
38080 +               if (!err && try_trunc
38081 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38082 +                       xino_try_trunc(sb, br);
38083 +       }
38084 +}
38085 +
38086 +/* ---------------------------------------------------------------------- */
38087 +
38088 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38089 +{
38090 +       int found, total, i;
38091 +
38092 +       found = -1;
38093 +       total = xi->xi_nondir.total;
38094 +       for (i = 0; i < total; i++) {
38095 +               if (xi->xi_nondir.array[i] != h_ino)
38096 +                       continue;
38097 +               found = i;
38098 +               break;
38099 +       }
38100 +
38101 +       return found;
38102 +}
38103 +
38104 +static int au_xinondir_expand(struct au_xino *xi)
38105 +{
38106 +       int err, sz;
38107 +       ino_t *p;
38108 +
38109 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38110 +
38111 +       err = -ENOMEM;
38112 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38113 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38114 +               goto out;
38115 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38116 +                        /*may_shrink*/0);
38117 +       if (p) {
38118 +               xi->xi_nondir.array = p;
38119 +               xi->xi_nondir.total <<= 1;
38120 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38121 +               err = 0;
38122 +       }
38123 +
38124 +out:
38125 +       return err;
38126 +}
38127 +
38128 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38129 +                      ino_t h_ino, int idx)
38130 +{
38131 +       struct au_xino *xi;
38132 +
38133 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38134 +       xi = au_sbr(sb, bindex)->br_xino;
38135 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38136 +
38137 +       spin_lock(&xi->xi_nondir.spin);
38138 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38139 +       xi->xi_nondir.array[idx] = 0;
38140 +       spin_unlock(&xi->xi_nondir.spin);
38141 +       wake_up_all(&xi->xi_nondir.wqh);
38142 +}
38143 +
38144 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38145 +                     int *idx)
38146 +{
38147 +       int err, found, empty;
38148 +       struct au_xino *xi;
38149 +
38150 +       err = 0;
38151 +       *idx = -1;
38152 +       if (!au_opt_test(au_mntflags(sb), XINO))
38153 +               goto out; /* no xino */
38154 +
38155 +       xi = au_sbr(sb, bindex)->br_xino;
38156 +
38157 +again:
38158 +       spin_lock(&xi->xi_nondir.spin);
38159 +       found = au_xinondir_find(xi, h_ino);
38160 +       if (found == -1) {
38161 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38162 +               if (empty == -1) {
38163 +                       empty = xi->xi_nondir.total;
38164 +                       err = au_xinondir_expand(xi);
38165 +                       if (unlikely(err))
38166 +                               goto out_unlock;
38167 +               }
38168 +               xi->xi_nondir.array[empty] = h_ino;
38169 +               *idx = empty;
38170 +       } else {
38171 +               spin_unlock(&xi->xi_nondir.spin);
38172 +               wait_event(xi->xi_nondir.wqh,
38173 +                          xi->xi_nondir.array[found] != h_ino);
38174 +               goto again;
38175 +       }
38176 +
38177 +out_unlock:
38178 +       spin_unlock(&xi->xi_nondir.spin);
38179 +out:
38180 +       return err;
38181 +}
38182 +
38183 +/* ---------------------------------------------------------------------- */
38184 +
38185 +int au_xino_path(struct seq_file *seq, struct file *file)
38186 +{
38187 +       int err;
38188 +
38189 +       err = au_seq_path(seq, &file->f_path);
38190 +       if (unlikely(err))
38191 +               goto out;
38192 +
38193 +#define Deleted "\\040(deleted)"
38194 +       seq->count -= sizeof(Deleted) - 1;
38195 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38196 +                        sizeof(Deleted) - 1));
38197 +#undef Deleted
38198 +
38199 +out:
38200 +       return err;
38201 +}
38202 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38203 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38204 +++ linux/include/uapi/linux/aufs_type.h        2018-08-12 23:43:05.466791610 +0200
38205 @@ -0,0 +1,448 @@
38206 +/* SPDX-License-Identifier: GPL-2.0 */
38207 +/*
38208 + * Copyright (C) 2005-2018 Junjiro R. Okajima
38209 + *
38210 + * This program, aufs is free software; you can redistribute it and/or modify
38211 + * it under the terms of the GNU General Public License as published by
38212 + * the Free Software Foundation; either version 2 of the License, or
38213 + * (at your option) any later version.
38214 + *
38215 + * This program is distributed in the hope that it will be useful,
38216 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38217 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38218 + * GNU General Public License for more details.
38219 + *
38220 + * You should have received a copy of the GNU General Public License
38221 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38222 + */
38223 +
38224 +#ifndef __AUFS_TYPE_H__
38225 +#define __AUFS_TYPE_H__
38226 +
38227 +#define AUFS_NAME      "aufs"
38228 +
38229 +#ifdef __KERNEL__
38230 +/*
38231 + * define it before including all other headers.
38232 + * sched.h may use pr_* macros before defining "current", so define the
38233 + * no-current version first, and re-define later.
38234 + */
38235 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38236 +#include <linux/sched.h>
38237 +#undef pr_fmt
38238 +#define pr_fmt(fmt) \
38239 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38240 +               (int)sizeof(current->comm), current->comm, current->pid
38241 +#else
38242 +#include <stdint.h>
38243 +#include <sys/types.h>
38244 +#endif /* __KERNEL__ */
38245 +
38246 +#include <linux/limits.h>
38247 +
38248 +#define AUFS_VERSION   "4.x-rcN-20180730"
38249 +
38250 +/* todo? move this to linux-2.6.19/include/magic.h */
38251 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38252 +
38253 +/* ---------------------------------------------------------------------- */
38254 +
38255 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38256 +typedef int8_t aufs_bindex_t;
38257 +#define AUFS_BRANCH_MAX 127
38258 +#else
38259 +typedef int16_t aufs_bindex_t;
38260 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38261 +#define AUFS_BRANCH_MAX 511
38262 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38263 +#define AUFS_BRANCH_MAX 1023
38264 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38265 +#define AUFS_BRANCH_MAX 32767
38266 +#endif
38267 +#endif
38268 +
38269 +#ifdef __KERNEL__
38270 +#ifndef AUFS_BRANCH_MAX
38271 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38272 +#endif
38273 +#endif /* __KERNEL__ */
38274 +
38275 +/* ---------------------------------------------------------------------- */
38276 +
38277 +#define AUFS_FSTYPE            AUFS_NAME
38278 +
38279 +#define AUFS_ROOT_INO          2
38280 +#define AUFS_FIRST_INO         11
38281 +
38282 +#define AUFS_WH_PFX            ".wh."
38283 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38284 +#define AUFS_WH_TMP_LEN                4
38285 +/* a limit for rmdir/rename a dir and copyup */
38286 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38287 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38288 +                               - 1                     /* dot */\
38289 +                               - AUFS_WH_TMP_LEN)      /* hex */
38290 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38291 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38292 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38293 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38294 +#define AUFS_DIRWH_DEF         3
38295 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38296 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38297 +#define AUFS_RDBLK_DEF         512 /* bytes */
38298 +#define AUFS_RDHASH_DEF                32
38299 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38300 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38301 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38302 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38303 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38304 +
38305 +/* pseudo-link maintenace under /proc */
38306 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38307 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38308 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38309 +
38310 +/* dirren, renamed dir */
38311 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38312 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38313 +/* whiteouted doubly */
38314 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38315 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38316 +
38317 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38318 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38319 +
38320 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38321 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38322 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38323 +
38324 +/* doubly whiteouted */
38325 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38326 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38327 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38328 +
38329 +/* branch permissions and attributes */
38330 +#define AUFS_BRPERM_RW         "rw"
38331 +#define AUFS_BRPERM_RO         "ro"
38332 +#define AUFS_BRPERM_RR         "rr"
38333 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38334 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38335 +#define AUFS_BRATTR_FHSM       "fhsm"
38336 +#define AUFS_BRATTR_UNPIN      "unpin"
38337 +#define AUFS_BRATTR_ICEX       "icex"
38338 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38339 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38340 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38341 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38342 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38343 +#define AUFS_BRRATTR_WH                "wh"
38344 +#define AUFS_BRWATTR_NLWH      "nolwh"
38345 +#define AUFS_BRWATTR_MOO       "moo"
38346 +
38347 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38348 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38349 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38350 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38351 +
38352 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38353 +#define AuBrAttr_COO_ALL       (1 << 4)
38354 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38355 +
38356 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38357 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38358 +                                                  branch. meaningless since
38359 +                                                  linux-3.18-rc1 */
38360 +
38361 +/* ignore error in copying XATTR */
38362 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38363 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38364 +#define AuBrAttr_ICEX_TR       (1 << 9)
38365 +#define AuBrAttr_ICEX_USR      (1 << 10)
38366 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38367 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38368 +                                | AuBrAttr_ICEX_SYS    \
38369 +                                | AuBrAttr_ICEX_TR     \
38370 +                                | AuBrAttr_ICEX_USR    \
38371 +                                | AuBrAttr_ICEX_OTH)
38372 +
38373 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38374 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38375 +
38376 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38377 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38378 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38379 +
38380 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38381 +
38382 +/* #warning test userspace */
38383 +#ifdef __KERNEL__
38384 +#ifndef CONFIG_AUFS_FHSM
38385 +#undef AuBrAttr_FHSM
38386 +#define AuBrAttr_FHSM          0
38387 +#endif
38388 +#ifndef CONFIG_AUFS_XATTR
38389 +#undef AuBrAttr_ICEX
38390 +#define AuBrAttr_ICEX          0
38391 +#undef AuBrAttr_ICEX_SEC
38392 +#define AuBrAttr_ICEX_SEC      0
38393 +#undef AuBrAttr_ICEX_SYS
38394 +#define AuBrAttr_ICEX_SYS      0
38395 +#undef AuBrAttr_ICEX_TR
38396 +#define AuBrAttr_ICEX_TR       0
38397 +#undef AuBrAttr_ICEX_USR
38398 +#define AuBrAttr_ICEX_USR      0
38399 +#undef AuBrAttr_ICEX_OTH
38400 +#define AuBrAttr_ICEX_OTH      0
38401 +#endif
38402 +#endif
38403 +
38404 +/* the longest combination */
38405 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38406 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38407 +                              "+" AUFS_BRATTR_COO_REG          \
38408 +                              "+" AUFS_BRATTR_FHSM             \
38409 +                              "+" AUFS_BRATTR_UNPIN            \
38410 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38411 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38412 +                              "+" AUFS_BRATTR_ICEX_USR         \
38413 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38414 +                              "+" AUFS_BRWATTR_NLWH)
38415 +
38416 +typedef struct {
38417 +       char a[AuBrPermStrSz];
38418 +} au_br_perm_str_t;
38419 +
38420 +static inline int au_br_writable(int brperm)
38421 +{
38422 +       return brperm & AuBrPerm_RW;
38423 +}
38424 +
38425 +static inline int au_br_whable(int brperm)
38426 +{
38427 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38428 +}
38429 +
38430 +static inline int au_br_wh_linkable(int brperm)
38431 +{
38432 +       return !(brperm & AuBrWAttr_NoLinkWH);
38433 +}
38434 +
38435 +static inline int au_br_cmoo(int brperm)
38436 +{
38437 +       return brperm & AuBrAttr_CMOO_Mask;
38438 +}
38439 +
38440 +static inline int au_br_fhsm(int brperm)
38441 +{
38442 +       return brperm & AuBrAttr_FHSM;
38443 +}
38444 +
38445 +/* ---------------------------------------------------------------------- */
38446 +
38447 +/* ioctl */
38448 +enum {
38449 +       /* readdir in userspace */
38450 +       AuCtl_RDU,
38451 +       AuCtl_RDU_INO,
38452 +
38453 +       AuCtl_WBR_FD,   /* pathconf wrapper */
38454 +       AuCtl_IBUSY,    /* busy inode */
38455 +       AuCtl_MVDOWN,   /* move-down */
38456 +       AuCtl_BR,       /* info about branches */
38457 +       AuCtl_FHSM_FD   /* connection for fhsm */
38458 +};
38459 +
38460 +/* borrowed from linux/include/linux/kernel.h */
38461 +#ifndef ALIGN
38462 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
38463 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
38464 +#endif
38465 +
38466 +/* borrowed from linux/include/linux/compiler-gcc3.h */
38467 +#ifndef __aligned
38468 +#define __aligned(x)                   __attribute__((aligned(x)))
38469 +#endif
38470 +
38471 +#ifdef __KERNEL__
38472 +#ifndef __packed
38473 +#define __packed                       __attribute__((packed))
38474 +#endif
38475 +#endif
38476 +
38477 +struct au_rdu_cookie {
38478 +       uint64_t        h_pos;
38479 +       int16_t         bindex;
38480 +       uint8_t         flags;
38481 +       uint8_t         pad;
38482 +       uint32_t        generation;
38483 +} __aligned(8);
38484 +
38485 +struct au_rdu_ent {
38486 +       uint64_t        ino;
38487 +       int16_t         bindex;
38488 +       uint8_t         type;
38489 +       uint8_t         nlen;
38490 +       uint8_t         wh;
38491 +       char            name[0];
38492 +} __aligned(8);
38493 +
38494 +static inline int au_rdu_len(int nlen)
38495 +{
38496 +       /* include the terminating NULL */
38497 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
38498 +                    sizeof(uint64_t));
38499 +}
38500 +
38501 +union au_rdu_ent_ul {
38502 +       struct au_rdu_ent __user        *e;
38503 +       uint64_t                        ul;
38504 +};
38505 +
38506 +enum {
38507 +       AufsCtlRduV_SZ,
38508 +       AufsCtlRduV_End
38509 +};
38510 +
38511 +struct aufs_rdu {
38512 +       /* input */
38513 +       union {
38514 +               uint64_t        sz;     /* AuCtl_RDU */
38515 +               uint64_t        nent;   /* AuCtl_RDU_INO */
38516 +       };
38517 +       union au_rdu_ent_ul     ent;
38518 +       uint16_t                verify[AufsCtlRduV_End];
38519 +
38520 +       /* input/output */
38521 +       uint32_t                blk;
38522 +
38523 +       /* output */
38524 +       union au_rdu_ent_ul     tail;
38525 +       /* number of entries which were added in a single call */
38526 +       uint64_t                rent;
38527 +       uint8_t                 full;
38528 +       uint8_t                 shwh;
38529 +
38530 +       struct au_rdu_cookie    cookie;
38531 +} __aligned(8);
38532 +
38533 +/* ---------------------------------------------------------------------- */
38534 +
38535 +/* dirren. the branch is identified by the filename who contains this */
38536 +struct au_drinfo {
38537 +       uint64_t ino;
38538 +       union {
38539 +               uint8_t oldnamelen;
38540 +               uint64_t _padding;
38541 +       };
38542 +       uint8_t oldname[0];
38543 +} __aligned(8);
38544 +
38545 +struct au_drinfo_fdata {
38546 +       uint32_t magic;
38547 +       struct au_drinfo drinfo;
38548 +} __aligned(8);
38549 +
38550 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38551 +/* future */
38552 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38553 +
38554 +/* ---------------------------------------------------------------------- */
38555 +
38556 +struct aufs_wbr_fd {
38557 +       uint32_t        oflags;
38558 +       int16_t         brid;
38559 +} __aligned(8);
38560 +
38561 +/* ---------------------------------------------------------------------- */
38562 +
38563 +struct aufs_ibusy {
38564 +       uint64_t        ino, h_ino;
38565 +       int16_t         bindex;
38566 +} __aligned(8);
38567 +
38568 +/* ---------------------------------------------------------------------- */
38569 +
38570 +/* error code for move-down */
38571 +/* the actual message strings are implemented in aufs-util.git */
38572 +enum {
38573 +       EAU_MVDOWN_OPAQUE = 1,
38574 +       EAU_MVDOWN_WHITEOUT,
38575 +       EAU_MVDOWN_UPPER,
38576 +       EAU_MVDOWN_BOTTOM,
38577 +       EAU_MVDOWN_NOUPPER,
38578 +       EAU_MVDOWN_NOLOWERBR,
38579 +       EAU_Last
38580 +};
38581 +
38582 +/* flags for move-down */
38583 +#define AUFS_MVDOWN_DMSG       1
38584 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
38585 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
38586 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
38587 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
38588 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
38589 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
38590 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
38591 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
38592 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
38593 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
38594 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
38595 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
38596 +
38597 +/* index for move-down */
38598 +enum {
38599 +       AUFS_MVDOWN_UPPER,
38600 +       AUFS_MVDOWN_LOWER,
38601 +       AUFS_MVDOWN_NARRAY
38602 +};
38603 +
38604 +/*
38605 + * additional info of move-down
38606 + * number of free blocks and inodes.
38607 + * subset of struct kstatfs, but smaller and always 64bit.
38608 + */
38609 +struct aufs_stfs {
38610 +       uint64_t        f_blocks;
38611 +       uint64_t        f_bavail;
38612 +       uint64_t        f_files;
38613 +       uint64_t        f_ffree;
38614 +};
38615 +
38616 +struct aufs_stbr {
38617 +       int16_t                 brid;   /* optional input */
38618 +       int16_t                 bindex; /* output */
38619 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
38620 +} __aligned(8);
38621 +
38622 +struct aufs_mvdown {
38623 +       uint32_t                flags;                  /* input/output */
38624 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
38625 +       int8_t                  au_errno;               /* output */
38626 +} __aligned(8);
38627 +
38628 +/* ---------------------------------------------------------------------- */
38629 +
38630 +union aufs_brinfo {
38631 +       /* PATH_MAX may differ between kernel-space and user-space */
38632 +       char    _spacer[4096];
38633 +       struct {
38634 +               int16_t id;
38635 +               int     perm;
38636 +               char    path[0];
38637 +       };
38638 +} __aligned(8);
38639 +
38640 +/* ---------------------------------------------------------------------- */
38641 +
38642 +#define AuCtlType              'A'
38643 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
38644 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
38645 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
38646 +                                    struct aufs_wbr_fd)
38647 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
38648 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
38649 +                                     struct aufs_mvdown)
38650 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
38651 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
38652 +
38653 +#endif /* __AUFS_TYPE_H__ */
38654 SPDX-License-Identifier: GPL-2.0
38655 aufs4.x-rcN loopback patch
38656
38657 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
38658 index 5678bf8..ec9c4fb 100644
38659 --- a/drivers/block/loop.c
38660 +++ b/drivers/block/loop.c
38661 @@ -626,6 +626,15 @@ static inline void loop_update_dio(struct loop_device *lo)
38662                         lo->use_dio);
38663  }
38664  
38665 +static struct file *loop_real_file(struct file *file)
38666 +{
38667 +       struct file *f = NULL;
38668 +
38669 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
38670 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
38671 +       return f;
38672 +}
38673 +
38674  static void loop_reread_partitions(struct loop_device *lo,
38675                                    struct block_device *bdev)
38676  {
38677 @@ -690,6 +699,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38678                           unsigned int arg)
38679  {
38680         struct file     *file, *old_file;
38681 +       struct file     *f, *virt_file = NULL, *old_virt_file;
38682         struct inode    *inode;
38683         int             error;
38684  
38685 @@ -706,6 +716,12 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38686         file = fget(arg);
38687         if (!file)
38688                 goto out;
38689 +       f = loop_real_file(file);
38690 +       if (f) {
38691 +               virt_file = file;
38692 +               file = f;
38693 +               get_file(file);
38694 +       }
38695  
38696         error = loop_validate_file(file, bdev);
38697         if (error)
38698 @@ -713,6 +729,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38699  
38700         inode = file->f_mapping->host;
38701         old_file = lo->lo_backing_file;
38702 +       old_virt_file = lo->lo_backing_virt_file;
38703  
38704         error = -EINVAL;
38705  
38706 @@ -724,6 +741,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38707         blk_mq_freeze_queue(lo->lo_queue);
38708         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
38709         lo->lo_backing_file = file;
38710 +       lo->lo_backing_virt_file = virt_file;
38711         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
38712         mapping_set_gfp_mask(file->f_mapping,
38713                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
38714 @@ -731,12 +749,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38715         blk_mq_unfreeze_queue(lo->lo_queue);
38716  
38717         fput(old_file);
38718 +       if (old_virt_file)
38719 +               fput(old_virt_file);
38720         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
38721                 loop_reread_partitions(lo, bdev);
38722         return 0;
38723  
38724   out_putf:
38725         fput(file);
38726 +       if (virt_file)
38727 +               fput(virt_file);
38728   out:
38729         return error;
38730  }
38731 @@ -924,7 +946,7 @@ static int loop_prepare_queue(struct loop_device *lo)
38732  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38733                        struct block_device *bdev, unsigned int arg)
38734  {
38735 -       struct file     *file;
38736 +       struct file     *file, *f, *virt_file = NULL;
38737         struct inode    *inode;
38738         struct address_space *mapping;
38739         int             lo_flags = 0;
38740 @@ -938,6 +960,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38741         file = fget(arg);
38742         if (!file)
38743                 goto out;
38744 +       f = loop_real_file(file);
38745 +       if (f) {
38746 +               virt_file = file;
38747 +               file = f;
38748 +               get_file(file);
38749 +       }
38750  
38751         error = -EBUSY;
38752         if (lo->lo_state != Lo_unbound)
38753 @@ -970,6 +998,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38754         lo->lo_device = bdev;
38755         lo->lo_flags = lo_flags;
38756         lo->lo_backing_file = file;
38757 +       lo->lo_backing_virt_file = virt_file;
38758         lo->transfer = NULL;
38759         lo->ioctl = NULL;
38760         lo->lo_sizelimit = 0;
38761 @@ -1003,6 +1032,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38762  
38763   out_putf:
38764         fput(file);
38765 +       if (virt_file)
38766 +               fput(virt_file);
38767   out:
38768         /* This is safe: open() is still holding a reference. */
38769         module_put(THIS_MODULE);
38770 @@ -1049,6 +1080,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
38771  static int loop_clr_fd(struct loop_device *lo)
38772  {
38773         struct file *filp = lo->lo_backing_file;
38774 +       struct file *virt_filp = lo->lo_backing_virt_file;
38775         gfp_t gfp = lo->old_gfp_mask;
38776         struct block_device *bdev = lo->lo_device;
38777  
38778 @@ -1080,6 +1112,7 @@ static int loop_clr_fd(struct loop_device *lo)
38779         spin_lock_irq(&lo->lo_lock);
38780         lo->lo_state = Lo_rundown;
38781         lo->lo_backing_file = NULL;
38782 +       lo->lo_backing_virt_file = NULL;
38783         spin_unlock_irq(&lo->lo_lock);
38784  
38785         loop_release_xfer(lo);
38786 @@ -1128,6 +1161,8 @@ static int loop_clr_fd(struct loop_device *lo)
38787          * bd_mutex which is usually taken before lo_ctl_mutex.
38788          */
38789         fput(filp);
38790 +       if (virt_filp)
38791 +               fput(virt_filp);
38792         return 0;
38793  }
38794  
38795 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
38796 index 4d42c7a..a4974ee 100644
38797 --- a/drivers/block/loop.h
38798 +++ b/drivers/block/loop.h
38799 @@ -46,7 +46,7 @@ struct loop_device {
38800         int             (*ioctl)(struct loop_device *, int cmd, 
38801                                  unsigned long arg); 
38802  
38803 -       struct file *   lo_backing_file;
38804 +       struct file *   lo_backing_file, *lo_backing_virt_file;
38805         struct block_device *lo_device;
38806         void            *key_data; 
38807  
38808 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
38809 index 937a20d..1355656 100644
38810 --- a/fs/aufs/f_op.c
38811 +++ b/fs/aufs/f_op.c
38812 @@ -358,7 +358,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
38813         if (IS_ERR(h_file))
38814                 goto out;
38815  
38816 -       if (au_test_loopback_kthread()) {
38817 +       if (0 && au_test_loopback_kthread()) {
38818                 au_warn_loopback(h_file->f_path.dentry->d_sb);
38819                 if (file->f_mapping != h_file->f_mapping) {
38820                         file->f_mapping = h_file->f_mapping;
38821 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
38822 index 3d84349..a642de5 100644
38823 --- a/fs/aufs/loop.c
38824 +++ b/fs/aufs/loop.c
38825 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
38826                 symbol_put(loop_backing_file);
38827         kfree(au_warn_loopback_array);
38828  }
38829 +
38830 +/* ---------------------------------------------------------------------- */
38831 +
38832 +/* support the loopback block device insude aufs */
38833 +
38834 +struct file *aufs_real_loop(struct file *file)
38835 +{
38836 +       struct file *f;
38837 +
38838 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
38839 +       fi_read_lock(file);
38840 +       f = au_hf_top(file);
38841 +       fi_read_unlock(file);
38842 +       AuDebugOn(!f);
38843 +       return f;
38844 +}
38845 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
38846 index 05d703d..6bb23c8 100644
38847 --- a/fs/aufs/loop.h
38848 +++ b/fs/aufs/loop.h
38849 @@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
38850  
38851  int au_loopback_init(void);
38852  void au_loopback_fin(void);
38853 +
38854 +struct file *aufs_real_loop(struct file *file);
38855  #else
38856 +AuStub(struct file *, loop_backing_file, return NULL)
38857 +
38858  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
38859            struct dentry *h_adding)
38860  AuStubInt0(au_test_loopback_kthread, void)
38861 @@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
38862  
38863  AuStubInt0(au_loopback_init, void)
38864  AuStubVoid(au_loopback_fin, void)
38865 +
38866 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
38867  #endif /* BLK_DEV_LOOP */
38868  
38869  #endif /* __KERNEL__ */
38870 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
38871 index 4020639..13e315f 100644
38872 --- a/fs/aufs/super.c
38873 +++ b/fs/aufs/super.c
38874 @@ -840,7 +840,10 @@ static const struct super_operations aufs_sop = {
38875         .statfs         = aufs_statfs,
38876         .put_super      = aufs_put_super,
38877         .sync_fs        = aufs_sync_fs,
38878 -       .remount_fs     = aufs_remount_fs
38879 +       .remount_fs     = aufs_remount_fs,
38880 +#ifdef CONFIG_AUFS_BDEV_LOOP
38881 +       .real_loop      = aufs_real_loop
38882 +#endif
38883  };
38884  
38885  /* ---------------------------------------------------------------------- */
38886 diff --git a/include/linux/fs.h b/include/linux/fs.h
38887 index 0d0d247f..50d77f8 100644
38888 --- a/include/linux/fs.h
38889 +++ b/include/linux/fs.h
38890 @@ -1861,6 +1861,10 @@ struct super_operations {
38891                                   struct shrink_control *);
38892         long (*free_cached_objects)(struct super_block *,
38893                                     struct shrink_control *);
38894 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
38895 +       /* and aufs */
38896 +       struct file *(*real_loop)(struct file *);
38897 +#endif
38898  };
38899  
38900  /*
This page took 4.470874 seconds and 3 git commands to generate.